-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
271 lines (228 loc) · 12.4 KB
/
Copy pathserver.py
File metadata and controls
271 lines (228 loc) · 12.4 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
# server.py
import socket, sys
import os
from time import gmtime, strftime
import serverUtilities.authentication as authentication
import serverUtilities.signup as signup
import serverUtilities.broadcast as broadcast
import serverUtilities.message as personal
import serverUtilities.deleteAccount as deleteAccount
import serverUtilities.asynchronous as asynchronous
import serverUtilities.updatePassword as updatePassword
import serverUtilities.createGroup as createGroup
import serverUtilities.addMember as addMember
import serverUtilities.messageGroup as messageGroup
import serverUtilities.deleteGroup as deleteGroup
import serverUtilities.groupList as groupList
import serverUtilities.groupMembers as groupMembers
import serverUtilities.leaveGroup as leaveGroup
from thread import *
# create a socket object
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# checking number of arguments given
if len(sys.argv) != 3:
print("Incorrect number of arguments")
exit()
# get local machine name
host = sys.argv[1]
port = sys.argv[2]
#bind to the port
serverSocket.bind((host, int(port)))
# queue up to 100 requests i.e. it can handle maximum of 100 online clients as of now
serverSocket.listen(100)
# this dictionary contain username to their respective client socket for all active users
onlineUsers = {}
path = os.path.abspath("./database/authenticationDetails.txt")
# taking authentication credentials from the database
with open(path) as f:
credentials = [x.strip().split(' ') for x in f.readlines()]
f.close()
usernames = []
lastTimeActive = {}
for username, password in credentials:
usernames.append(username)
def clientThread(clientSocket, addr):
Authenticated = False
while not Authenticated:
msg = clientSocket.recv(1024).decode('ascii')
clientSocket.send("pass".encode("ascii"))
if msg == "Exit" :
clientSocket.close()
return
elif msg == "Online_Users" :
msg = clientSocket.recv(1024).decode('ascii')
message = str(onlineUsers.keys())
clientSocket.send(message.encode('ascii'))
elif msg == "Login":
username, Authenticated = authentication.authenticate(onlineUsers, clientSocket)
if not Authenticated:
continue
onlineUsers[username] = clientSocket
print("%s | Got a connection from %s [%s]" % ( strftime("%d-%m-%Y %H:%M:%S", gmtime()), str(username), str(addr)))
blockedUsers = asynchronous.deliverMessage(username, clientSocket)
elif msg == "Signup":
username, Authenticated = signup.Signup(clientSocket)
if not Authenticated:
continue
onlineUsers[username] = clientSocket
usernames.append(username)
print("%s | Got a connection from %s [%s]" % ( strftime("%d-%m-%Y %H:%M:%S", gmtime()), str(username), str(addr)))
blockedUsers = []
while Authenticated:
lastTimeActive[username] = strftime("%d-%m-%Y %H:%M:%S", gmtime())
msg = clientSocket.recv(1024).decode('ascii')
if msg == "Block" :
clientSocket.send("Pass".encode('ascii'))
userToBlock = clientSocket.recv(1024).decode('ascii')
if not userToBlock in usernames:
message = "No such user exists.\n"
elif userToBlock in blockedUsers:
message = "user "+ userToBlock +" already blocked.\n"
else:
message = "user " + userToBlock + " blocked.\n"
blockedUsers.append(userToBlock)
f = open("./database/block/" + username + ".txt", "a")
f.write(userToBlock + "\n")
f.close()
clientSocket.send(message.encode('ascii'))
elif msg == "Exit" :
onlineUsers.pop(username)
print("%s | Disconnected from %s [%s]" % ( strftime("%d-%m-%Y %H:%M:%S", gmtime()), str(username), str(addr)))
clientSocket.close()
lastTimeActive[username] = strftime("%d-%m-%Y %H:%M:%S", gmtime())
return
elif msg == "Unblock" :
clientSocket.send("Pass".encode('ascii'))
userToBlock = clientSocket.recv(1024).decode('ascii')
if not userToBlock in usernames:
message = "No such user exists.\n"
elif not userToBlock in blockedUsers:
message = "user "+ userToBlock +" is not blocked by you.\n"
else:
message = "user " + userToBlock + " unblocked.\n"
blockedUsers.remove(userToBlock)
f = open("./database/block/" + username + ".txt", "w")
f.close()
f = open("./database/block/" + username + ".txt", "a")
for userToBlock in blockedUsers:
f.write(userToBlock + "\n")
f.close()
clientSocket.send(message.encode('ascii'))
elif msg == "Check_User" :
clientSocket.send("Pass".encode('ascii'))
userToCheck = clientSocket.recv(1024).decode('ascii')
if not userToCheck in usernames:
message = "No such user exists.\n"
elif userToCheck in onlineUsers:
message = "user \'"+ userToCheck +"\' is active now.\n"
elif userToCheck in lastTimeActive.keys():
message = "user \'"+ userToCheck +"\' was last time active at " + lastTimeActive[userToCheck] + ".\n"
else:
message = "user \'"+ userToCheck +"\' was never active in this session.\n"
clientSocket.send(message.encode('ascii'))
elif msg == "Online_Users" :
clientSocket.send("Pass".encode('ascii'))
msg = clientSocket.recv(1024).decode('ascii')
message = str(onlineUsers.keys())
clientSocket.send(message.encode('ascii'))
elif msg == "Group_List" :
clientSocket.send("Pass".encode('ascii'))
msg = clientSocket.recv(1024).decode('ascii')
message = str(groupList.getList(username))
clientSocket.send(message.encode('ascii'))
elif msg == "Broadcast":
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Give message to be Broadcasted"
clientSocket.send(message.encode('ascii'))
msg = clientSocket.recv(1024).decode('ascii')
broadcast.BroadcastMessage(onlineUsers, username, msg)
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) +": Broadcasted your message\n"
clientSocket.send(message.encode('ascii'))
elif msg == "Create_Group":
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Give name of the group you want to create."
clientSocket.send(message.encode('ascii'))
groupName = clientSocket.recv(1024).decode('ascii')
message = createGroup.create(username, groupName)
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) +": " + message
clientSocket.send(message.encode('ascii'))
elif msg == "Add_Member":
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Give name of the group to which you want to add member."
clientSocket.send(message.encode('ascii'))
groupName = clientSocket.recv(1024).decode('ascii')
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Give user which you want to add to the group."
clientSocket.send(message.encode('ascii'))
username2 = clientSocket.recv(1024).decode('ascii')
message = addMember.add(usernames, username, groupName, username2)
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) +": " + message
clientSocket.send(message.encode('ascii'))
elif msg == "Message_Group":
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Give name of the group you want to messaga."
clientSocket.send(message.encode('ascii'))
groupName = clientSocket.recv(1024).decode('ascii')
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Give your message."
clientSocket.send(message.encode('ascii'))
message = clientSocket.recv(1024).decode('ascii')
message = messageGroup.message(onlineUsers, usernames, username, groupName, message)
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) +": " + message
clientSocket.send(message.encode('ascii'))
elif msg == "Delete_Group":
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Give name of the group you want to delete."
clientSocket.send(message.encode('ascii'))
groupName = clientSocket.recv(1024).decode('ascii')
message = deleteGroup.deleteGroup(username, groupName)
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) +": " + message
clientSocket.send(message.encode('ascii'))
elif msg == "Group_Members":
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Give name of the group whose members you wanna see."
clientSocket.send(message.encode('ascii'))
groupName = clientSocket.recv(1024).decode('ascii')
message = groupMembers.getMembers(username, groupName)
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) +": " + message
clientSocket.send(message.encode('ascii'))
elif msg == "Leave_Group":
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Give name of the group you wanna leave."
clientSocket.send(message.encode('ascii'))
groupName = clientSocket.recv(1024).decode('ascii')
message = leaveGroup.leave(username, groupName)
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) +": " + message
clientSocket.send(message.encode('ascii'))
elif msg == "Message":
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Give message"
clientSocket.send(message.encode('ascii'))
msg = clientSocket.recv(1024).decode('ascii')
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Give user id of receiver"
clientSocket.send(message.encode('ascii'))
receiver = clientSocket.recv(1024).decode('ascii')
if receiver in usernames:
message = personal.PersonalMessage(onlineUsers, username, receiver, msg)
clientSocket.send(message.encode('ascii'))
else:
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) +": User \'" + receiver + "\' doesn't exist.\n"
clientSocket.send(message.encode('ascii'))
elif msg == "Delete_Account":
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Enter Current Password"
clientSocket.send(message.encode('ascii'))
currPassword = clientSocket.recv(1024).decode('ascii')
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Enter Confirm Password"
clientSocket.send(message.encode('ascii'))
confirmPassword = clientSocket.recv(1024).decode('ascii')
message = deleteAccount.Remove(username, currPassword, confirmPassword)
clientSocket.send(message.encode('ascii'))
if message == "User \'" + username + "\' deleted.\n":
usernames.remove(username)
elif msg == "Update_Password":
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Enter Current Password"
clientSocket.send(message.encode('ascii'))
currPassword = clientSocket.recv(1024).decode('ascii')
message = "SERVER "+ strftime("%d-%m-%Y %H:%M:%S", gmtime()) + ": Enter New Password"
clientSocket.send(message.encode('ascii'))
newPassword = clientSocket.recv(1024).decode('ascii')
message = updatePassword.update(username, currPassword, newPassword)
clientSocket.send(message.encode('ascii'))
while True:
# establish a connection
clientSocket, addr = serverSocket.accept()
welcomeString = strftime("%d-%m-%Y %H:%M:%S", gmtime()) +" Welcome to the chatting room\nPlease Login or Signup\n"
clientSocket.send(welcomeString.encode('ascii'))
start_new_thread(clientThread, (clientSocket, addr))
clientSocket.close()
serverSocket.close()