logmania/bot/replace.go

102 lines
2.7 KiB
Go
Raw Permalink Normal View History

2018-09-11 22:57:09 +02:00
package bot
import (
"fmt"
2019-08-08 15:51:55 +02:00
"dev.sum7.eu/sum7/logmania/database"
2018-09-11 22:57:09 +02:00
)
func NewReplace(db *database.DB) *Command {
return &Command{
Name: "replace",
2019-06-20 10:25:52 +02:00
Description: "list and configure replace content of message for channel",
2018-09-11 22:57:09 +02:00
Commands: []*Command{
2018-11-06 00:52:45 +01:00
{
2018-09-11 22:57:09 +02:00
Name: "add",
Description: "add regex replace for channel: [channel] regex replace",
Action: func(from string, params []string) string {
if len(params) < 1 {
return "invalid: [channel] regex replace"
}
of := from
regex := params[0]
value := params[1]
if len(params) > 2 {
of = params[0]
regex = params[1]
value = params[2]
}
n := db.NotifiesByAddress[of]
if err := n.AddRegexReplace(regex, value); err != nil {
return fmt.Sprintf("\"%s\" to \"%s\" is no valid regex replace expression: %s", regex, value, err)
}
return fmt.Sprintf("add replace in \"%s\" for \"%s\" to \"%s\"", of, regex, value)
},
},
2018-11-06 00:52:45 +01:00
{
2018-09-11 22:57:09 +02:00
Name: "del",
Description: "del regex replace for channel: [channel] regex replace",
Action: func(from string, params []string) string {
if len(params) < 1 {
return "invalid: [channel] regex replace"
}
of := from
regex := params[0]
if len(params) > 1 {
of = params[0]
regex = params[1]
}
n := db.NotifiesByAddress[of]
delete(n.RegexReplace, regex)
return "deleted"
},
},
2018-11-06 00:52:45 +01:00
{
2018-09-11 22:57:09 +02:00
Name: "all",
Description: "list of all channels",
Action: func(from string, params []string) string {
msg := "replaces:\n"
for _, n := range db.Notifies {
msg = fmt.Sprintf("%s%s\n-------------\n", msg, n.Address())
for expression, value := range n.RegexReplace {
msg = fmt.Sprintf("%s - \"%s\" : \"%s\"\n", msg, expression, value)
}
}
return msg
},
},
2018-11-06 00:52:45 +01:00
{
2018-09-11 22:57:09 +02:00
Name: "channel",
Description: "list of given channel: channel",
Action: func(from string, params []string) string {
if len(params) != 1 {
return "invalid: no channel given"
}
of := params[0]
msg := "replaces:\n"
if n, ok := db.NotifiesByAddress[of]; ok {
msg = fmt.Sprintf("%s%s\n-------------\n", msg, of)
for expression, value := range n.RegexReplace {
msg = fmt.Sprintf("%s - \"%s\" : \"%s\"\n", msg, expression, value)
}
}
return msg
},
},
},
Action: func(from string, params []string) string {
msg := "replaces:\n"
if n, ok := db.NotifiesByAddress[from]; ok {
msg = fmt.Sprintf("%s%s\n-------------\n", msg, from)
for expression, value := range n.RegexReplace {
msg = fmt.Sprintf("%s - \"%s\" : \"%s\"\n", msg, expression, value)
}
}
return msg
},
}
}