summaryrefslogtreecommitdiffstats
path: root/gateway/handlers.go
diff options
context:
space:
mode:
Diffstat (limited to 'gateway/handlers.go')
-rw-r--r--gateway/handlers.go39
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
+}