diff options
author | Wim <wim@42.be> | 2019-02-23 16:35:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-23 16:35:54 +0100 |
commit | 3190703dc8618896c932a23d8ca155fbbf6fab13 (patch) | |
tree | ab49eb5adbe6b59945dd2b21cb378ffb7538add9 /gateway/handlers.go | |
parent | 5095db8a43491fd2c5859f25cbb89a3be40bef6d (diff) | |
download | matterbridge-msglm-3190703dc8618896c932a23d8ca155fbbf6fab13.tar.gz matterbridge-msglm-3190703dc8618896c932a23d8ca155fbbf6fab13.tar.bz2 matterbridge-msglm-3190703dc8618896c932a23d8ca155fbbf6fab13.zip |
Support rewriting messages from relaybots using ExtractNicks. Fixes #466 (#730)
some examples:
this replaces a message like "Relaybot: <relayeduser> something interesting" to "relayeduser: something interesting"
ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ] ]
you can use multiple entries for multiplebots
this also replaces a message like "otherbot: (relayeduser) something else" to "relayeduser: something else"
ExtractNicks=[ [ "Relaybot", "<(.*?)>\\s+" ],[ "otherbot","\\((.*?)\\)\\s+" ]
OPTIONAL (default empty)
ExtractNicks=[ ["otherbot","<(.*?)>\\s+" ] ]
Diffstat (limited to 'gateway/handlers.go')
-rw-r--r-- | gateway/handlers.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/gateway/handlers.go b/gateway/handlers.go index 5af13c14..dfec2ab6 100644 --- a/gateway/handlers.go +++ b/gateway/handlers.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" "regexp" + "strings" "time" "github.com/42wim/matterbridge/bridge" @@ -225,3 +226,41 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM } return brMsgIDs } + +func (gw *Gateway) handleExtractNicks(msg *config.Message) { + var err error + br := gw.Bridges[msg.Account] + for _, outer := range br.GetStringSlice2D("ExtractNicks") { + search := outer[0] + replace := outer[1] + msg.Username, msg.Text, err = extractNick(search, replace, msg.Username, msg.Text) + if err != nil { + flog.Errorf("regexp in %s failed: %s", msg.Account, err) + break + } + } +} + +// extractNick searches for a username (based on "search" a regular expression). +// if this matches it extracts a nick (based on "extract" another regular expression) from text +// and replaces username with this result. +// returns error if the regexp doesn't compile. +func extractNick(search, extract, username, text string) (string, string, error) { + re, err := regexp.Compile(search) + if err != nil { + return username, text, err + } + if re.MatchString(username) { + re, err = regexp.Compile(extract) + if err != nil { + return username, text, err + } + res := re.FindAllStringSubmatch(text, 1) + // only replace if we have exactly 1 match + if len(res) > 0 && len(res[0]) == 2 { + username = res[0][1] + text = strings.Replace(text, res[0][0], "", 1) + } + } + return username, text, nil +} |