forked from zorchenhimer/MovieNight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatcommands.go
645 lines (548 loc) · 20.4 KB
/
chatcommands.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
package main
import (
"fmt"
"html"
"strings"
"time"
"github.com/zorchenhimer/MovieNight/common"
)
type CommandControl struct {
user map[string]Command
mod map[string]Command
admin map[string]Command
}
type Command struct {
HelpText string
Function CommandFunction
}
type CommandFunction func(client *Client, args []string) (string, error)
var commands = &CommandControl{
user: map[string]Command{
common.CNMe.String(): Command{
HelpText: "Display an action message.",
Function: func(client *Client, args []string) (string, error) {
if len(args) != 0 {
client.Me(strings.Join(args, " "))
return "", nil
}
return "", fmt.Errorf("Missing a message")
},
},
common.CNHelp.String(): Command{
HelpText: "This help text.",
Function: cmdHelp,
},
common.CNEmotes.String(): Command{
HelpText: "Display a list of available emotes.",
Function: func(client *Client, args []string) (string, error) {
client.SendChatData(common.NewChatCommand(common.CmdEmotes, []string{"/emotes"}))
return "Opening emote list in new window.", nil
},
},
common.CNCount.String(): Command{
HelpText: "Display number of users in chat.",
Function: func(client *Client, args []string) (string, error) {
return fmt.Sprintf("Users in chat: %d", client.belongsTo.UserCount()), nil
},
},
common.CNColor.String(): Command{
HelpText: "Change user color.",
Function: func(cl *Client, args []string) (string, error) {
if len(args) > 2 {
return "", fmt.Errorf("Too many arguments!")
}
// If the caller is privileged enough, they can change the color of another user
if len(args) == 2 {
if cl.CmdLevel == common.CmdlUser {
return "", fmt.Errorf("You cannot change someone else's color. PeepoSus")
}
name, color := "", ""
if strings.ToLower(args[0]) == strings.ToLower(args[1]) ||
(common.IsValidColor(args[0]) && common.IsValidColor(args[1])) {
return "", fmt.Errorf("Name and color are ambiguous. Prefix the name with '@' or color with '#'")
}
// Check for explicit name
if strings.HasPrefix(args[0], "@") {
name = strings.TrimLeft(args[0], "@")
color = args[1]
common.LogDebugln("[color:mod] Found explicit name: ", name)
} else if strings.HasPrefix(args[1], "@") {
name = strings.TrimLeft(args[1], "@")
color = args[0]
common.LogDebugln("[color:mod] Found explicit name: ", name)
// Check for explicit color
} else if strings.HasPrefix(args[0], "#") {
name = strings.TrimPrefix(args[1], "@") // this shouldn't be needed, but just in case.
color = args[0]
common.LogDebugln("[color:mod] Found explicit color: ", color)
} else if strings.HasPrefix(args[1], "#") {
name = strings.TrimPrefix(args[0], "@") // this shouldn't be needed, but just in case.
color = args[1]
common.LogDebugln("[color:mod] Found explicit color: ", color)
// Guess
} else if common.IsValidColor(args[0]) {
name = strings.TrimPrefix(args[1], "@")
color = args[0]
common.LogDebugln("[color:mod] Guessed name: ", name, " and color: ", color)
} else if common.IsValidColor(args[1]) {
name = strings.TrimPrefix(args[0], "@")
color = args[1]
common.LogDebugln("[color:mod] Guessed name: ", name, " and color: ", color)
}
if name == "" {
return "", fmt.Errorf("Cannot determine name. Prefix name with @.")
}
if color == "" {
return "", fmt.Errorf("Cannot determine color. Prefix name with @.")
}
if color == "" {
common.LogInfof("[color:mod] %s missing color\n", cl.name)
return "", fmt.Errorf("Missing color")
}
if name == "" {
common.LogInfof("[color:mod] %s missing name\n", cl.name)
return "", fmt.Errorf("Missing name")
}
if err := cl.belongsTo.ForceColorChange(name, color); err != nil {
return "", err
}
return fmt.Sprintf("Color changed for user %s to %s\n", name, color), nil
}
// Don't allow an unprivileged user to change their color if
// it was changed by a mod
if cl.IsColorForced {
common.LogInfof("[color] %s tried to change a forced color\n", cl.name)
return "", fmt.Errorf("You are not allowed to change your color.")
}
if time.Now().Before(cl.nextColor) && cl.CmdLevel == common.CmdlUser {
return "", fmt.Errorf("Slow down. You can change your color in %0.0f seconds.", time.Until(cl.nextColor).Seconds())
}
if len(args) == 0 {
cl.setColor(common.RandomColor())
return "Random color chosen: " + cl.color, nil
}
// Change the color of the user
if !common.IsValidColor(args[0]) {
return "", fmt.Errorf("To choose a specific color use the format <i>/color #c029ce</i>. Hex values expected.")
}
cl.nextColor = time.Now().Add(time.Second * settings.RateLimitColor)
err := cl.setColor(args[0])
if err != nil {
common.LogErrorf("[color] could not send color update to client: %v\n", err)
}
common.LogInfof("[color] %s new color: %s\n", cl.name, cl.color)
return "Color changed successfully.", nil
},
},
common.CNWhoAmI.String(): Command{
HelpText: "Shows debug user info",
Function: func(cl *Client, args []string) (string, error) {
return fmt.Sprintf("Name: %s IsMod: %t IsAdmin: %t",
cl.name,
cl.CmdLevel >= common.CmdlMod,
cl.CmdLevel == common.CmdlAdmin), nil
},
},
common.CNAuth.String(): Command{
HelpText: "Authenticate to admin",
Function: func(cl *Client, args []string) (string, error) {
if cl.CmdLevel == common.CmdlAdmin {
return "", fmt.Errorf("You are already authenticated.")
}
// TODO: handle back off policy
if time.Now().Before(cl.nextAuth) {
cl.nextAuth = time.Now().Add(time.Second * settings.RateLimitAuth)
return "", fmt.Errorf("Slow down.")
}
cl.authTries += 1 // this isn't used yet
cl.nextAuth = time.Now().Add(time.Second * settings.RateLimitAuth)
pw := html.UnescapeString(strings.Join(args, " "))
if settings.AdminPassword == pw {
cl.CmdLevel = common.CmdlAdmin
cl.belongsTo.AddModNotice(cl.name + " used the admin password")
common.LogInfof("[auth] %s used the admin password\n", cl.name)
return "Admin rights granted.", nil
}
if cl.belongsTo.redeemModPass(pw) {
cl.CmdLevel = common.CmdlMod
cl.belongsTo.AddModNotice(cl.name + " used a mod password")
common.LogInfof("[auth] %s used a mod password\n", cl.name)
return "Moderator privileges granted.", nil
}
cl.belongsTo.AddModNotice(cl.name + " attempted to auth without success")
common.LogInfof("[auth] %s gave an invalid password\n", cl.name)
return "", fmt.Errorf("Invalid password.")
},
},
common.CNUsers.String(): Command{
HelpText: "Show a list of users in chat",
Function: func(cl *Client, args []string) (string, error) {
names := cl.belongsTo.GetNames()
return strings.Join(names, " "), nil
},
},
common.CNNick.String(): Command{
HelpText: "Change display name",
Function: func(cl *Client, args []string) (string, error) {
if time.Now().Before(cl.nextNick) && cl.CmdLevel == common.CmdlUser {
//cl.nextNick = time.Now().Add(time.Second * settings.RateLimitNick)
return "", fmt.Errorf("Slow down. You can change your nick in %0.0f seconds.", time.Until(cl.nextNick).Seconds())
}
cl.nextNick = time.Now().Add(time.Second * settings.RateLimitNick)
if len(args) == 0 {
return "", fmt.Errorf("Missing name to change to.")
}
newName := strings.TrimLeft(args[0], "@")
oldName := cl.name
forced := false
// Two arguments to force a name change on another user: `/nick OldName NewName`
if len(args) == 2 {
if cl.CmdLevel == common.CmdlUser {
return "", fmt.Errorf("Only admins and mods can do that PeepoSus")
}
oldName = strings.TrimLeft(args[0], "@")
newName = strings.TrimLeft(args[1], "@")
forced = true
}
if len(args) == 1 && cl.IsNameForced && cl.CmdLevel != common.CmdlAdmin {
return "", fmt.Errorf("You cannot change your name once it has been changed by an admin.")
}
err := cl.belongsTo.changeName(oldName, newName, forced)
if err != nil {
return "", fmt.Errorf("Unable to change name: " + err.Error())
}
return "", nil
},
},
common.CNStats.String(): Command{
HelpText: "Show some stats for stream.",
Function: func(cl *Client, args []string) (string, error) {
cl.belongsTo.clientsMtx.Lock()
users := len(cl.belongsTo.clients)
cl.belongsTo.clientsMtx.Unlock()
// Just print max users and time alive here
return fmt.Sprintf("Current users in chat: <b>%d</b><br />Max users in chat: <b>%d</b><br />Server uptime: <b>%s</b><br />Stream uptime: <b>%s</b>",
users,
stats.getMaxUsers(),
time.Since(stats.start),
stats.getStreamLength(),
), nil
},
},
common.CNPin.String(): Command{
HelpText: "Display the current room access type and pin/password (if applicable).",
Function: func(cl *Client, args []string) (string, error) {
switch settings.RoomAccess {
case AccessPin:
return "Room is secured via PIN. Current PIN: " + settings.RoomAccessPin, nil
case AccessRequest:
return "Room is secured via access requests. Users must request to be granted access.", nil
}
return "Room is open access. Anybody can join.", nil
},
},
},
mod: map[string]Command{
common.CNSv.String(): Command{
HelpText: "Send a server announcement message. It will show up red with a border in chat.",
Function: func(cl *Client, args []string) (string, error) {
if len(args) == 0 {
return "", fmt.Errorf("Missing message")
}
svmsg := formatLinks(strings.Join(common.ParseEmotesArray(args), " "))
cl.belongsTo.AddModNotice("Server message from " + cl.name)
cl.belongsTo.AddMsg(cl, false, true, svmsg)
return "", nil
},
},
common.CNPlaying.String(): Command{
HelpText: "Set the title text and info link.",
Function: func(cl *Client, args []string) (string, error) {
// Clear/hide title if sent with no arguments.
if len(args) == 0 {
cl.belongsTo.ClearPlaying()
return "Title cleared", nil
}
link := ""
title := ""
// pick out the link (can be anywhere, as long as there are no spaces).
for _, word := range args {
word = html.UnescapeString(word)
if strings.HasPrefix(word, "http://") || strings.HasPrefix(word, "https://") {
link = word
} else {
title = title + " " + word
}
}
title = strings.TrimSpace(title)
link = strings.TrimSpace(link)
if len(title) > settings.TitleLength {
return "", fmt.Errorf("Title too long (%d/%d)", len(title), settings.TitleLength)
}
// Send a notice to the mods and admins
if len(link) == 0 {
cl.belongsTo.AddModNotice(cl.name + " set the playing title to '" + title + "' with no link")
} else {
cl.belongsTo.AddModNotice(cl.name + " set the playing title to '" + title + "' with link '" + link + "'")
}
cl.belongsTo.SetPlaying(title, link)
return "", nil
},
},
common.CNUnmod.String(): Command{
HelpText: "Revoke a user's moderator privilages. Moderators can only unmod themselves.",
Function: func(cl *Client, args []string) (string, error) {
if len(args) > 0 && cl.CmdLevel != common.CmdlAdmin && cl.name != args[0] {
return "", fmt.Errorf("You can only unmod yourself, not others.")
}
if len(args) == 0 || (len(args) == 1 && strings.TrimLeft(args[0], "@") == cl.name) {
cl.Unmod()
cl.belongsTo.AddModNotice(cl.name + " has unmodded themselves")
return "You have unmodded yourself.", nil
}
name := strings.TrimLeft(args[0], "@")
if err := cl.belongsTo.Unmod(name); err != nil {
return "", err
}
cl.belongsTo.AddModNotice(cl.name + " has unmodded " + name)
return "", nil
},
},
common.CNKick.String(): Command{
HelpText: "Kick a user from chat.",
Function: func(cl *Client, args []string) (string, error) {
if len(args) == 0 {
return "", fmt.Errorf("Missing name to kick.")
}
return "", cl.belongsTo.Kick(strings.TrimLeft(args[0], "@"))
},
},
common.CNBan.String(): Command{
HelpText: "Ban a user from chat. They will not be able to re-join chat, but will still be able to view the stream.",
Function: func(cl *Client, args []string) (string, error) {
if len(args) == 0 {
return "", fmt.Errorf("missing name to ban.")
}
name := strings.TrimLeft(args[0], "@")
common.LogInfof("[ban] Attempting to ban %s\n", name)
return "", cl.belongsTo.Ban(name)
},
},
common.CNUnban.String(): Command{
HelpText: "Remove a ban on a user.",
Function: func(cl *Client, args []string) (string, error) {
if len(args) == 0 {
return "", fmt.Errorf("missing name to unban.")
}
name := strings.TrimLeft(args[0], "@")
common.LogInfof("[ban] Attempting to unban %s\n", name)
err := settings.RemoveBan(name)
if err != nil {
return "", err
}
cl.belongsTo.AddModNotice(cl.name + " has unbanned " + name)
return "", nil
},
},
common.CNPurge.String(): Command{
HelpText: "Purge the chat.",
Function: func(cl *Client, args []string) (string, error) {
common.LogInfoln("[purge] clearing chat")
cl.belongsTo.AddCmdMsg(common.CmdPurgeChat, nil)
return "", nil
},
},
},
admin: map[string]Command{
common.CNMod.String(): Command{
HelpText: "Grant moderator privilages to a user.",
Function: func(cl *Client, args []string) (string, error) {
if len(args) == 0 {
return "", fmt.Errorf("Missing user to mod.")
}
name := strings.TrimLeft(args[0], "@")
if err := cl.belongsTo.Mod(name); err != nil {
return "", err
}
cl.belongsTo.AddModNotice(cl.name + " has modded " + name)
return "", nil
},
},
common.CNReloadPlayer.String(): Command{
HelpText: "Reload the stream player for everybody in chat.",
Function: func(cl *Client, args []string) (string, error) {
cl.belongsTo.AddModNotice(cl.name + " has modded forced a player reload")
cl.belongsTo.AddCmdMsg(common.CmdRefreshPlayer, nil)
return "Reloading player for all chatters.", nil
},
},
common.CNReloadEmotes.String(): Command{
HelpText: "Reload the emotes on the server.",
Function: func(cl *Client, args []string) (string, error) {
go commandReloadEmotes(cl)
return "Reloading emotes...", nil
},
},
common.CNModpass.String(): Command{
HelpText: "Generate a single-use mod password.",
Function: func(cl *Client, args []string) (string, error) {
cl.belongsTo.AddModNotice(cl.name + " generated a mod password")
password := cl.belongsTo.generateModPass()
return "Single use password: " + password, nil
},
},
common.CNRoomAccess.String(): Command{
HelpText: "Change the room access type.",
Function: func(cl *Client, args []string) (string, error) {
// Print current access type if no arguments given
if len(args) == 0 {
return "Current room access type: " + string(settings.RoomAccess), nil
}
switch AccessMode(strings.ToLower(args[0])) {
case AccessOpen:
settings.RoomAccess = AccessOpen
common.LogInfoln("[access] Room set to open")
return "Room access set to open", nil
case AccessPin:
// A pin/password was provided, use it.
if len(args) == 2 {
// TODO: make this a bit more robust. Currently, only accepts a single word as a pin/password
settings.RoomAccessPin = args[1]
// A pin/password was not provided, generate a new one.
} else {
_, err := settings.generateNewPin()
if err != nil {
common.LogErrorln("Error generating new access pin: ", err.Error())
return "", fmt.Errorf("Unable to generate a new pin, access unchanged: " + err.Error())
}
}
settings.RoomAccess = AccessPin
common.LogInfoln("[access] Room set to pin: " + settings.RoomAccessPin)
return "Room access set to Pin: " + settings.RoomAccessPin, nil
case AccessRequest:
settings.RoomAccess = AccessRequest
common.LogInfoln("[access] Room set to request")
return "Room access set to request. WARNING: this isn't implemented yet.", nil
default:
return "", fmt.Errorf("Invalid access mode")
}
},
},
common.CNIP.String(): Command{
HelpText: "List users and IP in the server console. Requires logging level to be set to info or above.",
Function: func(cl *Client, args []string) (string, error) {
cl.belongsTo.clientsMtx.Lock()
common.LogInfoln("Clients:")
for id, client := range cl.belongsTo.clients {
common.LogInfof(" [%d] %s %s\n", id, client.name, client.conn.Host())
}
cl.belongsTo.clientsMtx.Unlock()
return "see console for output", nil
},
},
common.CNAddEmotes.String(): Command{
HelpText: "Add emotes from a given twitch channel.",
Function: func(cl *Client, args []string) (string, error) {
// Fire this off in it's own goroutine so the client doesn't
// block waiting for the emote download to finish.
go func() {
// Pretty sure this breaks on partial downloads (eg, one good channel and one non-existent)
err := getEmotes(args)
if err != nil {
cl.SendChatData(common.NewChatMessage("", "",
err.Error(),
common.CmdlUser, common.MsgCommandResponse))
return
}
// If the emotes were able to be downloaded, add the channels to settings
settings.AddApprovedEmotes(args)
// reload emotes now that new ones were added
err = loadEmotes()
if err != nil {
cl.SendChatData(common.NewChatMessage("", "",
err.Error(),
common.CmdlUser, common.MsgCommandResponse))
return
}
cl.belongsTo.AddModNotice(cl.name + " has added emotes from the following channels: " + strings.Join(args, ", "))
commandReloadEmotes(cl)
}()
return "Emote download initiated for the following channels: " + strings.Join(args, ", "), nil
},
},
},
}
func (cc *CommandControl) RunCommand(command string, args []string, sender *Client) (string, error) {
// get correct command from combined commands
cmd := common.GetFullChatCommand(command)
// Look for user command
if userCmd, ok := cc.user[cmd]; ok {
common.LogInfof("[user] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return userCmd.Function(sender, args)
}
// Look for mod command
if modCmd, ok := cc.mod[cmd]; ok {
if sender.CmdLevel >= common.CmdlMod {
common.LogInfof("[mod] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return modCmd.Function(sender, args)
}
common.LogInfof("[mod REJECTED] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return "", fmt.Errorf("You are not a mod Jebaited")
}
// Look for admin command
if adminCmd, ok := cc.admin[cmd]; ok {
if sender.CmdLevel == common.CmdlAdmin {
common.LogInfof("[admin] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return adminCmd.Function(sender, args)
}
common.LogInfof("[admin REJECTED] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return "", fmt.Errorf("You are not the admin Jebaited")
}
// Command not found
common.LogInfof("[cmd|error] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return "", fmt.Errorf("Invalid command.")
}
func cmdHelp(cl *Client, args []string) (string, error) {
url := "/help"
if cl.CmdLevel >= common.CmdlMod {
url += "?mod=1"
}
if cl.CmdLevel == common.CmdlAdmin {
url += "&admin=1"
}
cl.SendChatData(common.NewChatCommand(common.CmdHelp, []string{url}))
return `Opening help in new window.`, nil
}
func getHelp(lvl common.CommandLevel) map[string]string {
var cmdList map[string]Command
switch lvl {
case common.CmdlUser:
cmdList = commands.user
case common.CmdlMod:
cmdList = commands.mod
case common.CmdlAdmin:
cmdList = commands.admin
}
helptext := map[string]string{}
for name, cmd := range cmdList {
helptext[name] = cmd.HelpText
}
return helptext
}
func commandReloadEmotes(cl *Client) {
cl.SendServerMessage("Reloading emotes")
err := loadEmotes()
if err != nil {
common.LogErrorf("Unbale to reload emotes: %s\n", err)
//return "", err
cl.SendChatData(common.NewChatMessage("", "",
err.Error(),
common.CmdlUser, common.MsgCommandResponse))
return
}
cl.belongsTo.AddChatMsg(common.NewChatHiddenMessage(common.CdEmote, common.Emotes))
cl.belongsTo.AddModNotice(cl.name + " has reloaded emotes")
num := len(common.Emotes)
common.LogInfof("Loaded %d emotes\n", num)
cl.belongsTo.AddModNotice(fmt.Sprintf("%s reloaded %d emotes.", cl.name, num))
}