diff options
| author | Mistivia <i@mistivia.com> | 2025-11-02 15:27:18 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-11-02 15:27:18 +0800 |
| commit | e9c24f4af7ed56760f6db7941827d09f6db9020b (patch) | |
| tree | 62128c43b883ce5e3148113350978755779bb5de /teleirc/matterbridge/gateway | |
| parent | 58d5e7cfda4781d8a57ec52aefd02983835c301a (diff) | |
add matterbridge
Diffstat (limited to 'teleirc/matterbridge/gateway')
30 files changed, 2114 insertions, 0 deletions
diff --git a/teleirc/matterbridge/gateway/bench.tengo b/teleirc/matterbridge/gateway/bench.tengo new file mode 100644 index 0000000..879d17a --- /dev/null +++ b/teleirc/matterbridge/gateway/bench.tengo @@ -0,0 +1,5 @@ +text := import("text") +if text.re_match("blah",msgText) { + msgText="replaced by this" + msgUsername="fakeuser" +} diff --git a/teleirc/matterbridge/gateway/bridgemap/api.go b/teleirc/matterbridge/gateway/bridgemap/api.go new file mode 100644 index 0000000..a8b6e61 --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/api.go @@ -0,0 +1,11 @@ +// +build !noapi + +package bridgemap + +import ( + "github.com/42wim/matterbridge/bridge/api" +) + +func init() { + FullMap["api"] = api.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bdiscord.go b/teleirc/matterbridge/gateway/bridgemap/bdiscord.go new file mode 100644 index 0000000..92b14ac --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bdiscord.go @@ -0,0 +1,12 @@ +// +build !nodiscord + +package bridgemap + +import ( + bdiscord "github.com/42wim/matterbridge/bridge/discord" +) + +func init() { + FullMap["discord"] = bdiscord.New + UserTypingSupport["discord"] = struct{}{} +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bgitter.go b/teleirc/matterbridge/gateway/bridgemap/bgitter.go new file mode 100644 index 0000000..64b3c25 --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bgitter.go @@ -0,0 +1,11 @@ +// +build !nogitter + +package bridgemap + +import ( + bgitter "github.com/42wim/matterbridge/bridge/gitter" +) + +func init() { + FullMap["gitter"] = bgitter.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bharmony.go b/teleirc/matterbridge/gateway/bridgemap/bharmony.go new file mode 100644 index 0000000..a747dda --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bharmony.go @@ -0,0 +1,12 @@ +//go:build !noharmony +// +build !noharmony + +package bridgemap + +import ( + bharmony "github.com/42wim/matterbridge/bridge/harmony" +) + +func init() { + FullMap["harmony"] = bharmony.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/birc.go b/teleirc/matterbridge/gateway/bridgemap/birc.go new file mode 100644 index 0000000..9e2bc33 --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/birc.go @@ -0,0 +1,11 @@ +// +build !noirc + +package bridgemap + +import ( + birc "github.com/42wim/matterbridge/bridge/irc" +) + +func init() { + FullMap["irc"] = birc.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bkeybase.go b/teleirc/matterbridge/gateway/bridgemap/bkeybase.go new file mode 100644 index 0000000..5dcdcda --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bkeybase.go @@ -0,0 +1,11 @@ +// +build !nokeybase + +package bridgemap + +import ( + bkeybase "github.com/42wim/matterbridge/bridge/keybase" +) + +func init() { + FullMap["keybase"] = bkeybase.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bmatrix.go b/teleirc/matterbridge/gateway/bridgemap/bmatrix.go new file mode 100644 index 0000000..e1d761e --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bmatrix.go @@ -0,0 +1,11 @@ +// +build !nomatrix + +package bridgemap + +import ( + bmatrix "github.com/42wim/matterbridge/bridge/matrix" +) + +func init() { + FullMap["matrix"] = bmatrix.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bmattermost.go b/teleirc/matterbridge/gateway/bridgemap/bmattermost.go new file mode 100644 index 0000000..3f0f044 --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bmattermost.go @@ -0,0 +1,11 @@ +// +build !nomattermost + +package bridgemap + +import ( + bmattermost "github.com/42wim/matterbridge/bridge/mattermost" +) + +func init() { + FullMap["mattermost"] = bmattermost.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bmsteams.go b/teleirc/matterbridge/gateway/bridgemap/bmsteams.go new file mode 100644 index 0000000..56da211 --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bmsteams.go @@ -0,0 +1,11 @@ +// +build !nomsteams + +package bridgemap + +import ( + bmsteams "github.com/42wim/matterbridge/bridge/msteams" +) + +func init() { + FullMap["msteams"] = bmsteams.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bmumble.go b/teleirc/matterbridge/gateway/bridgemap/bmumble.go new file mode 100644 index 0000000..7b9241f --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bmumble.go @@ -0,0 +1,11 @@ +// +build !nomumble + +package bridgemap + +import ( + bmumble "github.com/42wim/matterbridge/bridge/mumble" +) + +func init() { + FullMap["mumble"] = bmumble.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bnctalk.go b/teleirc/matterbridge/gateway/bridgemap/bnctalk.go new file mode 100644 index 0000000..c194392 --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bnctalk.go @@ -0,0 +1,11 @@ +// +build !nonctalk + +package bridgemap + +import ( + btalk "github.com/42wim/matterbridge/bridge/nctalk" +) + +func init() { + FullMap["nctalk"] = btalk.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bridgemap.go b/teleirc/matterbridge/gateway/bridgemap/bridgemap.go new file mode 100644 index 0000000..8851469 --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bridgemap.go @@ -0,0 +1,10 @@ +package bridgemap + +import ( + "github.com/42wim/matterbridge/bridge" +) + +var ( + FullMap = map[string]bridge.Factory{} + UserTypingSupport = map[string]struct{}{} +) diff --git a/teleirc/matterbridge/gateway/bridgemap/brocketchat.go b/teleirc/matterbridge/gateway/bridgemap/brocketchat.go new file mode 100644 index 0000000..6af4e8d --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/brocketchat.go @@ -0,0 +1,11 @@ +// +build !norocketchat + +package bridgemap + +import ( + brocketchat "github.com/42wim/matterbridge/bridge/rocketchat" +) + +func init() { + FullMap["rocketchat"] = brocketchat.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bslack.go b/teleirc/matterbridge/gateway/bridgemap/bslack.go new file mode 100644 index 0000000..fdea80c --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bslack.go @@ -0,0 +1,13 @@ +// +build !noslack + +package bridgemap + +import ( + bslack "github.com/42wim/matterbridge/bridge/slack" +) + +func init() { + FullMap["slack-legacy"] = bslack.NewLegacy + FullMap["slack"] = bslack.New + UserTypingSupport["slack"] = struct{}{} +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bsshchat.go b/teleirc/matterbridge/gateway/bridgemap/bsshchat.go new file mode 100644 index 0000000..14d4228 --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bsshchat.go @@ -0,0 +1,11 @@ +// +build !nosshchat + +package bridgemap + +import ( + bsshchat "github.com/42wim/matterbridge/bridge/sshchat" +) + +func init() { + FullMap["sshchat"] = bsshchat.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bsteam.go b/teleirc/matterbridge/gateway/bridgemap/bsteam.go new file mode 100644 index 0000000..c265473 --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bsteam.go @@ -0,0 +1,11 @@ +// +build !nosteam + +package bridgemap + +import ( + bsteam "github.com/42wim/matterbridge/bridge/steam" +) + +func init() { + FullMap["steam"] = bsteam.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/btelegram.go b/teleirc/matterbridge/gateway/bridgemap/btelegram.go new file mode 100644 index 0000000..ec0bbe6 --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/btelegram.go @@ -0,0 +1,11 @@ +// +build !notelegram + +package bridgemap + +import ( + btelegram "github.com/42wim/matterbridge/bridge/telegram" +) + +func init() { + FullMap["telegram"] = btelegram.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bvk.go b/teleirc/matterbridge/gateway/bridgemap/bvk.go new file mode 100644 index 0000000..ea3de19 --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bvk.go @@ -0,0 +1,11 @@ +// +build !novk + +package bridgemap + +import ( + bvk "github.com/42wim/matterbridge/bridge/vk" +) + +func init() { + FullMap["vk"] = bvk.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bwhatsapp.go b/teleirc/matterbridge/gateway/bridgemap/bwhatsapp.go new file mode 100644 index 0000000..e7b72b0 --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bwhatsapp.go @@ -0,0 +1,12 @@ +// +build !nowhatsapp +// +build !whatsappmulti + +package bridgemap + +import ( + bwhatsapp "github.com/42wim/matterbridge/bridge/whatsapp" +) + +func init() { + FullMap["whatsapp"] = bwhatsapp.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bwhatsappmulti.go b/teleirc/matterbridge/gateway/bridgemap/bwhatsappmulti.go new file mode 100644 index 0000000..055c6da --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bwhatsappmulti.go @@ -0,0 +1,11 @@ +// +build whatsappmulti + +package bridgemap + +import ( + bwhatsapp "github.com/42wim/matterbridge/bridge/whatsappmulti" +) + +func init() { + FullMap["whatsapp"] = bwhatsapp.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bxmpp.go b/teleirc/matterbridge/gateway/bridgemap/bxmpp.go new file mode 100644 index 0000000..6988a7d --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bxmpp.go @@ -0,0 +1,11 @@ +// +build !noxmpp + +package bridgemap + +import ( + bxmpp "github.com/42wim/matterbridge/bridge/xmpp" +) + +func init() { + FullMap["xmpp"] = bxmpp.New +} diff --git a/teleirc/matterbridge/gateway/bridgemap/bzulip.go b/teleirc/matterbridge/gateway/bridgemap/bzulip.go new file mode 100644 index 0000000..967f0dd --- /dev/null +++ b/teleirc/matterbridge/gateway/bridgemap/bzulip.go @@ -0,0 +1,11 @@ +// +build !nozulip + +package bridgemap + +import ( + bzulip "github.com/42wim/matterbridge/bridge/zulip" +) + +func init() { + FullMap["zulip"] = bzulip.New +} diff --git a/teleirc/matterbridge/gateway/gateway.go b/teleirc/matterbridge/gateway/gateway.go new file mode 100644 index 0000000..a2d572b --- /dev/null +++ b/teleirc/matterbridge/gateway/gateway.go @@ -0,0 +1,674 @@ +package gateway + +import ( + "fmt" + "io/ioutil" + "os" + "regexp" + "strings" + "time" + + "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" + lru "github.com/hashicorp/golang-lru" + "github.com/kyokomi/emoji/v2" + "github.com/sirupsen/logrus" +) + +type Gateway struct { + config.Config + + Router *Router + MyConfig *config.Gateway + Bridges map[string]*bridge.Bridge + Channels map[string]*config.ChannelInfo + ChannelOptions map[string]config.ChannelOptions + Message chan config.Message + Name string + Messages *lru.Cache + + logger *logrus.Entry +} + +type BrMsgID struct { + br *bridge.Bridge + ID string + ChannelID string +} + +const apiProtocol = "api" + +// New creates a new Gateway object associated with the specified router and +// following the given configuration. +func New(rootLogger *logrus.Logger, cfg *config.Gateway, r *Router) *Gateway { + logger := rootLogger.WithFields(logrus.Fields{"prefix": "gateway"}) + + cache, _ := lru.New(5000) + gw := &Gateway{ + Channels: make(map[string]*config.ChannelInfo), + Message: r.Message, + Router: r, + Bridges: make(map[string]*bridge.Bridge), + Config: r.Config, + Messages: cache, + logger: logger, + } + if err := gw.AddConfig(cfg); err != nil { + logger.Errorf("Failed to add configuration to gateway: %#v", err) + } + return gw +} + +// FindCanonicalMsgID returns the ID under which a message was stored in the cache. +func (gw *Gateway) FindCanonicalMsgID(protocol string, mID string) string { + ID := protocol + " " + mID + if gw.Messages.Contains(ID) { + return ID + } + + // If not keyed, iterate through cache for downstream, and infer upstream. + for _, mid := range gw.Messages.Keys() { + v, _ := gw.Messages.Peek(mid) + ids := v.([]*BrMsgID) + for _, downstreamMsgObj := range ids { + if ID == downstreamMsgObj.ID { + return mid.(string) + } + } + } + return "" +} + +// AddBridge sets up a new bridge in the gateway object with the specified configuration. +func (gw *Gateway) AddBridge(cfg *config.Bridge) error { + br := gw.Router.getBridge(cfg.Account) + if br == nil { + gw.checkConfig(cfg) + br = bridge.New(cfg) + br.Config = gw.Router.Config + br.General = &gw.BridgeValues().General + br.Log = gw.logger.WithFields(logrus.Fields{"prefix": br.Protocol}) + brconfig := &bridge.Config{ + Remote: gw.Message, + Bridge: br, + } + // add the actual bridger for this protocol to this bridge using the bridgeMap + if _, ok := gw.Router.BridgeMap[br.Protocol]; !ok { + gw.logger.Fatalf("Incorrect protocol %s specified in gateway configuration %s, exiting.", br.Protocol, cfg.Account) + } + br.Bridger = gw.Router.BridgeMap[br.Protocol](brconfig) + } + gw.mapChannelsToBridge(br) + gw.Bridges[cfg.Account] = br + return nil +} + +func (gw *Gateway) checkConfig(cfg *config.Bridge) { + match := false + for _, key := range gw.Router.Config.Viper().AllKeys() { + if strings.HasPrefix(key, strings.ToLower(cfg.Account)) { + match = true + break + } + } + if !match { + gw.logger.Fatalf("Account %s defined in gateway %s but no configuration found, exiting.", cfg.Account, gw.Name) + } +} + +// AddConfig associates a new configuration with the gateway object. +func (gw *Gateway) AddConfig(cfg *config.Gateway) error { + gw.Name = cfg.Name + gw.MyConfig = cfg + if err := gw.mapChannels(); err != nil { + gw.logger.Errorf("mapChannels() failed: %s", err) + } + for _, br := range append(gw.MyConfig.In, append(gw.MyConfig.InOut, gw.MyConfig.Out...)...) { + br := br // scopelint + err := gw.AddBridge(&br) + if err != nil { + return err + } + } + return nil +} + +func (gw *Gateway) mapChannelsToBridge(br *bridge.Bridge) { + for ID, channel := range gw.Channels { + if br.Account == channel.Account { + br.Channels[ID] = *channel + } + } +} + +func (gw *Gateway) reconnectBridge(br *bridge.Bridge) { + if err := br.Disconnect(); err != nil { + gw.logger.Errorf("Disconnect() %s failed: %s", br.Account, err) + } + time.Sleep(time.Second * 5) +RECONNECT: + gw.logger.Infof("Reconnecting %s", br.Account) + err := br.Connect() + if err != nil { + gw.logger.Errorf("Reconnection failed: %s. Trying again in 60 seconds", err) + time.Sleep(time.Second * 60) + goto RECONNECT + } + br.Joined = make(map[string]bool) + if err := br.JoinChannels(); err != nil { + gw.logger.Errorf("JoinChannels() %s failed: %s", br.Account, err) + } +} + +func (gw *Gateway) mapChannelConfig(cfg []config.Bridge, direction string) { + for _, br := range cfg { + if isAPI(br.Account) { + br.Channel = apiProtocol + } + // make sure to lowercase irc channels in config #348 + if strings.HasPrefix(br.Account, "irc.") { + br.Channel = strings.ToLower(br.Channel) + } + if strings.HasPrefix(br.Account, "mattermost.") && strings.HasPrefix(br.Channel, "#") { + gw.logger.Errorf("Mattermost channels do not start with a #: remove the # in %s", br.Channel) + os.Exit(1) + } + if strings.HasPrefix(br.Account, "zulip.") && !strings.Contains(br.Channel, "/topic:") { + gw.logger.Errorf("Breaking change, since matterbridge 1.14.0 zulip channels need to specify the topic with channel/topic:mytopic in %s of %s", br.Channel, br.Account) + os.Exit(1) + } + ID := br.Channel + br.Account + if _, ok := gw.Channels[ID]; !ok { + channel := &config.ChannelInfo{ + Name: br.Channel, + Direction: direction, + ID: ID, + Options: br.Options, + Account: br.Account, + SameChannel: make(map[string]bool), + } + channel.SameChannel[gw.Name] = br.SameChannel + gw.Channels[channel.ID] = channel + } else { + // if we already have a key and it's not our current direction it means we have a bidirectional inout + if gw.Channels[ID].Direction != direction { + gw.Channels[ID].Direction = "inout" + } + } + gw.Channels[ID].SameChannel[gw.Name] = br.SameChannel + } +} + +func (gw *Gateway) mapChannels() error { + gw.mapChannelConfig(gw.MyConfig.In, "in") + gw.mapChannelConfig(gw.MyConfig.Out, "out") + gw.mapChannelConfig(gw.MyConfig.InOut, "inout") + return nil +} + +func (gw *Gateway) getDestChannel(msg *config.Message, dest bridge.Bridge) []config.ChannelInfo { + var channels []config.ChannelInfo + + // for messages received from the api check that the gateway is the specified one + if msg.Protocol == apiProtocol && gw.Name != msg.Gateway { + return channels + } + + // discord join/leave is for the whole bridge, isn't a per channel join/leave + if msg.Event == config.EventJoinLeave && getProtocol(msg) == "discord" && msg.Channel == "" { + for _, channel := range gw.Channels { + if channel.Account == dest.Account && strings.Contains(channel.Direction, "out") && + gw.validGatewayDest(msg) { + channels = append(channels, *channel) + } + } + return channels + } + + // if source channel is in only, do nothing + for _, channel := range gw.Channels { + // lookup the channel from the message + if channel.ID == getChannelID(msg) { + // we only have destinations if the original message is from an "in" (sending) channel + if !strings.Contains(channel.Direction, "in") { + return channels + } + continue + } + } + for _, channel := range gw.Channels { + if _, ok := gw.Channels[getChannelID(msg)]; !ok { + continue + } + + // do samechannelgateway logic + if channel.SameChannel[msg.Gateway] { + if msg.Channel == channel.Name && msg.Account != dest.Account { + channels = append(channels, *channel) + } + continue + } + if strings.Contains(channel.Direction, "out") && channel.Account == dest.Account && gw.validGatewayDest(msg) { + channels = append(channels, *channel) + } + } + return channels +} + +func (gw *Gateway) getDestMsgID(msgID string, dest *bridge.Bridge, channel *config.ChannelInfo) string { + if res, ok := gw.Messages.Get(msgID); ok { + IDs := res.([]*BrMsgID) + for _, id := range IDs { + // check protocol, bridge name and channelname + // for people that reuse the same bridge multiple times. see #342 + if dest.Protocol == id.br.Protocol && dest.Name == id.br.Name && channel.ID == id.ChannelID { + return strings.Replace(id.ID, dest.Protocol+" ", "", 1) + } + } + } + return "" +} + +// ignoreTextEmpty returns true if we need to ignore a message with an empty text. +func (gw *Gateway) ignoreTextEmpty(msg *config.Message) bool { + if msg.Text != "" { + return false + } + if msg.Event == config.EventUserTyping { + return false + } + // we have an attachment or actual bytes, do not ignore + if msg.Extra != nil && + (msg.Extra["attachments"] != nil || + len(msg.Extra["file"]) > 0 || + len(msg.Extra[config.EventFileFailureSize]) > 0) { + return false + } + gw.logger.Debugf("ignoring empty message %#v from %s", msg, msg.Account) + return true +} + +func (gw *Gateway) ignoreMessage(msg *config.Message) bool { + // if we don't have the bridge, ignore it + if _, ok := gw.Bridges[msg.Account]; !ok { + return true + } + + igNicks := strings.Fields(gw.Bridges[msg.Account].GetString("IgnoreNicks")) + igMessages := strings.Fields(gw.Bridges[msg.Account].GetString("IgnoreMessages")) + if gw.ignoreTextEmpty(msg) || gw.ignoreText(msg.Username, igNicks) || gw.ignoreText(msg.Text, igMessages) || gw.ignoreFilesComment(msg.Extra, igMessages) { + return true + } + + return false +} + +// ignoreFilesComment returns true if we need to ignore a file with matched comment. +func (gw *Gateway) ignoreFilesComment(extra map[string][]interface{}, igMessages []string) bool { + if extra == nil { + return false + } + for _, f := range extra["file"] { + fi, ok := f.(config.FileInfo) + if !ok { + continue + } + if gw.ignoreText(fi.Comment, igMessages) { + return true + } + } + return false +} + +func (gw *Gateway) modifyUsername(msg *config.Message, dest *bridge.Bridge) string { + if dest.GetBool("StripNick") { + re := regexp.MustCompile("[^a-zA-Z0-9]+") + msg.Username = re.ReplaceAllString(msg.Username, "") + } + nick := dest.GetString("RemoteNickFormat") + + // loop to replace nicks + br := gw.Bridges[msg.Account] + for _, outer := range br.GetStringSlice2D("ReplaceNicks") { + search := outer[0] + replace := outer[1] + // TODO move compile to bridge init somewhere + re, err := regexp.Compile(search) + if err != nil { + gw.logger.Errorf("regexp in %s failed: %s", msg.Account, err) + break + } + msg.Username = re.ReplaceAllString(msg.Username, replace) + } + + if len(msg.Username) > 0 { + // fix utf-8 issue #193 + i := 0 + for index := range msg.Username { + if i == 1 { + i = index + break + } + i++ + } + nick = strings.ReplaceAll(nick, "{NOPINGNICK}", msg.Username[:i]+"\u200b"+msg.Username[i:]) + } + + nick = strings.ReplaceAll(nick, "{BRIDGE}", br.Name) + nick = strings.ReplaceAll(nick, "{PROTOCOL}", br.Protocol) + nick = strings.ReplaceAll(nick, "{GATEWAY}", gw.Name) + nick = strings.ReplaceAll(nick, "{LABEL}", br.GetString("Label")) + nick = strings.ReplaceAll(nick, "{NICK}", msg.Username) + nick = strings.ReplaceAll(nick, "{USERID}", msg.UserID) + nick = strings.ReplaceAll(nick, "{CHANNEL}", msg.Channel) + tengoNick, err := gw.modifyUsernameTengo(msg, br) + if err != nil { + gw.logger.Errorf("modifyUsernameTengo error: %s", err) + } + nick = strings.ReplaceAll(nick, "{TENGO}", tengoNick) + return nick +} + +func (gw *Gateway) modifyAvatar(msg *config.Message, dest *bridge.Bridge) string { + iconurl := dest.GetString("IconURL") + iconurl = strings.Replace(iconurl, "{NICK}", msg.Username, -1) + if msg.Avatar == "" { + msg.Avatar = iconurl + } + return msg.Avatar +} + +func (gw *Gateway) modifyMessage(msg *config.Message) { + if gw.BridgeValues().General.TengoModifyMessage != "" { + gw.logger.Warnf("General TengoModifyMessage=%s is deprecated and will be removed in v1.20.0, please move to Tengo InMessage=%s", gw.BridgeValues().General.TengoModifyMessage, gw.BridgeValues().General.TengoModifyMessage) + } + + if err := modifyInMessageTengo(gw.BridgeValues().General.TengoModifyMessage, msg); err != nil { + gw.logger.Errorf("TengoModifyMessage failed: %s", err) + } + + inMessage := gw.BridgeValues().Tengo.InMessage + if inMessage == "" { + inMessage = gw.BridgeValues().Tengo.Message + if inMessage != "" { + gw.logger.Warnf("Tengo Message=%s is deprecated and will be removed in v1.20.0, please move to Tengo InMessage=%s", inMessage, inMessage) + } + } + + if err := modifyInMessageTengo(inMessage, msg); err != nil { + gw.logger.Errorf("Tengo.Message failed: %s", err) + } + + // replace :emoji: to unicode + emoji.ReplacePadding = "" + msg.Text = emoji.Sprint(msg.Text) + + br := gw.Bridges[msg.Account] + // loop to replace messages + for _, outer := range br.GetStringSlice2D("ReplaceMessages") { + search := outer[0] + replace := outer[1] + // TODO move compile to bridge init somewhere + re, err := regexp.Compile(search) + if err != nil { + gw.logger.Errorf("regexp in %s failed: %s", msg.Account, err) + break + } + msg.Text = re.ReplaceAllString(msg.Text, replace) + } + + gw.handleExtractNicks(msg) + + // messages from api have Gateway specified, don't overwrite + if msg.Protocol != apiProtocol { + msg.Gateway = gw.Name + } +} + +// SendMessage sends a message (with specified parentID) to the channel on the selected +// destination bridge and returns a message ID or an error. +func (gw *Gateway) SendMessage( + rmsg *config.Message, + dest *bridge.Bridge, + channel *config.ChannelInfo, + canonicalParentMsgID string, +) (string, error) { + msg := *rmsg + // Only send the avatar download event to ourselves. + if msg.Event == config.EventAvatarDownload { + if channel.ID != getChannelID(rmsg) { + return "", nil + } + } else { + // do not send to ourself for any other event + if channel.ID == getChannelID(rmsg) { + return "", nil + } + } + + // Only send irc notices to irc + if msg.Event == config.EventNoticeIRC && dest.Protocol != "irc" { + return "", nil + } + + // Too noisy to log like other events + debugSendMessage := "" + if msg.Event != config.EventUserTyping { + debugSendMessage = fmt.Sprintf("=> Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, rmsg.Channel, dest.Account, channel.Name) + } + + msg.Channel = channel.Name + msg.Avatar = gw.modifyAvatar(rmsg, dest) + msg.Username = gw.modifyUsername(rmsg, dest) + + // exclude file delete event as the msg ID here is the native file ID that needs to be deleted + if msg.Event != config.EventFileDelete { + msg.ID = gw.getDestMsgID(rmsg.Protocol+" "+rmsg.ID, dest, channel) + } + + // for api we need originchannel as channel + if dest.Protocol == apiProtocol { + msg.Channel = rmsg.Channel + } + + msg.ParentID = gw.getDestMsgID(canonicalParentMsgID, dest, channel) + if msg.ParentID == "" { + msg.ParentID = strings.Replace(canonicalParentMsgID, dest.Protocol+" ", "", 1) + } + + // if the parentID is still empty and we have a parentID set in the original message + // this means that we didn't find it in the cache so set it to a "msg-parent-not-found" constant + if msg.ParentID == "" && rmsg.ParentID != "" { + msg.ParentID = config.ParentIDNotFound + } + + 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" { + gw.Router.MattermostPlugin <- msg + } + + defer func(t time.Time) { + gw.logger.Debugf("=> Send from %s (%s) to %s (%s) took %s", msg.Account, rmsg.Channel, dest.Account, channel.Name, time.Since(t)) + }(time.Now()) + + mID, err := dest.Send(msg) + if err != nil { + return mID, err + } + + // append the message ID (mID) from this bridge (dest) to our brMsgIDs slice + if mID != "" { + gw.logger.Debugf("mID %s: %s", dest.Account, mID) + return mID, nil + // brMsgIDs = append(brMsgIDs, &BrMsgID{dest, dest.Protocol + " " + mID, channel.ID}) + } + return "", nil +} + +func (gw *Gateway) validGatewayDest(msg *config.Message) bool { + return msg.Gateway == gw.Name +} + +func getChannelID(msg *config.Message) string { + return msg.Channel + msg.Account +} + +func isAPI(account string) bool { + return strings.HasPrefix(account, "api.") +} + +// ignoreText returns true if text matches any of the input regexes. +func (gw *Gateway) ignoreText(text string, input []string) bool { + for _, entry := range input { + if entry == "" { + continue + } + // TODO do not compile regexps everytime + re, err := regexp.Compile(entry) + if err != nil { + gw.logger.Errorf("incorrect regexp %s", entry) + continue + } + if re.MatchString(text) { + gw.logger.Debugf("matching %s. ignoring %s", entry, text) + return true + } + } + return false +} + +func getProtocol(msg *config.Message) string { + p := strings.Split(msg.Account, ".") + return p[0] +} + +func modifyInMessageTengo(filename string, msg *config.Message) error { + if filename == "" { + return nil + } + res, err := ioutil.ReadFile(filename) + if err != nil { + return err + } + s := tengo.NewScript(res) + s.SetImports(stdlib.GetModuleMap(stdlib.AllModuleNames()...)) + _ = s.Add("msgText", msg.Text) + _ = s.Add("msgUsername", msg.Username) + _ = s.Add("msgUserID", msg.UserID) + _ = s.Add("msgAccount", msg.Account) + _ = s.Add("msgChannel", msg.Channel) + 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 +} + +func (gw *Gateway) modifyUsernameTengo(msg *config.Message, br *bridge.Bridge) (string, error) { + filename := gw.BridgeValues().Tengo.RemoteNickFormat + if filename == "" { + return "", nil + } + res, err := ioutil.ReadFile(filename) + if err != nil { + return "", err + } + s := tengo.NewScript(res) + s.SetImports(stdlib.GetModuleMap(stdlib.AllModuleNames()...)) + _ = s.Add("result", "") + _ = s.Add("msgText", msg.Text) + _ = s.Add("msgUsername", msg.Username) + _ = s.Add("msgUserID", msg.UserID) + _ = s.Add("nick", msg.Username) + _ = s.Add("msgAccount", msg.Account) + _ = s.Add("msgChannel", msg.Channel) + _ = s.Add("channel", msg.Channel) + _ = s.Add("msgProtocol", msg.Protocol) + _ = s.Add("remoteAccount", br.Account) + _ = s.Add("protocol", br.Protocol) + _ = s.Add("bridge", br.Name) + _ = s.Add("gateway", gw.Name) + c, err := s.Compile() + if err != nil { + return "", err + } + if err := c.Run(); err != nil { + return "", err + } + return c.Get("result").String(), nil +} + +func (gw *Gateway) modifyOutMessageTengo(origmsg *config.Message, msg *config.Message, br *bridge.Bridge) (bool, error) { + filename := gw.BridgeValues().Tengo.OutMessage + var ( + res []byte + err error + drop bool + ) + + if filename == "" { + res, err = internal.Asset("tengo/outmessage.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", origmsg.Account) + _ = s.Add("inProtocol", origmsg.Protocol) + _ = s.Add("inChannel", origmsg.Channel) + _ = s.Add("inGateway", origmsg.Gateway) + _ = s.Add("inEvent", origmsg.Event) + _ = s.Add("outAccount", br.Account) + _ = s.Add("outProtocol", br.Protocol) + _ = s.Add("outChannel", msg.Channel) + _ = s.Add("outGateway", gw.Name) + _ = s.Add("outEvent", msg.Event) + _ = s.Add("msgText", msg.Text) + _ = s.Add("msgUsername", msg.Username) + _ = s.Add("msgUserID", msg.UserID) + _ = s.Add("msgDrop", drop) + 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 +} diff --git a/teleirc/matterbridge/gateway/gateway_test.go b/teleirc/matterbridge/gateway/gateway_test.go new file mode 100644 index 0000000..2170058 --- /dev/null +++ b/teleirc/matterbridge/gateway/gateway_test.go @@ -0,0 +1,541 @@ +package gateway + +import ( + "fmt" + "io/ioutil" + "strconv" + "testing" + + "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/gateway/bridgemap" + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" +) + +var testconfig = []byte(` +[irc.freenode] +server="" +[mattermost.test] +server="" +[gitter.42wim] +server="" +[discord.test] +server="" +[slack.test] +server="" + +[[gateway]] + name = "bridge1" + enable=true + + [[gateway.inout]] + account = "irc.freenode" + channel = "#wimtesting" + + [[gateway.inout]] + account="gitter.42wim" + channel="42wim/testroom" + #channel="matterbridge/Lobby" + + [[gateway.inout]] + account = "discord.test" + channel = "general" + + [[gateway.inout]] + account="slack.test" + channel="testing" + `) + +var testconfig2 = []byte(` +[irc.freenode] +server="" +[mattermost.test] +server="" +[gitter.42wim] +server="" +[discord.test] +server="" +[slack.test] +server="" + +[[gateway]] + name = "bridge1" + enable=true + + [[gateway.in]] + account = "irc.freenode" + channel = "#wimtesting" + + [[gateway.in]] + account="gitter.42wim" + channel="42wim/testroom" + + [[gateway.inout]] + account = "discord.test" + channel = "general" + + [[gateway.out]] + account="slack.test" + channel="testing" +[[gateway]] + name = "bridge2" + enable=true + + [[gateway.in]] + account = "irc.freenode" + channel = "#wimtesting2" + + [[gateway.out]] + account="gitter.42wim" + channel="42wim/testroom" + + [[gateway.out]] + account = "discord.test" + channel = "general2" + `) + +var testconfig3 = []byte(` +[irc.zzz] +server="" +[telegram.zzz] +server="" +[slack.zzz] +server="" +[[gateway]] +name="bridge" +enable=true + + [[gateway.inout]] + account="irc.zzz" + channel="#main" + + [[gateway.inout]] + account="telegram.zzz" + channel="-1111111111111" + + [[gateway.inout]] + account="slack.zzz" + channel="irc" + +[[gateway]] +name="announcements" +enable=true + + [[gateway.in]] + account="telegram.zzz" + channel="-2222222222222" + + [[gateway.out]] + account="irc.zzz" + channel="#main" + + [[gateway.out]] + account="irc.zzz" + channel="#main-help" + + [[gateway.out]] + account="telegram.zzz" + channel="--333333333333" + + [[gateway.out]] + account="slack.zzz" + channel="general" + +[[gateway]] +name="bridge2" +enable=true + + [[gateway.inout]] + account="irc.zzz" + channel="#main-help" + + [[gateway.inout]] + account="telegram.zzz" + channel="--444444444444" + + +[[gateway]] +name="bridge3" +enable=true + + [[gateway.inout]] + account="irc.zzz" + channel="#main-telegram" + + [[gateway.inout]] + account="telegram.zzz" + channel="--333333333333" +`) + +const ( + ircTestAccount = "irc.zzz" + tgTestAccount = "telegram.zzz" + slackTestAccount = "slack.zzz" +) + +func maketestRouter(input []byte) *Router { + logger := logrus.New() + logger.SetOutput(ioutil.Discard) + cfg := config.NewConfigFromString(logger, input) + r, err := NewRouter(logger, cfg, bridgemap.FullMap) + if err != nil { + fmt.Println(err) + } + return r +} +func TestNewRouter(t *testing.T) { + r := maketestRouter(testconfig) + assert.Equal(t, 1, len(r.Gateways)) + assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges)) + assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels)) + r = maketestRouter(testconfig2) + assert.Equal(t, 2, len(r.Gateways)) + assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges)) + assert.Equal(t, 3, len(r.Gateways["bridge2"].Bridges)) + assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels)) + assert.Equal(t, 3, len(r.Gateways["bridge2"].Channels)) + assert.Equal(t, &config.ChannelInfo{ + Name: "42wim/testroom", + Direction: "out", + ID: "42wim/testroomgitter.42wim", + Account: "gitter.42wim", + SameChannel: map[string]bool{"bridge2": false}, + }, r.Gateways["bridge2"].Channels["42wim/testroomgitter.42wim"]) + assert.Equal(t, &config.ChannelInfo{ + Name: "42wim/testroom", + Direction: "in", + ID: "42wim/testroomgitter.42wim", + Account: "gitter.42wim", + SameChannel: map[string]bool{"bridge1": false}, + }, r.Gateways["bridge1"].Channels["42wim/testroomgitter.42wim"]) + assert.Equal(t, &config.ChannelInfo{ + Name: "general", + Direction: "inout", + ID: "generaldiscord.test", + Account: "discord.test", + SameChannel: map[string]bool{"bridge1": false}, + }, r.Gateways["bridge1"].Channels["generaldiscord.test"]) +} + +func TestGetDestChannel(t *testing.T) { + r := maketestRouter(testconfig2) + msg := &config.Message{Text: "test", Channel: "general", Account: "discord.test", Gateway: "bridge1", Protocol: "discord", Username: "test"} + for _, br := range r.Gateways["bridge1"].Bridges { + switch br.Account { + case "discord.test": + assert.Equal(t, []config.ChannelInfo{{ + Name: "general", + Account: "discord.test", + Direction: "inout", + ID: "generaldiscord.test", + SameChannel: map[string]bool{"bridge1": false}, + Options: config.ChannelOptions{Key: ""}, + }}, r.Gateways["bridge1"].getDestChannel(msg, *br)) + case "slack.test": + assert.Equal(t, []config.ChannelInfo{{ + Name: "testing", + Account: "slack.test", + Direction: "out", + ID: "testingslack.test", + SameChannel: map[string]bool{"bridge1": false}, + Options: config.ChannelOptions{Key: ""}, + }}, r.Gateways["bridge1"].getDestChannel(msg, *br)) + case "gitter.42wim": + assert.Equal(t, []config.ChannelInfo(nil), r.Gateways["bridge1"].getDestChannel(msg, *br)) + case "irc.freenode": + assert.Equal(t, []config.ChannelInfo(nil), r.Gateways["bridge1"].getDestChannel(msg, *br)) + } + } +} + +func TestGetDestChannelAdvanced(t *testing.T) { + r := maketestRouter(testconfig3) + var msgs []*config.Message + i := 0 + for _, gw := range r.Gateways { + for _, channel := range gw.Channels { + msgs = append(msgs, &config.Message{Text: "text" + strconv.Itoa(i), Channel: channel.Name, Account: channel.Account, Gateway: gw.Name, Username: "user" + strconv.Itoa(i)}) + i++ + } + } + hits := make(map[string]int) + for _, gw := range r.Gateways { + for _, br := range gw.Bridges { + for _, msg := range msgs { + channels := gw.getDestChannel(msg, *br) + if gw.Name != msg.Gateway { + assert.Equal(t, []config.ChannelInfo(nil), channels) + continue + } + switch gw.Name { + case "bridge": + if (msg.Channel == "#main" || msg.Channel == "-1111111111111" || msg.Channel == "irc") && + (msg.Account == ircTestAccount || msg.Account == tgTestAccount || msg.Account == slackTestAccount) { + hits[gw.Name]++ + switch br.Account { + case ircTestAccount: + assert.Equal(t, []config.ChannelInfo{{ + Name: "#main", + Account: ircTestAccount, + Direction: "inout", + ID: "#mainirc.zzz", + SameChannel: map[string]bool{"bridge": false}, + Options: config.ChannelOptions{Key: ""}, + }}, channels) + case tgTestAccount: + assert.Equal(t, []config.ChannelInfo{{ + Name: "-1111111111111", + Account: tgTestAccount, + Direction: "inout", + ID: "-1111111111111telegram.zzz", + SameChannel: map[string]bool{"bridge": false}, + Options: config.ChannelOptions{Key: ""}, + }}, channels) + case slackTestAccount: + assert.Equal(t, []config.ChannelInfo{{ + Name: "irc", + Account: slackTestAccount, + Direction: "inout", + ID: "ircslack.zzz", + SameChannel: map[string]bool{"bridge": false}, + Options: config.ChannelOptions{Key: ""}, + }}, channels) + } + } + case "bridge2": + if (msg.Channel == "#main-help" || msg.Channel == "--444444444444") && + (msg.Account == ircTestAccount || msg.Account == tgTestAccount) { + hits[gw.Name]++ + switch br.Account { + case ircTestAccount: + assert.Equal(t, []config.ChannelInfo{{ + Name: "#main-help", + Account: ircTestAccount, + Direction: "inout", + ID: "#main-helpirc.zzz", + SameChannel: map[string]bool{"bridge2": false}, + Options: config.ChannelOptions{Key: ""}, + }}, channels) + case tgTestAccount: + assert.Equal(t, []config.ChannelInfo{{ + Name: "--444444444444", + Account: tgTestAccount, + Direction: "inout", + ID: "--444444444444telegram.zzz", + SameChannel: map[string]bool{"bridge2": false}, + Options: config.ChannelOptions{Key: ""}, + }}, channels) + } + } + case "bridge3": + if (msg.Channel == "#main-telegram" || msg.Channel == "--333333333333") && + (msg.Account == ircTestAccount || msg.Account == tgTestAccount) { + hits[gw.Name]++ + switch br.Account { + case ircTestAccount: + assert.Equal(t, []config.ChannelInfo{{ + Name: "#main-telegram", + Account: ircTestAccount, + Direction: "inout", + ID: "#main-telegramirc.zzz", + SameChannel: map[string]bool{"bridge3": false}, + Options: config.ChannelOptions{Key: ""}, + }}, channels) + case tgTestAccount: + assert.Equal(t, []config.ChannelInfo{{ + Name: "--333333333333", + Account: tgTestAccount, + Direction: "inout", + ID: "--333333333333telegram.zzz", + SameChannel: map[string]bool{"bridge3": false}, + Options: config.ChannelOptions{Key: ""}, + }}, channels) + } + } + case "announcements": + if msg.Channel != "-2222222222222" && msg.Account != "telegram" { + assert.Equal(t, []config.ChannelInfo(nil), channels) + continue + } + hits[gw.Name]++ + switch br.Account { + case ircTestAccount: + assert.Len(t, channels, 2) + assert.Contains(t, channels, config.ChannelInfo{ + Name: "#main", + Account: ircTestAccount, + Direction: "out", + ID: "#mainirc.zzz", + SameChannel: map[string]bool{"announcements": false}, + Options: config.ChannelOptions{Key: ""}, + }) + assert.Contains(t, channels, config.ChannelInfo{ + Name: "#main-help", + Account: ircTestAccount, + Direction: "out", + ID: "#main-helpirc.zzz", + SameChannel: map[string]bool{"announcements": false}, + Options: config.ChannelOptions{Key: ""}, + }) + case slackTestAccount: + assert.Equal(t, []config.ChannelInfo{{ + Name: "general", + Account: slackTestAccount, + Direction: "out", + ID: "generalslack.zzz", + SameChannel: map[string]bool{"announcements": false}, + Options: config.ChannelOptions{Key: ""}, + }}, channels) + case tgTestAccount: + assert.Equal(t, []config.ChannelInfo{{ + Name: "--333333333333", + Account: tgTestAccount, + Direction: "out", + ID: "--333333333333telegram.zzz", + SameChannel: map[string]bool{"announcements": false}, + Options: config.ChannelOptions{Key: ""}, + }}, channels) + } + } + } + } + } + assert.Equal(t, map[string]int{"bridge3": 4, "bridge": 9, "announcements": 3, "bridge2": 4}, hits) +} + +type ignoreTestSuite struct { + suite.Suite + + gw *Gateway +} + +func TestIgnoreSuite(t *testing.T) { + s := &ignoreTestSuite{} + suite.Run(t, s) +} + +func (s *ignoreTestSuite) SetupSuite() { + logger := logrus.New() + logger.SetOutput(ioutil.Discard) + s.gw = &Gateway{logger: logrus.NewEntry(logger)} +} +func (s *ignoreTestSuite) TestIgnoreTextEmpty() { + extraFile := make(map[string][]interface{}) + extraAttach := make(map[string][]interface{}) + extraFailure := make(map[string][]interface{}) + extraFile["file"] = append(extraFile["file"], config.FileInfo{}) + extraAttach["attachments"] = append(extraAttach["attachments"], []string{}) + extraFailure[config.EventFileFailureSize] = append(extraFailure[config.EventFileFailureSize], config.FileInfo{}) + + msgTests := map[string]struct { + input *config.Message + output bool + }{ + "usertyping": { + input: &config.Message{Event: config.EventUserTyping}, + output: false, + }, + "file attach": { + input: &config.Message{Extra: extraFile}, + output: false, + }, + "attachments": { + input: &config.Message{Extra: extraAttach}, + output: false, + }, + config.EventFileFailureSize: { + input: &config.Message{Extra: extraFailure}, + output: false, + }, + "nil extra": { + input: &config.Message{Extra: nil}, + output: true, + }, + "empty": { + input: &config.Message{}, + output: true, + }, + } + for testname, testcase := range msgTests { + output := s.gw.ignoreTextEmpty(testcase.input) + s.Assert().Equalf(testcase.output, output, "case '%s' failed", testname) + } + +} + +func (s *ignoreTestSuite) TestIgnoreTexts() { + msgTests := map[string]struct { + input string + re []string + output bool + }{ + "no regex": { + input: "a text message", + re: []string{}, + output: false, + }, + "simple regex": { + input: "a text message", + re: []string{"text"}, + output: true, + }, + "multiple regex fail": { + input: "a text message", + re: []string{"abc", "123$"}, + output: false, + }, + "multiple regex pass": { + input: "a text message", + re: []string{"lala", "sage$"}, + output: true, + }, + } + for testname, testcase := range msgTests { + output := s.gw.ignoreText(testcase.input, testcase.re) + s.Assert().Equalf(testcase.output, output, "case '%s' failed", testname) + } +} + +func (s *ignoreTestSuite) TestIgnoreNicks() { + msgTests := map[string]struct { + input string + re []string + output bool + }{ + "no entry": { + input: "user", + re: []string{}, + output: false, + }, + "one entry": { + input: "user", + re: []string{"user"}, + output: true, + }, + "multiple entries": { + input: "user", + re: []string{"abc", "user"}, + output: true, + }, + "multiple entries fail": { + input: "user", + re: []string{"abc", "def"}, + output: false, + }, + } + for testname, testcase := range msgTests { + output := s.gw.ignoreText(testcase.input, testcase.re) + s.Assert().Equalf(testcase.output, output, "case '%s' failed", testname) + } +} + +func BenchmarkTengo(b *testing.B) { + msg := &config.Message{Username: "user", Text: "blah testing", Account: "protocol.account", Channel: "mychannel"} + for n := 0; n < b.N; n++ { + err := modifyInMessageTengo("bench.tengo", msg) + if err != nil { + return + } + } +} diff --git a/teleirc/matterbridge/gateway/handlers.go b/teleirc/matterbridge/gateway/handlers.go new file mode 100644 index 0000000..44cefe4 --- /dev/null +++ b/teleirc/matterbridge/gateway/handlers.go @@ -0,0 +1,275 @@ +package gateway + +import ( + "bytes" + "crypto/sha1" //nolint:gosec + "fmt" + "io/ioutil" + "net/http" + "os" + "path/filepath" + "regexp" + "strings" + "time" + + "github.com/42wim/matterbridge/bridge" + "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/gateway/bridgemap" +) + +// handleEventFailure handles failures and reconnects bridges. +func (r *Router) handleEventFailure(msg *config.Message) { + if msg.Event != config.EventFailure { + return + } + for _, gw := range r.Gateways { + for _, br := range gw.Bridges { + if msg.Account == br.Account { + go gw.reconnectBridge(br) + return + } + } + } +} + +// handleEventGetChannelMembers handles channel members +func (r *Router) handleEventGetChannelMembers(msg *config.Message) { + if msg.Event != config.EventGetChannelMembers { + return + } + for _, gw := range r.Gateways { + for _, br := range gw.Bridges { + if msg.Account == br.Account { + cMembers := msg.Extra[config.EventGetChannelMembers][0].(config.ChannelMembers) + r.logger.Debugf("Syncing channelmembers from %s", msg.Account) + br.SetChannelMembers(&cMembers) + return + } + } + } +} + +// handleEventRejoinChannels handles rejoining of channels. +func (r *Router) handleEventRejoinChannels(msg *config.Message) { + if msg.Event != config.EventRejoinChannels { + return + } + for _, gw := range r.Gateways { + for _, br := range gw.Bridges { + if msg.Account == br.Account { + br.Joined = make(map[string]bool) + if err := br.JoinChannels(); err != nil { + r.logger.Errorf("channel join failed for %s: %s", msg.Account, err) + } + } + } + } +} + +// handleFiles uploads or places all files on the given msg to the MediaServer and +// adds the new URL of the file on the MediaServer onto the given msg. +func (gw *Gateway) handleFiles(msg *config.Message) { + reg := regexp.MustCompile("[^a-zA-Z0-9]+") + + // If we don't have a attachfield or we don't have a mediaserver configured return + if msg.Extra == nil || + (gw.BridgeValues().General.MediaServerUpload == "" && + gw.BridgeValues().General.MediaDownloadPath == "") { + return + } + + // If we don't have files, nothing to upload. + if len(msg.Extra["file"]) == 0 { + return + } + + for i, f := range msg.Extra["file"] { + fi := f.(config.FileInfo) + ext := filepath.Ext(fi.Name) + fi.Name = fi.Name[0 : len(fi.Name)-len(ext)] + fi.Name = reg.ReplaceAllString(fi.Name, "_") + fi.Name += ext + + sha1sum := fmt.Sprintf("%x", sha1.Sum(*fi.Data))[:8] //nolint:gosec + + if gw.BridgeValues().General.MediaServerUpload != "" { + // Use MediaServerUpload. Upload using a PUT HTTP request and basicauth. + if err := gw.handleFilesUpload(&fi); err != nil { + gw.logger.Error(err) + continue + } + } else { + // Use MediaServerPath. Place the file on the current filesystem. + if err := gw.handleFilesLocal(&fi); err != nil { + gw.logger.Error(err) + continue + } + } + + // Download URL. + durl := gw.BridgeValues().General.MediaServerDownload + "/" + sha1sum + "/" + fi.Name + + gw.logger.Debugf("mediaserver download URL = %s", durl) + + // We uploaded/placed the file successfully. Add the SHA and URL. + extra := msg.Extra["file"][i].(config.FileInfo) + extra.URL = durl + extra.SHA = sha1sum + msg.Extra["file"][i] = extra + } +} + +// handleFilesUpload uses MediaServerUpload configuration to upload the file. +// Returns error on failure. +func (gw *Gateway) handleFilesUpload(fi *config.FileInfo) error { + client := &http.Client{ + Timeout: time.Second * 5, + } + // Use MediaServerUpload. Upload using a PUT HTTP request and basicauth. + sha1sum := fmt.Sprintf("%x", sha1.Sum(*fi.Data))[:8] //nolint:gosec + url := gw.BridgeValues().General.MediaServerUpload + "/" + sha1sum + "/" + fi.Name + + req, err := http.NewRequest("PUT", url, bytes.NewReader(*fi.Data)) + if err != nil { + return fmt.Errorf("mediaserver upload failed, could not create request: %#v", err) + } + + gw.logger.Debugf("mediaserver upload url: %s", url) + + req.Header.Set("Content-Type", "binary/octet-stream") + _, err = client.Do(req) + if err != nil { + return fmt.Errorf("mediaserver upload failed, could not Do request: %#v", err) + } + return nil +} + +// handleFilesLocal use MediaServerPath configuration, places the file on the current filesystem. +// Returns error on failure. +func (gw *Gateway) handleFilesLocal(fi *config.FileInfo) error { + sha1sum := fmt.Sprintf("%x", sha1.Sum(*fi.Data))[:8] //nolint:gosec + dir := gw.BridgeValues().General.MediaDownloadPath + "/" + sha1sum + err := os.Mkdir(dir, os.ModePerm) + if err != nil && !os.IsExist(err) { + return fmt.Errorf("mediaserver path failed, could not mkdir: %s %#v", err, err) + } + + path := dir + "/" + fi.Name + gw.logger.Debugf("mediaserver path placing file: %s", path) + + err = ioutil.WriteFile(path, *fi.Data, os.ModePerm) + if err != nil { + return fmt.Errorf("mediaserver path failed, could not writefile: %s %#v", err, err) + } + return nil +} + +// ignoreEvent returns true if we need to ignore this event for the specified destination bridge. +func (gw *Gateway) ignoreEvent(event string, dest *bridge.Bridge) bool { + switch event { + case config.EventAvatarDownload: + // Avatar downloads are only relevant for telegram and mattermost for now + if dest.Protocol != "mattermost" && dest.Protocol != "telegram" && dest.Protocol != "xmpp" { + return true + } + case config.EventJoinLeave: + // only relay join/part when configured + if !dest.GetBool("ShowJoinPart") { + return true + } + case config.EventTopicChange: + // only relay topic change when used in some way on other side + if !dest.GetBool("ShowTopicChange") && !dest.GetBool("SyncTopic") { + return true + } + } + return false +} + +// handleMessage makes sure the message get sent to the correct bridge/channels. +// Returns an array of msg ID's +func (gw *Gateway) handleMessage(rmsg *config.Message, dest *bridge.Bridge) []*BrMsgID { + var brMsgIDs []*BrMsgID + + // Not all bridges support "user is typing" indications so skip the message + // if the targeted bridge does not support it. + if rmsg.Event == config.EventUserTyping { + if _, ok := bridgemap.UserTypingSupport[dest.Protocol]; !ok { + return nil + } + } + + // if we have an attached file, or other info + if rmsg.Extra != nil && len(rmsg.Extra[config.EventFileFailureSize]) != 0 && rmsg.Text == "" { + return brMsgIDs + } + + if gw.ignoreEvent(rmsg.Event, dest) { + return brMsgIDs + } + + // broadcast to every out channel (irc QUIT) + if rmsg.Channel == "" && rmsg.Event != config.EventJoinLeave { + gw.logger.Debug("empty channel") + return brMsgIDs + } + + // Get the ID of the parent message in thread + var canonicalParentMsgID string + if rmsg.ParentID != "" && dest.GetBool("PreserveThreading") { + canonicalParentMsgID = gw.FindCanonicalMsgID(rmsg.Protocol, rmsg.ParentID) + } + + channels := gw.getDestChannel(rmsg, *dest) + for idx := range channels { + channel := &channels[idx] + msgID, err := gw.SendMessage(rmsg, dest, channel, canonicalParentMsgID) + if err != nil { + gw.logger.Errorf("SendMessage failed: %s", err) + continue + } + if msgID == "" { + continue + } + brMsgIDs = append(brMsgIDs, &BrMsgID{dest, dest.Protocol + " " + msgID, channel.ID}) + } + return brMsgIDs +} + +func (gw *Gateway) handleExtractNicks(msg *config.Message) { + var err error + br := gw.Bridges[msg.Account] + for _, outer := range br.GetStringSlice2D("ExtractNicks") { + search := outer[0] + replace := outer[1] + msg.Username, msg.Text, err = extractNick(search, replace, msg.Username, msg.Text) + if err != nil { + gw.logger.Errorf("regexp in %s failed: %s", msg.Account, err) + break + } + } +} + +// extractNick searches for a username (based on "search" a regular expression). +// if this matches it extracts a nick (based on "extract" another regular expression) from text +// and replaces username with this result. +// returns error if the regexp doesn't compile. +func extractNick(search, extract, username, text string) (string, string, error) { + re, err := regexp.Compile(search) + if err != nil { + return username, text, err + } + if re.MatchString(username) { + re, err = regexp.Compile(extract) + if err != nil { + return username, text, err + } + res := re.FindAllStringSubmatch(text, 1) + // only replace if we have exactly 1 match + if len(res) > 0 && len(res[0]) == 2 { + username = res[0][1] + text = strings.Replace(text, res[0][0], "", 1) + } + } + return username, text, nil +} diff --git a/teleirc/matterbridge/gateway/handlers_test.go b/teleirc/matterbridge/gateway/handlers_test.go new file mode 100644 index 0000000..db7988a --- /dev/null +++ b/teleirc/matterbridge/gateway/handlers_test.go @@ -0,0 +1,75 @@ +package gateway + +import ( + "github.com/42wim/matterbridge/bridge" + "github.com/42wim/matterbridge/bridge/config" + "github.com/stretchr/testify/assert" + + "testing" +) + +func TestIgnoreEvent(t *testing.T) { + eventTests := map[string]struct { + input string + dest *bridge.Bridge + output bool + }{ + "avatar mattermost": { + input: config.EventAvatarDownload, + dest: &bridge.Bridge{Protocol: "mattermost"}, + output: false, + }, + "avatar slack": { + input: config.EventAvatarDownload, + dest: &bridge.Bridge{Protocol: "slack"}, + output: true, + }, + "avatar telegram": { + input: config.EventAvatarDownload, + dest: &bridge.Bridge{Protocol: "telegram"}, + output: false, + }, + } + gw := &Gateway{} + for testname, testcase := range eventTests { + output := gw.ignoreEvent(testcase.input, testcase.dest) + assert.Equalf(t, testcase.output, output, "case '%s' failed", testname) + } + +} + +func TestExtractNick(t *testing.T) { + eventTests := map[string]struct { + search string + extract string + username string + text string + resultUsername string + resultText string + }{ + "test1": { + search: "fromgitter", + extract: "<(.*?)>\\s+", + username: "fromgitter", + text: "<userx> blahblah", + resultUsername: "userx", + resultText: "blahblah", + }, + "test2": { + search: "<.*?bot>", + //extract: `\((.*?)\)\s+`, + extract: "\\((.*?)\\)\\s+", + username: "<matterbot>", + text: "(userx) blahblah (abc) test", + resultUsername: "userx", + resultText: "blahblah (abc) test", + }, + } + // gw := &Gateway{} + for testname, testcase := range eventTests { + resultUsername, resultText, _ := extractNick(testcase.search, testcase.extract, testcase.username, testcase.text) + assert.Equalf(t, testcase.resultUsername, resultUsername, "case '%s' failed", testname) + assert.Equalf(t, testcase.resultText, resultText, "case '%s' failed", testname) + } + +} diff --git a/teleirc/matterbridge/gateway/router.go b/teleirc/matterbridge/gateway/router.go new file mode 100644 index 0000000..a0d5f40 --- /dev/null +++ b/teleirc/matterbridge/gateway/router.go @@ -0,0 +1,193 @@ +package gateway + +import ( + "fmt" + "sync" + "time" + + "github.com/42wim/matterbridge/bridge" + "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/gateway/samechannel" + "github.com/sirupsen/logrus" +) + +type Router struct { + config.Config + sync.RWMutex + + BridgeMap map[string]bridge.Factory + Gateways map[string]*Gateway + Message chan config.Message + MattermostPlugin chan config.Message + + logger *logrus.Entry +} + +// NewRouter initializes a new Matterbridge router for the specified configuration and +// sets up all required gateways. +func NewRouter(rootLogger *logrus.Logger, cfg config.Config, bridgeMap map[string]bridge.Factory) (*Router, error) { + logger := rootLogger.WithFields(logrus.Fields{"prefix": "router"}) + + r := &Router{ + Config: cfg, + BridgeMap: bridgeMap, + Message: make(chan config.Message), + MattermostPlugin: make(chan config.Message), + Gateways: make(map[string]*Gateway), + logger: logger, + } + sgw := samechannel.New(cfg) + gwconfigs := append(sgw.GetConfig(), cfg.BridgeValues().Gateway...) + + for idx := range gwconfigs { + entry := &gwconfigs[idx] + if !entry.Enable { + continue + } + if entry.Name == "" { + return nil, fmt.Errorf("%s", "Gateway without name found") + } + if _, ok := r.Gateways[entry.Name]; ok { + return nil, fmt.Errorf("Gateway with name %s already exists", entry.Name) + } + r.Gateways[entry.Name] = New(rootLogger, entry, r) + } + return r, nil +} + +// Start will connect all gateways belonging to this router and subsequently route messages +// between them. +func (r *Router) Start() error { + m := make(map[string]*bridge.Bridge) + if len(r.Gateways) == 0 { + return fmt.Errorf("no [[gateway]] configured. See https://github.com/42wim/matterbridge/wiki/How-to-create-your-config for more info") + } + for _, gw := range r.Gateways { + r.logger.Infof("Parsing gateway %s", gw.Name) + if len(gw.Bridges) == 0 { + return fmt.Errorf("no bridges configured for gateway %s. See https://github.com/42wim/matterbridge/wiki/How-to-create-your-config for more info", gw.Name) + } + for _, br := range gw.Bridges { + m[br.Account] = br + } + } + for _, br := range m { + r.logger.Infof("Starting bridge: %s ", br.Account) + err := br.Connect() + if err != nil { + e := fmt.Errorf("Bridge %s failed to start: %v", br.Account, err) + if r.disableBridge(br, e) { + continue + } + return e + } + err = br.JoinChannels() + if err != nil { + e := fmt.Errorf("Bridge %s failed to join channel: %v", br.Account, err) + if r.disableBridge(br, e) { + continue + } + return e + } + } + // remove unused bridges + for _, gw := range r.Gateways { + for i, br := range gw.Bridges { + if br.Bridger == nil { + r.logger.Errorf("removing failed bridge %s", i) + delete(gw.Bridges, i) + } + } + } + go r.handleReceive() + //go r.updateChannelMembers() + return nil +} + +// disableBridge returns true and empties a bridge if we have IgnoreFailureOnStart configured +// otherwise returns false +func (r *Router) disableBridge(br *bridge.Bridge, err error) bool { + if r.BridgeValues().General.IgnoreFailureOnStart { + r.logger.Error(err) + // setting this bridge empty + *br = bridge.Bridge{ + Log: br.Log, + } + return true + } + return false +} + +func (r *Router) getBridge(account string) *bridge.Bridge { + for _, gw := range r.Gateways { + if br, ok := gw.Bridges[account]; ok { + return br + } + } + return nil +} + +func (r *Router) handleReceive() { + for msg := range r.Message { + msg := msg // scopelint + r.handleEventGetChannelMembers(&msg) + r.handleEventFailure(&msg) + r.handleEventRejoinChannels(&msg) + + // Set message protocol based on the account it came from + msg.Protocol = r.getBridge(msg.Account).Protocol + + filesHandled := false + for _, gw := range r.Gateways { + // record all the message ID's of the different bridges + var msgIDs []*BrMsgID + if gw.ignoreMessage(&msg) { + continue + } + msg.Timestamp = time.Now() + gw.modifyMessage(&msg) + if !filesHandled { + gw.handleFiles(&msg) + filesHandled = true + } + for _, br := range gw.Bridges { + msgIDs = append(msgIDs, gw.handleMessage(&msg, br)...) + } + + if msg.ID != "" { + _, exists := gw.Messages.Get(msg.Protocol + " " + msg.ID) + + // Only add the message ID if it doesn't already exist + // + // For some bridges we always add/update the message ID. + // This is necessary as msgIDs will change if a bridge returns + // a different ID in response to edits. + if !exists { + gw.Messages.Add(msg.Protocol+" "+msg.ID, msgIDs) + } + } + } + } +} + +// updateChannelMembers sends every minute an GetChannelMembers event to all bridges. +func (r *Router) updateChannelMembers() { + // TODO sleep a minute because slack can take a while + // fix this by having actually connectionDone events send to the router + time.Sleep(time.Minute) + for { + for _, gw := range r.Gateways { + for _, br := range gw.Bridges { + // only for slack now + if br.Protocol != "slack" { + continue + } + r.logger.Debugf("sending %s to %s", config.EventGetChannelMembers, br.Account) + if _, err := br.Send(config.Message{Event: config.EventGetChannelMembers}); err != nil { + r.logger.Errorf("updateChannelMembers: %s", err) + } + } + } + time.Sleep(time.Minute) + } +} diff --git a/teleirc/matterbridge/gateway/samechannel/samechannel.go b/teleirc/matterbridge/gateway/samechannel/samechannel.go new file mode 100644 index 0000000..4b6016c --- /dev/null +++ b/teleirc/matterbridge/gateway/samechannel/samechannel.go @@ -0,0 +1,28 @@ +package samechannel + +import ( + "github.com/42wim/matterbridge/bridge/config" +) + +type SameChannelGateway struct { + config.Config +} + +func New(cfg config.Config) *SameChannelGateway { + return &SameChannelGateway{Config: cfg} +} + +func (sgw *SameChannelGateway) GetConfig() []config.Gateway { + var gwconfigs []config.Gateway + cfg := sgw.Config + for _, gw := range cfg.BridgeValues().SameChannelGateway { + gwconfig := config.Gateway{Name: gw.Name, Enable: gw.Enable} + for _, account := range gw.Accounts { + for _, channel := range gw.Channels { + gwconfig.InOut = append(gwconfig.InOut, config.Bridge{Account: account, Channel: channel, SameChannel: true}) + } + } + gwconfigs = append(gwconfigs, gwconfig) + } + return gwconfigs +} diff --git a/teleirc/matterbridge/gateway/samechannel/samechannel_test.go b/teleirc/matterbridge/gateway/samechannel/samechannel_test.go new file mode 100644 index 0000000..17d816a --- /dev/null +++ b/teleirc/matterbridge/gateway/samechannel/samechannel_test.go @@ -0,0 +1,77 @@ +package samechannel + +import ( + "io/ioutil" + "testing" + + "github.com/42wim/matterbridge/bridge/config" + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" +) + +const testConfig = ` +[mattermost.test] +[slack.test] + +[[samechannelgateway]] + enable = true + name = "blah" + accounts = [ "mattermost.test","slack.test" ] + channels = [ "testing","testing2","testing10"] +` + +var ( + expectedConfig = config.Gateway{ + Name: "blah", + Enable: true, + In: []config.Bridge(nil), + Out: []config.Bridge(nil), + InOut: []config.Bridge{ + { + Account: "mattermost.test", + Channel: "testing", + Options: config.ChannelOptions{Key: ""}, + SameChannel: true, + }, + { + Account: "mattermost.test", + Channel: "testing2", + Options: config.ChannelOptions{Key: ""}, + SameChannel: true, + }, + { + Account: "mattermost.test", + Channel: "testing10", + Options: config.ChannelOptions{Key: ""}, + SameChannel: true, + }, + { + Account: "slack.test", + Channel: "testing", + Options: config.ChannelOptions{Key: ""}, + SameChannel: true, + }, + { + Account: "slack.test", + Channel: "testing2", + Options: config.ChannelOptions{Key: ""}, + SameChannel: true, + }, + { + Account: "slack.test", + Channel: "testing10", + Options: config.ChannelOptions{Key: ""}, + SameChannel: true, + }, + }, + } +) + +func TestGetConfig(t *testing.T) { + logger := logrus.New() + logger.SetOutput(ioutil.Discard) + cfg := config.NewConfigFromString(logger, []byte(testConfig)) + sgw := New(cfg) + configs := sgw.GetConfig() + assert.Equal(t, []config.Gateway{expectedConfig}, configs) +} |
