forked from maxim3007/TkinterChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
267 lines (221 loc) · 8.07 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
#the very beginning
import socket
import tkinter as tk
import tkinter.messagebox as msb
import tkinter.simpledialog as smp
from tkinter.scrolledtext import ScrolledText
import tkinter.filedialog as fd
import threading
import time
import datetime
from PIL import Image,ImageTk,ImageFile
import io
import random
import platform
#some imports
DEFAULT_PORT = 33535
ImageFile.LOAD_TRUNCATED_IMAGES = True
START_TEXT = """
Welcome to tkChat!
Please keep the chat civil and polite!
Type /online to get online users!
"""
#some defaults
class Client():
def __init__(self,host,port,name,wid):
self.host = host
self.port = port
self.username = name
self.wid=wid
self.last = None
self.images = []
def time(self):
return datetime.datetime.now().strftime("%H:%M")
def create_connection(self):
self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.s.connect((self.host,self.port))
self.s.send(self.username.encode())
message_handler = threading.Thread(target=self.handle_messages,args=())
message_handler.daemon=True
message_handler.start()
self.notify(START_TEXT)
def get_tag(self,name):
if name == " "+self.username+" ":
tag = 'you'
elif name == " System ":
tag = 'sys'
else:
tag = 'oth'
return tag
def handle_messages(self):
global client
while True:
try:
try:
msg = self.s.recv(9999999)
msg = msg.decode()
except:
try:
self.wid['state']="normal"
fp = " "+msg.split(b" - ")[0].decode()+" "
lp = b" - ".join(msg.split(b" - ")[1:])
bio = io.BytesIO(lp)
img = Image.open(bio)
imgtk = ImageTk.PhotoImage(img)
self.wid.insert("end",self.time()+" ",'time')
self.wid.insert("end",fp,self.get_tag(fp))
self.wid.image_create("end",image=imgtk)
self.wid.photo = imgtk
self.images.append(imgtk)
self.wid.insert("end","\n\n")
self.wid.see("end")
self.wid['state']="disabled"
continue
except Exception as e:
self.notify(str(e))
continue
except:
self.wid['state']="normal"
self.wid.insert("end",self.time()+" ",'time')
self.wid.insert("end"," Client ","sys")
self.wid.insert("end"," - Connection reset!")
self.wid.see("end")
self.wid['state']="disabled"
client = None
return
if not msg:
continue
if msg.startswith("!"):
cmd = msg[1:].split(" ")[0]
if cmd == "ONLINE":
members = msg[1:].split(" ")[1:]
self.notify("ONLINE MEMBERS: "+str(members[:-1]))
continue
msg += "\n\n"
user = " "+msg.split(' - ')[0]+" "
text = ' - '+(' - '.join(msg.split(" - ")[1:]))
self.wid['state']="normal"
self.wid.insert("end",self.time()+" ",'time')
self.wid.insert("end",user,self.get_tag(user))
self.wid.insert("end",text)
self.wid.see("end")
self.wid['state']="disabled"
def notify(self,text):
self.wid['state']="normal"
self.wid.insert("end",self.time()+" ",'time')
self.wid.insert("end"," Client ","sys")
self.wid.insert("end", " - "+text+"\n\n")
self.wid.see("end")
self.wid['state']="disabled"
def send(self,text,with_name=True):
if self.last:
if abs(self.last - time.time()) < 0.5:
self.notify("Please wait!")
return
if not isinstance(text, bytes):
if text.startswith("/"):
if text.lower().strip() == "/online":
self.s.send(b"ONLINE")
return
if with_name:
self.s.send(f"{self.username} - {text}".encode())
else:
self.s.send(text.encode())
else:
try:
if not with_name:
raise Exception
text = f"{self.username} - ".encode()+text
except:
pass
self.s.send(text)
self.last = time.time()
#we made client as an object
root = tk.Tk()
root.title("tkChat")
if platform.system()=="Windows":
root.geometry("600x350")
else:
root.geometry("600x380")
root.resizable(False,False)
#window settings
client=None
def connect():
global client
try:
host = smp.askstring("Connect", "Enter host:")
#port = smp.askinteger("Connect", "Enter port:")
port = DEFAULT_PORT
name = smp.askstring("Connect", "Enter Username:").replace(" ","_")
if len(name) == 0:
name = "anon"+str(random.randint(100,999))
if len(name)>20:
name=name[0:20]
if name.startswith("@"):
name = name.replace("@","*")
if name.startswith("!"):
name = name.replace("!","*")
client = Client(host, port, name, chat)
client.create_connection()
connb['state'] = 'disabled'
userl['text'] = name
except:
msb.showwarning("Connect","Can't connect to the chat!\nTry again later.")
def send():
if client:
msg = message.get()
if len(msg)>1024:
msg=msg[0:1024]
if not msg.isspace() and msg != "":
client.send(msg)
message.delete(0,"end")
images = []
def send_attach(tp):
if not client:
return
if tp == "photo":
file = fd.askopenfilename(filetypes=[("PNG",".png"),("JPEG",".jpeg"),("JPG",".jpg")])
if not file:
return
img = Image.open(file)
img.thumbnail((300,300))
bio = io.BytesIO()
img.save(bio, format='PNG')
client.send(bio.getvalue())
def leave():
if client:
client.send('LEAVE',False)
root.destroy()
quit()
root.wm_protocol("WM_DELETE_WINDOW",leave)
#connb = tk.Button(root,text="Connect",command=connect)
#connb.pack()
chatframe = tk.Frame(root,width=200,height=200,bg="white",highlightthickness=1,highlightbackground="lightgrey")
chatframe.pack()
chat = ScrolledText(chatframe,state="disabled",height=18)
chat.pack(fill="x")
chat.tag_configure("sys",background="red",foreground="#ffffff")
chat.tag_configure("oth",background="blue",foreground="#ffffff")
chat.tag_configure("you",background="green",foreground="#ffffff")
chat.tag_configure("time",foreground="#A9A9A9")
msgf = tk.Frame(root,bg="white",highlightthickness=0,highlightbackground="lightgrey")
msgf.pack(fill="x")
userl = tk.Label(msgf,text="\0", height=1)
userl.pack(side="left")
userl.bind("<Button-1>",lambda ev:[root.clipboard_clear(),root.clipboard_append(userl['text'])])
message = tk.Entry(msgf,width=40)
message.pack(side="left")
message.bind("<Return>",lambda ev:send())
popup = tk.Menu(root, tearoff=0)
popup.add_command(label="Photo",command=lambda:send_attach("photo"))
snd = tk.Button(msgf,text="Send",font="sans-serif 15 bold",height=1,command=send)
snd.pack(side="left")
def menu():
popup.post(add.winfo_rootx(), add.winfo_rooty())
add = tk.Button(msgf,text="Attach",font="sans-serif 15 bold",height=1,width=5,command=menu)
add.pack(side="left")
connect_button = tk.Button(msgf,text="Connect",font="sans-serif 15 bold",height=1,command=connect)
connect_button.pack(side="left")
#setting ui and buttons
root.mainloop()
#and we run everything