summaryrefslogtreecommitdiffstats
path: root/bridge/rocketchat
diff options
context:
space:
mode:
authorWim <wim@42.be>2019-02-15 18:20:32 +0100
committerGitHub <noreply@github.com>2019-02-15 18:20:32 +0100
commit716751cf7685471bbc969e19fe26f23e66f3c0b4 (patch)
tree6db2350f28a76ae6e1cf7c3cea7263503057d82f /bridge/rocketchat
parent077b818d82a98855d5577f4f9de5d2b2d2cc6a50 (diff)
parent6ebd5cbbd8a941e0bc5f99f0d8e99cfd1d8ac0d7 (diff)
downloadmatterbridge-msglm-716751cf7685471bbc969e19fe26f23e66f3c0b4.tar.gz
matterbridge-msglm-716751cf7685471bbc969e19fe26f23e66f3c0b4.tar.bz2
matterbridge-msglm-716751cf7685471bbc969e19fe26f23e66f3c0b4.zip
Refactor and update RocketChat bridge (#707)
* 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')
-rw-r--r--bridge/rocketchat/handlers.go69
-rw-r--r--bridge/rocketchat/helpers.go198
-rw-r--r--bridge/rocketchat/rocketchat.go150
3 files changed, 374 insertions, 43 deletions
diff --git a/bridge/rocketchat/handlers.go b/bridge/rocketchat/handlers.go
new file mode 100644
index 00000000..b44ea46f
--- /dev/null
+++ b/bridge/rocketchat/handlers.go
@@ -0,0 +1,69 @@
+package brocketchat
+
+import (
+ "github.com/42wim/matterbridge/bridge/config"
+)
+
+func (b *Brocketchat) handleRocket() {
+ messages := make(chan *config.Message)
+ if b.GetString("WebhookBindAddress") != "" {
+ b.Log.Debugf("Choosing webhooks based receiving")
+ go b.handleRocketHook(messages)
+ } else {
+ b.Log.Debugf("Choosing login/password based receiving")
+ go b.handleRocketClient(messages)
+ }
+ for message := range messages {
+ message.Account = b.Account
+ b.Log.Debugf("<= Sending message from %s on %s to gateway", message.Username, b.Account)
+ b.Log.Debugf("<= Message is %#v", message)
+ b.Remote <- *message
+ }
+}
+
+func (b *Brocketchat) handleRocketHook(messages chan *config.Message) {
+ for {
+ message := b.rh.Receive()
+ b.Log.Debugf("Receiving from rockethook %#v", message)
+ // do not loop
+ if message.UserName == b.GetString("Nick") {
+ continue
+ }
+ messages <- &config.Message{
+ UserID: message.UserID,
+ Username: message.UserName,
+ Text: message.Text,
+ Channel: message.ChannelName,
+ }
+ }
+}
+
+func (b *Brocketchat) handleRocketClient(messages chan *config.Message) {
+ for message := range b.messageChan {
+ b.Log.Debugf("message %#v", message)
+ m := message
+ if b.skipMessage(&m) {
+ b.Log.Debugf("Skipped message: %#v", message)
+ continue
+ }
+
+ rmsg := &config.Message{Text: message.Msg,
+ Username: message.User.UserName,
+ Channel: b.getChannelName(message.RoomID),
+ Account: b.Account,
+ UserID: message.User.ID,
+ ID: message.ID,
+ }
+ messages <- rmsg
+ }
+}
+
+func (b *Brocketchat) handleUploadFile(msg *config.Message) error {
+ for _, f := range msg.Extra["file"] {
+ fi := f.(config.FileInfo)
+ if err := b.uploadFile(&fi, b.getChannelID(msg.Channel)); err != nil {
+ return err
+ }
+ }
+ return nil
+}
diff --git a/bridge/rocketchat/helpers.go b/bridge/rocketchat/helpers.go
new file mode 100644
index 00000000..c6bd2dd8
--- /dev/null
+++ b/bridge/rocketchat/helpers.go
@@ -0,0 +1,198 @@
+package brocketchat
+
+import (
+ "context"
+ "io/ioutil"
+ "mime"
+ "net/http"
+ "net/url"
+ "strings"
+ "time"
+
+ "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"
+ "github.com/nelsonken/gomf"
+)
+
+func (b *Brocketchat) doConnectWebhookBind() error {
+ switch {
+ case b.GetString("WebhookURL") != "":
+ b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
+ 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")})
+ case b.GetString("Login") != "":
+ b.Log.Info("Connecting using login/password (sending)")
+ err := b.apiLogin()
+ if err != nil {
+ return err
+ }
+ default:
+ b.Log.Info("Connecting using webhookbindaddress (receiving)")
+ b.rh = rockethook.New(b.GetString("WebhookURL"), rockethook.Config{BindAddress: b.GetString("WebhookBindAddress")})
+ }
+ return nil
+}
+
+func (b *Brocketchat) doConnectWebhookURL() error {
+ b.Log.Info("Connecting using webhookurl (sending)")
+ b.mh = matterhook.New(b.GetString("WebhookURL"),
+ matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
+ DisableServer: true})
+ if b.GetString("Login") != "" {
+ b.Log.Info("Connecting using login/password (receiving)")
+ err := b.apiLogin()
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func (b *Brocketchat) apiLogin() error {
+ b.Log.Debugf("handling apiLogin()")
+ credentials := &models.UserCredentials{Email: b.GetString("login"), Password: b.GetString("password")}
+ myURL, err := url.Parse(b.GetString("server"))
+ if err != nil {
+ return err
+ }
+ client, err := realtime.NewClient(myURL, b.GetBool("debug"))
+ b.c = client
+ if err != nil {
+ return err
+ }
+ restclient := rest.NewClient(myURL, b.GetBool("debug"))
+ user, err := b.c.Login(credentials)
+ if err != nil {
+ return err
+ }
+ b.user = user
+ b.r = restclient
+ err = b.r.Login(credentials)
+ if err != nil {
+ return err
+ }
+ b.Log.Info("Connection succeeded")
+ return nil
+}
+
+func (b *Brocketchat) getChannelName(id string) string {
+ b.RLock()
+ defer b.RUnlock()
+ if name, ok := b.channelMap[id]; ok {
+ return name
+ }
+ return ""
+}
+
+func (b *Brocketchat) getChannelID(name string) string {
+ b.RLock()
+ defer b.RUnlock()
+ for k, v := range b.channelMap {
+ if v == name {
+ return k
+ }
+ }
+ return ""
+}
+
+func (b *Brocketchat) skipMessage(message *models.Message) bool {
+ return message.User.ID == b.user.ID
+}
+
+func (b *Brocketchat) uploadFile(fi *config.FileInfo, channel string) error {
+ fb := gomf.New()
+ if err := fb.WriteField("description", fi.Comment); err != nil {
+ return err
+ }
+ sp := strings.Split(fi.Name, ".")
+ mtype := mime.TypeByExtension("." + sp[len(sp)-1])
+ if !strings.Contains(mtype, "image") && !strings.Contains(mtype, "video") {
+ return nil
+ }
+ if err := fb.WriteFile("file", fi.Name, mtype, *fi.Data); err != nil {
+ return err
+ }
+ req, err := fb.GetHTTPRequest(context.TODO(), b.GetString("server")+"/api/v1/rooms.upload/"+channel)
+ if err != nil {
+ return err
+ }
+ req.Header.Add("X-Auth-Token", b.user.Token)
+ req.Header.Add("X-User-Id", b.user.ID)
+ client := &http.Client{
+ Timeout: time.Second * 5,
+ }
+ resp, err := client.Do(req)
+ if err != nil {
+ return err
+ }
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return err
+ }
+ if resp.StatusCode != 200 {
+ b.Log.Errorf("failed: %#v", string(body))
+ }
+ return nil
+}
+
+// sendWebhook uses the configured WebhookURL to send the message
+func (b *Brocketchat) sendWebhook(msg *config.Message) error {
+ // skip events
+ if msg.Event != "" {
+ return nil
+ }
+
+ if b.GetBool("PrefixMessagesWithNick") {
+ msg.Text = msg.Username + msg.Text
+ }
+ if msg.Extra != nil {
+ // this sends a message only if we received a config.EVENT_FILE_FAILURE_SIZE
+ 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,
+ Props: make(map[string]interface{}),
+ }
+ if err := b.mh.Send(matterMessage); err != nil {
+ b.Log.Errorf("sendWebhook failed: %s ", err)
+ }
+ }
+
+ // webhook doesn't support file uploads, so we add the url manually
+ if len(msg.Extra["file"]) > 0 {
+ for _, f := range msg.Extra["file"] {
+ fi := f.(config.FileInfo)
+ if fi.URL != "" {
+ msg.Text += fi.URL
+ }
+ }
+ }
+ }
+ iconURL := config.GetIconURL(msg, b.GetString("iconurl"))
+ matterMessage := matterhook.OMessage{
+ IconURL: iconURL,
+ Channel: msg.Channel,
+ UserName: msg.Username,
+ Text: msg.Text,
+ }
+ if msg.Avatar != "" {
+ matterMessage.IconURL = msg.Avatar
+ }
+ err := b.mh.Send(matterMessage)
+ if err != nil {
+ b.Log.Info(err)
+ return err
+ }
+ return nil
+}
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
}