-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmd.go
426 lines (342 loc) · 8.44 KB
/
cmd.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
package mast
import (
"strings"
"strconv"
"regexp"
"errors"
"os/exec"
"runtime"
"github.com/atotto/clipboard"
)
type CmdReturnCode int
const (
CodeOk CmdReturnCode = 0
CodeNotOk = 1
CodeCommandNotFound = 2
CodeUserNotFound = 3
CodeQuit = -1
CodeHelp = -2
)
var CmdContentRegex =
regexp.MustCompile(`(?m)(( {0,1}~#| {0,1}~:)\[([^\[\]]*)\]| {0,1}~!!)`)
func CmdAvailable() ([]string) {
return []string{
"home",
"local",
"public",
"notifications",
"hashtag",
"whois",
"t",
"toot",
"tp",
"tootprivate",
"tu",
"tootunlisted",
"td",
"tootdirect",
"re",
"reply",
"rep",
"replyprivate",
"reu",
"replyunlisted",
"red",
"replydirect",
"rt",
"retoot",
"boost",
"ut",
"unretoot",
"unboost",
"fav",
"unfav",
"open",
"share",
// "search",
"help",
"?",
"quit",
"exit",
"bye",
}
}
func CmdAutocompleter(input string, knownUsers map[string]string) ([]string) {
var entries []string
if input == "" {
return entries
}
if input[len(input)-1:] == "@" ||
input == "whois " {
for _, knownUser := range knownUsers {
line := input + knownUser
lineFound := false
for _, entry := range entries {
if entry == line {
lineFound = true
break;
}
}
if lineFound == false {
entries = append(entries, line)
}
}
return entries
}
for _, cmd := range CmdAvailable() {
if strings.HasPrefix(cmd, input) == true {
entries = append(entries, cmd + " ")
}
}
return entries
}
func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) {
split := strings.SplitN(input, " ", 2)
cmd := split[0]
args := split[1]
switch cmd {
case "home":
timeline.Switch(TimelineHome, nil)
return CodeOk
case "local":
timeline.Switch(TimelineLocal, nil)
return CodeOk
case "public":
timeline.Switch(TimelinePublic, nil)
return CodeOk
case "notifications":
timeline.Switch(TimelineNotifications, nil)
return CodeOk
case "hashtag":
hashtag, isLocal, err := CmdHelperGetHashtagParams(args)
if err != nil {
return CodeNotOk
}
timelineOptions := TimelineOptions{
Hashtag: hashtag,
IsLocal: isLocal,
}
timeline.Switch(TimelineHashtag, &timelineOptions)
return CodeOk
case "whois":
accounts, err := timeline.SearchUser(args, 1)
if err != nil || len(accounts) < 1 {
// TODO: pass info back to caller
return CodeUserNotFound
}
timelineOptions := TimelineOptions{
User: *accounts[0],
}
timeline.Switch(TimelineUser, &timelineOptions)
return CodeOk
case "t", "toot":
return CmdToot(timeline, args, -1, VisibilityPublic)
case "tp", "tootprivate":
return CmdToot(timeline, args, -1, VisibilityPrivate)
case "tu", "tootunlisted":
return CmdToot(timeline, args, -1, VisibilityUnlisted)
case "td", "tootdirect":
return CmdToot(timeline, args, -1, VisibilityUnlisted)
case "re", "reply":
tootId, args, err := CmdHelperGetReplyParams(args)
if err != nil {
return CodeNotOk
}
return CmdToot(timeline, args, tootId, VisibilityPublic)
case "rep", "replyprivate":
tootId, args, err := CmdHelperGetReplyParams(args)
if err != nil {
return CodeNotOk
}
return CmdToot(timeline, args, tootId, VisibilityPrivate)
case "reu", "replyunlisted":
tootId, args, err := CmdHelperGetReplyParams(args)
if err != nil {
return CodeNotOk
}
return CmdToot(timeline, args, tootId, VisibilityUnlisted)
case "red", "replydirect":
tootId, args, err := CmdHelperGetReplyParams(args)
if err != nil {
return CodeNotOk
}
return CmdToot(timeline, args, tootId, VisibilityDirect)
case "rt", "retoot", "boost":
tootId, err := CmdHelperGetBoostParams(args)
if err != nil {
return CodeNotOk
}
return CmdBoost(timeline, tootId)
case "ut", "unretoot", "unboost":
tootId, err := CmdHelperGetBoostParams(args)
if err != nil {
return CodeNotOk
}
return CmdUnboost(timeline, tootId)
case "fav":
tootId, err := CmdHelperGetFavParams(args)
if err != nil {
return CodeNotOk
}
return CmdFav(timeline, tootId)
case "unfav":
tootId, err := CmdHelperGetFavParams(args)
if err != nil {
return CodeNotOk
}
return CmdUnfav(timeline, tootId)
case "open":
tootId, err := CmdHelperGetOpenParams(args)
if err != nil {
return CodeNotOk
}
return CmdOpen(timeline, tootId)
case "share":
tootId, err := CmdHelperGetShareParams(args)
if err != nil {
return CodeNotOk
}
return CmdShare(timeline, tootId)
case "?", "help":
return CodeHelp
case "quit", "exit", "bye":
return CodeQuit
}
return CodeCommandNotFound
}
func CmdHelperGetHashtagParams(args string) (string, bool, error) {
splitArgs := strings.SplitN(args, " ", 2)
if len(splitArgs) < 2 {
return args, false, nil
}
hashtag := splitArgs[0]
isLocal := false
if strings.ToLower(splitArgs[1]) == "local" {
isLocal = true
}
return hashtag, isLocal, nil
}
func CmdHelperGetTootIDFromString(s string) (int, error) {
tootId, err := strconv.Atoi(s)
if err != nil {
return -1, errors.New("Toot ID invalid!")
}
return tootId, nil
}
func CmdHelperGetReplyParams(args string) (int, string, error) {
splitArgs := strings.SplitN(args, " ", 2)
if len(splitArgs) < 2 {
return -1, args, errors.New("Toot ID missing!")
}
tootId, err := CmdHelperGetTootIDFromString(splitArgs[0])
if err != nil {
return tootId, args, err
}
newArgs := splitArgs[1]
return tootId, newArgs, nil
}
func CmdHelperGetBoostParams(args string) (int, error) {
return CmdHelperGetTootIDFromString(args)
}
func CmdHelperGetFavParams(args string) (int, error) {
return CmdHelperGetTootIDFromString(args)
}
func CmdHelperGetOpenParams(args string) (int, error) {
return CmdHelperGetTootIDFromString(args)
}
func CmdHelperGetShareParams(args string) (int, error) {
return CmdHelperGetTootIDFromString(args)
}
func CmdToot(
timeline *Timeline,
content string,
inReplyTo int,
visibility string) (CmdReturnCode) {
var status string = ""
var spoiler string = ""
var sensitive bool = false
var filesToUpload []string
tokens := CmdContentRegex.FindAllStringSubmatch(content, -1)
for _, token := range tokens {
if len(token[0]) >= 3 && token[0][(len(token[0])-3):] == "~!!" {
sensitive = true
continue
} else if len(token) == 4 && len(token[2]) >= 2 {
switch token[2][(len(tokens[0][2])-2):] {
case "~#":
spoiler = token[3]
continue
case "~:":
filesToUpload = append(filesToUpload, token[3])
continue
}
}
}
status = CmdContentRegex.ReplaceAllString(content, "")
_, err := timeline.Toot(
&status,
inReplyTo,
filesToUpload,
&visibility,
sensitive,
&spoiler,
)
if err != nil {
return CodeNotOk
}
return CodeOk
}
func CmdBoost(timeline *Timeline, tootID int) (CmdReturnCode) {
_, err := timeline.Boost(tootID, true)
if err != nil {
return CodeNotOk
}
return CodeOk
}
func CmdUnboost(timeline *Timeline, tootID int) (CmdReturnCode) {
_, err := timeline.Boost(tootID, false)
if err != nil {
return CodeNotOk
}
return CodeOk
}
func CmdFav(timeline *Timeline, tootID int) (CmdReturnCode) {
_, err := timeline.Fav(tootID, true)
if err != nil {
return CodeNotOk
}
return CodeOk
}
func CmdUnfav(timeline *Timeline, tootID int) (CmdReturnCode) {
_, err := timeline.Fav(tootID, false)
if err != nil {
return CodeNotOk
}
return CodeOk
}
func CmdOpen(timeline *Timeline, tootID int) (CmdReturnCode) {
var err error
url := timeline.Toots[tootID].Status.URL
switch runtime.GOOS {
case "darwin":
err = exec.Command("open", url).Start()
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
default:
err = errors.New("Platform not supported!")
}
if err != nil {
return CodeNotOk
}
return CodeOk
}
func CmdShare(timeline *Timeline, tootID int) (CmdReturnCode) {
url := timeline.Toots[tootID].Status.URL
err := clipboard.WriteAll(url)
if err != nil {
return CodeNotOk
}
return CodeOk
}