summaryrefslogtreecommitdiffstats
path: root/bridge/rocketchat/rocketchat.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2019-02-10 17:00:11 +0100
committerWim <wim@42.be>2019-02-15 18:19:34 +0100
commit6ebd5cbbd8a941e0bc5f99f0d8e99cfd1d8ac0d7 (patch)
tree47070e58e7802afd80fce53f0048a87a014a7c0d /bridge/rocketchat/rocketchat.go
parent2cfd880cdb0df29771bf8f31df8d990ab897889d (diff)
downloadmatterbridge-msglm-6ebd5cbbd8a941e0bc5f99f0d8e99cfd1d8ac0d7.tar.gz
matterbridge-msglm-6ebd5cbbd8a941e0bc5f99f0d8e99cfd1d8ac0d7.tar.bz2
matterbridge-msglm-6ebd5cbbd8a941e0bc5f99f0d8e99cfd1d8ac0d7.zip
Refactor and update RocketChat bridge
* Add support for editing/deleting messages * Add support for uploading files * Add support for avatars * Use the Rocket.Chat.Go.SDK * Use the rest and streaming api
Diffstat (limited to 'bridge/rocketchat/rocketchat.go')
-rw-r--r--bridge/rocketchat/rocketchat.go150
1 files changed, 107 insertions, 43 deletions
diff --git a/bridge/rocketchat/rocketchat.go b/bridge/rocketchat/rocketchat.go
index 1dbc7be0..82b6627d 100644
--- a/bridge/rocketchat/rocketchat.go
+++ b/bridge/rocketchat/rocketchat.go
@@ -1,21 +1,37 @@
package brocketchat
import (
+ "errors"
+ "sync"
+
"github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper"
"github.com/42wim/matterbridge/hook/rockethook"
"github.com/42wim/matterbridge/matterhook"
+ "github.com/matterbridge/Rocket.Chat.Go.SDK/models"
+ "github.com/matterbridge/Rocket.Chat.Go.SDK/realtime"
+ "github.com/matterbridge/Rocket.Chat.Go.SDK/rest"
)
type Brocketchat struct {
mh *matterhook.Client
rh *rockethook.Client
+ c *realtime.Client
+ r *rest.Client
*bridge.Config
+ messageChan chan models.Message
+ channelMap map[string]string
+ user *models.User
+ sync.RWMutex
}
func New(cfg *bridge.Config) bridge.Bridger {
- return &Brocketchat{Config: cfg}
+ b := &Brocketchat{Config: cfg}
+ b.messageChan = make(chan models.Message)
+ b.channelMap = make(map[string]string)
+ b.Log.Debugf("enabling rocketchat")
+ return b
}
func (b *Brocketchat) Command(cmd string) string {
@@ -23,70 +39,118 @@ func (b *Brocketchat) Command(cmd string) string {
}
func (b *Brocketchat) Connect() error {
- b.Log.Info("Connecting webhooks")
- b.mh = matterhook.New(b.GetString("WebhookURL"),
- matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
- DisableServer: true})
- b.rh = rockethook.New(b.GetString("WebhookURL"), rockethook.Config{BindAddress: b.GetString("WebhookBindAddress")})
- go b.handleRocketHook()
+ 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")
+ }
return nil
}
func (b *Brocketchat) Disconnect() error {
return nil
-
}
func (b *Brocketchat) JoinChannel(channel config.ChannelInfo) error {
+ 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
+ }
return nil
}
func (b *Brocketchat) Send(msg config.Message) (string, error) {
- // ignore delete messages
+ channel := &models.Channel{ID: b.getChannelID(msg.Channel), Name: msg.Channel}
+
+ // Delete message
if msg.Event == config.EventMsgDelete {
- return "", nil
+ if msg.ID == "" {
+ return "", nil
+ }
+ return msg.ID, b.c.DeleteMessage(&models.Message{ID: msg.ID})
}
- b.Log.Debugf("=> Receiving %#v", msg)
+
+ // 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
if msg.Extra != nil {
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
- rmsg := rmsg // scopelint
- iconURL := config.GetIconURL(&rmsg, b.GetString("iconurl"))
- matterMessage := matterhook.OMessage{IconURL: iconURL, Channel: rmsg.Channel, UserName: rmsg.Username, Text: rmsg.Text}
- b.mh.Send(matterMessage)
+ 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)
+ }
}
if len(msg.Extra["file"]) > 0 {
- for _, f := range msg.Extra["file"] {
- fi := f.(config.FileInfo)
- if fi.URL != "" {
- msg.Text += fi.URL
- }
- }
+ return "", b.handleUploadFile(&msg)
}
}
- iconURL := config.GetIconURL(&msg, b.GetString("iconurl"))
- matterMessage := matterhook.OMessage{IconURL: iconURL}
- matterMessage.Channel = msg.Channel
- matterMessage.UserName = msg.Username
- matterMessage.Type = ""
- matterMessage.Text = msg.Text
- err := b.mh.Send(matterMessage)
- if err != nil {
- b.Log.Info(err)
- return "", err
+ smsg := &models.Message{
+ RoomID: channel.ID,
+ Msg: msg.Text,
+ PostMessage: models.PostMessage{
+ Avatar: msg.Avatar,
+ Alias: msg.Username,
+ },
}
- return "", nil
-}
-func (b *Brocketchat) handleRocketHook() {
- for {
- message := b.rh.Receive()
- b.Log.Debugf("Receiving from rockethook %#v", message)
- // do not loop
- if message.UserName == b.GetString("Nick") {
- continue
- }
- b.Log.Debugf("<= Sending message from %s on %s to gateway", message.UserName, b.Account)
- b.Remote <- config.Message{Text: message.Text, Username: message.UserName, Channel: message.ChannelName, Account: b.Account, UserID: message.UserID}
+ rmsg, err := b.c.SendMessage(smsg)
+ if rmsg == nil {
+ return "", err
}
+ return rmsg.ID, err
}