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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
package mattermost
import (
"net/http"
"fmt"
_ "os"
"strings"
"time"
"io/ioutil"
"encoding/json"
"github.com/mattermost/mattermost-server/model"
"github.com/42wim/matterbridge/matterclient"
log "github.com/sirupsen/logrus"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
)
// User id format: nickname@server
// Room id format: room_name@team@server
type Mattermost struct {
handler Handler
server string
username string
team string
conn *matterclient.MMClient
handlerStopChan chan bool
usermap map[string]string // map username to mm user id
sentjoinedmap map[string]bool // map username/room name to bool
userdisplaynamemap map[UserID]string // map username to last displayname
}
func (mm *Mattermost) SetHandler(h Handler) {
mm.handler = h
}
func (mm *Mattermost) Protocol() string {
return "mattermost"
}
func (mm *Mattermost) Configure(c Configuration) error {
if mm.conn != nil {
mm.Close()
}
var err error
mm.server, err = c.GetString("server")
if err != nil {
return err
}
mm.username, err = c.GetString("username")
if err != nil {
return err
}
mm.team, err = c.GetString("team")
if err != nil {
return err
}
notls, err := c.GetBool("no_tls", false)
if err != nil {
return err
}
password, _ := c.GetString("password", "")
token, _ := c.GetString("token", "")
if token != "" {
password = "token=" + token
}
mm.conn = matterclient.New(mm.username, password, mm.team, mm.server)
mm.conn.Credentials.NoTLS = notls
err = mm.conn.Login()
if err != nil {
return err
}
go mm.conn.WsReceiver()
go mm.conn.StatusLoop()
fmt.Printf("CLIENT4: %#v\n", mm.conn.Client)
for i := 0; i < 42; i++ {
time.Sleep(time.Duration(1) * time.Second)
if mm.conn.WsConnected {
mm.handleConnected()
return nil
}
}
return fmt.Errorf("Failed to connect after 42s attempting")
}
func (mm *Mattermost) User() UserID {
return UserID(mm.username + "@" + mm.server)
}
func (mm *Mattermost) getTeamIdByName(name string) string {
for _, team := range mm.conn.OtherTeams {
if team.Team.Name == name {
return team.Id
}
}
return ""
}
func (mm *Mattermost) checkRoomId(id RoomID) (string, error) {
x := strings.Split(string(id), "@")
if len(x) == 1 {
return "", fmt.Errorf("Please write whole room ID with team and server: %s@%s@%s", id, mm.team, mm.server)
}
if len(x) == 2 {
return x[0], nil
}
if len(x) != 3 || x[2] != mm.server {
return "", fmt.Errorf("Invalid room ID: %s", id)
}
team_id := mm.getTeamIdByName(x[1])
if team_id == "" {
return "", fmt.Errorf("Team not found: %s", id)
}
ch_id := mm.conn.GetChannelId(x[0], team_id)
if ch_id == "" {
return "", fmt.Errorf("Channel not found: %s", id)
}
return ch_id, nil
}
func (mm *Mattermost) reverseRoomId(id string) RoomID {
team := mm.conn.GetChannelTeamId(id)
if team == "" {
return RoomID(fmt.Sprintf("%s@%s", id, mm.server))
} else {
teamName := mm.conn.GetTeamName(team)
name := mm.conn.GetChannelName(id)
fmt.Printf("CHANNEL NAME: %s TEAM: %s\n", name, teamName)
return RoomID(fmt.Sprintf("%s@%s@%s", name, teamName, mm.server))
}
}
func (mm *Mattermost) checkUserId(id UserID) (string, error) {
x := strings.Split(string(id), "@")
if len(x) == 1 {
return "", fmt.Errorf("Please write whole user ID with server: %s@%s", id, mm.server)
}
if len(x) != 2 || x[1] != mm.server {
return "", fmt.Errorf("Invalid user ID: %s", id)
}
if user_id, ok := mm.usermap[x[0]]; ok {
return user_id, nil
}
u, resp := mm.conn.Client.GetUserByUsername(x[0], "")
if u == nil || resp.Error != nil {
return "", fmt.Errorf("Not found: %s (%s)", x[0], resp.Error)
}
mm.usermap[x[0]] = u.Id
return u.Id, nil
}
func (mm *Mattermost) SetUserInfo(info *UserInfo) error {
return fmt.Errorf("Not implemented")
}
func (mm *Mattermost) SetRoomInfo(roomId RoomID, info *RoomInfo) error {
ch, err := mm.checkRoomId(roomId)
if err != nil {
return err
}
if info.Topic != "" {
mm.conn.UpdateChannelHeader(ch, info.Topic)
}
if info.Picture != nil {
err = fmt.Errorf("Not supported: channel picture on mattermost")
}
if info.Name != "" {
err = fmt.Errorf("Not supported: channel name on mattermost")
}
return err
}
func (mm *Mattermost) Join(roomId RoomID) error {
ch, err := mm.checkRoomId(roomId)
if err != nil {
return err
}
return mm.conn.JoinChannel(ch)
}
func (mm *Mattermost) Invite(userId UserID, roomId RoomID) error {
if roomId == "" {
_, err := mm.checkUserId(userId)
return err
}
return fmt.Errorf("Not supported: invite on mattermost")
}
func (mm *Mattermost) Leave(roomId RoomID) {
// Not supported? TODO
}
func (mm *Mattermost) Send(event *Event) error {
post := &model.Post{
Message: event.Text,
}
if event.Type == EVENT_ACTION {
post.Type = "me"
}
if event.Room != "" {
ch, err := mm.checkRoomId(event.Room)
if err != nil {
return err
}
post.ChannelId = ch
} else if event.Recipient != "" {
ui, err := mm.checkUserId(event.Recipient)
if err != nil {
return err
}
_, resp := mm.conn.Client.CreateDirectChannel(mm.conn.User.Id, ui)
if resp.Error != nil {
return resp.Error
}
channelName := model.GetDMNameFromIds(ui, mm.conn.User.Id)
err = mm.conn.UpdateChannels()
if err != nil {
return err
}
post.ChannelId = mm.conn.GetChannelId(channelName, "")
} else {
return fmt.Errorf("Invalid target")
}
if event.Attachments != nil {
post.FileIds = []string{}
for _, file := range event.Attachments {
rdr, err := file.Read()
if err != nil {
return err
}
defer rdr.Close()
data, err := ioutil.ReadAll(rdr)
if err != nil {
return err
}
up_file, err := mm.conn.UploadFile(data, post.ChannelId, file.Filename())
if err != nil {
log.Warnf("UploadFile error: %s", err)
return err
}
post.FileIds = append(post.FileIds, up_file)
}
}
_, resp := mm.conn.Client.CreatePost(post)
if resp.Error != nil {
log.Warnf("CreatePost error: %s", resp.Error)
return resp.Error
}
return nil
}
func (mm *Mattermost) Close() {
mm.conn.WsQuit = true
if mm.handlerStopChan != nil {
mm.handlerStopChan <- true
mm.handlerStopChan = nil
}
}
func (mm *Mattermost) handleConnected() {
mm.handlerStopChan = make(chan bool)
mm.usermap = make(map[string]string)
mm.sentjoinedmap = make(map[string]bool)
mm.userdisplaynamemap = make(map[UserID]string)
go mm.handleLoop(mm.conn.MessageChan, mm.handlerStopChan)
fmt.Printf("Connected to mattermost\n")
chans := mm.conn.GetChannels()
for _, ch := range chans {
if len(strings.Split(ch.Name, "__")) == 2 {
continue // This is a DM channel
}
id := mm.reverseRoomId(ch.Id)
mm.handler.Joined(id)
// Update room info
room_info := &RoomInfo{
Name: ch.DisplayName,
Topic: ch.Header,
}
for _, t := range mm.conn.OtherTeams {
if t.Id == ch.TeamId {
if t.Team.DisplayName != "" {
room_info.Name = t.Team.DisplayName + " / " + room_info.Name
} else {
room_info.Name = t.Team.Name + " / " + room_info.Name
}
// TODO: cache last update time so we don't do this needlessly
if t.Team.LastTeamIconUpdate > 0 {
team_img, resp := mm.conn.Client.GetTeamIcon(t.Id, "")
if resp.Error == nil {
room_info.Picture = &BlobMediaObject{
ObjectFilename: t.Team.Name,
ObjectMimetype: http.DetectContentType(team_img),
ObjectData: team_img,
}
} else {
log.Warnf("Could not get team image: %s", resp.Error)
}
}
break
}
}
mm.handler.RoomInfoUpdated(id, UserID(""), room_info)
// Update member list
members, resp := mm.conn.Client.GetChannelMembers(ch.Id, 0, 1000, "")
if resp.Error == nil {
for _, mem := range *members {
if mem.UserId == mm.conn.User.Id {
continue
}
user := mm.conn.GetUser(mem.UserId)
if user != nil {
mm.ensureJoined(user, id)
mm.updateUserInfo(user)
} else {
log.Warnf("Could not find joined user: %s", mem.UserId)
}
}
} else {
log.Warnf("Could not get channel members: %s", resp.Error)
}
}
}
func (mm *Mattermost) handleLoop(msgCh chan *matterclient.Message, quitCh chan bool) {
for {
select {
case <-quitCh:
break
case msg := <-msgCh:
fmt.Printf("Mattermost: %#v\n", msg)
fmt.Printf("Mattermost raw: %#v\n", msg.Raw)
err := mm.handlePosted(msg.Raw)
if err != nil {
log.Warnf("Mattermost error: %s", err)
}
}
}
}
func (mm *Mattermost) updateUserInfo(user *model.User) {
userId := UserID(fmt.Sprintf("%s@%s", user.Username, mm.server))
userDisp := user.GetDisplayName(model.SHOW_NICKNAME_FULLNAME)
if lastdn, ok := mm.userdisplaynamemap[userId]; !ok || lastdn != userDisp {
ui := &UserInfo{
DisplayName: userDisp,
}
if user.LastPictureUpdate > 0 {
// TODO: cache last update time so we don't do this needlessly
img, resp := mm.conn.Client.GetProfileImage(user.Id, "")
if resp.Error == nil {
ui.Avatar = &BlobMediaObject{
ObjectFilename: user.Username,
ObjectMimetype: http.DetectContentType(img),
ObjectData: img,
}
} else {
log.Warnf("Could not get profile picture: %s", resp.Error)
}
mm.handler.UserInfoUpdated(userId, ui)
mm.userdisplaynamemap[userId] = userDisp
}
}
}
func (mm *Mattermost) ensureJoined(user *model.User, roomId RoomID) {
userId := UserID(fmt.Sprintf("%s@%s", user.Username, mm.server))
cache_key := fmt.Sprintf("%s / %s", userId, roomId)
if _, ok := mm.sentjoinedmap[cache_key]; !ok {
mm.handler.Event(&Event{
Author: userId,
Room: roomId,
Type: EVENT_JOIN,
})
mm.sentjoinedmap[cache_key] = true
}
}
func (mm *Mattermost) handlePosted(msg *model.WebSocketEvent) error {
channel_name := msg.Data["channel_name"].(string)
post_str := msg.Data["post"].(string)
var post model.Post
err := json.Unmarshal([]byte(post_str), &post)
if err != nil {
return err
}
// Skip self messages
if post.UserId == mm.conn.User.Id {
return nil
}
// Find sending user
user := mm.conn.GetUser(post.UserId)
if user == nil {
return fmt.Errorf("Invalid user")
}
userId := UserID(fmt.Sprintf("%s@%s", user.Username, mm.server))
mm.updateUserInfo(user)
// Build message event
msg_ev := &Event{
Author: userId,
Text: post.Message,
Type: EVENT_MESSAGE,
}
if post.Type == "me" {
msg_ev.Type = EVENT_ACTION
}
// Handle files
if post.FileIds != nil && len(post.FileIds) > 0 {
msg_ev.Attachments = []MediaObject{}
for _, file := range post.Metadata.Files {
blob, resp := mm.conn.Client.GetFile(file.Id)
if resp.Error != nil {
return resp.Error
}
media_object := &BlobMediaObject{
ObjectFilename: file.Name,
ObjectMimetype: file.MimeType,
ObjectData: blob,
}
if file.Width > 0 {
media_object.ObjectImageSize = &ImageSize{
Width: file.Width,
Height: file.Height,
}
}
msg_ev.Attachments = append(msg_ev.Attachments, media_object)
}
}
// Dispatch as PM or as room message
if len(strings.Split(channel_name, "__")) == 2 {
// Private message, no need to find room id
if user.Id == mm.conn.User.Id {
// Skip self sent messages
return nil
}
mm.handler.Event(msg_ev)
} else {
roomId := mm.reverseRoomId(post.ChannelId)
if roomId == "" {
return fmt.Errorf("Invalid channel id")
}
mm.ensureJoined(user, roomId)
if post.Type == "system_header_change" {
new_header := post.Props["new_header"].(string)
mm.handler.RoomInfoUpdated(roomId, userId, &RoomInfo{
Topic: new_header,
})
} else if post.Type == "" || post.Type == "me" {
msg_ev.Room = roomId
mm.handler.Event(msg_ev)
} else {
return fmt.Errorf("Unhandled post type: %s", post.Type)
}
}
return nil
}
|