5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-12-26 21:25:39 +00:00
matterbridge/bridge/mattermost/mattermost.go

149 lines
3.6 KiB
Go
Raw Normal View History

package bmattermost
import (
"errors"
"fmt"
2018-02-26 23:33:21 +00:00
"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"
2018-05-27 19:50:00 +00:00
"github.com/rs/xid"
)
type Bmattermost struct {
2018-02-23 22:07:23 +00:00
mh *matterhook.Client
mc *matterclient.MMClient
2018-05-27 19:50:00 +00:00
uuid string
2018-02-24 22:22:15 +00:00
TeamID string
*bridge.Config
avatarMap map[string]string
}
2018-11-13 19:40:15 +00:00
const mattermostPlugin = "mattermost.plugin"
func New(cfg *bridge.Config) bridge.Bridger {
b := &Bmattermost{Config: cfg, avatarMap: make(map[string]string)}
2018-05-27 19:50:00 +00:00
b.uuid = xid.New().String()
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 {
return nil
}
if b.GetString("WebhookBindAddress") != "" {
if err := b.doConnectWebhookBind(); err != nil {
return err
}
go b.handleMatter()
return nil
}
2018-11-08 21:01:29 +00:00
switch {
case b.GetString("WebhookURL") != "":
if err := b.doConnectWebhookURL(); err != nil {
return err
}
go b.handleMatter()
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)")
err := b.apiLogin()
if err != nil {
return err
}
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)")
err := b.apiLogin()
if err != nil {
2016-08-15 21:16:07 +00:00
return err
}
go b.handleMatter()
}
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-15 21:16:07 +00:00
return nil
}
func (b *Bmattermost) Disconnect() error {
return nil
}
func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error {
2018-11-13 19:40:15 +00:00
if b.Account == mattermostPlugin {
return nil
}
// we can only join channels using the API
if b.GetString("WebhookURL") == "" && b.GetString("WebhookBindAddress") == "" {
id := b.mc.GetChannelId(channel.Name, b.TeamID)
if id == "" {
return fmt.Errorf("Could not find channel ID for channel %s", channel.Name)
}
return b.mc.JoinChannel(id)
}
return nil
}
func (b *Bmattermost) Send(msg config.Message) (string, error) {
2018-11-13 19:40:15 +00:00
if b.Account == mattermostPlugin {
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
if msg.Event == config.EventUserAction {
msg.Text = "*" + msg.Text + "*"
}
// map the file SHA to our user (caches the avatar)
if msg.Event == config.EventAvatarDownload {
2018-02-23 22:07:23 +00:00
return b.cacheAvatar(&msg)
}
2018-02-23 22:07:23 +00:00
// Use webhook to send the message
if b.GetString("WebhookURL") != "" {
2018-02-23 22:07:23 +00:00
return b.sendWebhook(msg)
}
2018-02-23 22:07:23 +00:00
// Delete message
if msg.Event == config.EventMsgDelete {
if msg.ID == "" {
return "", nil
}
return msg.ID, b.mc.DeleteMessage(msg.ID)
}
2018-02-23 22:07:23 +00:00
// 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)
}
}
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)
}
}
2018-02-23 22:07:23 +00:00
// Prepend nick if configured
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
if msg.ID != "" {
2018-02-23 22:07:23 +00:00
return b.mc.EditMessage(msg.ID, msg.Text)
}
2018-02-23 22:07:23 +00:00
// Post normal message
return b.mc.PostMessage(b.mc.GetChannelId(msg.Channel, b.TeamID), msg.Text, msg.ParentID)
}