summaryrefslogtreecommitdiffstats
path: root/gateway
diff options
context:
space:
mode:
authorWim <wim@42.be>2020-10-21 21:57:14 +0200
committerGitHub <noreply@github.com>2020-10-21 21:57:14 +0200
commitae3bc3358b7339760ca0e6905d19905b298b671d (patch)
tree51c73c574a15620b9b97f16b24f90bfa71826cdc /gateway
parent1e0b4532bdb8a549e704918740601866e599c508 (diff)
downloadmatterbridge-msglm-ae3bc3358b7339760ca0e6905d19905b298b671d.tar.gz
matterbridge-msglm-ae3bc3358b7339760ca0e6905d19905b298b671d.tar.bz2
matterbridge-msglm-ae3bc3358b7339760ca0e6905d19905b298b671d.zip
Allow tengo to drop messages using msgDrop (#1272)
Diffstat (limited to 'gateway')
-rw-r--r--gateway/gateway.go41
1 files changed, 31 insertions, 10 deletions
diff --git a/gateway/gateway.go b/gateway/gateway.go
index 7a5ca8c8..6a46ecf8 100644
--- a/gateway/gateway.go
+++ b/gateway/gateway.go
@@ -1,6 +1,7 @@
package gateway
import (
+ "fmt"
"io/ioutil"
"os"
"regexp"
@@ -430,8 +431,9 @@ func (gw *Gateway) SendMessage(
}
// Too noisy to log like other events
+ debugSendMessage := ""
if msg.Event != config.EventUserTyping {
- gw.logger.Debugf("=> Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, rmsg.Channel, dest.Account, channel.Name)
+ debugSendMessage = fmt.Sprintf("=> Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, rmsg.Channel, dest.Account, channel.Name)
}
msg.Channel = channel.Name
@@ -456,11 +458,19 @@ func (gw *Gateway) SendMessage(
msg.ParentID = "msg-parent-not-found"
}
- err := gw.modifyOutMessageTengo(rmsg, &msg, dest)
+ drop, err := gw.modifyOutMessageTengo(rmsg, &msg, dest)
if err != nil {
gw.logger.Errorf("modifySendMessageTengo: %s", err)
}
+ if drop {
+ gw.logger.Debugf("=> Tengo dropping %#v from %s (%s) to %s (%s)", msg, msg.Account, rmsg.Channel, dest.Account, channel.Name)
+ return "", nil
+ }
+
+ if debugSendMessage != "" {
+ gw.logger.Debug(debugSendMessage)
+ }
// 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" {
@@ -577,22 +587,28 @@ func (gw *Gateway) modifyUsernameTengo(msg *config.Message, br *bridge.Bridge) (
return c.Get("result").String(), nil
}
-func (gw *Gateway) modifyOutMessageTengo(origmsg *config.Message, msg *config.Message, br *bridge.Bridge) error {
+func (gw *Gateway) modifyOutMessageTengo(origmsg *config.Message, msg *config.Message, br *bridge.Bridge) (bool, error) {
filename := gw.BridgeValues().Tengo.OutMessage
- var res []byte
- var err error
+ var (
+ res []byte
+ err error
+ drop bool
+ )
+
if filename == "" {
res, err = internal.Asset("tengo/outmessage.tengo")
if err != nil {
- return err
+ return drop, err
}
} else {
res, err = ioutil.ReadFile(filename)
if err != nil {
- return err
+ return drop, err
}
}
+
s := tengo.NewScript(res)
+
s.SetImports(stdlib.GetModuleMap(stdlib.AllModuleNames()...))
_ = s.Add("inAccount", origmsg.Account)
_ = s.Add("inProtocol", origmsg.Protocol)
@@ -606,14 +622,19 @@ func (gw *Gateway) modifyOutMessageTengo(origmsg *config.Message, msg *config.Me
_ = s.Add("outEvent", msg.Event)
_ = s.Add("msgText", msg.Text)
_ = s.Add("msgUsername", msg.Username)
+ _ = s.Add("msgDrop", drop)
c, err := s.Compile()
if err != nil {
- return err
+ return drop, err
}
+
if err := c.Run(); err != nil {
- return err
+ return drop, err
}
+
+ drop = c.Get("msgDrop").Bool()
msg.Text = c.Get("msgText").String()
msg.Username = c.Get("msgUsername").String()
- return nil
+
+ return drop, nil
}