summaryrefslogtreecommitdiffstats
path: root/bridge/rocketchat/rocketchat.go
blob: fa1dd28135e00a0f323cf8f0cea4c848c1f7fbef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package brocketchat

import (
	"errors"
	"strings"
	"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"
	lru "github.com/hashicorp/golang-lru"
	"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
	cache *lru.Cache
	*bridge.Config
	messageChan chan models.Message
	channelMap  map[string]string
	user        *models.User
	sync.RWMutex
}

func New(cfg *bridge.Config) bridge.Bridger {
	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,
	}
	b.Log.Debugf("enabling rocketchat")
	return b
}

func (b *Brocketchat) Command(cmd string) string {
	return ""
}

func (b *Brocketchat) Connect() error {
	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(strings.TrimPrefix(channel.Name, "#"))
	if err != nil {
		return err
	}
	b.Lock()
	b.channelMap[id] = channel.Name
	b.Unlock()
	mychannel := &models.Channel{ID: id, Name: strings.TrimPrefix(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) {
	// strip the # if people has set this
	msg.Channel = strings.TrimPrefix(msg.Channel, "#")
	channel := &models.Channel{ID: b.getChannelID(msg.Channel), Name: msg.Channel}

	// Delete message
	if msg.Event == config.EventMsgDelete {
		if msg.ID == "" {
			return "", nil
		}
		return msg.ID, b.c.DeleteMessage(&models.Message{ID: msg.ID})
	}

	// 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) {
			// strip the # if people has set this
			rmsg.Channel = strings.TrimPrefix(rmsg.Channel, "#")
			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 {
			return "", b.handleUploadFile(&msg)
		}
	}

	smsg := &models.Message{
		RoomID: channel.ID,
		Msg:    msg.Text,
		PostMessage: models.PostMessage{
			Avatar: msg.Avatar,
			Alias:  msg.Username,
		},
	}

	rmsg, err := b.c.SendMessage(smsg)
	if rmsg == nil {
		return "", err
	}
	return rmsg.ID, err
}