-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
464 lines (375 loc) · 11.7 KB
/
cli.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
// This program deletes old messages and files in your Slack workspace.
package main
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/slack-go/slack"
"github.com/spf13/pflag"
)
func main() {
// Slack OAuth Access Token beginning with `xoxp-`.
// To get a token you'll need to create a new Slack app and add
// it to your workspace:
// https://api.slack.com/apps
//
// You'll also need to give your app the correct scopes (permissions):
//
// channels:history - View messages and other content in a user’s public channels
// channels:read - View basic information about public channels in a workspace
// chat:write - Send messages on a user’s behalf
// files:read - View files shared in channels and conversations that a user has access to
// files:write - Upload, edit, and delete files on a user’s behalf
// groups:history - View messages and other content in a user’s private channels
// groups:read - View basic information about a user’s private channels
// im:history - View messages and other content in a user’s direct messages
// im:read - View basic information about a user’s direct messages
// mpim:history - View messages and other content in a user’s group direct messages
// mpim:read - View basic information about a user’s group direct messages
// users:read - View people in a workspace
//
token := pflag.String("token", "", "Slack token")
// Limited cleanup to messages and files owned by a specific Slack user.
user := pflag.String("user", "", "Limit cleanup to a specific Slack user (username without the `@` sign)")
// Filter public, private, multiparty and IM channels using this pattern.
// This limits cleanup to channels that match the filter.
filter := pflag.String("filter", "", "Filter channels (public, private, multi-party and IM) using this pattern, e.g. 'foo,bar' becomes regexp /(foo|bar)/i")
// Delete messages and files before this date.
before := pflag.String("before", "", "Delete messages and files before this date (REQUIRED, format: YYYYMMDD-HHII)")
commit := pflag.Bool("commit", false, "Perform the actual delete operations. Omitting this flag will perform a DRY-RUN.")
pflag.Parse()
// Try loading token from env var if no --token flag was passed.
if *token == "" {
*token = os.Getenv("MY_SLACK_TOKEN")
if *token == "" {
panic("--token flag (or MY_SLACK_TOKEN env var) required but missing")
}
}
if *before == "" {
pflag.PrintDefaults()
panic("--before flag is required but missing")
}
s := New(SlackCleanOptions{
Before: *before,
Token: *token,
})
// Get all users in workspace.
users, userID := s.Users(*user)
// Get channels.
channels := s.Channels(*filter, users)
// Get all files.
files := s.Files(userID, channels)
// Get messages.
messages := s.Messages(channels, userID)
fmt.Printf("\nFound %d messages and %d files to delete!\n\n", len(messages), len(files))
if !*commit {
fmt.Printf("Run command again with --commit flag to perform the delete operations!\n\n")
os.Exit(0)
}
// Delete messages.
s.DeleteMessages(messages)
// Delete files.
s.DeleteFiles(files)
fmt.Printf("\nIt's a Done Deal!\n\n")
}
type SlackClean struct {
c *slack.Client
beforeTS string
beforeTSUnix slack.JSONTime
}
type SlackCleanOptions struct {
Before string // e.g. 20060102-1504
Token string // e.g xoxp-...
}
func New(o SlackCleanOptions) (s *SlackClean) {
s = new(SlackClean)
t, err := time.Parse("20060102-1504", o.Before)
if err != nil {
panic(err)
}
s.beforeTS = s.TimeToSlackTS(t)
s.beforeTSUnix = slack.JSONTime(t.Unix())
fmt.Printf("Cleaning Slack messages before: %s (Slack TS: %s , Check: %s)\n", prettyDate(t), s.beforeTS, prettyDate(s.beforeTSUnix.Time()))
fmt.Printf("Using token: %s\n", o.Token)
s.c = slack.New(o.Token)
return
}
func (s *SlackClean) SlackTSToTime(ts string) time.Time {
parts := strings.Split(ts, ".")
sec, _ := strconv.ParseInt(parts[0], 10, 64)
nsec, _ := strconv.ParseInt(parts[1], 10, 64)
return time.Unix(sec, nsec*1000)
}
func (s *SlackClean) TimeToSlackTS(t time.Time) string {
ts := fmt.Sprintf("%d", t.UnixNano()/1000)
return fmt.Sprintf("%s.%s", ts[0:len(ts)-6], ts[len(ts)-6:])
}
func (s *SlackClean) ratelimitOrPanic(err error) {
if !strings.Contains(err.Error(), "rate limit") {
panic(err)
}
fmt.Printf("Rate limited, retrying in 1 sec ..\n")
time.Sleep(1000 * time.Millisecond)
}
func (s *SlackClean) Users(user string) (users map[string]string, userID string) {
users = make(map[string]string)
res, err := s.c.GetUsers()
if err != nil {
panic(err)
}
for _, u := range res {
if user != "" {
if u.Name == user {
userID = u.ID
} else {
fmt.Printf("Checked user %s (real name: %s)\n", u.Name, u.RealName)
}
}
users[u.ID] = u.Name
}
if user != "" {
if userID == "" {
panic("could not find user: " + user)
}
fmt.Printf("Found user %s (ID: %s)\n", users[userID], userID)
}
fmt.Printf("Fetched %d users\n", len(users))
return
}
func (s *SlackClean) Files(userID string, channels []slack.Channel) (filesToDelete []slack.File) {
var found int
for _, c := range channels {
p := &slack.ListFilesParameters{
Limit: 100,
Channel: c.ID,
}
if userID != "" {
p.User = userID
fmt.Printf("Fetching only files owned by user ID %s in channel %s (name: %s)\n", userID, c.ID, c.Name)
}
for {
var res []slack.File
var err error
res, p, err := s.c.ListFiles(*p)
if err != nil {
s.ratelimitOrPanic(err)
}
for _, f := range res {
if f.Created < s.beforeTSUnix {
found++
fmt.Printf("%04d. Found file %s (created: %s)\n", found, f.Name, prettyDate(f.Created.Time()))
filesToDelete = append(filesToDelete, f)
}
}
if p.Cursor == "" {
break
}
fmt.Printf("Fetching more files (cursor: %s , files: %d)\n", p.Cursor, len(filesToDelete))
}
}
fmt.Printf("Fetched %d files\n", len(filesToDelete))
return
}
func (s *SlackClean) Channels(filter string, users map[string]string) (channels []slack.Channel) {
var found int
var cursor string
// Create filter regexp.
var re *regexp.Regexp
if filter != "" {
re = regexp.MustCompile(`(?i)(` + strings.ReplaceAll(filter, ",", "|") + `)`)
fmt.Printf("Filtering channels using regexp pattern: %s\n", re.String())
}
for {
res, next, err := s.c.GetConversations(&slack.GetConversationsParameters{
Types: []string{"public_channel", "private_channel", "mpim", "im"},
Cursor: cursor,
})
if err != nil {
s.ratelimitOrPanic(err)
continue
}
for _, c := range res {
if c.IsMpIM {
// Include all MpIM channels if there's no filter.
if re == nil || re.MatchString(c.Name) {
found++
fmt.Printf("%04d. Found multiparty IM channel ID %s (name: %s)\n", found, c.ID, c.Name)
channels = append(channels, c)
}
} else if c.IsIM {
username := users[c.User]
// Include all IM channels if there's no filter.
if re == nil || re.MatchString(username) {
found++
fmt.Printf("%04d. Found IM channel ID %s (name: %s)\n", found, c.ID, username)
if c.Name == "" {
c.Name = username
}
channels = append(channels, c)
}
} else if c.IsPrivate {
if re == nil || re.MatchString(c.Name) {
found++
fmt.Printf("%04d. Found private channel ID %s (name: %s)\n", found, c.ID, c.Name)
channels = append(channels, c)
}
} else {
if re == nil || re.MatchString(c.Name) {
found++
fmt.Printf("%04d. Found public channel ID %s (name: %s)\n", found, c.ID, c.Name)
channels = append(channels, c)
}
}
}
if next == "" {
break
}
fmt.Printf("Fetching more channels (cursor: %s , channels: %d)\n", next, len(channels))
cursor = next
}
fmt.Printf("Fetched %d channels\n", len(channels))
return
}
func (s *SlackClean) Messages(channels []slack.Channel, userID string) (messagesToDelete []slack.Message) {
var found int
var keep int
var total int
clean := regexp.MustCompile(`[\r\n\t]+`)
for i, c := range channels {
fmt.Printf("%04d. Fetching messages for channel ID %s (name: %s)\n", i+1, c.ID, c.Name)
var cursor string
var last int
for {
res, err := s.c.GetConversationHistory(&slack.GetConversationHistoryParameters{
ChannelID: c.ID,
Cursor: cursor,
Limit: 1000,
})
if err != nil {
s.ratelimitOrPanic(err)
continue
}
for _, m := range res.Messages {
total++
if userID != "" {
if m.User == userID {
keep++
continue
}
}
if m.Timestamp > s.beforeTS {
keep++
continue
}
if found == last {
fmt.Println("")
}
found++
channelName := c.Name
if channelName == "" {
channelName = c.ID
}
preview := clean.ReplaceAllString(m.Text, " ")
if len(preview) > 50 {
preview = preview[0:46] + " ..."
}
fmt.Printf("%04d. [%-30s] [ts: %s] -- %s\n", found, "channel : "+channelName, prettyDate(s.SlackTSToTime(m.Timestamp)), preview)
m.Channel = c.ID
messagesToDelete = append(messagesToDelete, m)
if m.ThreadTimestamp != "" && m.Timestamp == m.ThreadTimestamp {
// Found parent message of a thread.
// Fetch replies and delete those too.
p := &slack.GetConversationRepliesParameters{
ChannelID: m.Channel,
Timestamp: m.Timestamp,
Limit: 1000,
}
for {
replies, hasMore, nextCursor, err := s.c.GetConversationReplies(p)
if err != nil {
s.ratelimitOrPanic(err)
continue
}
for _, r := range replies {
r.Channel = c.ID
messagesToDelete = append(messagesToDelete, r)
preview := clean.ReplaceAllString(r.Text, " ")
if len(preview) > 50 {
preview = preview[0:46] + " ..."
}
fmt.Printf("%04d. [%-30s] [ts: %s] -- %s\n", found, "thread : "+channelName, prettyDate(s.SlackTSToTime(r.Timestamp)), preview)
total++
found++
}
if !hasMore {
break
}
p.Cursor = nextCursor
}
}
}
if !res.HasMore {
break
}
cursor = res.ResponseMetaData.NextCursor
if found == last {
fmt.Printf(".")
} else {
fmt.Printf("Fetching more messages for channel ID %s (cursor: %s , messages: %d)\n", c.ID, cursor, len(messagesToDelete))
}
last = found
}
}
fmt.Printf("Fetched %d messages to delete (kept %d / %d)\n", len(messagesToDelete), keep, total)
return
}
func (s *SlackClean) DeleteMessages(messages []slack.Message) {
for i, m := range messages {
for {
ch, ts, err := s.c.DeleteMessage(m.Channel, m.Timestamp)
if err != nil {
if err.Error() == "cant_delete_message" {
fmt.Printf("%04d. [ERROR] Failed to delete message ID %s in channel ID %s\n", i+1, m.Timestamp, m.Channel)
break
}
if err.Error() == "message_not_found" {
fmt.Printf("%04d. [ERROR] Could not find message ID %s in channel ID %s\n", i+1, m.Timestamp, m.Channel)
break
}
s.ratelimitOrPanic(err)
continue
}
fmt.Printf("%04d. Deleted message ID %s in channel ID %s\n", i+1, ts, ch)
break
}
time.Sleep(50 * time.Millisecond)
}
}
func (s *SlackClean) DeleteFiles(files []slack.File) {
for i, f := range files {
for {
err := s.c.DeleteFile(f.ID)
if err != nil {
if err.Error() == "cant_delete_file" {
fmt.Printf("%04d. [ERROR] Failed to delete file named %s (created: %s)\n", i+1, f.Name, prettyDate(f.Created.Time()))
break
}
if err.Error() == "file_not_found" {
fmt.Printf("%04d. [ERROR] Could not find file named %s (created: %s)\n", i+1, f.Name, prettyDate(f.Created.Time()))
break
}
s.ratelimitOrPanic(err)
continue
}
fmt.Printf("%04d. Deleted file named %s (created: %s)\n", i+1, f.Name, prettyDate(f.Created.Time()))
break
}
time.Sleep(50 * time.Millisecond)
}
}
func prettyDate(t time.Time) string {
return t.UTC().Format(time.RFC3339)
}