forked from 42wim/mm-go-irckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmuser.go
598 lines (551 loc) · 16.8 KB
/
mmuser.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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package irckit
import (
"fmt"
"net"
"regexp"
"strings"
"time"
"github.com/42wim/matterbridge/matterclient"
"github.com/42wim/matterircd/config"
"github.com/mattermost/platform/model"
"github.com/sorcix/irc"
)
type MmInfo struct {
MmGhostUser bool
Srv Server
Credentials *MmCredentials
Cfg *MmCfg
mc *matterclient.MMClient
idleStop chan struct{}
}
type MmCredentials struct {
Login string
Team string
Pass string
Server string
}
type MmCfg struct {
AllowedServers []string
SlackSettings config.Settings
MattermostSettings config.Settings
DefaultServer string
DefaultTeam string
Insecure bool
SkipTLSVerify bool
JoinExclude []string
JoinInclude []string
PartFake bool
PrefixMainTeam bool
}
func NewUserMM(c net.Conn, srv Server, cfg *MmCfg) *User {
u := NewUser(&conn{
Conn: c,
Encoder: irc.NewEncoder(c),
Decoder: irc.NewDecoder(c),
})
u.Srv = srv
u.MmInfo.Cfg = cfg
u.MmInfo.Cfg.AllowedServers = cfg.MattermostSettings.Restrict
u.MmInfo.Cfg.DefaultServer = cfg.MattermostSettings.DefaultServer
u.MmInfo.Cfg.DefaultTeam = cfg.MattermostSettings.DefaultTeam
u.MmInfo.Cfg.JoinInclude = cfg.MattermostSettings.JoinInclude
u.MmInfo.Cfg.JoinExclude = cfg.MattermostSettings.JoinExclude
u.MmInfo.Cfg.PartFake = cfg.MattermostSettings.PartFake
u.MmInfo.Cfg.Insecure = cfg.MattermostSettings.Insecure
u.MmInfo.Cfg.SkipTLSVerify = cfg.MattermostSettings.SkipTLSVerify
u.MmInfo.Cfg.PrefixMainTeam = cfg.MattermostSettings.PrefixMainTeam
u.idleStop = make(chan struct{})
// used for login
u.createService("mattermost", "loginservice")
u.createService("slack", "loginservice")
return u
}
func (u *User) loginToMattermost() (*matterclient.MMClient, error) {
mc := matterclient.New(u.Credentials.Login, u.Credentials.Pass, u.Credentials.Team, u.Credentials.Server)
if u.Cfg.Insecure {
mc.Credentials.NoTLS = true
}
mc.Credentials.SkipTLSVerify = u.Cfg.SkipTLSVerify
mc.SetLogLevel(LogLevel)
logger.Infof("login as %s (team: %s) on %s", u.Credentials.Login, u.Credentials.Team, u.Credentials.Server)
err := mc.Login()
if err != nil {
logger.Error("login failed", err)
return nil, err
}
logger.Info("login succeeded")
u.mc = mc
u.mc.WsQuit = false
go mc.WsReceiver()
go u.handleWsMessage()
// do anti idle on town-square, every installation should have this channel
channels := u.mc.GetChannels()
for _, channel := range channels {
if channel.Name == "town-square" {
go u.antiIdle(channel.Id)
continue
}
}
return mc, nil
}
func (u *User) logoutFromMattermost() error {
logger.Infof("logout as %s (team: %s) on %s", u.Credentials.Login, u.Credentials.Team, u.Credentials.Server)
err := u.mc.Logout()
if err != nil {
logger.Error("logout failed")
}
logger.Info("logout succeeded")
u.Srv.Logout(u)
u.idleStop <- struct{}{}
return nil
}
func (u *User) createMMUser(mmuser *model.User) *User {
if mmuser == nil {
return nil
}
if ghost, ok := u.Srv.HasUser(mmuser.Username); ok {
return ghost
}
ghost := &User{Nick: mmuser.Username, User: mmuser.Id, Real: mmuser.FirstName + " " + mmuser.LastName, Host: u.mc.Client.Url, Roles: mmuser.Roles, channels: map[Channel]struct{}{}}
ghost.MmGhostUser = true
u.Srv.Add(ghost)
return ghost
}
func (u *User) createService(nick string, what string) {
service := &User{Nick: nick, User: nick, Real: what, Host: "service", channels: map[Channel]struct{}{}}
service.MmGhostUser = true
u.Srv.Add(service)
}
func (u *User) addUserToChannel(user *model.User, channel string, channelId string) {
if user == nil {
return
}
ghost := u.createMMUser(user)
if ghost == nil {
logger.Warnf("Cannot join %v into %s", user, channel)
return
}
logger.Debugf("adding %s to %s", ghost.Nick, channel)
ch := u.Srv.Channel(channelId)
ch.Join(ghost)
}
func (u *User) addUsersToChannels() {
srv := u.Srv
throttle := time.Tick(time.Millisecond * 50)
logger.Debug("in addUsersToChannels()")
// add all users, also who are not on channels
ch := srv.Channel("&users")
for _, mmuser := range u.mc.GetUsers() {
// do not add our own nick
if mmuser.Id == u.mc.User.Id {
continue
}
u.createMMUser(mmuser)
u.addUserToChannel(mmuser, "&users", "&users")
}
ch.Join(u)
// channel that receives messages from channels not joined on irc
ch = srv.Channel("&messages")
ch.Join(u)
channels := make(chan *model.Channel, 5)
for i := 0; i < 10; i++ {
go u.addUserToChannelWorker(channels, throttle)
}
for _, mmchannel := range u.mc.GetChannels() {
logger.Debug("Adding channel", mmchannel)
channels <- mmchannel
}
close(channels)
}
func (u *User) addUserToChannelWorker(channels <-chan *model.Channel, throttle <-chan time.Time) {
for {
mmchannel, ok := <-channels
if !ok {
logger.Debug("Done adding user to channels")
return
}
logger.Debug("addUserToChannelWorker", mmchannel)
<-throttle
// exclude direct messages
var spoof func(string, string)
if strings.Contains(mmchannel.Name, "__") {
userId := strings.Split(mmchannel.Name, "__")[0]
u.createMMUser(u.mc.GetUser(userId))
spoof = u.MsgSpoofUser
} else {
channelName := mmchannel.Name
if mmchannel.TeamId != u.mc.Team.Id || u.Cfg.PrefixMainTeam {
channelName = u.mc.GetTeamName(mmchannel.TeamId) + "/" + mmchannel.Name
}
u.syncMMChannel(mmchannel.Id, channelName)
ch := u.Srv.Channel(mmchannel.Id)
spoof = ch.SpoofMessage
}
since := u.mc.GetLastViewedAt(mmchannel.Id)
// ignore invalid/deleted/old channels
if since == 0 {
continue
}
// post everything to the channel you haven't seen yet
postlist := u.mc.GetPostsSince(mmchannel.Id, since)
if postlist == nil {
// if the channel is not from the primary team id, we can't get posts
if mmchannel.TeamId == u.mc.Team.Id {
logger.Errorf("something wrong with getPostsSince for channel %s (%s)", mmchannel.Id, mmchannel.Name)
}
continue
}
var prevDate string
// traverse the order in reverse
for i := len(postlist.Order) - 1; i >= 0; i-- {
p := postlist.Posts[postlist.Order[i]]
if p.Type == model.POST_JOIN_LEAVE {
continue
}
if p.DeleteAt > p.CreateAt {
continue
}
ts := time.Unix(0, p.CreateAt*int64(time.Millisecond))
for _, post := range strings.Split(p.Message, "\n") {
if user, ok := u.mc.Users[p.UserId]; ok {
date := ts.Format("2006-01-02")
if date != prevDate {
spoof("matterircd", fmt.Sprintf("Replaying since %s", date))
prevDate = date
}
spoof(user.Username, fmt.Sprintf("[%s] %s", ts.Format("15:04"), post))
}
}
}
u.mc.UpdateLastViewed(mmchannel.Id)
}
}
func (u *User) handleWsMessage() {
updateChannelsThrottle := time.Tick(time.Second * 60)
for {
if u.mc.WsQuit {
logger.Debug("exiting handleWsMessage")
return
}
logger.Debug("in handleWsMessage", len(u.mc.MessageChan))
message := <-u.mc.MessageChan
logger.Debugf("MMUser WsReceiver: %#v", message.Raw)
// check if we have the users/channels in our cache. If not update
u.checkWsActionMessage(message.Raw, updateChannelsThrottle)
switch message.Raw.Event {
case model.WEBSOCKET_EVENT_POSTED:
u.handleWsActionPost(message.Raw)
case model.WEBSOCKET_EVENT_POST_EDITED:
u.handleWsActionPost(message.Raw)
case model.WEBSOCKET_EVENT_USER_REMOVED:
u.handleWsActionUserRemoved(message.Raw)
case model.WEBSOCKET_EVENT_USER_ADDED:
u.handleWsActionUserAdded(message.Raw)
}
}
}
func (u *User) handleWsActionPost(rmsg *model.WebSocketEvent) {
var ch Channel
data := model.PostFromJson(strings.NewReader(rmsg.Data["post"].(string)))
props := rmsg.Data
extraProps := model.StringInterfaceFromJson(strings.NewReader(rmsg.Data["post"].(string)))["props"].(map[string]interface{})
logger.Debugf("handleWsActionPost() receiving userid %s", data.UserId)
if rmsg.Event == model.WEBSOCKET_EVENT_POST_EDITED && data.HasReactions == true {
logger.Debugf("edit post with reactions, do not relay. We don't know if a reaction is added or the post has been edited")
return
}
if data.UserId == u.mc.User.Id {
if _, ok := extraProps["matterircd_"+u.mc.User.Id].(bool); ok {
logger.Debugf("message is sent from matterirc, not relaying %#v", data.Message)
return
}
if data.Type == model.POST_JOIN_LEAVE {
logger.Debugf("our own join/leave message. not relaying %#v", data.Message)
return
}
}
if data.ParentId != "" {
parentPost, resp := u.mc.Client.GetPost(data.ParentId, "")
if resp.Error != nil {
logger.Debugf("Unable to get parent post for", data)
} else {
parentGhost := u.createMMUser(u.mc.GetUser(parentPost.UserId))
data.Message = fmt.Sprintf("%s (re @%s: %s)", data.Message, parentGhost.Nick, parentPost.Message)
}
}
// create new "ghost" user
ghost := u.createMMUser(u.mc.GetUser(data.UserId))
// our own message, set our IRC self as user, not our mattermost self
if data.UserId == u.mc.User.Id {
ghost = u
}
spoofUsername := data.UserId
if ghost != nil {
spoofUsername = ghost.Nick
}
// if we got attachments (eg slack attachments) and we have a fallback message, show this.
if entries, ok := extraProps["attachments"].([]interface{}); ok {
for _, entry := range entries {
if f, ok := entry.(map[string]interface{}); ok {
data.Message = data.Message + "\n" + f["fallback"].(string)
}
}
}
// check if we have a override_username (from webhooks) and use it
overrideUsername, _ := extraProps["override_username"].(string)
if overrideUsername != "" {
// only allow valid irc nicks
re := regexp.MustCompile("^[a-zA-Z0-9_]*$")
if re.MatchString(overrideUsername) {
spoofUsername = overrideUsername
}
}
msgs := strings.Split(data.Message, "\n")
// direct message
if props["channel_type"] == "D" {
// our own message, ignore because we can't handle/fake those on IRC
if data.UserId == u.mc.User.Id {
return
}
}
if data.Type == model.POST_JOIN_LEAVE || data.Type == "system_leave_channel" || data.Type == "system_join_channel" || data.Type == "system_add_to_channel" || data.Type == "system_remove_from_channel" {
logger.Debugf("join/leave message. not relaying %#v", data.Message)
u.mc.UpdateChannels()
ch = u.Srv.Channel(data.ChannelId)
if data.Type == "system_add_to_channel" {
if added, ok := extraProps["addedUsername"].(string); ok {
user, resp := u.mc.Client.GetUserByUsername(added, "")
if resp.Error != nil {
fmt.Println(resp.Error)
return
}
ghost := u.createMMUser(user)
// we are added ourselves
if user.Id == u.mc.User.Id {
u.syncMMChannel(data.ChannelId, u.mc.GetChannelName(data.ChannelId))
return
}
ch.Join(ghost)
if adder, ok := extraProps["username"].(string); ok {
ch.SpoofMessage("system", "added "+added+" to the channel by "+adder)
}
}
return
}
if data.Type == "system_remove_from_channel" {
if removed, ok := extraProps["removedUsername"].(string); ok {
user, resp := u.mc.Client.GetUserByUsername(removed, "")
if resp.Error != nil {
fmt.Println(resp.Error)
return
}
ghost := u.createMMUser(user)
// we are removed
// TODO this doesn't actually work yet, we don't see the "system_remove_from_channel" message when we are removed ourselves
if user.Id == u.mc.User.Id {
ch.Part(u, "")
return
}
ch.Part(ghost, "")
ch.SpoofMessage("system", "removed "+removed+" from the channel")
}
return
}
if ghost == nil {
return
}
if !ch.HasUser(ghost) {
// TODO use u.handleWsActionUserAdded()
ch.Join(ghost)
} else {
// TODO use u.handleWsActionUserRemoved()
//u.handleWsActionUserRemoved(data)
ch.Part(ghost, "")
}
return
}
// not a private message so do channel stuff
if props["channel_type"] != "D" && ghost != nil {
ch = u.Srv.Channel(data.ChannelId)
// join if not in channel
if !ch.HasUser(ghost) {
logger.Debugf("User %s is not in channel %s. Joining now", ghost.Nick, ch.String())
//ch = u.Srv.Channel("&messages")
ch.Join(ghost)
}
// excluded channel
if stringInSlice(ch.String(), u.Cfg.JoinExclude) {
logger.Debugf("channel %s is in JoinExclude, send to &messages", ch.String())
ch = u.Srv.Channel("&messages")
}
// not in included channel
if len(u.Cfg.JoinInclude) > 0 && !stringInSlice(ch.String(), u.Cfg.JoinInclude) {
logger.Debugf("channel %s is not in JoinInclude, send to &messages", ch.String())
ch = u.Srv.Channel("&messages")
}
}
// add an edited string when messages are edited
if len(msgs) > 0 && rmsg.Event == model.WEBSOCKET_EVENT_POST_EDITED {
msgs[len(msgs)-1] = msgs[len(msgs)-1] + " (edited)"
}
// append channel name where messages are sent from
if props["channel_type"] != "D" && ch.ID() == "&messages" {
spoofUsername += "/" + u.Srv.Channel(data.ChannelId).String()
}
for _, m := range msgs {
if m == "" {
continue
}
if props["channel_type"] == "D" {
u.MsgSpoofUser(spoofUsername, m)
continue
}
if strings.Contains(data.Message, "@channel") || strings.Contains(data.Message, "@here") {
ch.SpoofNotice(spoofUsername, m)
continue
}
ch.SpoofMessage(spoofUsername, m)
}
if len(data.FileIds) > 0 {
logger.Debugf("files detected")
for _, fname := range u.mc.GetFileLinks(data.FileIds) {
if props["channel_type"] == "D" {
u.MsgSpoofUser(spoofUsername, "download file - "+fname)
} else {
ch.SpoofMessage(spoofUsername, "download file - "+fname)
}
}
}
logger.Debugf("handleWsActionPost() user %s sent %s", u.mc.GetUser(data.UserId).Username, data.Message)
logger.Debugf("%#v", data)
// updatelastviewed
u.mc.UpdateLastViewed(data.ChannelId)
}
func (u *User) handleWsActionUserRemoved(rmsg *model.WebSocketEvent) {
userId, ok := rmsg.Data["user_id"].(string)
if !ok {
return
}
ch := u.Srv.Channel(rmsg.Broadcast.ChannelId)
// remove ourselves from the channel
if userId == u.mc.User.Id {
return
}
ghost := u.createMMUser(u.mc.GetUser(userId))
if ghost == nil {
logger.Debugf("couldn't remove user %s (%s)", userId, u.mc.GetUser(userId).Username)
return
}
ch.Part(ghost, "")
}
func (u *User) handleWsActionUserAdded(rmsg *model.WebSocketEvent) {
userId, ok := rmsg.Data["user_id"].(string)
if !ok {
return
}
// do not add ourselves to the channel
if userId == u.mc.User.Id {
logger.Debugf("ACTION_USER_ADDED not adding myself to %s (%s)", u.mc.GetChannelName(rmsg.Broadcast.ChannelId), rmsg.Broadcast.ChannelId)
return
}
u.addUserToChannel(u.mc.GetUser(userId), "#"+u.mc.GetChannelName(rmsg.Broadcast.ChannelId), rmsg.Broadcast.ChannelId)
}
func (u *User) checkWsActionMessage(rmsg *model.WebSocketEvent, throttle <-chan time.Time) {
if u.mc.GetChannelName(rmsg.Broadcast.ChannelId) == "" {
select {
case <-throttle:
logger.Debugf("Updating channels for %#v", rmsg.Broadcast)
go u.mc.UpdateChannels()
default:
}
}
if rmsg.Data == nil {
return
}
}
func (u *User) MsgUser(toUser *User, msg string) {
u.Encode(&irc.Message{
Prefix: toUser.Prefix(),
Command: irc.PRIVMSG,
Params: []string{u.Nick},
Trailing: msg,
})
}
func (u *User) MsgSpoofUser(rcvuser string, msg string) {
for len(msg) > 400 {
u.Encode(&irc.Message{
Prefix: &irc.Prefix{Name: rcvuser, User: rcvuser, Host: rcvuser},
Command: irc.PRIVMSG,
Params: []string{u.Nick},
Trailing: msg[:400] + "\n",
})
msg = msg[400:]
}
u.Encode(&irc.Message{
Prefix: &irc.Prefix{Name: rcvuser, User: rcvuser, Host: rcvuser},
Command: irc.PRIVMSG,
Params: []string{u.Nick},
Trailing: msg,
})
}
// sync IRC with mattermost channel state
func (u *User) syncMMChannel(id string, name string) {
srv := u.Srv
mmusers, resp := u.mc.Client.GetUsersInChannel(id, 0, 50000, "")
if resp.Error != nil {
return
}
for _, user := range mmusers {
if user.Id != u.mc.User.Id {
u.addUserToChannel(user, "#"+name, id)
}
}
// before joining ourself
for _, user := range mmusers {
// join all the channels we're on on MM
if user.Id == u.mc.User.Id {
ch := srv.Channel(id)
// only join when we're not yet on the channel
if !ch.HasUser(u) {
logger.Debugf("syncMMChannel adding myself to %s (id: %s)", name, id)
if !stringInSlice(ch.String(), u.Cfg.JoinExclude) {
if len(u.Cfg.JoinInclude) > 0 {
if stringInSlice(ch.String(), u.Cfg.JoinInclude) {
ch.Join(u)
}
} else {
ch.Join(u)
}
}
svc, _ := srv.HasUser("mattermost")
ch.Topic(svc, u.mc.GetChannelHeader(id))
}
break
}
}
}
func (u *User) isValidMMServer(server string) bool {
if len(u.Cfg.AllowedServers) > 0 {
logger.Debugf("allowedservers: %s", u.Cfg.AllowedServers)
for _, srv := range u.Cfg.AllowedServers {
if srv == server {
return true
}
}
return false
}
return true
}
// antiIdle does a lastviewed every 60 seconds so that the user is shown as online instead of away
func (u *User) antiIdle(channelId string) {
ticker := time.NewTicker(time.Second * 60)
for {
select {
case <-u.idleStop:
logger.Debug("stopping antiIdle loop")
return
case <-ticker.C:
u.mc.UpdateLastViewed(channelId)
}
}
}