commit b8a7753bcd15d0c6713fc80700e7f95cc23ce50c
Author: Wim <wim@42.be>
Date: Sat Feb 23 16:39:44 2019 +0000
diff --git a/README.md b/README.md
index 4ccfd9d..4923e36 100644
--- a/README.md
+++ b/README.md
@@ -2826 +2827 @@ Matterbridge wouldn't exist without these libraries:
* xmpp - https://github.com/mattn/go-xmpp
* whatsapp - https://github.com/Rhymen/go-whatsapp/
* zulip - https://github.com/ifo/gozulipbot
+* tengo - https://github.com/d5/tengo
<!-- Links -->
diff --git a/bridge/config/config.go b/bridge/config/config.go
index 7ab6aef..4791495 100644
--- a/bridge/config/config.go
+++ b/bridge/config/config.go
@@ -1296 +1297 @@ type Protocol struct {
SkipTLSVerify bool // IRC, mattermost
StripNick bool // all protocols
SyncTopic bool // slack
+ TengoModifyMessage string // general
Team string // mattermost
Token string // gitter, slack, discord, api
Topic string // zulip
diff --git a/contrib/example.tengo b/contrib/example.tengo
new file mode 100644
index 0000000..da4eede
--- /dev/null
+++ b/contrib/example.tengo
@@ -00 +12 @@
+text := import("text")
+msgText=text.re_replace("matterbridge",msgText,"matterbridge (https://github.com/42wim/matterbridge)")
diff --git a/gateway/bench.tengo b/gateway/bench.tengo
new file mode 100644
index 0000000..879d17a
--- /dev/null
+++ b/gateway/bench.tengo
@@ -00 +15 @@
+text := import("text")
+if text.re_match("blah",msgText) {
+ msgText="replaced by this"
+ msgUsername="fakeuser"
+}
diff --git a/gateway/gateway.go b/gateway/gateway.go
index 2f79773..72d7c72 100644
--- a/gateway/gateway.go
+++ b/gateway/gateway.go
@@ -16 +17 @@
package gateway
import (
+ "io/ioutil"
"os"
"regexp"
"strings"
@@ -86 +97 @@ import (
"github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config"
+ "github.com/d5/tengo/script"
"github.com/hashicorp/golang-lru"
"github.com/peterhellberg/emojilib"
"github.com/sirupsen/logrus"
@@ -3346 +33610 @@ func (gw *Gateway) modifyAvatar(msg config.Message, dest *bridge.Bridge) string
}
func (gw *Gateway) modifyMessage(msg *config.Message) {
+ if err := modifyMessageTengo(gw.BridgeValues().General.TengoModifyMessage, msg); err != nil {
+ flog.Errorf("TengoModifyMessage failed: %s", err)
+ }
+
// replace :emoji: to unicode
msg.Text = emojilib.Replace(msg.Text)
@@ -4583 +46428 @@ func getProtocol(msg *config.Message) string {
p := strings.Split(msg.Account, ".")
return p[0]
}
+
+func modifyMessageTengo(filename string, msg *config.Message) error {
+ if filename == "" {
+ return nil
+ }
+ res, err := ioutil.ReadFile(filename)
+ if err != nil {
+ return err
+ }
+ s := script.New(res)
+ _ = s.Add("msgText", msg.Text)
+ _ = s.Add("msgUsername", msg.Username)
+ _ = s.Add("msgAccount", msg.Account)
+ _ = s.Add("msgChannel", msg.Channel)
+ c, err := s.Compile()
+ if err != nil {
+ return err
+ }
+ if err := c.Run(); err != nil {
+ return err
+ }
+ msg.Text = c.Get("msgText").String()
+ msg.Username = c.Get("msgUsername").String()
+ return nil
+}
diff --git a/gateway/gateway_test.go b/gateway/gateway_test.go
index 9621ab7..677afde 100644
--- a/gateway/gateway_test.go
+++ b/gateway/gateway_test.go
@@ -4993 +49913 @@ func TestIgnoreNicks(t *testing.T) {
assert.Equalf(t, testcase.output, output, "case '%s' failed", testname)
}
}
+
+func BenchmarkTengo(b *testing.B) {
+ msg := &config.Message{Username: "user", Text: "blah testing", Account: "protocol.account", Channel: "mychannel"}
+ for n := 0; n < b.N; n++ {
+ err := modifyMessageTengo("bench.tengo", msg)
+ if err != nil {
+ return
+ }
+ }
+}
diff --git a/go.mod b/go.mod
index 0d79c67..3a59013 100644
--- a/go.mod
+++ b/go.mod
@@ -86 +87 @@ require (
github.com/Philipp15b/go-steam v1.0.1-0.20180818081528-681bd9573329
github.com/Rhymen/go-whatsapp v0.0.0-20190208184307-c9a81e957884
github.com/bwmarrin/discordgo v0.19.0
+ github.com/d5/tengo v1.9.2
github.com/dfordsoft/golib v0.0.0-20180902042739-76ee6ab99bec
github.com/fsnotify/fsnotify v1.4.7
github.com/go-telegram-bot-api/telegram-bot-api v4.6.5-0.20181225215658-ec221ba9ea45+incompatible
diff --git a/go.sum b/go.sum
index 4aa24ac..92b0371 100644
--- a/go.sum
+++ b/go.sum
@@ -206 +208 @@ github.com/bwmarrin/discordgo v0.19.0/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVO
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
+github.com/d5/tengo v1.9.2 h1:UE/X8PYl7bLS4Ww2zGeh91nq5PTnkhe8ncgNeA5PK7k=
+github.com/d5/tengo v1.9.2/go.mod h1:gsbjo7lBXzBIWBd6NQp1lRKqqiDDANqBOyhW8rTlFsY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample
index 51faa19..19cb09b 100644
--- a/matterbridge.toml.sample
+++ b/matterbridge.toml.sample
@@ -15276 +152729 @@ MediaDownloadBlacklist=[".html$",".htm$"]
#OPTIONAL (default false)
IgnoreFailureOnStart=false
+
+#TengoModifyMessage allows you to specify the location of a tengo (https://github.com/d5/tengo/) script.
+#This script will receive every incoming message and can be used to modify the Username and the Text of that message.
+#The script will have the following global variables:
+#to modify: msgUsername and msgText
+#to read: msgChannel and msgAccount
+#
+#The script is reloaded on every message, so you can modify the script on the fly.
+#
+#Example script can be found in https://github.com/42wim/matterbridge/tree/master/gateway/bench.tengo
+#and https://github.com/42wim/matterbridge/tree/master/contrib/example.tengo
+#
+#The example below will check if the text contains blah and if so, it'll replace the text and the username of that message.
+#text := import("text")
+#if text.re_match("blah",msgText) {
+# msgText="replaced by this"
+# msgUsername="fakeuser"
+#}
+#More information about tengo on: https://github.com/d5/tengo/blob/master/docs/tutorial.md and
+#https://github.com/d5/tengo/blob/master/docs/stdlib.md
+#OPTIONAL (default empty)
+TengoModifyMessage="example.tengo"
+
###################################################################
#Gateway configuration
###################################################################