| 1 | package brocketchat |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "sync" |
| 7 | |
| 8 | lru "github.com/hashicorp/golang-lru" |
| 9 | "github.com/matterbridge-org/matterbridge/bridge" |
| 10 | "github.com/matterbridge-org/matterbridge/bridge/config" |
| 11 | "github.com/matterbridge-org/matterbridge/bridge/helper" |
| 12 | "github.com/matterbridge-org/matterbridge/hook/rockethook" |
| 13 | "github.com/matterbridge-org/matterbridge/matterhook" |
| 14 | // Library even upstream no longer actively maintained, should be replaced: |
| 15 | "github.com/matterbridge/Rocket.Chat.Go.SDK/models" |
| 16 | "github.com/matterbridge/Rocket.Chat.Go.SDK/realtime" |
| 17 | "github.com/matterbridge/Rocket.Chat.Go.SDK/rest" |
| 18 | ) |
| 19 | |
| 20 | type Brocketchat struct { |
| 21 | mh *matterhook.Client |
| 22 | rh *rockethook.Client |
| 23 | c *realtime.Client |
| 24 | r *rest.Client |
| 25 | cache *lru.Cache |
| 26 | *bridge.Config |
| 27 | messageChan chan models.Message |
| 28 | channelMap map[string]string |
| 29 | user *models.User |
| 30 | sync.RWMutex |
| 31 | } |
| 32 | |
| 33 | const ( |
| 34 | sUserJoined = "uj" |
| 35 | sUserLeft = "ul" |
| 36 | sRoomChangedTopic = "room_changed_topic" |
| 37 | ) |
| 38 | |
| 39 | func New(cfg *bridge.Config) bridge.Bridger { |
| 40 | newCache, err := lru.New(100) |
| 41 | if err != nil { |
| 42 | cfg.Log.Fatalf("Could not create LRU cache for rocketchat bridge: %v", err) |
| 43 | } |
| 44 | b := &Brocketchat{ |
| 45 | Config: cfg, |
| 46 | messageChan: make(chan models.Message), |
| 47 | channelMap: make(map[string]string), |
| 48 | cache: newCache, |
| 49 | } |
| 50 | b.Log.Debugf("enabling rocketchat") |
| 51 | return b |
| 52 | } |
| 53 | |
| 54 | func (b *Brocketchat) Command(cmd string) string { |
| 55 | return "" |
| 56 | } |
| 57 | |
| 58 | func (b *Brocketchat) Connect() error { |
| 59 | if b.GetString("WebhookBindAddress") != "" { |
| 60 | if err := b.doConnectWebhookBind(); err != nil { |
| 61 | return err |
| 62 | } |
| 63 | go b.handleRocket() |
| 64 | return nil |
| 65 | } |
| 66 | switch { |
| 67 | case b.GetString("WebhookURL") != "": |
| 68 | if err := b.doConnectWebhookURL(); err != nil { |
| 69 | return err |
| 70 | } |
| 71 | go b.handleRocket() |
| 72 | return nil |
| 73 | case b.GetString("Login") != "": |
| 74 | b.Log.Info("Connecting using login/password (sending and receiving)") |
| 75 | err := b.apiLogin() |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | go b.handleRocket() |
| 80 | } |
| 81 | if b.GetString("WebhookBindAddress") == "" && b.GetString("WebhookURL") == "" && |
| 82 | b.GetString("Login") == "" { |
| 83 | return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Login/Password/Server configured") |
| 84 | } |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | func (b *Brocketchat) Disconnect() error { |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | func (b *Brocketchat) JoinChannel(channel config.ChannelInfo) error { |
| 93 | if b.c == nil { |
| 94 | return nil |
| 95 | } |
| 96 | id, err := b.c.GetChannelId(strings.TrimPrefix(channel.Name, "#")) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | b.Lock() |
| 101 | b.channelMap[id] = channel.Name |
| 102 | b.Unlock() |
| 103 | mychannel := &models.Channel{ID: id, Name: strings.TrimPrefix(channel.Name, "#")} |
| 104 | if err := b.c.JoinChannel(id); err != nil { |
| 105 | return err |
| 106 | } |
| 107 | if err := b.c.SubscribeToMessageStream(mychannel, b.messageChan); err != nil { |
| 108 | return err |
| 109 | } |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | func (b *Brocketchat) Send(msg config.Message) (string, error) { |
| 114 | // strip the # if people has set this |
| 115 | msg.Channel = strings.TrimPrefix(msg.Channel, "#") |
| 116 | channel := &models.Channel{ID: b.getChannelID(msg.Channel), Name: msg.Channel} |
| 117 | |
| 118 | // Make a action /me of the message |
| 119 | if msg.Event == config.EventUserAction { |
| 120 | msg.Text = "_" + msg.Text + "_" |
| 121 | } |
| 122 | |
| 123 | // Delete message |
| 124 | if msg.Event == config.EventMsgDelete { |
| 125 | if msg.ID == "" { |
| 126 | return "", nil |
| 127 | } |
| 128 | return msg.ID, b.c.DeleteMessage(&models.Message{ID: msg.ID}) |
| 129 | } |
| 130 | |
| 131 | // Use webhook to send the message |
| 132 | if b.GetString("WebhookURL") != "" { |
| 133 | return "", b.sendWebhook(&msg) |
| 134 | } |
| 135 | |
| 136 | // Prepend nick if configured |
| 137 | if b.GetBool("PrefixMessagesWithNick") { |
| 138 | msg.Text = msg.Username + msg.Text |
| 139 | } |
| 140 | |
| 141 | // Edit message if we have an ID |
| 142 | if msg.ID != "" { |
| 143 | return msg.ID, b.c.EditMessage(&models.Message{ID: msg.ID, Msg: msg.Text, RoomID: b.getChannelID(msg.Channel)}) |
| 144 | } |
| 145 | |
| 146 | // Upload a file if it exists |
| 147 | if msg.Extra != nil { |
| 148 | for _, rmsg := range helper.HandleExtra(&msg, b.General) { |
| 149 | // strip the # if people has set this |
| 150 | rmsg.Channel = strings.TrimPrefix(rmsg.Channel, "#") |
| 151 | smsg := &models.Message{ |
| 152 | RoomID: b.getChannelID(rmsg.Channel), |
| 153 | Msg: rmsg.Username + rmsg.Text, |
| 154 | PostMessage: models.PostMessage{ |
| 155 | Avatar: rmsg.Avatar, |
| 156 | Alias: rmsg.Username, |
| 157 | }, |
| 158 | } |
| 159 | if _, err := b.c.SendMessage(smsg); err != nil { |
| 160 | b.Log.Errorf("SendMessage failed: %s", err) |
| 161 | } |
| 162 | } |
| 163 | if len(msg.Extra["file"]) > 0 { |
| 164 | return "", b.handleUploadFile(&msg) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | smsg := &models.Message{ |
| 169 | RoomID: channel.ID, |
| 170 | Msg: msg.Text, |
| 171 | PostMessage: models.PostMessage{ |
| 172 | Avatar: msg.Avatar, |
| 173 | Alias: msg.Username, |
| 174 | }, |
| 175 | } |
| 176 | |
| 177 | rmsg, err := b.c.SendMessage(smsg) |
| 178 | if rmsg == nil { |
| 179 | return "", err |
| 180 | } |
| 181 | return rmsg.ID, err |
| 182 | } |
| 183 | |