-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
608 lines (476 loc) · 24.9 KB
/
client.py
File metadata and controls
608 lines (476 loc) · 24.9 KB
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
import socket
import json
import sys
import tkinter
import tkinter.scrolledtext
from tkinter import simpledialog
from threading import Thread
class client:
def __init__(self):
# Main stuff
self.name = 0
self.bufferSize = 1024
self.guiDone = False
self.running = True
self.sockClient = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
self.sockClient.settimeout(3)
self.channels=[]
self.connected = False
#GUI Threads
guiThread = Thread(target=self.guiLoop)
guiThread.start()
self.emojis = {
':grin:' : "\U0001f600",
':laugh:' : "\U0001F923",
':sad:' : "\U0001F97A",
':wink:' : "\U0001F609",
':sick:' : "\U0001F922",
':sleepy:' : "\U0001F62A",
':kiss:' : "\U0001F618",
':cry:' : "\U0001F62D",
':heart:' : "\U0001F497",
':poop:' : "\U0001F4A9",
':angry:' : "\U0001F621"
}
def getEmojis(self, message):
scan = message.split()
i = 0
for word in scan:
if word in self.emojis:
scan[i] = self.emojis[word]
i = i + 1
return ' '.join(scan)
def doCommand(self, sentCommand):
command = sentCommand.split()
match command[0]:
case "0":
return True
case "/join":
if self.connected:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: You are already connected\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Make sure to put valid IP address and port number")
return True
else:
self.sockClient = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
self.sockClient.settimeout(3)
if len(command) < 3 or len(command) > 3:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Make sure to put valid IP address and port number\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Make sure to put valid IP address and port number")
return True
if not command[2].isdigit():
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Make sure to put valid port number\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Make sure to put valid port number")
return True
ipPort = (command[1], int(command[2]))
msg = json.dumps({"command":"join"})
print(msg)
try:
self.sockClient.connect(ipPort)
self.sockClient.sendall(bytes(msg, encoding="utf-8"))
received = self.sockClient.recvfrom(self.bufferSize)
message = json.loads(received[0])
self.chatArea.config(state='normal')
self.chatArea.insert('end', message["message"] + "\n", 'green')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print(message["message"])
#starts the reading input
self.receiveStart = Thread(target = self.receive)
self.receiveFlag = True
self.receiveStart.start()
self.connected = True
except socket.error:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Connection to the Message Board Server has failed! Please check IP Address and Port Number.\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Connection to the Message Board Server has failed! Please check IP Address and Port Number.")
return True
case "/leave":
if len(command) > 1:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Wrong command use. Use '/leave' only\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Wrong command use. Use '/leave' only")
return True
msg = json.dumps({"command":"leave"})
try:
self.sockClient.sendall(bytes(msg, encoding="utf-8"))
received = self.sockClient.recvfrom(self.bufferSize)
message = json.loads(received[0])
self.chatArea.config(state='normal')
self.chatArea.insert('end', message["message"] + "\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print(message["message"])
#Stops the reading input
self.receiveFlag = False
self.receiveStart.join()
self.sockClient.close()
self.connected = False
self.name = 0
self.chatLabel.config(text="Handle: ", bg="lightgray", foreground='black')
except socket.error:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Disconnection failed. Please connect to the server first.\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Disconnection failed. Please connect to the server first.")
return True
case "/register":
if len(command) > 2 or len(command) < 2:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Invalid handle. Should only be 1 word\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Invalid handle. Should only be 1 word")
return True
if command[1] == '0':
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Invalid handle. Don't use 0\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Invalid handle. Don't use 0")
return True
msg = json.dumps({"command":"register", "handle": command[1]})
try:
self.sockClient.sendall(bytes(msg, encoding="utf-8"))
except socket.error:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Registration failed. Please connect to the server first.\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Registration failed. Please connect to the server first.")
return True
case "/all":
if len(command) < 2:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Message to all not sent. Please input a message when sending\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Message to all not sent. Please input a message when sending")
return True
command.remove("/all")
message = ' '.join(command)
msg = json.dumps({"command":"all", "message": message})
try:
self.sockClient.sendall(bytes(msg, encoding="utf-8"))
except socket.error:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Message to all is not sent. Make sure that you are connected to the server\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Message to all is not sent. Make sure that you are connected to the server")
return True
case "/msg":
if len(command) < 3:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Message to all not sent. Please input handle and message when sending\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Message to all not sent. Please input handle and message when sending")
return True
command.pop(0)
user = command.pop(0)
message = ' '.join(command)
msg = json.dumps({"command":"msg", "handle": user, "message": message})
try:
self.sockClient.sendall(bytes(msg, encoding="utf-8"))
except socket.error:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Direct message is not sent. Please make sure you are connected\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Direct message is not sent. Please make sure you are connected")
return True
case "/close":
msg = json.dumps({"command":"close"})
try:
self.sockClient.sendall(bytes(msg, encoding="utf-8"))
received = self.sockClient.recvfrom(self.bufferSize)
message = json.loads(received[0])
print(message["message"])
#Stops the reading input
self.receiveFlag = False
self.connected = False
self.receiveStart.join()
self.sockClient.close()
except socket.error:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Server closure failed. Please connect to the server first.\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Server closure failed. Please connect to the server first.")
return False
case "/users":
msg = json.dumps({"command":"users"})
try:
self.sockClient.sendall(bytes(msg, encoding="utf-8"))
except socket.error:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Command error. Connect to server first\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Command error. Connect to server first")
return True
case "/?":
# GUI Show
self.chatArea.config(state='normal')
self.chatArea.insert('end', 'List of commands:\n', 'gray')
self.chatArea.insert('end', '/join <IP Address> <Port> -> Join a server\n', 'gray')
self.chatArea.insert('end', "/leave -> Leave a server\n", 'gray')
self.chatArea.insert('end', "/register <Handle or nickname> -> Register yourself a handle in a server\n", 'gray')
self.chatArea.insert('end', "/all <Message> -> Send a message to all users registered in the server\n", 'gray')
self.chatArea.insert('end', "/msg <Handle or nickname> <Message> -> Send a direct message to someone registered in the server\n", 'gray')
self.chatArea.insert('end', "/close -> Close the server\n", 'gray')
self.chatArea.insert('end', "/users -> Show a list of registered users in the server\n", 'gray')
self.chatArea.insert('end', "/emojis -> Show a list of emojis\n", 'gray')
#Commands about channel
self.chatArea.insert('end', "/new_ch <channel name> -> Create new channel in server\n", 'gray')
self.chatArea.insert('end', "/join_ch <channel name> -> Join a channel\n", 'gray')
self.chatArea.insert('end', "/leave_ch <channel name> -> leave a channel\n", 'gray')
self.chatArea.insert('end', "/msg_ch <channel name> <message> -> Send a message to a channel\n", 'gray')
self.chatArea.insert('end', "/user_ch -> Show a list of joined channels of the user\n", 'gray')
self.chatArea.insert('end', "/server_ch -> Show a list of channels in the server\n", 'gray')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
return True
case '/emojis':
self.chatArea.config(state='normal')
self.chatArea.insert('end', 'List of Emojis: ' + '\n', 'black')
for emoji in self.emojis:
self.chatArea.insert('end', emoji + " - " + self.emojis[emoji] + '\n', 'black')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
return True
#Shows all channels of the server
case '/server_ch':
msg = json.dumps({"command":"server_ch"})
try:
self.sockClient.sendall(bytes(msg, encoding="utf-8"))
except socket.error:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Command error. Connect to server first\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Command error. Connect to server first")
return True
#Shows all joined channels of the user
case '/user_ch':
if len(self.channels) > 0:
self.chatArea.config(state='normal')
self.chatArea.insert('end', 'List of joined channels: ' + '\n', 'black')
for channel in self.channels:
self.chatArea.insert('end', channel + '\n', 'black')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
else:
self.chatArea.config(state='normal')
self.chatArea.insert('end', 'You are not part of any channel' + '\n', 'black')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
return True
#Create new channel
case '/new_ch':
if len(command) > 2 or len(command) < 2:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Invalid channel name. Should only be 1 word\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Invalid channel name. Should only be 1 word")
return True
if command[1] == '0':
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Invalid channel name. Don't use 0\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Invalid channel name. Don't use 0")
return True
msg = json.dumps({"command":"new_ch", "channel": command[1]})
try:
self.sockClient.sendall(bytes(msg, encoding="utf-8"))
except socket.error:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Channel creation failed. Please connect to the server first.\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Channel creation failed. Please connect to the server first.")
return True
#Join a channel
case '/join_ch':
if len(command) > 2 or len(command) < 2:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Invalid channel name. Should only be 1 word\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Invalid channel name. Should only be 1 word")
return True
msg = json.dumps({"command":"join_ch", "channel": command[1]})
try:
self.sockClient.sendall(bytes(msg, encoding="utf-8"))
except socket.error:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Attempt to join channel failed. Please connect to the server first.\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Attempt to join channel failed. Please connect to the server first.")
return True
#Leave a channel
case '/leave_ch':
if len(command) > 2 or len(command) < 2:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Invalid channel name. Should only be 1 word\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Invalid channel name. Should only be 1 word")
return True
msg = json.dumps({"command":"leave_ch", "channel": command[1]})
try:
self.sockClient.sendall(bytes(msg, encoding="utf-8"))
except socket.error:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Attempt to leave channel failed. Please connect to the server first.\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Attempt to leave channel failed. Please connect to the server first.")
return True
#Message in a channel
case '/msg_ch':
if len(command) < 3:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Message to channel not sent. Please input the channel then message when sending\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Message to all not sent. Please input the channel then message when sending")
return True
command.remove("/msg_ch")
channelSend = command[0]
command.remove(command[0])
message = ' '.join(command)
msg = json.dumps({"command":"msg_ch", "message": message, 'channel': channelSend})
try:
self.sockClient.sendall(bytes(msg, encoding="utf-8"))
except socket.error:
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Message to channel is not sent. Make sure that you are connected to the server\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Message to channel is not sent. Make sure that you are connected to the server")
return True
case other:
# GUI Show Error
self.chatArea.config(state='normal')
self.chatArea.insert('end', "Error: Command doesn't exist\n", 'red')
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
print("Error: Command doesn't exist\n")
return True
def receive(self):
while True:
if not self.receiveFlag:
break
try:
received = self.sockClient.recvfrom(self.bufferSize)
message = json.loads(received[0])
#If GUI is done updating
if self.guiDone:
checkedMsg = self.getEmojis(message["message"])
# Add message to the GUI
self.chatArea.config(state='normal')
self.chatArea.insert('end', checkedMsg + '\n', self.colorText(message["message"]))
self.chatArea.yview('end')
self.chatArea.config(state='disabled')
#Print in terminal
print(checkedMsg)
except socket.error:
continue
def input(self, event):
command = self.msgInput.get('1.0', 'end')
#command = input("Enter command: ")
print(command)
self.stopper = self.doCommand(command)
self.msgInput.delete('1.0', 'end')
def inputButton(self):
command = self.msgInput.get('1.0', 'end')
#command = input("Enter command: ")
print(command)
self.stopper = self.doCommand(command)
self.msgInput.delete('1.0', 'end')
def colorText(self, message):
first = message.split()
match first[0]:
case 'Error:':
return 'red'
case '[From':
return 'blue'
case '[To':
return 'gray'
case 'Welcome':
self.name = first[1][:-1]
self.chatLabel.config(text="Handle: " + self.name, bg="lightgray", foreground='blue')
return 'green'
case 'Connection':
return 'green'
case 'Channel:Created':
self.channels.append(first[6][:-1])
return 'purple'
case 'Channel:Welcome':
self.channels.append(first[3][:-1])
return 'purple'
case 'Channel:Goodbye!':
self.channels.remove(first[3])
return 'purple'
case 'Channel':
return 'purple'
case 'List':
return 'gray'
case other:
return 'black'
#GUI Stuff
def guiLoop(self):
self.win = tkinter.Tk()
self.win.configure(bg="lightgray")
self.win.title("Chat Window")
self.chatLabel = tkinter.Label(self.win, text="Handle: ", bg="lightgray")
self.chatLabel.config(font=("Arial", 12))
self.chatLabel.pack(padx=20, pady=5)
self.chatArea = tkinter.scrolledtext.ScrolledText(self.win)
self.chatArea.pack(padx=20, pady=5)
self.chatArea.config(font=("Arial", 12), state="disabled")
#Chat Area color text configs
self.chatArea.tag_config("red", foreground="red")
self.chatArea.tag_config("gray", foreground="gray")
self.chatArea.tag_config("blue", foreground="blue")
self.chatArea.tag_config("black", foreground="black")
self.chatArea.tag_config("green", foreground="green")
self.chatArea.tag_config("lightgray", foreground="lightgray")
self.chatArea.tag_config("purple", foreground="purple")
self.msgLabel = tkinter.Label(self.win, text="Message:", bg="lightgray")
self.msgLabel.config(font=("Arial", 12))
self.msgLabel.pack(padx=20, pady=5)
self.msgInput = tkinter.Text(self.win, height=3)
self.msgInput.pack(padx=20, pady=5)
self.win.bind("<Return>", self.input)
self.button = tkinter.Button(self.win, text="send", command=self.inputButton)
self.button.config(font=("Arial", 12))
self.button.pack(padx=20, pady=5)
self.guiDone = True
self.win.protocol("WM_DELETE_WINDOW", self.stop)
self.win.mainloop()
def stop(self):
self.running = False
self.win.destroy()
self.sockClient.close()
exit(0)
#START
start = client()