From b56f80b1b82d58c40919598662ef6184d617976c Mon Sep 17 00:00:00 2001 From: Wim Date: Sat, 16 Oct 2021 23:11:51 +0200 Subject: [PATCH] Add support for mattermost v6 --- bridge/helper/libtgsconverter.go | 2 + bridge/mattermost/handlers.go | 162 +++++++++++++++++++++++++- bridge/mattermost/helpers.go | 187 ++++++++++++++++++++++++++++--- bridge/mattermost/mattermost.go | 94 +++++++++++++--- 4 files changed, 409 insertions(+), 36 deletions(-) diff --git a/bridge/helper/libtgsconverter.go b/bridge/helper/libtgsconverter.go index 7181da90..7b2dffbf 100644 --- a/bridge/helper/libtgsconverter.go +++ b/bridge/helper/libtgsconverter.go @@ -1,9 +1,11 @@ +//go:build cgo // +build cgo package helper import ( "fmt" + "github.com/Benau/tgsconverter/libtgsconverter" "github.com/sirupsen/logrus" ) diff --git a/bridge/mattermost/handlers.go b/bridge/mattermost/handlers.go index 58af43a8..2b386c02 100644 --- a/bridge/mattermost/handlers.go +++ b/bridge/mattermost/handlers.go @@ -4,7 +4,9 @@ import ( "github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/matterclient" + matterclient6 "github.com/matterbridge/matterclient" "github.com/mattermost/mattermost-server/v5/model" + model6 "github.com/mattermost/mattermost-server/v6/model" ) // handleDownloadAvatar downloads the avatar of userid from channel @@ -21,12 +23,26 @@ func (b *Bmattermost) handleDownloadAvatar(userid string, channel string) { Extra: make(map[string][]interface{}), } if _, ok := b.avatarMap[userid]; !ok { - data, resp := b.mc.Client.GetProfileImage(userid, "") - if resp.Error != nil { - b.Log.Errorf("ProfileImage download failed for %#v %s", userid, resp.Error) - return + var ( + data []byte + err error + resp *model.Response + ) + if b.mc6 != nil { + data, _, err = b.mc6.Client.GetProfileImage(userid, "") + if err != nil { + b.Log.Errorf("ProfileImage download failed for %#v %s", userid, err) + return + } + } else { + data, resp = b.mc.Client.GetProfileImage(userid, "") + if resp.Error != nil { + b.Log.Errorf("ProfileImage download failed for %#v %s", userid, resp.Error) + return + } } - err := helper.HandleDownloadSize(b.Log, &rmsg, userid+".png", int64(len(data)), b.General) + + err = helper.HandleDownloadSize(b.Log, &rmsg, userid+".png", int64(len(data)), b.General) if err != nil { b.Log.Error(err) return @@ -38,6 +54,10 @@ func (b *Bmattermost) handleDownloadAvatar(userid string, channel string) { // handleDownloadFile handles file download func (b *Bmattermost) handleDownloadFile(rmsg *config.Message, id string) error { + if b.mc6 != nil { + return b.handleDownloadFile6(rmsg, id) + } + url, _ := b.mc.Client.GetFileLink(id) finfo, resp := b.mc.Client.GetFileInfo(id) if resp.Error != nil { @@ -55,6 +75,25 @@ func (b *Bmattermost) handleDownloadFile(rmsg *config.Message, id string) error return nil } +// nolint:wrapcheck +func (b *Bmattermost) handleDownloadFile6(rmsg *config.Message, id string) error { + url, _, _ := b.mc6.Client.GetFileLink(id) + finfo, _, err := b.mc6.Client.GetFileInfo(id) + if err != nil { + return err + } + err = helper.HandleDownloadSize(b.Log, rmsg, finfo.Name, finfo.Size, b.General) + if err != nil { + return err + } + data, _, err := b.mc6.Client.DownloadFile(id, true) + if err != nil { + return err + } + helper.HandleDownloadData(b.Log, rmsg, finfo.Name, rmsg.Text, url, &data, b.General) + return nil +} + func (b *Bmattermost) handleMatter() { messages := make(chan *config.Message) if b.GetString("WebhookBindAddress") != "" { @@ -87,6 +126,12 @@ func (b *Bmattermost) handleMatter() { } func (b *Bmattermost) handleMatterClient(messages chan *config.Message) { + if b.mc6 != nil { + b.Log.Debug("starting matterclient6") + b.handleMatterClient6(messages) + return + } + for message := range b.mc.MessageChan { b.Log.Debugf("%#v", message.Raw.Data) @@ -140,6 +185,61 @@ func (b *Bmattermost) handleMatterClient(messages chan *config.Message) { } } +// nolint:cyclop +func (b *Bmattermost) handleMatterClient6(messages chan *config.Message) { + for message := range b.mc6.MessageChan { + b.Log.Debugf("%#v", message.Raw.GetData()) + + if b.skipMessage6(message) { + b.Log.Debugf("Skipped message: %#v", message) + continue + } + + // only download avatars if we have a place to upload them (configured mediaserver) + if b.General.MediaServerUpload != "" || b.General.MediaDownloadPath != "" { + b.handleDownloadAvatar(message.UserID, message.Channel) + } + + b.Log.Debugf("== Receiving event %#v", message) + + rmsg := &config.Message{ + Username: message.Username, + UserID: message.UserID, + Channel: message.Channel, + Text: message.Text, + ID: message.Post.Id, + ParentID: message.Post.RootId, // ParentID is obsolete with mattermost + Extra: make(map[string][]interface{}), + } + + // handle mattermost post properties (override username and attachments) + b.handleProps6(rmsg, message) + + // create a text for bridges that don't support native editing + if message.Raw.EventType() == model6.WebsocketEventPostEdited && !b.GetBool("EditDisable") { + rmsg.Text = message.Text + b.GetString("EditSuffix") + } + + if message.Raw.EventType() == model6.WebsocketEventPostDeleted { + rmsg.Event = config.EventMsgDelete + } + + for _, id := range message.Post.FileIds { + err := b.handleDownloadFile(rmsg, id) + if err != nil { + b.Log.Errorf("download failed: %s", err) + } + } + + // Use nickname instead of username if defined + if nick := b.mc6.GetNickName(rmsg.UserID); nick != "" { + rmsg.Username = nick + } + + messages <- rmsg + } +} + func (b *Bmattermost) handleMatterHook(messages chan *config.Message) { for { message := b.mh.Receive() @@ -155,6 +255,10 @@ func (b *Bmattermost) handleMatterHook(messages chan *config.Message) { // handleUploadFile handles native upload of files func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) { + if b.mc6 != nil { + return b.handleUploadFile6(msg) + } + var err error var res, id string channelID := b.mc.GetChannelId(msg.Channel, b.TeamID) @@ -173,6 +277,26 @@ func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) { return res, err } +// nolint:forcetypeassert,wrapcheck +func (b *Bmattermost) handleUploadFile6(msg *config.Message) (string, error) { + var err error + var res, id string + channelID := b.mc6.GetChannelID(msg.Channel, b.TeamID) + for _, f := range msg.Extra["file"] { + fi := f.(config.FileInfo) + id, err = b.mc6.UploadFile(*fi.Data, channelID, fi.Name) + if err != nil { + return "", err + } + msg.Text = fi.Comment + if b.GetBool("PrefixMessagesWithNick") { + msg.Text = msg.Username + msg.Text + } + res, err = b.mc6.PostMessageWithFiles(channelID, msg.Text, msg.ParentID, []string{id}) + } + return res, err +} + func (b *Bmattermost) handleProps(rmsg *config.Message, message *matterclient.Message) { props := message.Post.Props if props == nil { @@ -197,3 +321,31 @@ func (b *Bmattermost) handleProps(rmsg *config.Message, message *matterclient.Me } } } + +// nolint:forcetypeassert +func (b *Bmattermost) handleProps6(rmsg *config.Message, message *matterclient6.Message) { + props := message.Post.Props + if props == nil { + return + } + if _, ok := props["override_username"].(string); ok { + rmsg.Username = props["override_username"].(string) + } + if _, ok := props["attachments"].([]interface{}); ok { + rmsg.Extra["attachments"] = props["attachments"].([]interface{}) + if rmsg.Text != "" { + return + } + + for _, attachment := range rmsg.Extra["attachments"] { + attach := attachment.(map[string]interface{}) + if attach["text"].(string) != "" { + rmsg.Text += attach["text"].(string) + continue + } + if attach["fallback"].(string) != "" { + rmsg.Text += attach["fallback"].(string) + } + } + } +} diff --git a/bridge/mattermost/helpers.go b/bridge/mattermost/helpers.go index 7e52eef0..f0128139 100644 --- a/bridge/mattermost/helpers.go +++ b/bridge/mattermost/helpers.go @@ -1,13 +1,16 @@ package bmattermost import ( + "net/http" "strings" "github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/matterclient" "github.com/42wim/matterbridge/matterhook" + matterclient6 "github.com/matterbridge/matterclient" "github.com/mattermost/mattermost-server/v5/model" + model6 "github.com/mattermost/mattermost-server/v6/model" ) func (b *Bmattermost) doConnectWebhookBind() error { @@ -15,25 +18,47 @@ func (b *Bmattermost) doConnectWebhookBind() error { case b.GetString("WebhookURL") != "": b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)") b.mh = matterhook.New(b.GetString("WebhookURL"), - matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"), - BindAddress: b.GetString("WebhookBindAddress")}) + matterhook.Config{ + InsecureSkipVerify: b.GetBool("SkipTLSVerify"), + BindAddress: b.GetString("WebhookBindAddress"), + }) case b.GetString("Token") != "": b.Log.Info("Connecting using token (sending)") - err := b.apiLogin() - if err != nil { - return err + b.Log.Infof("Using mattermost v6 methods: %t", b.v6) + + if b.v6 { + err := b.apiLogin6() + if err != nil { + return err + } + } else { + err := b.apiLogin() + if err != nil { + return err + } } case b.GetString("Login") != "": b.Log.Info("Connecting using login/password (sending)") - err := b.apiLogin() - if err != nil { - return err + b.Log.Infof("Using mattermost v6 methods: %t", b.v6) + + if b.v6 { + err := b.apiLogin6() + if err != nil { + return err + } + } else { + err := b.apiLogin() + if err != nil { + return err + } } default: b.Log.Info("Connecting using webhookbindaddress (receiving)") b.mh = matterhook.New(b.GetString("WebhookURL"), - matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"), - BindAddress: b.GetString("WebhookBindAddress")}) + matterhook.Config{ + InsecureSkipVerify: b.GetBool("SkipTLSVerify"), + BindAddress: b.GetString("WebhookBindAddress"), + }) } return nil } @@ -41,19 +66,39 @@ func (b *Bmattermost) doConnectWebhookBind() error { func (b *Bmattermost) doConnectWebhookURL() error { b.Log.Info("Connecting using webhookurl (sending)") b.mh = matterhook.New(b.GetString("WebhookURL"), - matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"), - DisableServer: true}) + matterhook.Config{ + InsecureSkipVerify: b.GetBool("SkipTLSVerify"), + DisableServer: true, + }) if b.GetString("Token") != "" { b.Log.Info("Connecting using token (receiving)") - err := b.apiLogin() - if err != nil { - return err + b.Log.Infof("Using mattermost v6 methods: %t", b.v6) + + if b.v6 { + err := b.apiLogin6() + if err != nil { + return err + } + } else { + err := b.apiLogin() + if err != nil { + return err + } } } else if b.GetString("Login") != "" { b.Log.Info("Connecting using login/password (receiving)") - err := b.apiLogin() - if err != nil { - return err + b.Log.Infof("Using mattermost v6 methods: %t", b.v6) + + if b.v6 { + err := b.apiLogin6() + if err != nil { + return err + } + } else { + err := b.apiLogin() + if err != nil { + return err + } } } return nil @@ -84,6 +129,31 @@ func (b *Bmattermost) apiLogin() error { return nil } +// nolint:wrapcheck +func (b *Bmattermost) apiLogin6() error { + password := b.GetString("Password") + if b.GetString("Token") != "" { + password = "token=" + b.GetString("Token") + } + + b.mc6 = matterclient6.New(b.GetString("Login"), password, b.GetString("Team"), b.GetString("Server"), "") + if b.GetBool("debug") { + b.mc6.SetLogLevel("debug") + } + b.mc6.SkipTLSVerify = b.GetBool("SkipTLSVerify") + b.mc6.SkipVersionCheck = b.GetBool("SkipVersionCheck") + b.mc6.NoTLS = b.GetBool("NoTLS") + b.Log.Infof("Connecting %s (team: %s) on %s", b.GetString("Login"), b.GetString("Team"), b.GetString("Server")) + + if err := b.mc6.Login(); err != nil { + return err + } + + b.Log.Info("Connection succeeded") + b.TeamID = b.mc6.GetTeamID() + return nil +} + // replaceAction replace the message with the correct action (/me) code func (b *Bmattermost) replaceAction(text string) (string, bool) { if strings.HasPrefix(text, "*") && strings.HasSuffix(text, "*") { @@ -223,3 +293,84 @@ func (b *Bmattermost) skipMessage(message *matterclient.Message) bool { } return false } + +// skipMessages returns true if this message should not be handled +// nolint:gocyclo,cyclop +func (b *Bmattermost) skipMessage6(message *matterclient6.Message) bool { + // Handle join/leave + if message.Type == "system_join_leave" || + message.Type == "system_join_channel" || + message.Type == "system_leave_channel" { + if b.GetBool("nosendjoinpart") { + return true + } + b.Log.Debugf("Sending JOIN_LEAVE event from %s to gateway", b.Account) + b.Remote <- config.Message{ + Username: "system", + Text: message.Text, + Channel: message.Channel, + Account: b.Account, + Event: config.EventJoinLeave, + } + return true + } + + // Handle edited messages + if (message.Raw.EventType() == model6.WebsocketEventPostEdited) && b.GetBool("EditDisable") { + return true + } + + // Ignore non-post messages + if message.Post == nil { + b.Log.Debugf("ignoring nil message.Post: %#v", message) + return true + } + + // Ignore messages sent from matterbridge + if message.Post.Props != nil { + if _, ok := message.Post.Props["matterbridge_"+b.uuid].(bool); ok { + b.Log.Debugf("sent by matterbridge, ignoring") + return true + } + } + + // Ignore messages sent from a user logged in as the bot + if b.mc6.User.Username == message.Username { + return true + } + + // if the message has reactions don't repost it (for now, until we can correlate reaction with message) + if message.Post.HasReactions { + return true + } + + // ignore messages from other teams than ours + if message.Raw.GetData()["team_id"].(string) != b.TeamID { + return true + } + + // only handle posted, edited or deleted events + if !(message.Raw.EventType() == "posted" || message.Raw.EventType() == model6.WebsocketEventPostEdited || + message.Raw.EventType() == model6.WebsocketEventPostDeleted) { + return true + } + return false +} + +func (b *Bmattermost) getVersion() string { + proto := "https" + + if b.GetBool("notls") { + proto = "http" + } + + resp, err := http.Get(proto + "://" + b.GetString("server")) + if err != nil { + b.Log.Error("failed getting version") + return "" + } + + defer resp.Body.Close() + + return resp.Header.Get("X-Version-Id") +} diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index bb44c48a..4c7c05b5 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -3,18 +3,22 @@ package bmattermost import ( "errors" "fmt" + "strings" "github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/matterclient" "github.com/42wim/matterbridge/matterhook" + matterclient6 "github.com/matterbridge/matterclient" "github.com/rs/xid" ) type Bmattermost struct { mh *matterhook.Client mc *matterclient.MMClient + mc6 *matterclient6.Client + v6 bool uuid string TeamID string *bridge.Config @@ -25,7 +29,10 @@ const mattermostPlugin = "mattermost.plugin" func New(cfg *bridge.Config) bridge.Bridger { b := &Bmattermost{Config: cfg, avatarMap: make(map[string]string)} + + b.v6 = b.GetBool("v6") b.uuid = xid.New().String() + return b } @@ -37,6 +44,13 @@ func (b *Bmattermost) Connect() error { if b.Account == mattermostPlugin { return nil } + + if strings.HasPrefix(b.getVersion(), "6.") { + if !b.v6 { + b.v6 = true + } + } + if b.GetString("WebhookBindAddress") != "" { if err := b.doConnectWebhookBind(); err != nil { return err @@ -53,16 +67,34 @@ func (b *Bmattermost) Connect() error { return nil case b.GetString("Token") != "": b.Log.Info("Connecting using token (sending and receiving)") - err := b.apiLogin() - if err != nil { - return err + b.Log.Infof("Using mattermost v6 methods: %t", b.v6) + + if b.v6 { + err := b.apiLogin6() + if err != nil { + return err + } + } else { + err := b.apiLogin() + if err != nil { + return err + } } go b.handleMatter() case b.GetString("Login") != "": b.Log.Info("Connecting using login/password (sending and receiving)") - err := b.apiLogin() - if err != nil { - return err + b.Log.Infof("Using mattermost v6 methods: %t", b.v6) + + if b.v6 { + err := b.apiLogin6() + if err != nil { + return err + } + } else { + err := b.apiLogin() + if err != nil { + return err + } } go b.handleMatter() } @@ -83,10 +115,20 @@ func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error { } // we can only join channels using the API if b.GetString("WebhookURL") == "" && b.GetString("WebhookBindAddress") == "" { - id := b.mc.GetChannelId(channel.Name, b.TeamID) + var id string + if b.mc6 != nil { + id = b.mc6.GetChannelID(channel.Name, b.TeamID) + } else { + id = b.mc.GetChannelId(channel.Name, b.TeamID) + } if id == "" { return fmt.Errorf("Could not find channel ID for channel %s", channel.Name) } + + if b.mc6 != nil { + return b.mc6.JoinChannel(id) // nolint:wrapcheck + } + return b.mc.JoinChannel(id) } return nil @@ -118,6 +160,10 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { if msg.ID == "" { return "", nil } + if b.mc6 != nil { + return msg.ID, b.mc6.DeleteMessage(msg.ID) // nolint:wrapcheck + } + return msg.ID, b.mc.DeleteMessage(msg.ID) } @@ -129,18 +175,32 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { // we only can reply to the root of the thread, not to a specific ID (like discord for example does) if msg.ParentID != "" { - post, res := b.mc.Client.GetPost(msg.ParentID, "") - if res.Error != nil { - b.Log.Errorf("getting post %s failed: %s", msg.ParentID, res.Error.DetailedError) + if b.mc6 != nil { + post, _, err := b.mc6.Client.GetPost(msg.ParentID, "") + if err != nil { + b.Log.Errorf("getting post %s failed: %s", msg.ParentID, err) + } + msg.ParentID = post.RootId + } else { + post, res := b.mc.Client.GetPost(msg.ParentID, "") + if res.Error != nil { + b.Log.Errorf("getting post %s failed: %s", msg.ParentID, res.Error.DetailedError) + } + msg.ParentID = post.RootId } - msg.ParentID = post.RootId } // Upload a file if it exists if msg.Extra != nil { for _, rmsg := range helper.HandleExtra(&msg, b.General) { - if _, err := b.mc.PostMessage(b.mc.GetChannelId(rmsg.Channel, b.TeamID), rmsg.Username+rmsg.Text, msg.ParentID); err != nil { - b.Log.Errorf("PostMessage failed: %s", err) + if b.mc6 != nil { + if _, err := b.mc6.PostMessage(b.mc.GetChannelId(rmsg.Channel, b.TeamID), rmsg.Username+rmsg.Text, msg.ParentID); err != nil { + b.Log.Errorf("PostMessage failed: %s", err) + } + } else { + if _, err := b.mc.PostMessage(b.mc.GetChannelId(rmsg.Channel, b.TeamID), rmsg.Username+rmsg.Text, msg.ParentID); err != nil { + b.Log.Errorf("PostMessage failed: %s", err) + } } } if len(msg.Extra["file"]) > 0 { @@ -155,9 +215,17 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { // Edit message if we have an ID if msg.ID != "" { + if b.mc6 != nil { + return b.mc6.EditMessage(msg.ID, msg.Text) // nolint:wrapcheck + } + return b.mc.EditMessage(msg.ID, msg.Text) } // Post normal message + if b.mc6 != nil { + return b.mc6.PostMessage(b.mc6.GetChannelID(msg.Channel, b.TeamID), msg.Text, msg.ParentID) // nolint:wrapcheck + } + return b.mc.PostMessage(b.mc.GetChannelId(msg.Channel, b.TeamID), msg.Text, msg.ParentID) }