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