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

WIP: add tengo support when downloading files #1307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
60 changes: 60 additions & 0 deletions bridge/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function handleDownloadTengo has 5 arguments (exceeds 4 allowed). Consider refactoring.

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 {
Expand Down