@@ -23,18 +23,35 @@ def broadcast(message):
2323def handle (client ):
2424 while True :
2525 try :
26- message = client .recv (1024 )
27- # As soon as message recieved, broadcast it.
28- broadcast (message )
26+ msg = message = client .recv (1024 )
27+ if msg .decode ('ascii' ).startswith ('KICK' ):
28+ if nicknames [clients .index (client )] == 'admin' :
29+ name_to_kick = msg .decode ('ascii' )[5 :]
30+ kick_user (name_to_kick )
31+ else :
32+ client .send ('Command Refused!' .encode ('ascii' ))
33+ elif msg .decode ('ascii' ).startswith ('BAN' ):
34+ if nicknames [clients .index (client )] == 'admin' :
35+ name_to_ban = msg .decode ('ascii' )[4 :]
36+ kick_user (name_to_ban )
37+ with open ('bans.txt' ,'a' ) as f :
38+ f .write (f'{ name_to_ban } \n ' )
39+ print (f'{ name_to_ban } was banned by the Admin!' )
40+ else :
41+ client .send ('Command Refused!' .encode ('ascii' ))
42+ else :
43+ broadcast (message ) # As soon as message recieved, broadcast it.
44+
2945 except :
30- index = clients .index (client )
31- #Index is used to remove client from list after getting diconnected
32- client .remove (client )
33- client .close
34- nickname = nicknames [index ]
35- broadcast (f'{ nickname } left the Chat!' .encode ('ascii' ))
36- nicknames .remove (nickname )
37- break
46+ if client in clients :
47+ index = clients .index (client )
48+ #Index is used to remove client from list after getting diconnected
49+ client .remove (client )
50+ client .close
51+ nickname = nicknames [index ]
52+ broadcast (f'{ nickname } left the Chat!' .encode ('ascii' ))
53+ nicknames .remove (nickname )
54+ break
3855# Main Recieve method
3956def recieve ():
4057 while True :
@@ -43,6 +60,24 @@ def recieve():
4360 # Ask the clients for Nicknames
4461 client .send ('NICK' .encode ('ascii' ))
4562 nickname = client .recv (1024 ).decode ('ascii' )
63+ # If the Client is an Admin promopt for the password.
64+ with open ('bans.txt' , 'r' ) as f :
65+ bans = f .readlines ()
66+
67+ if nickname + '\n ' in bans :
68+ client .send ('BAN' .encode ('ascii' ))
69+ client .close ()
70+ continue
71+
72+ if nickname == 'admin' :
73+ client .send ('PASS' .encode ('ascii' ))
74+ password = client .recv (1024 ).decode ('ascii' )
75+ # I know it is lame, but my focus is mainly for Chat system and not a Login System
76+ if password != 'adminpass' :
77+ client .send ('REFUSE' .encode ('ascii' ))
78+ client .close ()
79+ continue
80+
4681 nicknames .append (nickname )
4782 clients .append (client )
4883
@@ -54,6 +89,17 @@ def recieve():
5489 thread = threading .Thread (target = handle , args = (client ,))
5590 thread .start ()
5691
92+ def kick_user (name ):
93+ if name in nicknames :
94+ name_index = nicknames .index (name )
95+ client_to_kick = clients [name_index ]
96+ clients .remove (client_to_kick )
97+ client_to_kick .send ('You Were Kicked from Chat !' .encode ('ascii' ))
98+ client_to_kick .close ()
99+ nicknames .remove (name )
100+ broadcast (f'{ name } was kicked from the server!' .encode ('ascii' ))
101+
102+
57103#Calling the main method
58104print ('Server is Listening ...' )
59105recieve ()
0 commit comments