Skip to content

Commit

Permalink
Merge pull request #1 from IamLucif3r/v2.0
Browse files Browse the repository at this point in the history
v2.0 with enhanced Features
  • Loading branch information
IamLucif3r authored May 22, 2021
2 parents 0d00cb4 + 861da22 commit 06ed315
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 13 deletions.
Empty file added bans.txt
Empty file.
35 changes: 33 additions & 2 deletions client.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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()
Expand Down
68 changes: 57 additions & 11 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand All @@ -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()

0 comments on commit 06ed315

Please sign in to comment.