forked from zorchenhimer/MovieNight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatroom.go
475 lines (388 loc) · 11.2 KB
/
chatroom.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
package main
import (
"fmt"
"strings"
"sync"
"time"
"github.com/zorchenhimer/MovieNight/common"
)
const (
ColorServerMessage string = "#ea6260"
)
type ChatRoom struct {
clients []*Client // this needs to be a pointer. key is suid.
clientsMtx sync.Mutex
queue chan common.ChatData
modqueue chan common.ChatData // mod and admin broadcast messages
playing string
playingLink string
modPasswords []string // single-use mod passwords
modPasswordsMtx sync.Mutex
}
//initializing the chatroom
func newChatRoom() (*ChatRoom, error) {
cr := &ChatRoom{
queue: make(chan common.ChatData, 1000),
modqueue: make(chan common.ChatData, 1000),
clients: []*Client{},
}
err := loadEmotes()
if err != nil {
return nil, fmt.Errorf("error loading emotes: %s", err)
}
common.LogInfof("Loaded %d emotes\n", len(common.Emotes))
//the "heartbeat" for broadcasting messages
go cr.Broadcast()
return cr, nil
}
// A new client joined
func (cr *ChatRoom) Join(conn *chatConnection, data common.JoinData) (*Client, error) {
defer cr.clientsMtx.Unlock()
cr.clientsMtx.Lock()
sendHiddenMessage := func(cd common.ClientDataType, i interface{}) {
// If the message cant be converted, then just don't send
if d, err := common.NewChatHiddenMessage(cd, i).ToJSON(); err == nil {
conn.WriteJSON(d)
}
}
if settings.RoomAccess == AccessPin && data.Name == settings.RoomAccessPin {
sendHiddenMessage(common.CdNotify, "That's the access pin! Please enter a name.")
return nil, UserFormatError{Name: data.Name}
}
if !common.IsValidName(data.Name) {
sendHiddenMessage(common.CdNotify, common.InvalidNameError)
return nil, UserFormatError{Name: data.Name}
}
nameLower := strings.ToLower(data.Name)
for _, client := range cr.clients {
if strings.ToLower(client.name) == nameLower {
sendHiddenMessage(common.CdNotify, "Name already taken")
return nil, UserTakenError{Name: data.Name}
}
}
// If color is invalid, then set it to a random color
if !common.IsValidColor(data.Color) {
data.Color = common.RandomColor()
}
client, err := NewClient(conn, cr, data.Name, data.Color)
if err != nil {
sendHiddenMessage(common.CdNotify, "Could not join client")
return nil, fmt.Errorf("Unable to join client: %v", err)
}
// Overwrite to use client instead
sendHiddenMessage = func(cd common.ClientDataType, i interface{}) {
client.SendChatData(common.NewChatHiddenMessage(cd, i))
}
host := client.Host()
if banned, names := settings.IsBanned(host); banned {
sendHiddenMessage(common.CdNotify, "You are banned")
return nil, newBannedUserError(host, data.Name, names)
}
cr.clients = append(cr.clients, client)
common.LogChatf("[join] %s %s\n", host, data.Color)
playingCommand, err := common.NewChatCommand(common.CmdPlaying, []string{cr.playing, cr.playingLink}).ToJSON()
if err != nil {
common.LogErrorf("Unable to encode playing command on join: %s\n", err)
} else {
client.Send(playingCommand)
}
cr.AddEventMsg(common.EvJoin, data.Name, data.Color)
sendHiddenMessage(common.CdJoin, nil)
sendHiddenMessage(common.CdEmote, common.Emotes)
stats.updateMaxUsers(len(cr.clients))
return client, nil
}
// TODO: fix this up a bit. kick and leave are the same, incorrect, error: "That
// name was already used!" leaving the chatroom
func (cr *ChatRoom) Leave(name, color string) {
defer cr.clientsMtx.Unlock()
cr.clientsMtx.Lock() //preventing simultaneous access to the `clients` map
client, id, err := cr.getClient(name)
if err != nil {
common.LogErrorf("[leave] Unable to get client suid %v\n", err)
return
}
host := client.Host()
name = client.name // grab the name from here for proper capitalization
client.conn.Close()
cr.delClient(id)
cr.AddEventMsg(common.EvLeave, name, color)
common.LogChatf("[leave] %s %s\n", host, name)
}
// kicked from the chatroom
func (cr *ChatRoom) Kick(name string) error {
defer cr.clientsMtx.Unlock()
cr.clientsMtx.Lock() //preventing simultaneous access to the `clients` map
client, id, err := cr.getClient(name)
if err != nil {
return fmt.Errorf("Unable to get client for name " + name)
}
if client.CmdLevel == common.CmdlMod {
return fmt.Errorf("You cannot kick another mod.")
}
if client.CmdLevel == common.CmdlAdmin {
return fmt.Errorf("Jebaited No.")
}
color := client.color
host := client.Host()
client.conn.Close()
cr.delClient(id)
cr.AddEventMsg(common.EvKick, name, color)
common.LogInfof("[kick] %s %s has been kicked\n", host, name)
return nil
}
func (cr *ChatRoom) Ban(name string) error {
defer cr.clientsMtx.Unlock()
cr.clientsMtx.Lock()
client, id, err := cr.getClient(name)
if err != nil {
common.LogErrorf("[ban] Unable to get client for name %q\n", name)
return fmt.Errorf("Cannot find that name")
}
if client.CmdLevel == common.CmdlAdmin {
return fmt.Errorf("You cannot ban an admin Jebaited")
}
names := []string{}
host := client.Host()
color := client.color
// Remove the named client
client.conn.Close()
cr.delClient(id)
// Remove additional clients on that IP address
for id, c := range cr.clients {
if c.Host() == host {
names = append(names, client.name)
client.conn.Close()
cr.delClient(id)
}
}
err = settings.AddBan(host, names)
if err != nil {
common.LogErrorf("[BAN] Error banning %q: %s\n", name, err)
cr.AddEventMsg(common.EvKick, name, color)
} else {
cr.AddEventMsg(common.EvBan, name, color)
}
return nil
}
// Add a chat message from a viewer
func (cr *ChatRoom) AddMsg(from *Client, isAction, isServer bool, msg string) {
t := common.MsgChat
if isAction {
t = common.MsgAction
}
if isServer {
t = common.MsgServer
}
cr.AddChatMsg(common.NewChatMessage(from.name, from.color, msg, from.CmdLevel, t))
}
// Add a chat message object to the queue
func (cr *ChatRoom) AddChatMsg(data common.ChatData) {
select {
case cr.queue <- data:
default:
common.LogErrorln("Unable to queue chat message. Channel full.")
}
}
func (cr *ChatRoom) AddCmdMsg(command common.CommandType, args []string) {
select {
case cr.queue <- common.NewChatCommand(command, args):
default:
common.LogErrorln("Unable to queue command message. Channel full.")
}
}
func (cr *ChatRoom) AddModNotice(message string) {
select {
case cr.modqueue <- common.NewChatMessage("", "", message, common.CmdlUser, common.MsgNotice):
default:
common.LogErrorln("Unable to queue notice. Channel full.")
}
}
func (cr *ChatRoom) AddEventMsg(event common.EventType, name, color string) {
select {
case cr.queue <- common.NewChatEvent(event, name, color):
default:
common.LogErrorln("Unable to queue event message. Channel full.")
}
}
func (cr *ChatRoom) Unmod(name string) error {
defer cr.clientsMtx.Unlock()
cr.clientsMtx.Lock()
client, _, err := cr.getClient(name)
if err != nil {
return err
}
client.Unmod()
client.SendServerMessage(`You have been unmodded.`)
return nil
}
func (cr *ChatRoom) Mod(name string) error {
defer cr.clientsMtx.Unlock()
cr.clientsMtx.Lock()
client, _, err := cr.getClient(name)
if err != nil {
return err
}
if client.CmdLevel < common.CmdlMod {
client.CmdLevel = common.CmdlMod
client.SendServerMessage(`You have been modded.`)
}
return nil
}
func (cr *ChatRoom) ForceColorChange(name, color string) error {
defer cr.clientsMtx.Unlock()
cr.clientsMtx.Lock()
client, _, err := cr.getClient(name)
if err != nil {
return err
}
client.IsColorForced = true
client.color = color
return nil
}
func (cr *ChatRoom) UserCount() int {
return len(cr.clients)
}
//broadcasting all the messages in the queue in one block
func (cr *ChatRoom) Broadcast() {
send := func(data common.ChatData, client *Client) {
err := client.SendChatData(data)
if err != nil {
common.LogErrorf("Error sending data to client: %v\n", err)
}
}
for {
select {
case msg := <-cr.queue:
cr.clientsMtx.Lock()
for _, client := range cr.clients {
go send(msg, client)
}
cr.clientsMtx.Unlock()
case msg := <-cr.modqueue:
cr.clientsMtx.Lock()
for _, client := range cr.clients {
if client.CmdLevel >= common.CmdlMod {
send(msg, client)
}
}
cr.clientsMtx.Unlock()
default:
time.Sleep(50 * time.Millisecond)
// No messages to send
// This default block is required so the above case
// does not block.
}
}
}
func (cr *ChatRoom) ClearPlaying() {
cr.playing = ""
cr.playingLink = ""
cr.AddCmdMsg(common.CmdPlaying, []string{"", ""})
}
func (cr *ChatRoom) SetPlaying(title, link string) {
cr.playing = title
cr.playingLink = link
cr.AddCmdMsg(common.CmdPlaying, []string{title, link})
}
func (cr *ChatRoom) GetNames() []string {
names := []string{}
defer cr.clientsMtx.Unlock()
cr.clientsMtx.Lock()
for _, val := range cr.clients {
names = append(names, val.name)
}
return names
}
func (cr *ChatRoom) delClient(sliceId int) {
cr.clients = append(cr.clients[:sliceId], cr.clients[sliceId+1:]...)
}
func (cr *ChatRoom) getClient(name string) (*Client, int, error) {
for id, client := range cr.clients {
if client.name == name {
return client, id, nil
}
}
return nil, -1, fmt.Errorf("client with that name not found")
}
func (cr *ChatRoom) generateModPass() string {
defer cr.modPasswordsMtx.Unlock()
cr.modPasswordsMtx.Lock()
pass, err := generatePass(time.Now().Unix())
if err != nil {
return fmt.Sprintf("Error generating moderator password: %s", err)
}
// Make sure the password is unique
for existsInSlice(cr.modPasswords, pass) {
pass, err = generatePass(time.Now().Unix())
if err != nil {
return fmt.Sprintf("Error generating moderator password: %s", err)
}
}
cr.modPasswords = append(cr.modPasswords, pass)
return pass
}
func (cr *ChatRoom) redeemModPass(pass string) bool {
if pass == "" {
return false
}
defer cr.modPasswordsMtx.Unlock()
cr.modPasswordsMtx.Lock()
if existsInSlice(cr.modPasswords, pass) {
cr.modPasswords = removeFromSlice(cr.modPasswords, pass)
return true
}
return false
}
func removeFromSlice(slice []string, needle string) []string {
slc := []string{}
for _, item := range slice {
if item != needle {
slc = append(slc, item)
}
}
return slc
}
func existsInSlice(slice []string, needle string) bool {
for _, item := range slice {
if item == needle {
return true
}
}
return false
}
func (cr *ChatRoom) changeName(oldName, newName string, forced bool) error {
cr.clientsMtx.Lock()
defer cr.clientsMtx.Unlock()
if !common.IsValidName(newName) {
return fmt.Errorf("%q nick is not a valid name", newName)
}
newLower := strings.ToLower(newName)
oldLower := strings.ToLower(oldName)
var currentClient *Client
for _, client := range cr.clients {
if strings.ToLower(client.name) == newLower {
if strings.ToLower(client.name) != oldLower {
return fmt.Errorf("%q is already taken.", newName)
}
}
if strings.ToLower(client.name) == oldLower {
currentClient = client
}
}
if currentClient != nil {
err := currentClient.setName(newName)
if err != nil {
return fmt.Errorf("could not set client name to %#v: %v", newName, err)
}
common.LogDebugf("%q -> %q\n", oldName, newName)
if forced {
cr.AddEventMsg(common.EvNameChangeForced, oldName+":"+newName, currentClient.color)
currentClient.IsNameForced = true
} else {
cr.AddEventMsg(common.EvNameChange, oldName+":"+newName, currentClient.color)
}
return nil
}
return fmt.Errorf("Client not found with name %q", oldName)
}