From 2d277a15f5b70a6188aa2871333e089f2f95ceec Mon Sep 17 00:00:00 2001 From: Wim Date: Fri, 19 Apr 2019 18:27:31 +0200 Subject: 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. --- gateway/gateway.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'gateway') 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 +} -- cgit v1.2.3