-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
1201 lines (1131 loc) · 43.7 KB
/
client.py
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import socket
import threading
import time
import json
import hashlib
import uuid
import os
import errorcodes # Local Import
class Client(threading.Thread):
'''
Class that implements the client threads in this server
Client Modes:
B = Bot
O = Oper
k = can kill users from the server
w = receives oper messages
'''
def __init__(self, client_sock, server):
'''
Initialize the object, save the socket that this thread will use.
'''
threading.Thread.__init__(self)
self.client = client_sock
self.server = server
self.ip = self.client.getpeername()[0] # Get the clients IP address
self.nick = str(uuid.uuid4())
self.channels = {}
self.account = None
self.flags = self.server.CONFIG["DEFAULT_CLIENT_FLAGS"]
def run(self):
'''
Thread's main loop. Once this function returns, the thread is finished
and dies.
'''
try:
# client needs to change their nick
self.writeline(json.dumps({
"type": "PICKNICK"
}))
self.set_nick(self, self.readline())
# Need to declare QUIT as global, since the method can change it
# Read data from the socket and process it
while True:
cmd = self.readline()
args = cmd.split(" ")
# Command `quit`
if args[0].lower() == "quit":
if len(args) > 1:
self.command_quit(message=' '.join(args[1:]))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: quit <message>"
}))
# Command `nick`:
elif args[0].lower() == "nick":
if len(args) > 1:
self.command_nick(nick=args[1])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: nick <nick>"
}))
# Command `userflag`
elif args[0].lower() == "userflag":
if len(args) > 2:
self.command_set_userflag(switch=args[1], flag=args[2])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "userflag <add/remove> <flag>"
}))
# Command `chanlist`
elif args[0].lower() == "chanlist":
self.command_channel_list()
# Command `register`
elif args[0].lower() == "register":
if len(args) > 2:
self.command_register(password=args[1], email=args[2])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: register <password> <email>"
}))
# Command `login`
elif args[0].lower() == "login":
if len(args) > 1:
self.command_login(password=args[1])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: login <password>"
}))
# Command `logout`
elif args[0].lower() == "logout":
if self.logged_in():
self.logout()
else:
client.writeline(json.dumps({
"type": "SERVERMSG",
"message": "You're not logged in."
}))
# Command `usermsg`
elif args[0].lower() == "usermsg":
if len(args) > 2:
self.command_message_user(
nick=args[1], message=' '.join(args[2:]))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: usermsg <nick> <message>"
}))
# Command `whois`
elif args[0].lower() == "whois":
if len(args) > 1:
self.command_whois(nick=args[1])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: whois <nick>"
}))
# Command `chanjoin`
elif args[0].lower() == "chanjoin":
if len(args) == 2:
self.command_channel_join(
chan_name=args[1], password=None)
elif len(args) == 3:
self.command_channel_join(
chan_name=args[1], password=args[2])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: chanjoin <channel> [password]"
}))
# Command `chanpart`
elif args[0].lower() == "chanpart":
if len(args) > 2:
self.command_channel_part(
chan_name=args[1], message=' '.join(args[2:]))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: chanpart <channel> <message>"
}))
# Command `chanmsg`
elif args[0].lower() == "chanmsg":
if len(args) > 2:
self.command_channel_message(
chan_name=args[1], message=' '.join(args[2:]))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: chanmsg <channel> <message>"
}))
# Command `chankick`
elif args[0].lower() == "chankick":
if len(args) > 3:
self.command_channel_kick(
chan_name=args[1], nick=args[2],
message=' '.join(args[3:]))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: chankick <channel> <nick> <reason>"
}))
# Comamnd `chanflag`
elif args[0] == "chanflag":
if len(args) > 4:
self.command_channel_flag(chan_name=args[1], switch=args[2],
flag=args[3], args=' '.join(args[4:]))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: chanflag <channel> <add/remove> <flag> <args>"
}))
# Command `chanban`
elif args[0] == "chanban":
if len(args) > 2:
self.command_channel_ban(
chan_name=args[1], nick=args[2])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: chanban <channel> <nick>"
}))
# Command `chanunban`
elif args[0].lower() == "chanunban":
if len(args) > 2:
self.command_channel_unban(
chan_name=args[1], ip=args[2])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: chanunban <channel> <IP>"
}))
# Command `chanregister`
elif args[0].lower() == "chanregister":
if len(args) > 1:
self.command_channel_register(chan_name=args[1])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: chanregister <channel>"
}))
# Command `chanbadword`
elif args[0].lower() == "chanbadword":
if len(args) > 3:
self.command_channel_badword(
chan_name=args[1], switch=args[2],
badword=args[3])
else:
self.writeline(json.dumps({
"type": "SERVERMGS",
"message": "help: chanbadword <channel> <add/remove> <word>"
}))
# Command `chanclientflag`
elif args[0].lower() == "chanclientflag":
if len(args) > 4:
self.command_channel_clientflag(
chan_name=args[1], switch=args[2],
nick=args[3], flag=args[4])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: chanflag <channel> <add/remove> <nick> <flag>"
}))
# Command `chanusers`
elif args[0].lower() == "chanusers":
if len(args) > 1:
self.command_channel_members(chan_name=args[1])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: chanusers <channel>"
}))
# Command `oper`
elif args[0].lower() == "oper":
if len(args) > 1:
self.command_oper(password=args[1])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: oper <password>"
}))
# Command `opermsg`
elif args[0].lower() == "opermsg":
if len(args) > 1:
self.command_oper_message(message=' '.join(args[1:]))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: opermsg <message>"
}))
# Command `kill`
elif args[0].lower() == "kill":
if len(args) > 1:
self.command_oper_kill(args[1])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: kill <nick>"
}))
# Command `sanick`
elif args[0].lower() == "sanick":
if len(args) > 2:
self.command_oper_sanick(
nick=args[1], new_nick=args[2])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: sanick <nick> <new nick>"
}))
# Command `sajoin`
elif args[0].lower() == "sajoin":
if len(args) > 2:
self.command_oper_sajoin(
nick=args[1], chan_name=args[2])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: sajoin <nick> <channel>"
}))
# Command `sapart`
elif args[0].lower() == "sapart":
if len(args) > 2:
self.command_oper_sapart(
nick=args[1], chan_name=args[2])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: sapart <nick> <channel>"
}))
# Command `serverban`
elif args[0].lower() == "serverban":
if len(args) > 1:
self.command_oper_server_ban(ip=args[1])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: serverban <IP>"
}))
# Command `globalmsg`
elif args[0].lower() == "globalmsg":
if len(args) > 1:
self.command_oper_global_message(
message=' '.join(args[1:]))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: globalmsg <message>"
}))
# Command `note`
elif args[0].lower() == "usernote":
if len(args) > 2:
self.command_usernote(nick=args[1],
message=' '.join(args[2:]))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: usernote <nick> <message>"
}))
# Command `setpass`
elif args[0].lower() == "setpass":
if len(args) > 1:
self.command_set_pass(password=args[1])
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "help: setpass <new password>"
}))
else:
self.writeline(json.dumps({
"type": "INVALIDCOMMAND"
}))
# Make sure the socket is closed once we're done with it
self.client.close()
return
except Exception, err:
print err
self.quit("client error")
return
# Commands
def command_quit(self, message="bye bye"):
"""
Disconnects the client from the server
"""
self.writeline(json.dumps({
"type": "YOUQUIT",
"message": message
}))
self.quit(message)
def command_nick(self, nick):
"""
Changes the clients nick
"""
self.set_nick(self, nick)
def command_set_userflag(self, switch, flag):
"""
Lets the client set flags on themselves
"""
allowed_flags = list("Bia")
if switch == "add":
if flag in allowed_flags and flag not in self.flags:
self.flags.append(flag)
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "You set +%s on yourself" % flag
}))
elif switch == "remove":
if flag in self.flags:
self.flags.remove(flag)
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "You set -%s on yourself" % flag
}))
def command_usernote(self, nick, message):
"""
Lets a client leave a note for another client
"""
account = self.server.get_account(nick)
if account:
account.get("notes").append({
"from": self.nick,
"message": message
})
# Save changes
with open("accounts/%s.json" % nick, 'w') as f:
f.write(json.dumps(account,
sort_keys=True, indent=4, separators=(',', ': ')))
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "You sent a note to %s" % nick
}))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "%s doesn't have an account." % nick
}))
def command_channel_list(self):
"""
Sends the client the server channel list
Public channels if the client isn't an oper
All channels if the client is an oper
"""
self.server.channel_list(self)
def command_set_pass(self, password):
"""
Change the account password
"""
if self.logged_in():
hashedpw = hashlib.md5(password).hexdigest()
self.server.change_account_passwd(self, hashedpw)
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "You need to be logged in to change your password"
}))
def command_register(self, password, email):
"""
Register an account with the server
password: plain text password
email: email address of the user
"""
hashedpw = hashlib.md5(password).hexdigest()
self.server.register_account(self, email, hashedpw)
def command_login(self, password):
"""
Login into the server
password: plain text password
"""
hashedpw = hashlib.md5(password).hexdigest()
self.server.client_login(self, hashedpw)
def command_message_user(self, nick, message):
"""
Send a message to other user on this server
nick: nick of client you want to send the message to
message: the message you want to send to that client
"""
self.message_nick(nick, message)
def command_whois(self, nick):
"""
Get WHOIS data from other client on the server
nick: nick of client you want to get the data from
"""
self.server.client_whois(self, nick)
def command_channel_join(self, chan_name, password=None):
"""
Joins this client to a channel on the server
chan_name: Channel name
password: Channel password
"""
self.join(chan_name, password)
def command_channel_part(self, chan_name, message="bye"):
"""
Parts this client from a channel on this server
chan_name: Channel name
message: part message
"""
self.part(chan_name, message)
def command_channel_message(self, chan_name, message):
"""
Sends a message to a channel on the server
chan_name: Channel name
message: message you want to send to the channel
"""
self.message_channel(chan_name, message)
def command_channel_members(self, chan_name):
"""
Gives a list of all the clients in a channel
chan_name: Channel name
"""
if chan_name in self.channels:
self.writeline(json.dumps({
"type": "CHANUSERS",
"channel": chan_name,
"members": self.channels[chan_name].users.keys()
}))
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("not in channel"),
"message": "You are not in %s" % chan_name
}))
def command_channel_kick(self, chan_name, nick, message="GTFO"):
"""
Kick a client from a channel
chan_name: Channel name
nick: nick of client you want to kick from channel
message: reason you're kicking this client
"""
if chan_name in self.channels:
self.channels[chan_name].kick_user(self, nick, message)
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("not in channel"),
"message": "You are not in %s" % chan_name
}))
def command_channel_ban(self, chan_name, nick):
"""
Bad a client from a channel
chan_name: Channel name
nick: nick of user you want to bad
"""
if chan_name in self.channels:
self.channels[chan_name].ban_user(self, nick)
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("not in channel"),
"message": "You are not in %s" % chan_name
}))
def command_channel_unban(self, chan_name, ip):
"""
Unban an IP from a channel
chan_name: Channel name
ip: ip you want to unban
"""
if chan_name in self.channels:
self.channels[chan_name].unban_ip(self, ip)
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("not in channel"),
"message": "You are not in %s" % chan_name
}))
def command_channel_register(self, chan_name):
"""
Register a channel on the serevr
chan_name: name of channel you want to register
"""
if chan_name in self.channels:
self.channels[chan_name].register(self)
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("not in channel"),
"message": "You're not in %s" % chan_name
}))
def command_channel_flag(self, chan_name, switch, flag, args=True):
"""
Sets a flag in a channel
chan_name: name of channel you want to set the
flag in
flag: the flag you want to set
args: arguments for that flag
"""
if args == "true":
args = True
elif args == "false":
args = False
elif args.isdigit():
args = int(args)
if switch == "add":
if chan_name in self.channels:
self.channels[chan_name].add_channel_flag(self, flag, args)
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("not in channel"),
"message": "You're not in %s" % chan_name
}))
elif switch == "remove":
if chan_name in self.channels:
self.channels[chan_name].remove_channel_flag(self, flag)
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("not in channel"),
"message": "You're not in %s" % chan_name
}))
def command_channel_badword(self, chan_name, switch, badword):
"""
Adds a word to the channel badword list
chan_name: Channel name
switch: add or remove bad word
badword: word to add to the badword list
"""
if chan_name in self.channels:
if switch.lower() == "add":
self.channels[chan_name].add_badword(self, badword)
elif switch.lower() == "remove":
self.channels[chan_name].remove_badword(badword)
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("not in channel"),
"message": "You're not in %s" % chan_name
}))
def command_channel_clientflag(self, chan_name, switch, nick, flag):
"""
Add a flag to a client on a channel
chan_name: Channel name
switch: add or remove bad word
flag: flag you want to add/remove
"""
if chan_name in self.channels:
if switch.lower() == "add":
self.channels[chan_name].add_client_flag(
self, nick, flag)
elif switch.lower() == "remove":
self.channels[chan_name].remove_client_flag(
self, nick, flag)
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("not in channel"),
"message": "You're not in %s" % chan_name
}))
def command_oper(self, password):
"""
Turns this client into an oper
password: password
"""
self.server.oper(self, hashlib.md5(password).hexdigest())
def command_oper_message(self, message):
"""
Sends a message to all opers connected to the server
"""
if self.is_oper():
self.server.oper_message(self, message)
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("not an oper"),
"message": "You need to be an oper to use the `kill` command"
}))
def command_oper_kill(self, nick):
"""
Disconnects a client from the server
nick: nick of client you want to disconnect
"""
self.kill(nick)
def command_oper_sanick(self, nick, new_nick):
"""
Changes a clients nick to a new nick by force
nick: clients current nick
new_nick: clients new nick
"""
self.sanick(nick, new_nick)
def command_oper_sajoin(self, nick, chan_name):
"""
Forces a client to join a channel
nick: client to force into channel
chan_name: channel to force client into
"""
self.sajoin(nick, chan_name)
def command_oper_sapart(self, nick, chan_name):
"""
Forces a client to leave a channel
nick: client to force to leave channel
chan_name: channel to force client to leave
"""
self.sapart(nick, chan_name)
def command_oper_server_ban(self, ip):
"""
Bans a client from the server
ip: ip to ban
"""
self.ban_ip(ip)
def command_oper_global_message(self, message):
"""
Sends a message to all clients connected to the server
"""
if self.is_oper():
self.server.global_message(message)
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("not an oper"),
"message": "You need to be an oper to use the `announcement` command"
}))
def command_oper_rehash(self):
if self.is_oper():
self.server.rehash()
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("not an oper"),
"message": "You need to be an oper to use the `announcement` command"
}))
# End Commands
def set_nick(self, client, nick):
"""
Sets the clients nick
"""
self.logout()
if len(nick) <= self.server.CONFIG["MAX_NICK_LENGTH"]:
if len(nick) > self.server.CONFIG["MIN_NICK_LENGTH"]:
for c in nick: # make sure each char in in the set
if not c in self.server.CONFIG["NICK_CHAR_SET"]:
client.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("invalid channel/nick"),
"message": "Your nick my only contain these chars: %s" % (
self.server.CONFIG["NICK_CHAR_SET"]
)
}))
self.set_nick(client, client.readline())
return
if not self.server.find_nick(nick) and nick not in self.server.CONFIG["RESERVED_NICKS"]:
old_nick = str(client.nick)
client.nick = str(nick)
del self.server.users[old_nick]
self.server.users[client.nick] = self
self.writeline(json.dumps({
"type": "NICK",
"old_nick": old_nick,
"new_nick": client.nick
}))
self.server.writeline(
"%s is now known as %s" % (old_nick, nick))
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("nick in use"),
"message": "%s is already in use. Please choose a new nick." % nick
}))
self.set_nick(client, client.readline())
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("nick excecced limit"),
"message": "Your nick is too short. Please choose a nick with more than %i chars" %
self.server.CONFIG["MIN_NICK_LENGTH"]
}))
self.set_nick(client, client.readline())
else:
self.writeline(json.dumps({
"type": "ERROR",
"code": errorcodes.get("nick excecced limit"),
"message": "Your nick is too long. Please choose a nick with more than %i chars" %
self.server.CONFIG["MAX_NICK_LENGTH"]
}))
self.set_nick(client, client.readline())
def logged_in(self):
return self.account
def logout(self):
"""
Logs the user out of their account
"""
self.account = None
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "You were logged out"
}))
def readline(self):
"""
Helper function, reads up to MAX_RECV_SIZE chars from the socket,
and returns them as a string, without any end of line markers
"""
result = self.client.recv(self.server.CONFIG["MAX_RECV_SIZE"])
if(result != None):
result = result.strip()
return result
def writeline(self, message):
"""
Helper function, writes the given string to the socket, with an end of
line marker appended at the end
"""
self.client.send(message.strip() + '\n')
def join(self, channel, key=None):
"""
Joins the client to a channel on the server
if CHANNEL_CREATION is False the client
can only join channels that are already made.
if CHANNEL_CREATION is True the client can
join premade channels as well as create new
channels.
"""
if channel in self.server.channels:
if channel not in self.channels:
if self.server.channels[channel].on_join(self, key):
self.channels[channel] = self.server.channels[channel]
self.writeline(json.dumps({
"type": "YOUJOIN",
"channel": channel
}))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "You're already in %s" % channel
}))
else:
if self.server.CONFIG.get("CHANNEL_CREATION"):
if self.server.create_channel(self, channel):
self.channels[channel] = self.server.channels[channel]
self.server.channels[channel].on_join(self, key)
self.writeline(json.dumps({
"type": "YOUJOIN",
"channel": channel
}))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "This server does not allow the creation of channels"
}))
def part(self, channel, message="bye"):
"""
Make the client leave the channel
"""
if channel in self.channels:
self.channels[channel].on_part(self, message)
del self.channels[channel]
self.writeline(json.dumps({
"type": "YOUPART",
"channel": channel,
"message": message
}))
else:
self.writeline(json.dumps({
"type": "SERVERMSG",
"message": "You are not in %s" % channel
}))
def add_flag(self, flag):
"""
Gives this client a flag
"""
if not flag in self.flags:
self.flags.append(flag)
def add_flags(self, flags):
"""
Gives this client a flag
"""
for flag in flags:
self.add_flag(flag)
def remove_flag(self, flag):
"""
Removes a flag from this client
"""
if flag in self.flags:
self.flags.remove(flag)
def remove_flags(self, flags):
"""
Removes flags from this client
"""
for flag in flags:
self.remove_flag(flag)
def quit(self, message):
"""
Disconnects the client from the server
"""
for channel in self.channels:
self.channels[channel].on_quit(self, message)
if self in self.server.clients:
self.server.clients.remove(self)
del self.server.users[self.nick]
self.client.close()
quit()
##### Handlers #####
def on_kill(self, message):
"""
Runs when the user gets killed from the network
"""
self.writeline(json.dumps({
"type": "YOUKILLED",
"message": message
}))
self.quit()
def on_kick(self, channel, reason):
"""
Runs when you've been kicked from a channel
"""
del self.channels[channel.name]
self.writeline(json.dumps({
"type": "YOUKICKED",
"channel": channel.name,
"message": reason
}))
def on_ban(self, channel):
"""
Runs when you've been banned from a channel
"""
self.writeline(json.dumps({
"type": "YOUBANED",
"channel": channel.name
}))
def on_sanick(self, new_nick):
"""
Runs when a oper uses sanick on this client
"""
self.writeline(
"Your nick was changed by a server admin to %s" % new_nick)
self.writeline(json.dumps({
"type": "YOUSANICKED",
"new_nick": new_nick
}))
def on_whois(self, client):
"""
Runs when a user uses the whois command on this client
"""
client.writeline(json.dumps({
"type": "WHOIS",
"nick": self.nick,
"message": "Nick: %s" % (self.nick)
}))
client.writeline(json.dumps({
"type": "WHOIS",
"nick": self.nick,
"message": "IP: %s" % (self.ip)
}))
client.writeline(json.dumps({
"type": "WHOIS",
"nick": self.nick,
"message": "Oper: %s" % (self.is_oper())
}))
client.writeline(json.dumps({
"type": "WHOIS",
"nick": self.nick,
"message": "Logged In: %s" % (bool(self.logged_in()))
}))
if not 'i' in self.flags or client.is_oper():
client.writeline(json.dumps({
"type": "WHOIS",
"nick": self.nick,
"message": "Channels: %s" % ', '.join(self.channels)
}))
if self.logged_in():
client.writeline(json.dumps({
"type": "WHOIS",
"nick": self.nick,
"message": "Account UUID: %s" % (self.account["uuid"])
}))
if client.is_oper():
client.writeline(json.dumps({
"type": "WHOIS",
"nick": self.nick,
"message": "Flags: %s" % ', '.join(self.flags)
}))
def on_sajoin(self, channel):
"""
Runs when a server admin forces you into a channel
"""
self.channels[channel] = self.server.channels[channel]
self.writeline(json.dumps({
"type": "YOUSAJOINED",
"channel": channel
}))