diff --git a/bans.txt b/bans.txt new file mode 100644 index 0000000..e69de29 diff --git a/client.py b/client.py index 5d12b12..15f6743 100644 --- a/client.py +++ b/client.py @@ -1,18 +1,36 @@ import socket import threading +nickname = input("Choose Your Nickname:") +if nickname == 'admin': + password = input("Enter Password for Admin:") + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Connect to a host client.connect(('127.0.0.1',5555)) -nickname = input("Choose Your Nickname:") +stop_thread = False def recieve(): while True: + global stop_thread + if stop_thread: + break try: message = client.recv(1024).decode('ascii') if message == 'NICK': client.send(nickname.encode('ascii')) + next_message = client.recv(1024).decode('ascii') + if next_message == 'PASS': + client.send(password.encode('ascii')) + if client.recv(1024).decode('ascii') == 'REFUSE': + print("Connection is Refused !! Wrong Password") + stop_thread = True + # Clients those are banned can't reconnect + elif next_message == 'BAN': + print('Connection Refused due to Ban') + client.close() + stop_thread = True else: print(message) except: @@ -22,9 +40,22 @@ def recieve(): def write(): while True: + if stop_thread: + break #Getting Messages message = f'{nickname}: {input("")}' - client.send(message.encode('ascii')) + if message[len(nickname)+2:].startswith('/'): + if nickname == 'admin': + if message[len(nickname)+2:].startswith('/kick'): + # 2 for : and whitespace and 6 for /KICK_ + client.send(f'KICK {message[len(nickname)+2+6:]}'.encode('ascii')) + elif message[len(nickname)+2:].startswith('/ban'): + # 2 for : and whitespace and 5 for /BAN + client.send(f'BAN {message[len(nickname)+2+5:]}'.encode('ascii')) + else: + print("Commands can be executed by Admins only !!") + else: + client.send(message.encode('ascii')) recieve_thread = threading.Thread(target=recieve) recieve_thread.start() diff --git a/server.py b/server.py index 97edd74..db5af74 100644 --- a/server.py +++ b/server.py @@ -23,18 +23,35 @@ def broadcast(message): def handle(client): while True: try: - message = client.recv(1024) - # As soon as message recieved, broadcast it. - broadcast(message) + msg = message = client.recv(1024) + if msg.decode('ascii').startswith('KICK'): + if nicknames[clients.index(client)] == 'admin': + name_to_kick = msg.decode('ascii')[5:] + kick_user(name_to_kick) + else: + client.send('Command Refused!'.encode('ascii')) + elif msg.decode('ascii').startswith('BAN'): + if nicknames[clients.index(client)] == 'admin': + name_to_ban = msg.decode('ascii')[4:] + kick_user(name_to_ban) + with open('bans.txt','a') as f: + f.write(f'{name_to_ban}\n') + print(f'{name_to_ban} was banned by the Admin!') + else: + client.send('Command Refused!'.encode('ascii')) + else: + broadcast(message) # As soon as message recieved, broadcast it. + except: - index = clients.index(client) - #Index is used to remove client from list after getting diconnected - client.remove(client) - client.close - nickname = nicknames[index] - broadcast(f'{nickname} left the Chat!'.encode('ascii')) - nicknames.remove(nickname) - break + if client in clients: + index = clients.index(client) + #Index is used to remove client from list after getting diconnected + client.remove(client) + client.close + nickname = nicknames[index] + broadcast(f'{nickname} left the Chat!'.encode('ascii')) + nicknames.remove(nickname) + break # Main Recieve method def recieve(): while True: @@ -43,6 +60,24 @@ def recieve(): # Ask the clients for Nicknames client.send('NICK'.encode('ascii')) nickname = client.recv(1024).decode('ascii') + # If the Client is an Admin promopt for the password. + with open('bans.txt', 'r') as f: + bans = f.readlines() + + if nickname+'\n' in bans: + client.send('BAN'.encode('ascii')) + client.close() + continue + + if nickname == 'admin': + client.send('PASS'.encode('ascii')) + password = client.recv(1024).decode('ascii') + # I know it is lame, but my focus is mainly for Chat system and not a Login System + if password != 'adminpass': + client.send('REFUSE'.encode('ascii')) + client.close() + continue + nicknames.append(nickname) clients.append(client) @@ -54,6 +89,17 @@ def recieve(): thread = threading.Thread(target=handle, args=(client,)) thread.start() +def kick_user(name): + if name in nicknames: + name_index = nicknames.index(name) + client_to_kick = clients[name_index] + clients.remove(client_to_kick) + client_to_kick.send('You Were Kicked from Chat !'.encode('ascii')) + client_to_kick.close() + nicknames.remove(name) + broadcast(f'{name} was kicked from the server!'.encode('ascii')) + + #Calling the main method print('Server is Listening ...') recieve() \ No newline at end of file