2016-12-02 23:10:29 +00:00
|
|
|
package brocketchat
|
|
|
|
|
|
|
|
import (
|
2019-02-10 16:00:11 +00:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
2018-02-26 23:33:21 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
2016-12-02 23:10:29 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2018-02-03 00:11:11 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2016-12-02 23:10:29 +00:00
|
|
|
"github.com/42wim/matterbridge/hook/rockethook"
|
|
|
|
"github.com/42wim/matterbridge/matterhook"
|
2019-03-02 21:58:14 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2019-02-10 16:00:11 +00:00
|
|
|
"github.com/matterbridge/Rocket.Chat.Go.SDK/models"
|
|
|
|
"github.com/matterbridge/Rocket.Chat.Go.SDK/realtime"
|
|
|
|
"github.com/matterbridge/Rocket.Chat.Go.SDK/rest"
|
2016-12-02 23:10:29 +00:00
|
|
|
)
|
|
|
|
|
2018-11-07 23:38:33 +00:00
|
|
|
type Brocketchat struct {
|
2019-03-02 21:58:14 +00:00
|
|
|
mh *matterhook.Client
|
|
|
|
rh *rockethook.Client
|
|
|
|
c *realtime.Client
|
|
|
|
r *rest.Client
|
|
|
|
cache *lru.Cache
|
2018-03-04 22:52:14 +00:00
|
|
|
*bridge.Config
|
2019-02-10 16:00:11 +00:00
|
|
|
messageChan chan models.Message
|
|
|
|
channelMap map[string]string
|
|
|
|
user *models.User
|
|
|
|
sync.RWMutex
|
2016-12-02 23:10:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
2019-03-02 21:58:14 +00:00
|
|
|
newCache, err := lru.New(100)
|
|
|
|
if err != nil {
|
|
|
|
cfg.Log.Fatalf("Could not create LRU cache for rocketchat bridge: %v", err)
|
|
|
|
}
|
|
|
|
b := &Brocketchat{
|
|
|
|
Config: cfg,
|
|
|
|
messageChan: make(chan models.Message),
|
|
|
|
channelMap: make(map[string]string),
|
|
|
|
cache: newCache,
|
|
|
|
}
|
2019-02-10 16:00:11 +00:00
|
|
|
b.Log.Debugf("enabling rocketchat")
|
|
|
|
return b
|
2016-12-02 23:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Brocketchat) Command(cmd string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Brocketchat) Connect() error {
|
2019-02-10 16:00:11 +00:00
|
|
|
if b.GetString("WebhookBindAddress") != "" {
|
|
|
|
if err := b.doConnectWebhookBind(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go b.handleRocket()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case b.GetString("WebhookURL") != "":
|
|
|
|
if err := b.doConnectWebhookURL(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go b.handleRocket()
|
|
|
|
return nil
|
|
|
|
case b.GetString("Login") != "":
|
|
|
|
b.Log.Info("Connecting using login/password (sending and receiving)")
|
|
|
|
err := b.apiLogin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go b.handleRocket()
|
|
|
|
}
|
|
|
|
if b.GetString("WebhookBindAddress") == "" && b.GetString("WebhookURL") == "" &&
|
|
|
|
b.GetString("Login") == "" {
|
|
|
|
return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Login/Password/Server configured")
|
|
|
|
}
|
2016-12-02 23:10:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:12:02 +00:00
|
|
|
func (b *Brocketchat) Disconnect() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-12 12:51:41 +00:00
|
|
|
func (b *Brocketchat) JoinChannel(channel config.ChannelInfo) error {
|
2019-02-10 16:00:11 +00:00
|
|
|
if b.c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
id, err := b.c.GetChannelId(channel.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.Lock()
|
|
|
|
b.channelMap[id] = channel.Name
|
|
|
|
b.Unlock()
|
|
|
|
mychannel := &models.Channel{ID: id, Name: channel.Name}
|
|
|
|
if err := b.c.JoinChannel(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.c.SubscribeToMessageStream(mychannel, b.messageChan); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-02 23:10:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 20:59:37 +00:00
|
|
|
func (b *Brocketchat) Send(msg config.Message) (string, error) {
|
2019-02-10 16:00:11 +00:00
|
|
|
channel := &models.Channel{ID: b.getChannelID(msg.Channel), Name: msg.Channel}
|
|
|
|
|
|
|
|
// Delete message
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventMsgDelete {
|
2019-02-10 16:00:11 +00:00
|
|
|
if msg.ID == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return msg.ID, b.c.DeleteMessage(&models.Message{ID: msg.ID})
|
2017-09-11 20:45:15 +00:00
|
|
|
}
|
2019-02-10 16:00:11 +00:00
|
|
|
|
|
|
|
// Use webhook to send the message
|
|
|
|
if b.GetString("WebhookURL") != "" {
|
|
|
|
return "", b.sendWebhook(&msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend nick if configured
|
|
|
|
if b.GetBool("PrefixMessagesWithNick") {
|
|
|
|
msg.Text = msg.Username + msg.Text
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edit message if we have an ID
|
|
|
|
if msg.ID != "" {
|
|
|
|
return msg.ID, b.c.EditMessage(&models.Message{ID: msg.ID, Msg: msg.Text, RoomID: b.getChannelID(msg.Channel)})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload a file if it exists
|
2018-02-03 00:11:11 +00:00
|
|
|
if msg.Extra != nil {
|
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
2019-02-10 16:00:11 +00:00
|
|
|
smsg := &models.Message{
|
|
|
|
RoomID: b.getChannelID(rmsg.Channel),
|
|
|
|
Msg: rmsg.Username + rmsg.Text,
|
|
|
|
PostMessage: models.PostMessage{
|
|
|
|
Avatar: rmsg.Avatar,
|
|
|
|
Alias: rmsg.Username,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if _, err := b.c.SendMessage(smsg); err != nil {
|
|
|
|
b.Log.Errorf("SendMessage failed: %s", err)
|
|
|
|
}
|
2018-02-03 00:11:11 +00:00
|
|
|
}
|
|
|
|
if len(msg.Extra["file"]) > 0 {
|
2019-02-10 16:00:11 +00:00
|
|
|
return "", b.handleUploadFile(&msg)
|
2018-02-03 00:11:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 16:00:11 +00:00
|
|
|
smsg := &models.Message{
|
|
|
|
RoomID: channel.ID,
|
|
|
|
Msg: msg.Text,
|
|
|
|
PostMessage: models.PostMessage{
|
|
|
|
Avatar: msg.Avatar,
|
|
|
|
Alias: msg.Username,
|
|
|
|
},
|
2016-12-02 23:10:29 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 16:00:11 +00:00
|
|
|
rmsg, err := b.c.SendMessage(smsg)
|
|
|
|
if rmsg == nil {
|
|
|
|
return "", err
|
2016-12-02 23:10:29 +00:00
|
|
|
}
|
2019-02-10 16:00:11 +00:00
|
|
|
return rmsg.ID, err
|
2016-12-02 23:10:29 +00:00
|
|
|
}
|