-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.py
271 lines (216 loc) · 9.95 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import configparser
import datetime
import json
import os
import socket
import sys
import threading
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/terminal-text-boxes/src/")
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/common/")
import terminalTextBoxes as ttb
import unicode as uni
import pkg_type as pt
class ChatAppClient():
""" ChatAppClient. """
def __init__(self, host, port):
""" Class initialization.
Arguments:
host - The host IP to connect to. (str)
port - The port of the server. (int)
"""
# Initialize terminalTextBoxes
self.tb = ttb.TerminalTextBoxes(self.character_callback, self.enter_callback)
self.tb.create_text_box_setup("main")
self.tb.create_text_box("main", "chat", hOrient=ttb.H_ORIENT["left"], wTextIndent=1, frameAttr="green",
frameChar="doubleLine")
self.tb.create_text_box("main", "info", 25, hOrient=ttb.H_ORIENT["right"], wTextIndent=1, frameAttr="yellow",
frameChar="doubleLine", scrollVisable=False)
self.tb.debug = False
self.tb.set_focus_box("main", "chat")
# Socket variables
self.host = host
self.port = port
self.serverComm = None
# General variables
self.active = False
self.onlineUsers = list()
self.commandPrefix = "!"
self.passwordMode = True
self.passwordString = ""
def start(self):
""" Start ChatApp client. """
# Initialize socket
self.serverComm = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.serverComm.settimeout(10)
try:
self.serverComm.connect((self.host, self.port))
except:
print("Could not connect to the server")
return
# Start the GUI
self.tb.start()
self.add_chat_info_message("SERVER", "Please enter the correct password to enter ChatApp", "yellow")
# Start the incoming package thread
self.active = True
self.__incoming_package_thread()
def stop(self):
""" Stop ChatApp client. """
self.active = False
self.send_package(pt.P_TYPE["command"], None, "stop", None)
self.serverComm.close()
def character_callback(self, character):
""" Callback function from TerminalTextBoxes module for every key press.
Arguments:
character - The character sent from the TerminalTextBoxes module. (character)
"""
if character == "\x1b":
self.stop()
return
# Password mode
if self.passwordMode and uni.isUnicode(character):
cPos = self.tb.get_prompt_cursor_pos()
self.passwordString = self.passwordString[:cPos] + str(character) + self.passwordString[cPos:]
self.tb.set_prompt_string(len(self.passwordString) * "*")
return
def enter_callback(self, message):
""" Callback function from TerminalTextBoxes module for every enter pressed.
Arguments:
message - The message received from the TerminalTextBoxes module. (str)
"""
if message == "":
return
# Password mode
if self.passwordMode:
self.serverComm.send(self.passwordString.encode())
self.passwordString = ""
return
# Regular mode
if not self.command_handler(message):
# If not a command, send as regular message
self.send_package(pt.P_TYPE["message"], message, None, None)
def command_handler(self, string):
""" Handler for commands.
Arguments:
string - the string to be evaluated as a command or not. (str)
"""
p = self.commandPrefix
# Local commands
if string == f"{p}c" or string == f"{p}clear":
self.tb.clear_text_items("main", "chat")
self.tb.update()
return True
elif string == f"{p}getUsers":
users = str(self.onlineUsers)
users = users[1:][:-1]
string = f"Online users: {users}"
self.add_info_prompt_message(string, "yellow")
return True
# Send the command to the server if the command wasn't local
if string.startswith(self.commandPrefix):
string = string[len(self.commandPrefix):]
for command in pt.CLIENT_COMMANDS:
if string.startswith(command + " "):
string = string.replace(command + " ", "")
self.send_package(pt.P_TYPE["command"], string, command, None)
return True
return False
def send_package(self, pType, pData, pInfo, pInitiator):
""" Send a package to the server.
Arguments:
pType - The type of message P_TYPE. (int)
pData - The data to be sent. (any)
pInfo - Additional info. (any)
pInitiator - The initiator of the message. (str)
"""
package = pt.create_package(pType, pData, pInfo, pInitiator)
self.serverComm.send(package)
def __incoming_package_thread(self):
""" Thread for incoming package data. """
while self.active:
try:
recv = self.serverComm.recv(2048)
pkg = json.loads(recv.decode("utf-8"))
if not pt.valid_package(pkg, False):
# Invalid package, skip this time
continue
if pkg["type"] == pt.P_TYPE["command"]:
if pkg["info"] == "updateUsers":
self.onlineUsers = list()
for user in pkg["data"]:
self.onlineUsers.append(user)
self.update_infobox()
elif pkg["info"] == "password":
if "Correct" in pkg["data"]:
self.add_info_prompt_message(pkg["data"], "green")
self.passwordMode = False
else:
self.add_info_prompt_message(pkg["data"], "red")
elif pkg["type"] == pt.P_TYPE["message"]:
self.add_chat_user_message(pkg["initiator"], pkg["data"], "green")
elif pkg["type"] == pt.P_TYPE["notify"]:
if pkg["info"] == "chat_info":
self.add_chat_info_message(pkg["initiator"], pkg["data"], "yellow")
elif pkg["info"] == "info_prompt":
self.add_info_prompt_message(pkg["data"], "yellow")
elif pkg["type"] == pt.P_TYPE["error"]:
if pkg["info"] == "chat_error":
self.add_chat_info_message(pkg["initiator"], pkg["data"], "red")
elif pkg["info"] == "info_prompt":
self.add_info_prompt_message(pkg["data"], "red")
except Exception as e:
pass
def update_infobox(self):
""" Update the infobox that displays the online users. """
self.tb.clear_text_items("main", "info")
self.tb.add_text_item("main", "info", " Online users:", "yellow")
for user in self.onlineUsers:
self.tb.add_text_item("main", "info", f" - {user}")
self.tb.update()
def add_chat_user_message(self, who, message, attributes):
""" Adds a chat user message to the chat text box.
Arguments:
who - Who initiated the info message. (str)
message - The message to be added to the chat text box. (str)
attributes - The attributes of the message. (str/list)
"""
who = f"{self.get_time()} < {who} >"
self.tb.add_text_item("main", "chat", who, ["white", "standout"])
self.tb.add_text_item("main", "chat", message, attributes)
self.tb.update()
def add_chat_info_message(self, who, message, attributes):
""" Adds info message to the chat text box.
Arguments:
who - Who initiated the info message. (str)
message - The message to be added to the chat text box. (str)
attributes - The attributes of the message. (str/list)
"""
message = f"{self.get_time()} < {who} > {message}"
self.tb.add_text_item("main", "chat", message, attributes)
self.tb.update()
def add_info_prompt_message(self, message, attributes, timeout=5000):
""" Adds a message to the info prompt for timeout amount of ms.
Arguments:
message - The message to be added to the info prompt. (str)
attributes - Attributes of the message. (str/list)
timeout - The timeout before the message disappears. (int)
"""
self.tb.set_info_prompt_text_attr(attributes)
self.tb.set_info_prompt_text(message, timeout)
self.tb.update()
def get_time(self):
""" Returns the current time in format 'HH:MM:SS'. """
return datetime.datetime.now().strftime("%H:%M:%S")
if __name__ == "__main__":
config = configparser.ConfigParser()
config.read("config.ini", encoding="utf-8")
host = str(config["General"]["server_ip"])
port = str(config["General"]["server_port"])
if host == "None":
raise Exception("Server address is not set in config.ini.")
if port == "None":
raise Exception("Server port is not set in config.ini.")
port = int(port)
cac = ChatAppClient(host, port)
cac.start()