-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.pyw
More file actions
328 lines (258 loc) · 11 KB
/
Client.pyw
File metadata and controls
328 lines (258 loc) · 11 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""
Version 1.0.4
Write and receive messages (GUI)
Author:
Nilusink
"""
from plyer import notification
from core.client import *
from tkinter import ttk
from typing import Dict
import tkinter as tk
def newline_parser(string: str, line_length: int, word_sensitive=False):
new_string = string
active = False
newlines = 1
correction = int()
for i in range(len(string)):
i += correction
if i % line_length == 0 and i != 0:
active = word_sensitive
if not word_sensitive:
new_string = new_string[:i]+'\n'+new_string[i:]
newlines += 1
if string[i] == ' ' and active:
new_string = new_string[:i]+'\n'+new_string[i:].lstrip(' ')
newlines += 1
active = False
return new_string, newlines
class ChatBox:
def __init__(self, r, message: str, sent_time: str, sent_by: str, width: int = 1000):
lines = 1
if len(message) > int(width / 10):
message, lines = newline_parser(message, int(width / 10), True)
height = 40 + 24 * lines
for_time = sent_time
r.grid_columnconfigure(0, weight=1)
self.canvas = tk.Canvas(master=r, borderwidth=5, bg='gray', height=height, width=1000)
self.canvas.create_text(20, 20, fill="black", font="mono 15", text=message, anchor=tk.NW)
self.canvas.create_text(20, height - 20, fill="black", font="mono 10", text=sent_by,
anchor=tk.NW)
self.canvas.create_text(900, height - 20, fill="black", font="mono 10", text=for_time, anchor=tk.NW)
def pack(self, *args, **kwargs):
self.canvas.pack(*args, expand=True, fill='x', anchor=tk.NW, **kwargs)
def pack_forget(self):
self.canvas.pack_forget()
def grid(self, *args, **kwargs):
self.canvas.grid(*args, **kwargs)
def grid_forget(self):
self.canvas.grid_forget()
class Window:
__done_messages: list = []
__done_objects: list = []
__connection: Connection
def __init__(self, server_secret: str | bytes, client_secret: str | bytes) -> None:
"""
Create a gui windows for chatting
:param server_secret: the secret key for the server
:param client_secret: the secret key for the clients
"""
# save server information
self.__server_ip: str
self.__server_port: int
self.__server_secret = server_secret
self.__client_secret = client_secret
# later used variables
self.__unread_messages: list[dict] = []
# GUI color config
self.__colors: Dict[str, str] = {
"bg": "gray23",
"fg": "white",
"error": "red"
}
# create GUI
self.root = tk.Tk()
self.root.title("SecureMess")
self.root.config(bg=self["bg"])
# connection screen
self.__set_size(200, 150)
self.login_frame = tk.Frame(self.root, bg=self["bg"])
self.ip_label = tk.Label(self.login_frame, text="Please enter the Server ip", fg=self["fg"], bg=self["bg"])
self.ip_entry = ttk.Entry(self.login_frame, width=25)
self.port_label = tk.Label(self.login_frame, text="Please enter the server port", fg=self["fg"], bg=self["bg"])
self.port_entry = ttk.Entry(self.login_frame, width=25)
self.port_entry.insert(0, "3333")
self.login_label = tk.Label(self.login_frame, text="Please enter your Username", fg=self["fg"], bg=self["bg"])
self.name_entry = ttk.Entry(self.login_frame, width=25)
# grid all the stuff
self.ip_label.grid(row=0, column=0)
self.port_label.grid(row=2, column=0)
self.login_label.grid(row=4, column=0)
self.ip_entry.grid(row=1, column=0)
self.port_entry.grid(row=3, column=0)
self.name_entry.grid(row=5, column=0)
self.ip_entry.bind("<Return>", self.try_login)
self.port_entry.bind("<Return>", self.try_login)
self.name_entry.bind("<Return>", self.try_login)
self.connection_invalid_label = tk.Label(self.login_frame, text="", fg=self["error"], bg=self["bg"])
self.connection_invalid_label.grid(row=6, column=0)
# create main chatting frame
self.main_frame = tk.Frame(self.root, bg=self["bg"])
self.main_frame.grid_rowconfigure(0, weight=1)
self.main_frame.grid_rowconfigure(1, weight=0)
self.main_frame.grid_columnconfigure(0, weight=1)
self.main_frame.grid_columnconfigure(1, weight=0)
self.scrollbar = tk.Scrollbar(self.main_frame)
self.messages_frame = tk.Listbox(self.main_frame, yscrollcommand=self.scrollbar.set)
self.root.bind("<MouseWheel>", self.scroll_chat)
self.send_message_entry = ttk.Entry(self.main_frame)
self.scrollbar.grid(row=0, column=1, sticky=tk.NSEW)
self.messages_frame.grid(row=0, column=0, sticky=tk.NSEW)
self.send_message_entry.grid(row=1, column=0, columnspan=2, sticky=tk.NSEW)
self.send_message_entry.bind("<Return>", self.send_message)
# place login frame
self.login_frame.pack()
def scroll_chat(self, event) -> None:
self.messages_frame.yview_scroll(-1*int(event.delta/abs(event.delta)), "units")
@property
def colors(self) -> dict:
"""
colors for configuration GUI
"""
return self.__colors
@property
def unread_messages(self) -> list[dict]:
return self.__unread_messages
def color_config(self, color_type: str, color: str) -> str:
"""
set one color
:param color_type: type - bg | fg ...
:param color: the color to change to
:return: the old color
"""
if color_type not in self.colors:
raise KeyError("Invalid color type")
# change color
o_color = self[color_type]
self.__colors[color_type] = color
return o_color
def update_colors(self) -> None:
"""
updates every color by the colors dict
"""
# login frame
self.root.config(bg=self["bg"])
self.login_frame.configure(bg=self["bg"])
self.login_label.configure(bg=self["bg"], fg=self["fg"])
self.connection_invalid_label.configure(bg=self["bg"], fg=self["error"])
# main frame
self.main_frame.config(bg=self["bg"])
self.root.update()
def __set_size(self, width: int, height: int, set_max: bool = True, set_min: bool = True) -> None:
"""
set window size
:param width: width of the window
:param height: height of the window
"""
if set_max:
self.root.maxsize(width=width, height=height)
else:
self.root.maxsize(width=self.root.winfo_screenwidth(), height=self.root.winfo_screenheight())
if set_min:
self.root.minsize(width=width, height=height)
else:
self.root.minsize(width=1, height=1)
def run(self) -> None:
"""
run the tkinter mainloop
"""
self.root.mainloop()
self.end()
def try_login(self, *_tk_trash) -> None:
"""
try to login with the username from self.name_entry
"""
name = self.name_entry.get()
if not name:
self.connection_invalid_label["text"] = "Please enter a username"
self.root.after(800, lambda: self.connection_invalid_label.config(text=""))
return
try:
self.__connection = Connection(
ip=self.ip_entry.get(), port=int(self.port_entry.get()), username=name,
server_secret=self.__server_secret, clients_secret=self.__client_secret
)
except NameError:
self.connection_invalid_label["text"] = "User already logged in"
self.root.after(800, lambda: self.connection_invalid_label.config(text=""))
return
except ValueError:
self.connection_invalid_label["text"] = "Invalid Port"
self.root.after(800, lambda: self.connection_invalid_label.config(text=""))
return
except ConnectionRefusedError:
self.connection_invalid_label["text"] = "Wrong IP / Server (program) down"
self.root.after(1500, lambda: self.connection_invalid_label.config(text=""))
return
except (ConnectionError, TimeoutError):
self.connection_invalid_label["text"] = "Invalid IP / Server (computer) down"
self.root.after(1500, lambda: self.connection_invalid_label.config(text=""))
return
except InvalidSecret:
self.connection_invalid_label["text"] = "Server-Secret Wrong!"
self.root.after(1000, lambda: self.connection_invalid_label.config(text=""))
return
# switch to main chat frame
self.login_frame.pack_forget()
self.__set_size(300, 500, set_max=False)
self.main_frame.pack(fill=tk.BOTH, padx=10, pady=10, expand=True)
self.__update_messages()
def __update_messages(self, *_tk_trash) -> None:
"""
check if there are new messages
"""
updated_messages: list[dict] = []
for message in self.__connection.new_messages:
if message not in self.__done_messages:
updated_messages.append(message)
self.messages_frame.insert(tk.END, f"{message['user']}>> {message['message']}")
self.__done_messages.append(message)
if updated_messages:
self.messages_frame.yview(tk.END)
# if the window is not focused, send a notification
if not self.root.focus_get():
self.__unread_messages += updated_messages
users = {message["user"] for message in self.unread_messages}
title = f"{len(self.unread_messages)} unread message(s) from {len(users)} user(s)"
notify = "\n".join(f"{message['user']}: {message['message']}" for message in self.unread_messages)
notification.notify(
title=title,
message=notify,
app_name="SecureMess"
)
if self.root.focus_get():
self.unread_messages.clear()
self.root.after(200, self.__update_messages)
def send_message(self, *_tk_trash) -> None:
"""
send a message from the self.send_message_entry
"""
self.__connection.send_message(self.send_message_entry.get())
self.send_message_entry.delete(0, tk.END)
def end(self) -> None:
"""
close all windows and end connection
"""
self.__connection.end()
with suppress(Exception):
self.root.destroy()
def __getitem__(self, item: str):
if item in self.colors:
return self.colors[item]
elif item in self.__dict__:
return self.__dict__[item]
raise KeyError("invalid item")
if __name__ == '__main__':
secrets = json.load(open("config.json", "r"))
w = Window(secrets["server_secret"], secrets["client_secret"])
w.run()