From bd940a7656c300f3404bc86c5b224f2f360fb603 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Fri, 18 May 2018 11:28:01 +0200 Subject: [PATCH] add replace --- bot/command.go | 89 +++++++++++++++++++++++++++++++++++++++++---- bot/main.go | 3 ++ database/notify.go | 37 +++++++++++++++++-- notify/xmpp/main.go | 8 +++- 4 files changed, 124 insertions(+), 13 deletions(-) diff --git a/bot/command.go b/bot/command.go index 35ea8bd..788c26d 100644 --- a/bot/command.go +++ b/bot/command.go @@ -2,7 +2,6 @@ package bot import ( "fmt" - "strings" timeago "github.com/ararog/timeago" log "github.com/sirupsen/logrus" @@ -235,11 +234,16 @@ func (b *Bot) addRegex(answer func(string), from string, params []string) { answer("invalid: CMD regex\n or\n CMD channel regex") return } - regex := strings.Join(params, " ") + of := from + regex := params[0] + if len(params) > 1 { + of = params[0] + regex = params[1] + } - n := b.db.NotifiesByAddress[from] + n := b.db.NotifiesByAddress[of] if err := n.AddRegex(regex); err == nil { - answer(fmt.Sprintf("add regex for \"%s\" to %s", from, regex)) + answer(fmt.Sprintf("add regex for \"%s\" to %s", of, regex)) } else { answer(fmt.Sprintf("\"%s\" is no valid regex expression: %s", regex, err)) } @@ -251,8 +255,79 @@ func (b *Bot) delRegex(answer func(string), from string, params []string) { answer("invalid: CMD regex\n or\n CMD channel regex") return } - n := b.db.NotifiesByAddress[from] - regex := strings.Join(params, " ") + of := from + regex := params[0] + if len(params) > 1 { + of = params[0] + regex = params[1] + } + n := b.db.NotifiesByAddress[of] delete(n.RegexIn, regex) - b.listRegex(answer, from, []string{}) + b.listRegex(answer, of, []string{}) +} + +// list of regex replace +func (b *Bot) listRegexReplace(answer func(string), from string, params []string) { + msg := "replaces:\n" + if len(params) > 0 && params[0] == "all" { + for _, n := range b.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) + } + } + } else { + of := from + if len(params) > 0 { + of = params[0] + } + if n, ok := b.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) + } + } + } + answer(msg) +} + +// add a regex replace +func (b *Bot) addRegexReplace(answer func(string), from string, params []string) { + if len(params) < 1 { + answer("invalid: CMD regex replace\n or\n CMD channel regex replace") + return + } + of := from + regex := params[0] + value := params[1] + if len(params) > 2 { + of = params[0] + regex = params[1] + value = params[2] + } + + n := b.db.NotifiesByAddress[of] + if err := n.AddRegexReplace(regex, value); err == nil { + answer(fmt.Sprintf("add replace in \"%s\" for \"%s\" to \"%s\"", of, regex, value)) + } else { + answer(fmt.Sprintf("\"%s\" to \"%s\" is no valid regex replace expression: %s", regex, value, err)) + } +} + +// del a regex replace +func (b *Bot) delRegexReplace(answer func(string), from string, params []string) { + if len(params) < 1 { + answer("invalid: CMD regex\n or\n CMD channel regex") + return + } + of := from + regex := params[0] + if len(params) > 1 { + of = params[0] + regex = params[1] + } + n := b.db.NotifiesByAddress[of] + + delete(n.RegexReplace, regex) + b.listRegexReplace(answer, of, []string{}) } diff --git a/bot/main.go b/bot/main.go index bdf22ff..9f9280a 100644 --- a/bot/main.go +++ b/bot/main.go @@ -30,6 +30,9 @@ func NewBot(db *database.DB) *Bot { "regex-add": b.addRegex, "regex-list": b.listRegex, "regex-del": b.delRegex, + "replace-add": b.addRegexReplace, + "replace-list": b.listRegexReplace, + "replace-del": b.delRegexReplace, } for k := range b.commandsMap { b.commands = append(b.commands, k) diff --git a/database/notify.go b/database/notify.go index ebe9624..6fbcd3c 100644 --- a/database/notify.go +++ b/database/notify.go @@ -8,22 +8,36 @@ import ( ) type Notify struct { - Protocoll string `json:"proto"` - To string `json:"to"` - RegexIn map[string]*regexp.Regexp `json:"regexIn"` - MaxPrioIn log.Level `json:"maxLevel"` + Protocoll string `json:"proto"` + To string `json:"to"` + RegexIn map[string]*regexp.Regexp `json:"regexIn"` + RegexReplace map[string]string `json:"regexReplace"` + MaxPrioIn log.Level `json:"maxLevel"` + regexReplaceExpression map[string]*regexp.Regexp } func (n *Notify) Init() { if n.RegexIn == nil { n.RegexIn = make(map[string]*regexp.Regexp) } + if n.RegexReplace == nil { + n.RegexReplace = make(map[string]string) + } + if n.regexReplaceExpression == nil { + n.regexReplaceExpression = make(map[string]*regexp.Regexp) + } for exp := range n.RegexIn { regex, err := regexp.Compile(exp) if err == nil { n.RegexIn[exp] = regex } } + for exp := range n.RegexReplace { + regex, err := regexp.Compile(exp) + if err == nil { + n.regexReplaceExpression[exp] = regex + } + } } func (n *Notify) AddRegex(expression string) error { @@ -33,6 +47,21 @@ func (n *Notify) AddRegex(expression string) error { } return err } +func (n *Notify) AddRegexReplace(expression, value string) error { + regex, err := regexp.Compile(expression) + if err == nil { + n.regexReplaceExpression[expression] = regex + n.RegexReplace[expression] = value + } + return err +} +func (n *Notify) RunReplace(msg string) string { + for key, re := range n.regexReplaceExpression { + value := n.RegexReplace[key] + msg = re.ReplaceAllString(msg, value) + } + return msg +} func (n *Notify) Address() string { return n.Protocoll + ":" + n.To diff --git a/notify/xmpp/main.go b/notify/xmpp/main.go index fe3b7f4..e3c5d8a 100644 --- a/notify/xmpp/main.go +++ b/notify/xmpp/main.go @@ -150,6 +150,10 @@ func (n *Notifier) Send(e *log.Entry) error { return err } for _, to := range tos { + modifyText := string(text) + if notify, ok := n.db.NotifiesByAddress[to.Address()]; ok { + modifyText = notify.RunReplace(modifyText) + } if to.Protocoll == protoGroup { if _, ok := n.channels[to.To]; ok { toJID := xmppbase.NewJID(to.To) @@ -166,7 +170,7 @@ func (n *Notifier) Send(e *log.Entry) error { err := n.client.Send(&xmpp.MessageClient{ Type: xmpp.MessageTypeGroupchat, To: xmppbase.NewJID(to.To), - Body: string(text), + Body: modifyText, }) if err != nil { logger.Error("xmpp to ", to.To, " error:", err) @@ -175,7 +179,7 @@ func (n *Notifier) Send(e *log.Entry) error { err := n.client.Send(&xmpp.MessageClient{ Type: xmpp.MessageTypeChat, To: xmppbase.NewJID(to.To), - Body: string(text), + Body: modifyText, }) if err != nil { logger.Error("xmpp to ", to, " error:", err)