This repository has been archived by the owner on Nov 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mastopurge.go
438 lines (383 loc) · 12.3 KB
/
mastopurge.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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
const (
Pagelimit = 40
)
/*
* Helper function to easily read input from console
*/
func readFromConsole() (stringstore string) {
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
stringstore = scanner.Text()
}
return
}
// Given an array of statuses, returns an array of their ids
func getStatusIds(vs []Status) []uint64 {
vsm := make([]uint64, len(vs))
for i, v := range vs {
vsm[i] = v.ID
}
return vsm
}
// Returns true if `a` is an element of the array
func idInSlice(a uint64, list []uint64) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
/*
* Various data structs
*/
type MastoPurgeSettings struct {
Server string `json:"server"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
AccessToken string `json:"access_token"`
}
type AccountInfo struct {
ID int `json:"id,string"`
Username string `json:"username"`
}
type RespAppRegister struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}
type RespAccessToken struct {
AccessToken string `json:"access_token"`
}
type Status struct {
ID uint64 `json:"id,string"`
//Content string `json:"content"`
CreatedAt time.Time `json:"created_at"`
}
var (
noninteractiveMode = flag.Bool("noninteractive", false, "Run in non-interactive mode, suitable for eg. cron jobs. (When run with a missing settings file, the config process will run interactively.)")
maxAgeArgument = flag.String("maxage", "", "Max age of posts you want to keep. Required when running in non-interactive mode. Allowed units: hours, days, weeks, months, years. Example: \"6 months\".")
configFile = flag.String("config", ".mastopurgesettings", "Path + filename for the settings file.")
printVersion = flag.Bool("version", false, "Print version, and exit.")
quietMode = flag.Bool("quiet", false, "Reduce output to the most important messages only.")
dryRun = flag.Bool("dryrun", false, "Run MastoPurge to preview its results, but without actually deleting any statuses.")
)
var versionString string = "0.0.0"
func main() {
flag.Parse()
if *printVersion {
fmt.Printf("MastoPurge version %s\n", versionString)
os.Exit(0)
}
interactiveMode := !(*noninteractiveMode)
if interactiveMode && !*quietMode {
/*
* Add fancyness!
*/
fmt.Println("\nWelcome to ...")
fmt.Println(" _ ")
fmt.Println(" _ __ ___ __ _ ___| |_ ___ _ __ _ _ _ __ __ _ ___ ")
fmt.Println("| '_ ` _ \\ / _` / __| __/ _ \\| '_ \\| | | | '__/ _` |/ _ \\")
fmt.Println("| | | | | | (_| \\__ \\ || (_) | |_) | |_| | | | (_| | __/")
fmt.Println("|_| |_| |_|\\__,_|___/\\__\\___/| .__/ \\__,_|_| \\__, |\\___|")
fmt.Println(" |_| |___/ ")
fmt.Println(" ... add German Datenhygiene to your Mastodon-Account!")
fmt.Print("\n\n")
fmt.Printf("Version %s\n\n", versionString)
}
/*
* Set up settings and Httpclient
*/
settings := MastoPurgeSettings{}
hc := &APIClient{}
hc.Timeout = time.Second * 5
hc.Init()
/*
* Check if configuration file (*configFile, default .mastopurgesettings) exists
* - Create if it does not exist
* - Use if exists
*/
configRaw, readerr := ioutil.ReadFile(*configFile)
if readerr != nil {
log.Printf("MastoPurge configuration file %s does not exist or is not accessible.\n", *configFile)
fmt.Println("\nFirst we need to connect MastoPurge to your Mastodon account.")
fmt.Println("Enter the domain of your Mastodon home instance: (e.g. \"metalhead.club\")")
fmt.Print("[Mastodon home instance]: ")
settings.Server = readFromConsole()
hc.Server = "https://" + settings.Server
// Register application for user on server
if !*quietMode {
log.Println(">>>>>> Registering MastoPurge App on " + settings.Server)
}
params := url.Values{}
params.Add("client_name", "MastoPurge")
params.Add("redirect_uris", "urn:ietf:wg:oauth:2.0:oob")
params.Add("scopes", "read write")
body, registerErr := hc.Request(http.MethodPost, "/api/v1/apps", params)
if registerErr != nil {
log.Fatal(registerErr)
}
// Parse response JSON
respAppRegister := RespAppRegister{}
err := json.Unmarshal(body, &respAppRegister)
if err != nil {
log.Fatal(err)
}
settings.ClientID = respAppRegister.ClientID
settings.ClientSecret = respAppRegister.ClientSecret
// User must manually authenticate app
authurl := "https://" + settings.Server + "/oauth/authorize?scope=read%20write&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&client_id=" + settings.ClientID
fmt.Println("\nPlease visit this URL in your web browser:")
fmt.Println(authurl)
fmt.Println("\n... and enter the code here:")
fmt.Print("[Auth code]: ")
code := readFromConsole()
// Request auth token via auth code ...
params = url.Values{}
params.Add("client_id", settings.ClientID)
params.Add("client_secret", settings.ClientSecret)
params.Add("grant_type", "authorization_code")
params.Add("redirect_uri", "urn:ietf:wg:oauth:2.0:oob")
params.Add("code", code)
body, err = hc.Request(http.MethodPost, "/oauth/token", params)
if err != nil {
log.Fatal(err)
}
respAccessToken := RespAccessToken{}
err = json.Unmarshal(body, &respAccessToken)
if err != nil {
log.Fatal(err)
}
settings.AccessToken = respAccessToken.AccessToken
hc.AccessToken = settings.AccessToken
// Write settings to config file
configRaw, _ = json.Marshal(settings)
err = ioutil.WriteFile(*configFile, configRaw, 0600)
if err != nil {
log.Fatal(err)
}
} else {
if !*quietMode {
log.Println("MastoPurge configuration found! Reading config.")
}
/*
* Load settings
*/
err := json.Unmarshal(configRaw, &settings)
if err != nil {
log.Fatalf("Config file is malformed :(\nPlease consider deleting '%s' from your file system and starting MastoPurge again.", *configFile)
}
hc.Server = "https://" + settings.Server
hc.AccessToken = settings.AccessToken
}
/*
* Check if account access is okay
*/
if !*quietMode {
log.Println("Requesting access to Mastodon account")
}
body, accessErr := hc.Request(http.MethodGet, "/api/v1/accounts/verify_credentials", nil)
if accessErr != nil {
log.Fatal(accessErr)
} else {
accountinfo := AccountInfo{}
err := json.Unmarshal(body, &accountinfo)
if accountinfo.ID == 0 {
if !*quietMode {
log.Println("Access DENIED :-(")
}
log.Fatalf("Unfortunately API access was not granted. Consider deleting '%s' and starting MastoPurge again!", *configFile)
} else {
if !*quietMode {
log.Println("Access GRANTED :-)")
log.Println(">>> Account ID:", accountinfo.ID)
log.Println(">>> Username:", accountinfo.Username)
}
// Do some date calculations ...
var maxage time.Duration
var maxtime time.Time
for {
maxagestring := *maxAgeArgument
if maxagestring == "" {
if interactiveMode {
fmt.Println("\nEnter the maximum age of the posts you want to KEEP, e.g. \"30 days\". Older posts will be deleted. Allowed units: hours, days, weeks, months, years.")
fmt.Print("[Maximum post age]: ")
maxagestring = readFromConsole()
} else {
log.Println("missing required argument --maxage")
flag.PrintDefaults()
os.Exit(1)
}
}
parts := strings.Split(maxagestring, " ")
if len(parts) == 2 {
maxagenum, converr := strconv.Atoi(parts[0])
if converr == nil {
var factor time.Duration
switch parts[1] {
case "hours", "hour":
factor = time.Hour
case "days", "day":
factor = time.Hour * time.Duration(24)
case "weeks", "week":
factor = time.Hour * time.Duration(24*7)
case "months", "month":
factor = time.Hour * time.Duration(24*30)
case "years", "year":
factor = time.Hour * time.Duration(24*365)
default:
factor = 0
}
if factor != 0 {
maxage = factor * time.Duration(maxagenum)
break
}
}
}
if interactiveMode {
fmt.Println("Error: Invalid age format.")
} else {
log.Fatalf("Invalid maximum age \"%s\".", maxagestring)
}
}
maxtime = time.Now().Add(-maxage)
if interactiveMode {
fmt.Println("Okay, let's do it! ")
fmt.Println("Posts older than", maxtime, "will be deleted!")
fmt.Print("Loading gun ")
for i := 0; i < 40; i++ {
fmt.Print(".")
time.Sleep(time.Duration(50) * time.Millisecond)
}
time.Sleep(time.Duration(2) * time.Second)
fmt.Print("\n\n")
} else {
log.Println("Posts older than", maxtime.Format("Jan 2, 2006 at 3:04:05 PM MST"), "will be deleted!")
}
// Get IDs of pinned posts (these won't be deleted)
if !*quietMode {
log.Printf("========== Fetching pinned statuses ==========\n")
}
params := url.Values{}
params.Add("pinned", "true")
resp, fetchErr := hc.Request(http.MethodGet, "/api/v1/accounts/"+strconv.Itoa(accountinfo.ID)+"/statuses", params)
if fetchErr != nil {
log.Fatal(fetchErr)
}
var pinnedStatuses []Status
err = json.Unmarshal(resp, &pinnedStatuses)
if err != nil {
// Maybe server response is an error message?
log.Println(string(resp))
log.Fatal(err)
}
pinnedStatusIds := getStatusIds(pinnedStatuses)
log.Printf("Found %d pinned statuses, which will not be deleted.", len(pinnedStatusIds))
var maxid uint64 = 0
var prevmaxid uint64 = 1
var deletedcount uint16
// Fetch new pages until there are no more pages
for {
if !*quietMode {
log.Printf("========== Fetching new statuses until status %d ==========\n", maxid)
}
nodeletions := true
// Fetch posts
params := url.Values{}
params.Add("limit", strconv.Itoa(Pagelimit))
if maxid != 0 {
params.Add("max_id", fmt.Sprint(maxid))
}
resp, fetchErr := hc.Request(http.MethodGet, "/api/v1/accounts/"+strconv.Itoa(accountinfo.ID)+"/statuses", params)
if fetchErr != nil {
log.Fatal(fetchErr)
}
var statuses []Status
err = json.Unmarshal(resp, &statuses)
if err != nil {
// Maybe server response is an error message?
log.Println(string(resp))
log.Fatal(err)
}
// Exit killer loop if there are no more statuses or if we are in a loop (maxid == prevmaxid)
if (len(statuses) == 0) || (maxid == prevmaxid) {
break
}
for _, status := range statuses {
// Parse time
if status.CreatedAt.Before(maxtime) {
if idInSlice(status.ID, pinnedStatusIds) {
if !*quietMode {
log.Println("Status " + fmt.Sprint(status.ID) + " is pinned; not deleting.")
}
continue
}
// Delete post
nodeletions = false
if !*dryRun {
delResp, delErr := hc.Request(http.MethodDelete, "/api/v1/statuses/"+fmt.Sprint(status.ID), nil)
if delErr != nil {
log.Println("!!! Could not delete status " + fmt.Sprint(status.ID) + " !!!")
}
var delStatus Status
err = json.Unmarshal(delResp, &delStatus)
if err != nil {
log.Println(string(delResp))
log.Fatal(err)
}
if delStatus.ID == status.ID {
deletedcount++
} else {
log.Println("Status " + fmt.Sprint(status.ID) + " could not be deleted :( \nResponse: " + string(delResp))
}
}
}
if status.ID < maxid || maxid == 0 {
maxid = status.ID - 1
prevmaxid = maxid
}
}
if nodeletions {
if !*quietMode {
log.Println("No posts to be deleted on this page. Trying next page ...")
}
} else {
if deletedcount == 0 && *dryRun {
if !*quietMode {
log.Println("0 statuses deleted, because -dryRun was passed.")
}
} else {
if !*quietMode {
log.Println(deletedcount, "statuses deleted.")
}
}
// Wait before fetching a new page. Give server time to re-assemble pages.
time.Sleep(time.Duration(1) * time.Second)
}
}
if deletedcount == 0 && *dryRun {
log.Println("[dryRun] 0 statuses deleted in total, because -dryRun was passed.")
}
// No more pages, done! :-)
if interactiveMode {
fmt.Println(">>>>>> ", deletedcount, "statuses were successfully deleted.")
} else {
log.Println(deletedcount, "statuses were successfully deleted.")
}
}
}
}