Skip to content

Commit 06ed315

Browse files
authored
Merge pull request #1 from IamLucif3r/v2.0
v2.0 with enhanced Features
2 parents 0d00cb4 + 861da22 commit 06ed315

File tree

3 files changed

+90
-13
lines changed

3 files changed

+90
-13
lines changed

bans.txt

Whitespace-only changes.

client.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
import socket
22
import threading
33

4+
nickname = input("Choose Your Nickname:")
5+
if nickname == 'admin':
6+
password = input("Enter Password for Admin:")
7+
48
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
59
#Connect to a host
610
client.connect(('127.0.0.1',5555))
711

8-
nickname = input("Choose Your Nickname:")
12+
stop_thread = False
913

1014
def recieve():
1115
while True:
16+
global stop_thread
17+
if stop_thread:
18+
break
1219
try:
1320
message = client.recv(1024).decode('ascii')
1421
if message == 'NICK':
1522
client.send(nickname.encode('ascii'))
23+
next_message = client.recv(1024).decode('ascii')
24+
if next_message == 'PASS':
25+
client.send(password.encode('ascii'))
26+
if client.recv(1024).decode('ascii') == 'REFUSE':
27+
print("Connection is Refused !! Wrong Password")
28+
stop_thread = True
29+
# Clients those are banned can't reconnect
30+
elif next_message == 'BAN':
31+
print('Connection Refused due to Ban')
32+
client.close()
33+
stop_thread = True
1634
else:
1735
print(message)
1836
except:
@@ -22,9 +40,22 @@ def recieve():
2240

2341
def write():
2442
while True:
43+
if stop_thread:
44+
break
2545
#Getting Messages
2646
message = f'{nickname}: {input("")}'
27-
client.send(message.encode('ascii'))
47+
if message[len(nickname)+2:].startswith('/'):
48+
if nickname == 'admin':
49+
if message[len(nickname)+2:].startswith('/kick'):
50+
# 2 for : and whitespace and 6 for /KICK_
51+
client.send(f'KICK {message[len(nickname)+2+6:]}'.encode('ascii'))
52+
elif message[len(nickname)+2:].startswith('/ban'):
53+
# 2 for : and whitespace and 5 for /BAN
54+
client.send(f'BAN {message[len(nickname)+2+5:]}'.encode('ascii'))
55+
else:
56+
print("Commands can be executed by Admins only !!")
57+
else:
58+
client.send(message.encode('ascii'))
2859

2960
recieve_thread = threading.Thread(target=recieve)
3061
recieve_thread.start()

server.py

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,35 @@ def broadcast(message):
2323
def 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
3956
def 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
58104
print('Server is Listening ...')
59105
recieve()

0 commit comments

Comments
 (0)