add regex replace in notify message

This commit is contained in:
Martin/Geno 2018-05-18 11:28:01 +02:00
parent fbbd66e555
commit 627255139f
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
4 changed files with 124 additions and 13 deletions

View File

@ -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{})
}

View File

@ -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)

View File

@ -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

View File

@ -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)