-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
311 lines (276 loc) · 11.5 KB
/
client.py
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/python3
from tkinter import *
import tkinter as tk
from tkinter import messagebox
import socket
import hashlib
import time
import threading
# Main screen
global main_screen
# Login/Register screen
global subscreen
# socket
global s
# Used since server works in a specific way
global first
global userFailed
# Used in messages
global usernameFinal
# Active users list
global activeUsers
activeUsers = []
def do_stuff(opt="default",user="",pas=""):
global subscreen
global s
global first
global userFailed
global usernameFinal
if(len(pas)<8):
messagebox.showerror("Error","Password should be of length > 8")
return
else:
if(opt=="Login"):
# do login stuff
# Only need to send option once even if other checks fail.
if(first==0):
s.sendall(b"1")
first = 1
# Once username is passed, we don't need to send it again if password fails.
if(userFailed==0):
s.sendall(bytes(user,'ascii'))
data = s.recv(50)
if(data==b"Found"):
userFailed = 1
else:
messagebox.showerror("Not Found","A user with that username was not found in our database\nPlease re-enter")
return
# Check for password
hash_object = hashlib.md5(pas.encode())
md5_hash = hash_object.hexdigest()
s.sendall(bytes(md5_hash,'ascii'))
data = s.recv(7)
if(data==b"Success"):
# If username and password correct, close login page and start chatscreen
usernameFinal = user
messagebox.showinfo("Success","Succesfully Logged in.")
subscreen.destroy()
openChat()
## Start chat
pass
else:
messagebox.showerror("Failed","Make sure you enter the correct username & password")
return
else:
# do register stuff
# Only need to send option once
if(first==0):
s.sendall(b"2")
first = 1
s.sendall(bytes(user,'ascii'))
data = s.recv(50)
# If username already doesn't exist, take password.
if(data==b"Not Found"):
# Start chat after sending password.
# Registration
hash_object = hashlib.md5(pas.encode())
md5_hash = hash_object.hexdigest()
s.sendall(bytes(md5_hash,'ascii'))
usernameFinal = user
messagebox.showinfo("Success","Succesfully registered. You will be automatically logged in.")
subscreen.destroy()
openChat()
# Start chat
else:
messagebox.showerror("Already exists","Username already exists. Please choose a different one.")
return
def send(chat, T):
if(not len(chat.get())>0):
return
mess = chat.get()+"\n"
global s
mess = usernameFinal+":"+mess
s.sendall(bytes(mess,'ascii'))
T.configure(state="normal")
mess = mess[len(usernameFinal)+1:]
mess = mess.rjust(77," ") + " "*3
T.insert(tk.END,mess)
T.configure(state="disabled")
chat.set("")
return
def logout(chatWindow):
global s
s.send(b"quit")
chatWindow.destroy()
s.shutdown(socket.SHUT_RDWR)
s.close()
init()
def quit(chatWindow):
global s
s.send(b"quit")
chatWindow.destroy()
exit(0)
def active():
activeWindow = Tk()
activeWindow.geometry("600x350")
Label(activeWindow, text="Active Users",bg="RoyalBlue2",fg="white",width="600", height="2").pack()
activeWindow.wm_attributes('-type', 'splash')
T = Text(activeWindow, height = 15, width = 80)
T.pack()
T.configure(yscrollcommand=True)
T.configure(bd="1")
for i in range(len(activeUsers)):
T.insert(tk.END,("User {a} : {b}\n".format(a=i+1,b=activeUsers[i])))
T.configure(state='disabled')
Button(activeWindow,text="Close", height="2",bg="tomato",activebackground="orange red",fg="white", command=lambda: activeWindow.destroy(), width="10").pack()
def waitForMessage(T):
global s
global activeUsers
while(True):
## Looks like thread is not stopping since we are not exiting while
## logging out. Need to find another way to handle this
try:
data = s.recv(500)
if(len(data)>0):
data = data.strip()
data = data.decode('ascii')
temp = data.split("\n")
for data in temp:
if(data[:len(usernameFinal)]==usernameFinal):
T.configure(state="normal")
mess = data[len(usernameFinal)+1:]
mess = mess.rjust(77," ") + " "*3
T.insert(tk.END,mess)
T.configure(state="disabled")
else:
data = data.strip() + "\n"
# If someone joined chat
if(data[0]=='\x05'):
activeUsers.append(data[1:-1])
data = data[1:-1] + " joined the chat\n"
# Since person joined after this person, a message is send
# informing the presence of this user.
newmsg = '\x04'.encode('ascii') + bytes(usernameFinal,'ascii') + b"\n"
# Wait for some time so this ping won't be
# together with other history messages
time.sleep(2)
s.sendall(newmsg)
T.configure(state="normal")
T.insert(tk.END,data)
T.configure(state="disabled")
# If a person leaves the chat
elif(data[0]=='\x06'):
name = data[1:-1]
activeUsers.remove(name)
data = data[1:-1] + " left the chat \n"
T.configure(state="normal")
T.insert(tk.END,data)
T.configure(state="disabled")
# This is received by new users connecting
elif(data[0]=='\x04'):
name = data[1:-1]
if name not in activeUsers:
activeUsers.append(name)
else:
T.configure(state="normal")
data = " "*3 + data
T.insert(tk.END,data)
T.configure(state="disabled")
except:
pass
def openChat():
global s
global activeUsers
activeUsers.append(usernameFinal+" (me)")
chatWindow = Tk()
chatWindow.wm_attributes('-type', 'splash')
chatWindow.geometry("700x550")
Label(chatWindow, text="Geek Chat Room!",bg="RoyalBlue2",fg="white",width="700", height="2").pack()
Label(chatWindow, text="Current session : "+usernameFinal,width="77", height="2").pack()
T = Text(chatWindow, height = 14, width = 80)
T.pack()
T.configure(yscrollcommand=True)
T.configure(state='disabled')
T.configure(bd="1")
chat = StringVar()
chat_entry = Entry(chatWindow, textvariable=chat, width="80")
chat_entry.pack()
Button(chatWindow,text="Send", height="2",bg="SpringGreen2",activebackground="SpringGreen1", command=lambda: send(chat, T), width="77").pack()
Button(chatWindow,text="Active Users", height="2",bg="SpringGreen2",activebackground="SpringGreen1", command=lambda: active(), width="77").pack()
Button(chatWindow,text="Logout", height="2",bg="gray25",fg="white",activebackground="gray22", command=lambda: logout(chatWindow), width="77").pack()
Button(chatWindow,text="Quit", height="2",bg="tomato",fg="white",activebackground="orange red", command=lambda: quit(chatWindow), width="10").pack()
# Using thread to receive message since the process is not working
t = threading.Thread(target = waitForMessage, args=(T,))
# Should run in the background
t.setDaemon(True)
t.start()
chatWindow.mainloop()
# Same function for login and register
# Can be later split to check specific conditions
# Since it looks the same, this avoids code repetition
def registerorlogin(opt="default"):
global subscreen
global main_screen
main_screen.destroy()
subscreen = Tk()
subscreen.geometry("600x320")
#subscreen.eval('tk::PlaceWindow . center')
subscreen.wm_attributes('-type', 'splash')
username = StringVar()
password = StringVar()
Label(text=opt, bg="RoyalBlue1",fg="white", width="300", height="2", font=("Calibri", 13)).pack()
Label(subscreen, text="**Enter your details**").pack(pady=(20,0))
Label(subscreen, text="").pack()
username_lable = Label(subscreen, text="Username * ")
username_lable.pack()
username_entry = Entry(subscreen, textvariable=username)
username_entry.pack()
password_lable = Label(subscreen, text="Password * ")
password_lable.pack()
password_entry = Entry(subscreen, textvariable=password, show='*')
password_entry.pack()
Label(subscreen, text="").pack()
# Call a function that checks for conditions.
Button(subscreen, text=opt,bg="SpringGreen2",activebackground="SpringGreen3", width=10, height=1, command =lambda: do_stuff(opt,username.get(),password.get())).pack()
Button(subscreen, text="Go back",bg="RoyalBlue1",fg="white",activebackground="RoyalBlue2", width=10, height=1, command =lambda: go_back(subscreen)).pack()
def go_back(subscreen):
global s
msg = b"\x08"
s.sendall(msg)
subscreen.destroy()
s.shutdown(socket.SHUT_RDWR)
s.close()
init()
def quit_main(main_screen):
global s
s.sendall(b"3")
main_screen.destroy()
# Starting screen
def mainScreen():
global main_screen
main_screen = Tk()
main_screen.wm_attributes('-type', 'splash')
main_screen.geometry("600x280")
Label(text="Welcome to Geek Chat!", bg="RoyalBlue1",fg="white", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
Button(text="Login", height="2",bg="gray25",fg="white",activebackground="gray22", command=lambda: registerorlogin("Login"), width="30").pack(pady=(30,5) )
Button(text="Register", height="2",bg="SpringGreen2",activebackground="SpringGreen3", width="30", command=lambda: registerorlogin("Register")).pack()
Button(text="Close", height="2", width="6",bg="tomato",fg="white",activebackground="orange red", command=lambda: quit_main(main_screen)).pack(pady=20)
main_screen.mainloop()
# If user logout, program does not exit
# User can login/register again
def init():
# Creating the socket
global s
global first
global userFailed
userFailed = 0
first = 0
global activeUsers
activeUsers = []
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Server has to be started before this.
s.connect(("localhost",8080))
mainScreen()
if __name__=='__main__':
init()