2016-08-14 19:48:51 +00:00
|
|
|
package bmattermost
|
|
|
|
|
|
|
|
import (
|
2017-07-15 14:49:47 +00:00
|
|
|
"errors"
|
2017-08-30 12:01:17 +00:00
|
|
|
"fmt"
|
2021-10-16 21:11:51 +00:00
|
|
|
"strings"
|
2018-06-09 10:47:40 +00:00
|
|
|
|
2018-02-26 23:33:21 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
2016-08-14 19:48:51 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2018-02-03 00:11:11 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2016-08-14 19:48:51 +00:00
|
|
|
"github.com/42wim/matterbridge/matterclient"
|
|
|
|
"github.com/42wim/matterbridge/matterhook"
|
2021-10-16 21:11:51 +00:00
|
|
|
matterclient6 "github.com/matterbridge/matterclient"
|
2018-05-27 19:50:00 +00:00
|
|
|
"github.com/rs/xid"
|
2016-08-14 19:48:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Bmattermost struct {
|
2018-02-23 22:07:23 +00:00
|
|
|
mh *matterhook.Client
|
|
|
|
mc *matterclient.MMClient
|
2021-10-16 21:11:51 +00:00
|
|
|
mc6 *matterclient6.Client
|
|
|
|
v6 bool
|
2018-05-27 19:50:00 +00:00
|
|
|
uuid string
|
2018-02-24 22:22:15 +00:00
|
|
|
TeamID string
|
2018-03-04 22:52:14 +00:00
|
|
|
*bridge.Config
|
2018-02-19 23:54:35 +00:00
|
|
|
avatarMap map[string]string
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 19:40:15 +00:00
|
|
|
const mattermostPlugin = "mattermost.plugin"
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
|
|
|
b := &Bmattermost{Config: cfg, avatarMap: make(map[string]string)}
|
2021-10-16 21:11:51 +00:00
|
|
|
|
|
|
|
b.v6 = b.GetBool("v6")
|
2018-05-27 19:50:00 +00:00
|
|
|
b.uuid = xid.New().String()
|
2021-10-16 21:11:51 +00:00
|
|
|
|
2016-08-15 21:16:07 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bmattermost) Command(cmd string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bmattermost) Connect() error {
|
2018-11-13 19:40:15 +00:00
|
|
|
if b.Account == mattermostPlugin {
|
2018-11-11 21:42:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-16 21:11:51 +00:00
|
|
|
|
|
|
|
if strings.HasPrefix(b.getVersion(), "6.") {
|
|
|
|
if !b.v6 {
|
|
|
|
b.v6 = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("WebhookBindAddress") != "" {
|
2018-11-30 23:49:08 +00:00
|
|
|
if err := b.doConnectWebhookBind(); err != nil {
|
|
|
|
return err
|
2017-07-15 14:49:47 +00:00
|
|
|
}
|
|
|
|
go b.handleMatter()
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-08 21:01:29 +00:00
|
|
|
switch {
|
|
|
|
case b.GetString("WebhookURL") != "":
|
2018-11-30 23:49:08 +00:00
|
|
|
if err := b.doConnectWebhookURL(); err != nil {
|
|
|
|
return err
|
2017-07-15 14:49:47 +00:00
|
|
|
}
|
2018-11-30 23:49:08 +00:00
|
|
|
go b.handleMatter()
|
2017-07-15 14:49:47 +00:00
|
|
|
return nil
|
2018-11-08 21:01:29 +00:00
|
|
|
case b.GetString("Token") != "":
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting using token (sending and receiving)")
|
2021-10-16 21:11:51 +00:00
|
|
|
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
|
|
|
|
}
|
2017-08-23 20:49:42 +00:00
|
|
|
}
|
|
|
|
go b.handleMatter()
|
2018-11-08 21:01:29 +00:00
|
|
|
case b.GetString("Login") != "":
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting using login/password (sending and receiving)")
|
2021-10-16 21:11:51 +00:00
|
|
|
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
|
|
|
|
}
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
2017-07-15 14:49:47 +00:00
|
|
|
go b.handleMatter()
|
|
|
|
}
|
2018-11-30 23:49:08 +00:00
|
|
|
if b.GetString("WebhookBindAddress") == "" && b.GetString("WebhookURL") == "" &&
|
|
|
|
b.GetString("Login") == "" && b.GetString("Token") == "" {
|
2018-02-24 22:22:15 +00:00
|
|
|
return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Token/Login/Password/Server/Team configured")
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
2016-08-15 21:16:07 +00:00
|
|
|
return nil
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 20:12:02 +00:00
|
|
|
func (b *Bmattermost) Disconnect() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-12 12:51:41 +00:00
|
|
|
func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error {
|
2018-11-13 19:40:15 +00:00
|
|
|
if b.Account == mattermostPlugin {
|
2018-11-11 21:42:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-20 10:20:44 +00:00
|
|
|
// we can only join channels using the API
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("WebhookURL") == "" && b.GetString("WebhookBindAddress") == "" {
|
2021-10-16 21:11:51 +00:00
|
|
|
var id string
|
|
|
|
if b.mc6 != nil {
|
|
|
|
id = b.mc6.GetChannelID(channel.Name, b.TeamID)
|
|
|
|
} else {
|
|
|
|
id = b.mc.GetChannelId(channel.Name, b.TeamID)
|
|
|
|
}
|
2017-08-30 12:01:17 +00:00
|
|
|
if id == "" {
|
|
|
|
return fmt.Errorf("Could not find channel ID for channel %s", channel.Name)
|
|
|
|
}
|
2021-10-16 21:11:51 +00:00
|
|
|
|
|
|
|
if b.mc6 != nil {
|
|
|
|
return b.mc6.JoinChannel(id) // nolint:wrapcheck
|
|
|
|
}
|
|
|
|
|
2017-08-30 12:01:17 +00:00
|
|
|
return b.mc.JoinChannel(id)
|
2016-09-20 10:20:44 +00:00
|
|
|
}
|
|
|
|
return nil
|
2016-09-18 17:21:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 20:59:37 +00:00
|
|
|
func (b *Bmattermost) Send(msg config.Message) (string, error) {
|
2018-11-13 19:40:15 +00:00
|
|
|
if b.Account == mattermostPlugin {
|
2018-11-11 21:42:33 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("=> Receiving %#v", msg)
|
2018-02-23 22:07:23 +00:00
|
|
|
|
|
|
|
// Make a action /me of the message
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventUserAction {
|
2017-07-30 15:48:23 +00:00
|
|
|
msg.Text = "*" + msg.Text + "*"
|
|
|
|
}
|
2016-08-14 19:48:51 +00:00
|
|
|
|
2018-02-19 23:54:35 +00:00
|
|
|
// map the file SHA to our user (caches the avatar)
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventAvatarDownload {
|
2018-02-23 22:07:23 +00:00
|
|
|
return b.cacheAvatar(&msg)
|
2018-02-19 23:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 22:07:23 +00:00
|
|
|
// Use webhook to send the message
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("WebhookURL") != "" {
|
2018-02-23 22:07:23 +00:00
|
|
|
return b.sendWebhook(msg)
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
2018-02-23 22:07:23 +00:00
|
|
|
|
|
|
|
// Delete message
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventMsgDelete {
|
2017-09-11 20:45:15 +00:00
|
|
|
if msg.ID == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
2021-10-16 21:11:51 +00:00
|
|
|
if b.mc6 != nil {
|
|
|
|
return msg.ID, b.mc6.DeleteMessage(msg.ID) // nolint:wrapcheck
|
|
|
|
}
|
|
|
|
|
2017-09-11 20:45:15 +00:00
|
|
|
return msg.ID, b.mc.DeleteMessage(msg.ID)
|
|
|
|
}
|
2018-02-23 22:07:23 +00:00
|
|
|
|
2019-04-19 21:31:45 +00:00
|
|
|
// Handle prefix hint for unthreaded messages.
|
2020-12-31 18:01:57 +00:00
|
|
|
if msg.ParentNotFound() {
|
2019-04-19 21:31:45 +00:00
|
|
|
msg.ParentID = ""
|
|
|
|
msg.Text = fmt.Sprintf("[thread]: %s", msg.Text)
|
|
|
|
}
|
|
|
|
|
2020-12-31 15:59:47 +00:00
|
|
|
// we only can reply to the root of the thread, not to a specific ID (like discord for example does)
|
|
|
|
if msg.ParentID != "" {
|
2021-10-16 21:11:51 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-01-09 23:37:09 +00:00
|
|
|
if post.RootId != "" {
|
|
|
|
msg.ParentID = post.RootId
|
|
|
|
}
|
2021-10-16 21:11:51 +00:00
|
|
|
} 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)
|
|
|
|
}
|
2022-01-09 23:37:09 +00:00
|
|
|
if post.RootId != "" {
|
|
|
|
msg.ParentID = post.RootId
|
|
|
|
}
|
2020-12-31 15:59:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 22:07:23 +00:00
|
|
|
// Upload a file if it exists
|
2017-09-21 20:35:21 +00:00
|
|
|
if msg.Extra != nil {
|
2018-02-03 00:11:11 +00:00
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
2021-10-16 21:11:51 +00:00
|
|
|
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)
|
|
|
|
}
|
2018-11-30 23:49:08 +00:00
|
|
|
}
|
2018-02-03 00:11:11 +00:00
|
|
|
}
|
2017-09-21 21:15:04 +00:00
|
|
|
if len(msg.Extra["file"]) > 0 {
|
2018-02-23 22:07:23 +00:00
|
|
|
return b.handleUploadFile(&msg)
|
2017-09-21 20:35:21 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-23 22:07:23 +00:00
|
|
|
|
|
|
|
// Prepend nick if configured
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetBool("PrefixMessagesWithNick") {
|
2018-02-23 22:07:23 +00:00
|
|
|
msg.Text = msg.Username + msg.Text
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edit message if we have an ID
|
2017-08-27 22:33:17 +00:00
|
|
|
if msg.ID != "" {
|
2021-10-16 21:11:51 +00:00
|
|
|
if b.mc6 != nil {
|
|
|
|
return b.mc6.EditMessage(msg.ID, msg.Text) // nolint:wrapcheck
|
|
|
|
}
|
|
|
|
|
2018-02-23 22:07:23 +00:00
|
|
|
return b.mc.EditMessage(msg.ID, msg.Text)
|
2017-08-27 22:33:17 +00:00
|
|
|
}
|
2018-02-23 22:07:23 +00:00
|
|
|
|
|
|
|
// Post normal message
|
2021-10-16 21:11:51 +00:00
|
|
|
if b.mc6 != nil {
|
|
|
|
return b.mc6.PostMessage(b.mc6.GetChannelID(msg.Channel, b.TeamID), msg.Text, msg.ParentID) // nolint:wrapcheck
|
|
|
|
}
|
|
|
|
|
2019-01-09 20:50:03 +00:00
|
|
|
return b.mc.PostMessage(b.mc.GetChannelId(msg.Channel, b.TeamID), msg.Text, msg.ParentID)
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|