2016-08-14 19:48:51 +00:00
|
|
|
package bmattermost
|
|
|
|
|
|
|
|
import (
|
2024-05-24 21:08:09 +00:00
|
|
|
"context"
|
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"
|
2022-02-06 17:26:30 +00:00
|
|
|
"sync"
|
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/matterhook"
|
2023-01-28 20:57:06 +00:00
|
|
|
"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
|
2023-01-28 20:57:06 +00:00
|
|
|
mc *matterclient.Client
|
2021-10-16 21:11:51 +00:00
|
|
|
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
|
2022-02-06 17:26:30 +00:00
|
|
|
avatarMap map[string]string
|
|
|
|
channelsMutex sync.RWMutex
|
|
|
|
channelInfoMap map[string]*config.ChannelInfo
|
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 {
|
2022-02-06 17:26:30 +00:00
|
|
|
b := &Bmattermost{
|
|
|
|
Config: cfg,
|
|
|
|
avatarMap: make(map[string]string),
|
|
|
|
channelInfoMap: make(map[string]*config.ChannelInfo),
|
|
|
|
}
|
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
|
|
|
|
2022-06-24 22:57:17 +00:00
|
|
|
if strings.HasPrefix(b.getVersion(), "6.") || strings.HasPrefix(b.getVersion(), "7.") {
|
2021-10-16 21:11:51 +00:00
|
|
|
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)")
|
2023-01-28 20:57:06 +00:00
|
|
|
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)
|
2023-01-28 20:57:06 +00:00
|
|
|
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
|
|
|
|
}
|
2022-02-06 17:26:30 +00:00
|
|
|
|
|
|
|
b.channelsMutex.Lock()
|
|
|
|
b.channelInfoMap[channel.ID] = &channel
|
|
|
|
b.channelsMutex.Unlock()
|
|
|
|
|
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") == "" {
|
2022-02-06 17:26:30 +00:00
|
|
|
id := b.getChannelID(channel.Name)
|
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
|
|
|
|
2017-08-30 12:01:17 +00:00
|
|
|
return b.mc.JoinChannel(id)
|
2016-09-20 10:20:44 +00:00
|
|
|
}
|
2022-02-06 17:26:30 +00:00
|
|
|
|
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
|
|
|
|
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 != "" {
|
2024-05-24 21:08:09 +00:00
|
|
|
post, _, err := b.mc.Client.GetPost(context.TODO(), msg.ParentID, "")
|
2023-01-28 20:57:06 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("getting post %s failed: %s", msg.ParentID, err)
|
|
|
|
}
|
2023-03-11 17:55:29 +00:00
|
|
|
if post != nil && post.RootId != "" {
|
2023-01-28 20:57:06 +00:00
|
|
|
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) {
|
2023-01-28 20:57:06 +00:00
|
|
|
if _, err := b.mc.PostMessage(b.getChannelID(rmsg.Channel), 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 != "" {
|
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
|
2022-02-06 17:26:30 +00:00
|
|
|
return b.mc.PostMessage(b.getChannelID(msg.Channel), msg.Text, msg.ParentID)
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|