summaryrefslogtreecommitdiffstats
path: root/gateway/gateway.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2019-04-19 18:27:31 +0200
committerGitHub <noreply@github.com>2019-04-19 18:27:31 +0200
commit2d277a15f5b70a6188aa2871333e089f2f95ceec (patch)
tree68fb9f5932bf199409cec2a10ffe5dd10f6e6aea /gateway/gateway.go
parentd60468bb05f9fc4066a669cd9704174629a19dd3 (diff)
downloadmatterbridge-msglm-2d277a15f5b70a6188aa2871333e089f2f95ceec.tar.gz
matterbridge-msglm-2d277a15f5b70a6188aa2871333e089f2f95ceec.tar.bz2
matterbridge-msglm-2d277a15f5b70a6188aa2871333e089f2f95ceec.zip
Add scripting (tengo) support for every outgoing message (#806)
Adds a new key OutMessage under [tengo] table, which specifies the location of the script that will be invoked on each message being sent to a bridge and can be used to modify the Username and the Text of that message. The script will have the following global variables: read-only: inAccount, inProtocol, inChannel, inGateway outAccount, outProtocol, outChannel, outGateway read-write: msgText, msgUsername The script is reloaded on every message, so you can modify the script on the fly. The default script in https://github.com/42wim/matterbridge/tree/master/internal/tengo/outmessage.tengo is compiled in and will be executed if no script is specified.
Diffstat (limited to 'gateway/gateway.go')
-rw-r--r--gateway/gateway.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/gateway/gateway.go b/gateway/gateway.go
index d46f75d6..a9c331bc 100644
--- a/gateway/gateway.go
+++ b/gateway/gateway.go
@@ -9,6 +9,7 @@ import (
"github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config"
+ "github.com/42wim/matterbridge/internal"
"github.com/d5/tengo/script"
"github.com/d5/tengo/stdlib"
lru "github.com/hashicorp/golang-lru"
@@ -429,6 +430,11 @@ func (gw *Gateway) SendMessage(
msg.ParentID = "msg-parent-not-found"
}
+ err := gw.modifySendMessageTengo(rmsg, &msg, dest)
+ if err != nil {
+ gw.logger.Errorf("modifySendMessageTengo: %s", err)
+ }
+
// if we are using mattermost plugin account, send messages to MattermostPlugin channel
// that can be picked up by the mattermost matterbridge plugin
if dest.Account == "mattermost.plugin" {
@@ -544,3 +550,42 @@ func (gw *Gateway) modifyUsernameTengo(msg *config.Message, br *bridge.Bridge) (
}
return c.Get("result").String(), nil
}
+
+func (gw *Gateway) modifySendMessageTengo(origmsg *config.Message, msg *config.Message, br *bridge.Bridge) error {
+ filename := gw.BridgeValues().Tengo.OutMessage
+ var res []byte
+ var err error
+ if filename == "" {
+ res, err = internal.Asset("tengo/outmessage.tengo")
+ if err != nil {
+ return err
+ }
+ } else {
+ res, err = ioutil.ReadFile(filename)
+ if err != nil {
+ return err
+ }
+ }
+ s := script.New(res)
+ s.SetImports(stdlib.GetModuleMap(stdlib.AllModuleNames()...))
+ _ = s.Add("inAccount", origmsg.Account)
+ _ = s.Add("inProtocol", origmsg.Protocol)
+ _ = s.Add("inChannel", origmsg.Channel)
+ _ = s.Add("inGateway", origmsg.Gateway)
+ _ = s.Add("outAccount", br.Account)
+ _ = s.Add("outProtocol", br.Protocol)
+ _ = s.Add("outChannel", msg.Channel)
+ _ = s.Add("outGateway", gw.Name)
+ _ = s.Add("msgText", msg.Text)
+ _ = s.Add("msgUsername", msg.Username)
+ 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
+}