From c1c732b87abefe6f1c5379b3498c43263cc2776e Mon Sep 17 00:00:00 2001 From: Wim Date: Sun, 22 Nov 2020 22:57:42 +0100 Subject: [PATCH] WIP: add tengo support when downloading files Fixes #912 --- bridge/helper/helper.go | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/bridge/helper/helper.go b/bridge/helper/helper.go index 2b449e14..5a2b3522 100644 --- a/bridge/helper/helper.go +++ b/bridge/helper/helper.go @@ -16,7 +16,11 @@ import ( "golang.org/x/image/webp" + "github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/internal" + "github.com/d5/tengo/v2" + "github.com/d5/tengo/v2/stdlib" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/html" "github.com/gomarkdown/markdown/parser" @@ -114,6 +118,62 @@ func GetAvatar(av map[string]string, userid string, general *config.Protocol) st return "" } +func handleDownloadTengo(br *bridge.Bridge, msg *config.Message, name string, size int64, general *config.Protocol) (bool, error) { + var ( + res []byte + err error + drop bool + ) + + filename := br.GetString("tengo.download") + + if filename == "" { + res, err = internal.Asset("tengo/download.tengo") + if err != nil { + return drop, err + } + } else { + res, err = ioutil.ReadFile(filename) + if err != nil { + return drop, err + } + } + + s := tengo.NewScript(res) + + s.SetImports(stdlib.GetModuleMap(stdlib.AllModuleNames()...)) + + _ = s.Add("inAccount", msg.Account) + _ = s.Add("inProtocol", msg.Protocol) + _ = s.Add("inChannel", msg.Channel) + _ = s.Add("inGateway", msg.Gateway) + _ = s.Add("inEvent", msg.Event) + _ = s.Add("outAccount", br.Account) + _ = s.Add("outProtocol", br.Protocol) + _ = s.Add("outChannel", msg.Channel) + _ = s.Add("outEvent", msg.Event) + _ = s.Add("msgText", msg.Text) + _ = s.Add("msgUsername", msg.Username) + _ = s.Add("msgDrop", drop) + _ = s.Add("downloadName", name) + _ = s.Add("downloadSize", size) + + c, err := s.Compile() + if err != nil { + return drop, err + } + + if err := c.Run(); err != nil { + return drop, err + } + + drop = c.Get("msgDrop").Bool() + msg.Text = c.Get("msgText").String() + msg.Username = c.Get("msgUsername").String() + + return drop, nil +} + // HandleDownloadSize checks a specified filename against the configured download blacklist // and checks a specified file-size against the configure limit. func HandleDownloadSize(logger *logrus.Entry, msg *config.Message, name string, size int64, general *config.Protocol) error {