-
Notifications
You must be signed in to change notification settings - Fork 12
/
connector.go
200 lines (190 loc) · 4.62 KB
/
connector.go
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Package rocket implements a connector for Rocket.Chat
package rocket
import (
"crypto/md5"
"time"
models "github.com/lnxjedi/gopherbot/connectors/rocket/models"
"github.com/lnxjedi/robot"
)
var incoming chan models.Message
func (rc *rocketConnector) Run(stop <-chan struct{}) {
rc.Lock()
// This should never happen, just a bit of defensive coding
if rc.running {
rc.Unlock()
return
}
rc.running = true
rc.Unlock()
rc.updateChannels()
if err := rc.rt.SubscribeRoomUpdates("__my_messages__"); err != nil {
rc.Log(robot.Error, "failed subscribing to '__my_messages__, won't hear messages: %v", err)
}
// Detect UserName
// TODO: is there a better way to get the robot's UserName?
if sch, err := rc.rt.GetChannelSubscriptions(); err == nil {
detected := false
for _, ch := range sch {
if len(ch.User.UserName) > 0 {
detected = true
userName = ch.User.UserName
rc.SetBotMention(userName)
break
}
}
if !detected {
rc.Log(robot.Warn, "unable to detect username from rocket channel subscriptions")
}
} else {
rc.Log(robot.Error, "failed getting rocket channel subscriptions, can't get username: %v", err)
}
mstop := make(chan struct{})
// duplicate messages loop
go func() {
mloop:
for {
select {
case <-mstop:
break mloop
case mq := <-check:
m := mq.msgTrack
_, ok := trackedMsgs[m]
if !ok {
rc.Log(robot.Debug, "DEBUG: recording message %s", m.msgID)
trackedMsgs[m] = time.Now()
}
mq.reply <- ok
case <-time.After(msgExpire / 2):
now := time.Now()
for m, t := range trackedMsgs {
if now.Sub(t) > msgExpire {
rc.Log(robot.Debug, "DEBUG: expiring message %s", m.msgID)
delete(trackedMsgs, m)
}
}
}
}
}()
loop:
for {
select {
case pmsg := <-incoming:
rc.processMessage(&pmsg)
case <-stop:
rc.Log(robot.Debug, "Received stop in connector")
break loop
}
}
}
// processMessage creates a robot.ConnectorMessage and calls
// robot.IncomingMessage
func (rc *rocketConnector) processMessage(msg *models.Message) {
if len(msg.Msg) == 0 {
return
}
if msg.User.ID == userID {
return
}
if msg.User.UserName == userName {
return
}
hearIt := false
directMsg := false
mapUser := false
rc.RLock()
chName := rc.channelNames[msg.RoomID]
if _, ok := rc.dmChannels[msg.RoomID]; ok {
hearIt = true
directMsg = true
}
if _, ok := rc.privChannels[msg.RoomID]; ok {
hearIt = true
}
if _, ok := rc.joinedChannels[msg.RoomID]; ok {
hearIt = true
}
if _, ok := rc.userNameIDMap[msg.User.UserName]; !ok {
mapUser = true
}
rc.RUnlock()
if mapUser {
// TODO: is there a better way of mapping username to ID?
rc.Lock()
rc.userNameIDMap[msg.User.UserName] = msg.User.ID
rc.userIDNameMap[msg.User.ID] = msg.User.UserName
rc.Unlock()
}
if !hearIt {
return
}
rc.Log(robot.Debug, "DEBUG: Raw incoming msg: %+v", *msg)
rc.Log(robot.Debug, "DEBUG: Raw incoming user: %+v", *msg.User)
// Check for and ignore duplicate messages
mHash := msgQuery{make(chan bool), msgTrack{msg.ID, md5.Sum([]byte(msg.Msg))}}
if check <- mHash; <-mHash.reply {
rc.Log(robot.Debug, "DEBUG: ignoring duplicate message %s", msg.ID)
return
}
botMsg := &robot.ConnectorMessage{
Protocol: "rocket",
UserID: msg.User.ID,
UserName: msg.User.UserName,
ChannelID: msg.RoomID,
ChannelName: chName,
MessageText: msg.Msg,
MessageObject: msg,
Client: rc.rt,
DirectMessage: directMsg,
}
rc.IncomingMessage(botMsg)
}
func (rc *rocketConnector) updateChannels() {
inChannels, ierr := rc.rt.GetChannelsIn()
if ierr != nil {
rc.Log(robot.Error, "rocket getting channels in: %v", ierr)
return
}
rc.Lock()
defer rc.Unlock()
if len(inChannels) > 0 {
for _, ich := range inChannels {
if len(ich.Name) > 0 {
rc.channelNames[ich.ID] = ich.Name
rc.channelIDs[ich.Name] = ich.ID
}
if ich.Type == "d" {
rc.dmChannels[ich.ID] = struct{}{}
}
if ich.Type == "p" {
rc.privChannels[ich.ID] = struct{}{}
}
}
}
}
func formatMessage(msg string, f robot.MessageFormat) string {
if f == robot.Fixed {
msg = "```" + msg + "```"
}
return msg
}
// sendMessage takes "channel" or "<chanID>" and sends the pre-formatted
// message.
func (rc *rocketConnector) sendMessage(ch, msg string) (ret robot.RetVal) {
var chanID string
found := false
chanID, found = rc.ExtractID(ch)
if !found {
rc.RLock()
chanID, found = rc.channelIDs[ch]
rc.RUnlock()
}
if !found {
return robot.ChannelNotFound
}
sendChan := models.Channel{ID: chanID}
m := rc.rt.NewMessage(&sendChan, msg)
if _, err := rc.rt.SendMessage(m); err != nil {
return robot.FailedMessageSend
}
return robot.Ok
}