Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tengo to drop messages using msgDrop #1272

Merged
merged 1 commit into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gateway

import (
"fmt"
"io/ioutil"
"os"
"regexp"
Expand Down Expand Up @@ -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
Expand All @@ -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" {
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
4 changes: 3 additions & 1 deletion matterbridge.toml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -1726,7 +1726,9 @@ InMessage="example.tengo"
#outAccount, outProtocol, outChannel, outGateway, outEvent
#
#read-write:
#msgText, msgUsername
#msgText, msgUsername, msgDrop
#
#msgDrop is a bool which is default false, when set true this message will be dropped
#
#The script is reloaded on every message, so you can modify the script on the fly.
#
Expand Down