-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patharchivebot.go
584 lines (464 loc) · 12.8 KB
/
archivebot.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
package archivebot
import (
"crypto/sha1"
"crypto/tls"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"math/rand"
"os"
"path"
"runtime"
"time"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
elastic "gopkg.in/olivere/elastic.v5"
"github.com/gorilla/websocket"
"github.com/nlopes/slack"
logging "github.com/op/go-logging"
config "github.com/dutchcoders/slackarchive-bot/config"
models "github.com/dutchcoders/slackarchive-bot/models"
utils "github.com/dutchcoders/slackarchive-bot/utils"
// elastigo "github.com/mattbaird/elastigo/lib"
durable "github.com/dutchcoders/durable"
log2 "log"
"net/http"
"net/url"
)
const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second
// Time allowed to read the next pong message from the peer.
pongWait = 60 * time.Second
// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = 2 * time.Second
// Maximum message size allowed from peer.
maxMessageSize = 16384
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
var log = logging.MustGetLogger("archivebot")
type archiveBot struct {
session *mgo.Session
es *elastic.Client
// we are using both an indexer and message chan
// because all messages are being persisted
indexerChan chan interface{}
messageChan chan interface{}
archivers map[string]*archiveClient
config *config.Config
work chan func()
}
func New(conf *config.Config) *archiveBot {
session, err := mgo.Dial(conf.DSN)
if err != nil {
log.Error("Error opening database: %s", err.Error())
return nil
}
session.SetMode(mgo.Monotonic, true)
es, err := elastic.NewClient(elastic.SetURL(conf.ElasticSearch.Host), elastic.SetSniff(false))
if err != nil {
panic(err)
}
writer := make(chan interface{})
datadir := path.Join(conf.DataDir, fmt.Sprintf("slackarchive-%s", time.Now().Format("20060102150405")))
if err := os.Mkdir(datadir, 0755); err != nil {
log.Error("Could not create data dir %s: %s", datadir, err)
return nil
}
c := durable.Channel(writer, &durable.Config{
Name: "",
DataPath: datadir,
MaxBytesPerFile: 1048576,
MinMsgSize: 0,
MaxMsgSize: 16384,
SyncEvery: 10,
SyncTimeout: time.Second * 10,
Logger: log2.New(os.Stdout, "", 0),
})
return &archiveBot{
config: conf,
session: session,
es: es,
work: make(chan func()),
messageChan: writer,
indexerChan: c,
archivers: map[string]*archiveClient{},
}
}
func hash(s string) string {
h := sha1.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
// alternative messagehandler using websockets
func (ab *archiveBot) runMessageHandler2() {
u, err := url.Parse(ab.config.ApiURL)
if err != nil {
log.Error("Could not parse url %s: %s", ab.config.ApiURL, err.Error())
return
}
// basic authentication or certificates?
dialer := websocket.Dialer{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
dialer.TLSClientConfig = &tls.Config{}
go func() {
count := 0
for {
func() {
requestHeader := http.Header{}
requestHeader.Set("Authorization", fmt.Sprintf("Token %s", hash(ab.config.ApiToken)))
c, _, err := dialer.Dial(u.String(), requestHeader)
if err != nil {
log.Error("Could not connect to (%s): ", u.String(), err.Error())
return
}
defer c.Close()
ticker := time.NewTicker(pingPeriod)
defer func() {
ticker.Stop()
c.Close()
}()
c.SetReadLimit(maxMessageSize)
c.SetReadDeadline(time.Now().Add(pongWait))
c.SetPongHandler(func(string) error { c.SetReadDeadline(time.Now().Add(pongWait)); return nil })
log.Info("Connected to api socket.")
ch := make(chan bool)
// read messages
go func(c *websocket.Conn) {
defer func() {
ch <- true
}()
for {
if _, _, err := c.ReadMessage(); err == nil {
} else if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
log.Error("Connection closed unexpectedly: %s", err.Error())
break
} else {
log.Info("Error reading message: %s", err.Error())
break
}
}
}(c)
// write messages
for {
select {
case <-ch:
return
case msg := <-ab.indexerChan:
if data, err := json.Marshal(msg); err != nil {
log.Error("Could not serialize message: %s", err.Error())
continue
} else if err := c.WriteMessage(websocket.TextMessage, data); err != nil {
log.Error("Could not write message: %s", err.Error())
return
}
count++
if count%100 == 0 {
log.Info("Number of messages harvested: %d.", count)
}
case <-ticker.C:
c.SetWriteDeadline(time.Now().Add(writeWait))
if err := c.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
log.Error("writePump error: %s", err.Error())
return
}
}
}
}()
log.Info("Connection lost. Reconnecting in 5 seconds.")
time.Sleep(time.Second * 5)
}
}()
}
type archiveClient struct {
*slack.Client
ab *archiveBot
token string
team *slack.TeamInfo
channels []slack.Channel
}
func (ac *archiveClient) Sync() error {
for {
ac.ab.work <- func() func() {
return func() {
session := ac.ab.session.Copy()
defer session.Close()
db := Database(session)
log.Info("Syncing team")
team, err := ac.GetTeamInfo()
if err != nil {
log.Error("GetTeamInfo: %s", err.Error())
return
}
// update team
ac.team = team
// update team
t := models.Team{}
t.Token = ac.token
if err := utils.Merge(&t, *team); err != nil {
log.Error("Error merging team(%s): %s", team.ID, err.Error())
return
}
if _, err = db.Teams.UpsertId(t.ID, bson.M{
"$set": &t,
}); err != nil {
log.Error("Error upserting team(%s): %s", team.ID, err.Error())
return
}
log.Info("Syncing team finished")
}
}()
ac.ab.work <- func(teamID string) func() {
return func() {
session := ac.ab.session.Copy()
defer session.Close()
db := Database(session)
log.Info("Syncing users(%s)", teamID)
count := 0
users, err := ac.GetUsers()
for _, user := range users {
u := models.User{}
if err := utils.Merge(&u, user); err != nil {
log.Error("Error merging user(%s): %s", user.ID, err.Error())
continue
}
u.Team = ac.team.ID
if _, err = db.Users.UpsertId(u.ID, &u); err != nil {
log.Error("Error upserting user(%s): %s", user.ID, err.Error())
continue
}
count += 1
}
log.Info("Syncing users completed(%s): %#v %d", teamID, err, count)
}
}(ac.team.ID)
ac.ab.work <- func(teamID string) func() {
return func() {
session := ac.ab.session.Copy()
defer session.Close()
db := Database(session)
log.Info("Syncing channels (%s)", teamID)
count := 0
channels, err := ac.GetChannels(false)
if err != nil {
log.Error("Error syncing channels (%s): %s", teamID, err.Error())
return
}
ac.channels = channels
for _, channel := range channels {
c := models.Channel{}
if err := utils.Merge(&c, channel); err != nil {
log.Error("Error merging channel(%s): %s", channel.ID, err.Error())
continue
}
c.Team = teamID
if _, err = db.Channels.UpsertId(c.ID, &c); err != nil {
log.Error("Error upserting channel(%s): %s", channel.ID, err.Error())
continue
}
count += 1
}
log.Info("Syncing channels completed (%s): %#v %d", teamID, err, count)
}
}(ac.team.ID)
for _, channel := range ac.channels {
ac.ab.work <- func(teamID string, channelID string) func() {
return func() {
log.Info("Syncing latest channel messages: %s", channelID)
params := slack.NewHistoryParameters()
params.Inclusive = true
var history *slack.History
if v, err := ac.GetChannelHistory(channelID, params); err != nil {
log.Error("Error retrieving channel history: %s", err.Error())
return
} else {
history = v
}
for _, message := range history.Messages {
m := models.Message{}
if err := utils.Merge(&m, message.Msg); err != nil {
log.Error("Error merging message: %s", err.Error())
continue
}
m.ID = fmt.Sprintf("%s-%s-%s", teamID, channelID, m.Timestamp)
m.Team = teamID
m.Channel = channelID
ac.ab.messageChan <- NewMessage("message", m)
}
log.Info("Syncing latest channels messages completed: %s", channelID)
}
}(ac.team.ID, channel.ID)
}
}
return nil
}
type Message struct {
Category string
Body interface{}
}
func NewMessage(category string, body interface{}) Message {
return Message{
Category: category,
Body: body,
}
}
func (ac *archiveClient) Bot() {
rtm := ac.NewRTM()
go rtm.ManageConnection()
Loop:
for {
select {
case msg := <-rtm.IncomingEvents:
switch ev := msg.Data.(type) {
case *slack.HelloEvent:
// Ignore hello
case *slack.ConnectedEvent:
// log.Debug("Connection counter: %d %s", ev.ConnectionCount, ev.Info.Team.Domain)
ac.channels = ev.Info.Channels
case *slack.MessageEvent:
m := models.Message{}
if err := utils.Merge(&m, msg.Data.(*slack.MessageEvent).Msg); err != nil {
log.Error("Merge error: %s", err.Error())
continue
}
m.ID = fmt.Sprintf("%s-%s-%s", ac.team.ID, m.Channel, m.Timestamp)
m.Team = ac.team.ID
ac.ab.messageChan <- NewMessage("message", m)
case *slack.PresenceChangeEvent:
// log.Debug("Presence Change: %v", ev)
case *slack.LatencyReport:
// log.Debug("Current latency: %v", ev.Value)
case *slack.UserTypingEvent:
log.Debug("User(%s) typing...", msg.Data.(*slack.UserTypingEvent).User)
case *slack.RTMError:
log.Debug("Error: %s", ev.Error())
case *slack.ConnectingEvent:
log.Debug("Connecting...")
case slack.RTMEvent:
// 2016/09/04 12:19:18 Unexpected: slack.RTMEvent{Type:"connection_error", Data:(*slack.ConnectionErrorEvent)(0xc4243f6380
if err, ok := ev.Data.(error); ok {
log.Error("Event: %s %s", ev.Type, err.Error())
} else {
log.Debug("Event: %s", ev.Type)
}
case *slack.ReconnectUrlEvent:
// log.Debug("ReconnectURL: %#v", ev)
case *slack.InvalidAuthEvent:
log.Debug("Invalid credentials")
break Loop
// case *slack.DesktopNotification:
default:
// Ignore other events..
log.Debug("Unexpected: %s, %#v", ac.team.ID, ac.team.Domain, msg)
}
}
}
}
func (ab *archiveBot) NewArchiveClient(token string) (*archiveClient, error) {
ac := archiveClient{
slack.New(token),
ab,
token,
nil,
nil,
}
ac.SetDebug(true)
if team, err := ac.GetTeamInfo(); err != nil {
return nil, err
} else {
ac.team = team
ab.archivers[team.ID] = &ac
}
return &ac, nil
}
func (ac *archiveClient) Start() {
go func() {
defer func() {
if err := recover(); err != nil {
trace := make([]byte, 1024)
count := runtime.Stack(trace, true)
log.Error("Error: %s", err)
log.Debug("Stack of %d bytes: %s", count, trace)
return
}
}()
if err := ac.Sync(); err != nil {
log.Error("Sync error: %s", err.Error())
}
}()
go func() {
defer func() {
if err := recover(); err != nil {
trace := make([]byte, 1024)
count := runtime.Stack(trace, true)
log.Error("Error: %s", err)
log.Debug("Stack of %d bytes: %s", count, trace)
return
}
}()
ac.Bot()
}()
}
func (ab *archiveBot) worker() {
// this runs periodically with a random time, specific functions
// to prevent rate limiting to happen
for {
fn := <-ab.work
wait := 5 + rand.Intn(5)
log.Info("Waiting for %d seconds", wait)
time.Sleep(time.Duration(wait) * time.Second)
fn()
}
}
func (ab *archiveBot) Reload() {
}
// test feed
func (ab *archiveBot) test() {
for {
m := models.Message{}
teamID := "TEST"
channelID := "TEST"
m.ID = fmt.Sprintf("%s-%s-%d", teamID, channelID, time.Now().Unix())
m.Team = teamID
m.Channel = channelID
ab.messageChan <- NewMessage("message", m)
fmt.Printf("Pushed message %+v\n", m)
time.Sleep(5 * time.Second)
}
}
func (ab *archiveBot) Start() {
go ab.runMessageHandler2()
go ab.worker()
session := ab.session.Copy()
defer session.Close()
db := Database(session)
tokens := ab.config.Tokens
for _, token := range tokens {
var team models.Team
if err := db.Teams.Find(bson.M{
"token": token,
}).One(&team); err == nil {
log.Info("Starting archive bot for team: %s", team.Domain)
} else {
log.Info("Starting archive bot for token: %s", token)
}
ac, err := ab.NewArchiveClient(token)
if err == nil {
} else if err.Error() == "invalid_auth" || err.Error() == "account_inactive" {
continue
} else if err != nil {
log.Error("Error starting client %s: %s", token, err)
continue
}
ac.Start()
}
}