mirror of
https://github.com/42wim/matterbridge.git
synced 2024-11-29 22:22:01 -08:00
Add support for mattermost v6
This commit is contained in:
parent
20f6c05ec5
commit
b56f80b1b8
@ -1,9 +1,11 @@
|
|||||||
|
//go:build cgo
|
||||||
// +build cgo
|
// +build cgo
|
||||||
|
|
||||||
package helper
|
package helper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/Benau/tgsconverter/libtgsconverter"
|
"github.com/Benau/tgsconverter/libtgsconverter"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
@ -4,7 +4,9 @@ import (
|
|||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
"github.com/42wim/matterbridge/bridge/helper"
|
"github.com/42wim/matterbridge/bridge/helper"
|
||||||
"github.com/42wim/matterbridge/matterclient"
|
"github.com/42wim/matterbridge/matterclient"
|
||||||
|
matterclient6 "github.com/matterbridge/matterclient"
|
||||||
"github.com/mattermost/mattermost-server/v5/model"
|
"github.com/mattermost/mattermost-server/v5/model"
|
||||||
|
model6 "github.com/mattermost/mattermost-server/v6/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
// handleDownloadAvatar downloads the avatar of userid from channel
|
// 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{}),
|
Extra: make(map[string][]interface{}),
|
||||||
}
|
}
|
||||||
if _, ok := b.avatarMap[userid]; !ok {
|
if _, ok := b.avatarMap[userid]; !ok {
|
||||||
data, resp := b.mc.Client.GetProfileImage(userid, "")
|
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 {
|
if resp.Error != nil {
|
||||||
b.Log.Errorf("ProfileImage download failed for %#v %s", userid, resp.Error)
|
b.Log.Errorf("ProfileImage download failed for %#v %s", userid, resp.Error)
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
b.Log.Error(err)
|
b.Log.Error(err)
|
||||||
return
|
return
|
||||||
@ -38,6 +54,10 @@ func (b *Bmattermost) handleDownloadAvatar(userid string, channel string) {
|
|||||||
|
|
||||||
// handleDownloadFile handles file download
|
// handleDownloadFile handles file download
|
||||||
func (b *Bmattermost) handleDownloadFile(rmsg *config.Message, id string) error {
|
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)
|
url, _ := b.mc.Client.GetFileLink(id)
|
||||||
finfo, resp := b.mc.Client.GetFileInfo(id)
|
finfo, resp := b.mc.Client.GetFileInfo(id)
|
||||||
if resp.Error != nil {
|
if resp.Error != nil {
|
||||||
@ -55,6 +75,25 @@ func (b *Bmattermost) handleDownloadFile(rmsg *config.Message, id string) error
|
|||||||
return nil
|
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() {
|
func (b *Bmattermost) handleMatter() {
|
||||||
messages := make(chan *config.Message)
|
messages := make(chan *config.Message)
|
||||||
if b.GetString("WebhookBindAddress") != "" {
|
if b.GetString("WebhookBindAddress") != "" {
|
||||||
@ -87,6 +126,12 @@ func (b *Bmattermost) handleMatter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bmattermost) handleMatterClient(messages chan *config.Message) {
|
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 {
|
for message := range b.mc.MessageChan {
|
||||||
b.Log.Debugf("%#v", message.Raw.Data)
|
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) {
|
func (b *Bmattermost) handleMatterHook(messages chan *config.Message) {
|
||||||
for {
|
for {
|
||||||
message := b.mh.Receive()
|
message := b.mh.Receive()
|
||||||
@ -155,6 +255,10 @@ func (b *Bmattermost) handleMatterHook(messages chan *config.Message) {
|
|||||||
|
|
||||||
// handleUploadFile handles native upload of files
|
// handleUploadFile handles native upload of files
|
||||||
func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) {
|
func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) {
|
||||||
|
if b.mc6 != nil {
|
||||||
|
return b.handleUploadFile6(msg)
|
||||||
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var res, id string
|
var res, id string
|
||||||
channelID := b.mc.GetChannelId(msg.Channel, b.TeamID)
|
channelID := b.mc.GetChannelId(msg.Channel, b.TeamID)
|
||||||
@ -173,6 +277,26 @@ func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) {
|
|||||||
return res, err
|
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) {
|
func (b *Bmattermost) handleProps(rmsg *config.Message, message *matterclient.Message) {
|
||||||
props := message.Post.Props
|
props := message.Post.Props
|
||||||
if props == nil {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
package bmattermost
|
package bmattermost
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
"github.com/42wim/matterbridge/bridge/helper"
|
"github.com/42wim/matterbridge/bridge/helper"
|
||||||
"github.com/42wim/matterbridge/matterclient"
|
"github.com/42wim/matterbridge/matterclient"
|
||||||
"github.com/42wim/matterbridge/matterhook"
|
"github.com/42wim/matterbridge/matterhook"
|
||||||
|
matterclient6 "github.com/matterbridge/matterclient"
|
||||||
"github.com/mattermost/mattermost-server/v5/model"
|
"github.com/mattermost/mattermost-server/v5/model"
|
||||||
|
model6 "github.com/mattermost/mattermost-server/v6/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Bmattermost) doConnectWebhookBind() error {
|
func (b *Bmattermost) doConnectWebhookBind() error {
|
||||||
@ -15,25 +18,47 @@ func (b *Bmattermost) doConnectWebhookBind() error {
|
|||||||
case b.GetString("WebhookURL") != "":
|
case b.GetString("WebhookURL") != "":
|
||||||
b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
|
b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
|
||||||
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
||||||
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
matterhook.Config{
|
||||||
BindAddress: b.GetString("WebhookBindAddress")})
|
InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
|
BindAddress: b.GetString("WebhookBindAddress"),
|
||||||
|
})
|
||||||
case b.GetString("Token") != "":
|
case b.GetString("Token") != "":
|
||||||
b.Log.Info("Connecting using token (sending)")
|
b.Log.Info("Connecting using token (sending)")
|
||||||
|
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()
|
err := b.apiLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
case b.GetString("Login") != "":
|
case b.GetString("Login") != "":
|
||||||
b.Log.Info("Connecting using login/password (sending)")
|
b.Log.Info("Connecting using login/password (sending)")
|
||||||
|
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()
|
err := b.apiLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
||||||
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
||||||
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
matterhook.Config{
|
||||||
BindAddress: b.GetString("WebhookBindAddress")})
|
InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
|
BindAddress: b.GetString("WebhookBindAddress"),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -41,21 +66,41 @@ func (b *Bmattermost) doConnectWebhookBind() error {
|
|||||||
func (b *Bmattermost) doConnectWebhookURL() error {
|
func (b *Bmattermost) doConnectWebhookURL() error {
|
||||||
b.Log.Info("Connecting using webhookurl (sending)")
|
b.Log.Info("Connecting using webhookurl (sending)")
|
||||||
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
||||||
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
matterhook.Config{
|
||||||
DisableServer: true})
|
InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
|
DisableServer: true,
|
||||||
|
})
|
||||||
if b.GetString("Token") != "" {
|
if b.GetString("Token") != "" {
|
||||||
b.Log.Info("Connecting using token (receiving)")
|
b.Log.Info("Connecting using token (receiving)")
|
||||||
|
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()
|
err := b.apiLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else if b.GetString("Login") != "" {
|
} else if b.GetString("Login") != "" {
|
||||||
b.Log.Info("Connecting using login/password (receiving)")
|
b.Log.Info("Connecting using login/password (receiving)")
|
||||||
|
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()
|
err := b.apiLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,6 +129,31 @@ func (b *Bmattermost) apiLogin() error {
|
|||||||
return nil
|
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
|
// replaceAction replace the message with the correct action (/me) code
|
||||||
func (b *Bmattermost) replaceAction(text string) (string, bool) {
|
func (b *Bmattermost) replaceAction(text string) (string, bool) {
|
||||||
if strings.HasPrefix(text, "*") && strings.HasSuffix(text, "*") {
|
if strings.HasPrefix(text, "*") && strings.HasSuffix(text, "*") {
|
||||||
@ -223,3 +293,84 @@ func (b *Bmattermost) skipMessage(message *matterclient.Message) bool {
|
|||||||
}
|
}
|
||||||
return false
|
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")
|
||||||
|
}
|
||||||
|
@ -3,18 +3,22 @@ package bmattermost
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge"
|
"github.com/42wim/matterbridge/bridge"
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
"github.com/42wim/matterbridge/bridge/helper"
|
"github.com/42wim/matterbridge/bridge/helper"
|
||||||
"github.com/42wim/matterbridge/matterclient"
|
"github.com/42wim/matterbridge/matterclient"
|
||||||
"github.com/42wim/matterbridge/matterhook"
|
"github.com/42wim/matterbridge/matterhook"
|
||||||
|
matterclient6 "github.com/matterbridge/matterclient"
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bmattermost struct {
|
type Bmattermost struct {
|
||||||
mh *matterhook.Client
|
mh *matterhook.Client
|
||||||
mc *matterclient.MMClient
|
mc *matterclient.MMClient
|
||||||
|
mc6 *matterclient6.Client
|
||||||
|
v6 bool
|
||||||
uuid string
|
uuid string
|
||||||
TeamID string
|
TeamID string
|
||||||
*bridge.Config
|
*bridge.Config
|
||||||
@ -25,7 +29,10 @@ const mattermostPlugin = "mattermost.plugin"
|
|||||||
|
|
||||||
func New(cfg *bridge.Config) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
b := &Bmattermost{Config: cfg, avatarMap: make(map[string]string)}
|
b := &Bmattermost{Config: cfg, avatarMap: make(map[string]string)}
|
||||||
|
|
||||||
|
b.v6 = b.GetBool("v6")
|
||||||
b.uuid = xid.New().String()
|
b.uuid = xid.New().String()
|
||||||
|
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,6 +44,13 @@ func (b *Bmattermost) Connect() error {
|
|||||||
if b.Account == mattermostPlugin {
|
if b.Account == mattermostPlugin {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(b.getVersion(), "6.") {
|
||||||
|
if !b.v6 {
|
||||||
|
b.v6 = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if b.GetString("WebhookBindAddress") != "" {
|
if b.GetString("WebhookBindAddress") != "" {
|
||||||
if err := b.doConnectWebhookBind(); err != nil {
|
if err := b.doConnectWebhookBind(); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -53,17 +67,35 @@ func (b *Bmattermost) Connect() error {
|
|||||||
return nil
|
return nil
|
||||||
case b.GetString("Token") != "":
|
case b.GetString("Token") != "":
|
||||||
b.Log.Info("Connecting using token (sending and receiving)")
|
b.Log.Info("Connecting using token (sending and receiving)")
|
||||||
|
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()
|
err := b.apiLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
go b.handleMatter()
|
go b.handleMatter()
|
||||||
case b.GetString("Login") != "":
|
case b.GetString("Login") != "":
|
||||||
b.Log.Info("Connecting using login/password (sending and receiving)")
|
b.Log.Info("Connecting using login/password (sending and receiving)")
|
||||||
|
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()
|
err := b.apiLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
go b.handleMatter()
|
go b.handleMatter()
|
||||||
}
|
}
|
||||||
if b.GetString("WebhookBindAddress") == "" && b.GetString("WebhookURL") == "" &&
|
if b.GetString("WebhookBindAddress") == "" && b.GetString("WebhookURL") == "" &&
|
||||||
@ -83,10 +115,20 @@ func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error {
|
|||||||
}
|
}
|
||||||
// we can only join channels using the API
|
// we can only join channels using the API
|
||||||
if b.GetString("WebhookURL") == "" && b.GetString("WebhookBindAddress") == "" {
|
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 == "" {
|
if id == "" {
|
||||||
return fmt.Errorf("Could not find channel ID for channel %s", channel.Name)
|
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 b.mc.JoinChannel(id)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -118,6 +160,10 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) {
|
|||||||
if msg.ID == "" {
|
if msg.ID == "" {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
if b.mc6 != nil {
|
||||||
|
return msg.ID, b.mc6.DeleteMessage(msg.ID) // nolint:wrapcheck
|
||||||
|
}
|
||||||
|
|
||||||
return msg.ID, b.mc.DeleteMessage(msg.ID)
|
return msg.ID, b.mc.DeleteMessage(msg.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,20 +175,34 @@ 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)
|
// we only can reply to the root of the thread, not to a specific ID (like discord for example does)
|
||||||
if msg.ParentID != "" {
|
if msg.ParentID != "" {
|
||||||
|
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, "")
|
post, res := b.mc.Client.GetPost(msg.ParentID, "")
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
b.Log.Errorf("getting post %s failed: %s", msg.ParentID, res.Error.DetailedError)
|
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
|
// Upload a file if it exists
|
||||||
if msg.Extra != nil {
|
if msg.Extra != nil {
|
||||||
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
||||||
|
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 {
|
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)
|
b.Log.Errorf("PostMessage failed: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if len(msg.Extra["file"]) > 0 {
|
if len(msg.Extra["file"]) > 0 {
|
||||||
return b.handleUploadFile(&msg)
|
return b.handleUploadFile(&msg)
|
||||||
}
|
}
|
||||||
@ -155,9 +215,17 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) {
|
|||||||
|
|
||||||
// Edit message if we have an ID
|
// Edit message if we have an ID
|
||||||
if msg.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)
|
return b.mc.EditMessage(msg.ID, msg.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post normal message
|
// 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)
|
return b.mc.PostMessage(b.mc.GetChannelId(msg.Channel, b.TeamID), msg.Text, msg.ParentID)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user