logmania/bot/filter.go

98 lines
2.5 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 NewFilter(db *database.DB) *Command {
return &Command{
Name: "filter",
2019-06-20 10:25:52 +02:00
Description: "list and configure regex filter for channel by message content",
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 filter for channel: [channel] regex",
Action: func(from string, params []string) string {
if len(params) < 1 {
return "invalid: [channel] regex"
}
of := from
regex := params[0]
if len(params) > 1 {
of = params[0]
regex = params[1]
}
n := db.NotifiesByAddress[of]
if err := n.AddRegex(regex); err != nil {
return fmt.Sprintf("\"%s\" is no valid regex expression: %s", regex, err)
}
return fmt.Sprintf("add regex for \"%s\" to %s", of, regex)
},
},
2018-11-06 00:52:45 +01:00
{
2018-09-11 22:57:09 +02:00
Name: "del",
Description: "del regex filter for channel: [channel] regex",
Action: func(from string, params []string) string {
if len(params) < 1 {
return "invalid: [channel] regex"
}
of := from
regex := params[0]
if len(params) > 1 {
of = params[0]
regex = params[1]
}
n := db.NotifiesByAddress[of]
delete(n.RegexIn, 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 := "filter:\n"
for _, n := range db.Notifies {
msg = fmt.Sprintf("%s%s\n-------------\n", msg, n.Address())
for expression := range n.RegexIn {
msg = fmt.Sprintf("%s - %s\n", msg, expression)
}
}
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 {
msg := "filter:\n"
if len(params) != 1 {
return "invalid: no channel given"
}
of := params[0]
if n, ok := db.NotifiesByAddress[of]; ok {
msg = fmt.Sprintf("%s%s\n-------------\n", msg, of)
for expression := range n.RegexIn {
msg = fmt.Sprintf("%s - %s\n", msg, expression)
}
}
return msg
},
},
},
Action: func(from string, params []string) string {
msg := "filter:\n"
if n, ok := db.NotifiesByAddress[from]; ok {
msg = fmt.Sprintf("%s%s\n-------------\n", msg, from)
for expression := range n.RegexIn {
msg = fmt.Sprintf("%s - %s\n", msg, expression)
}
}
return msg
},
}
}