-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjim_message.py
More file actions
216 lines (166 loc) · 6.5 KB
/
jim_message.py
File metadata and controls
216 lines (166 loc) · 6.5 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
# This Python file uses the following encoding: utf-8
import json
import time
ACTION_AUTH = 1
ACTION_QUIT = 2
ACTION_PROBE = 3
ACTION_PRESENCE = 4
ACTION_MSG = 5
RESPONSE_BASIC_NOTIFICATION = 101
RESPONSE_MAJOR_NOTIFICATION = 101
RESPONSE_SUCCESS_OK = 200
RESPONSE_SUCCESS_CREATED = 201
RESPONSE_SUCCESS_ACCEPTED = 202
RESPONSE_CLIENT_BAD_REQUEST = 400
RESPONSE_CLIENT_UNAUTHORIZED = 401
RESPONSE_CLIENT_INVALID_CRED = 402
RESPONSE_CLIENT_FORBIDDEN = 403
RESPONSE_CLIENT_NOT_FOUND = 404
RESPONSE_CLIENT_CONFLICT = 409
RESPONSE_CLIENT_OFFLINE = 410
RESPONSE_SERVER_ERROR = 500
RESPONSE_MESSAGE_DICT = {
RESPONSE_BASIC_NOTIFICATION: "Базовое уведомление",
RESPONSE_MAJOR_NOTIFICATION: "Важное уведомление",
RESPONSE_SUCCESS_OK: "ОК",
RESPONSE_SUCCESS_CREATED: "Объект создан",
RESPONSE_SUCCESS_ACCEPTED: "Подтверждение",
RESPONSE_CLIENT_BAD_REQUEST: "Некорректный запрос",
RESPONSE_CLIENT_UNAUTHORIZED: "Пользователь не авторизован",
RESPONSE_CLIENT_INVALID_CRED: "Неверный логин/пароль",
RESPONSE_CLIENT_FORBIDDEN: "Пользователь заблокирован",
RESPONSE_CLIENT_NOT_FOUND: "Пользователь/чат отсутствует на сервере",
RESPONSE_CLIENT_CONFLICT: "Уже имеется подвключение с указанным логином",
RESPONSE_CLIENT_OFFLINE: "Адресат не в сети",
RESPONSE_SERVER_ERROR: "Ошибка сервера"
}
def _decode_message(data, encoding):
try:
json_str = data.decode(encoding)
dict_ = json.loads(json_str, encoding=encoding)
return dict_
except Exception as e:
print(e)
return None
def _encode_message(dict_, encoding):
try:
json_str = json.dumps(dict_, ensure_ascii=False)
data = json_str.encode(encoding)
return data
except Exception as e:
print(e)
return None
class JimMessage:
def __init__(self, encoding):
self.action = None
self.response = None
self.user = None
self.from_ = None
self.to_ = None
self.message = None
self.time = time.time()
self.encoding = encoding
def __str__(self):
if self.time is not None:
str_time = time.strftime("%d.%m.%Y %H:%M:%S", time.localtime(self.time))
else:
str_time = "None"
if self.action is not None:
return f"JimMessage(action: {self.action}, time: {str_time}, enc: {self.encoding}, user: {self.user}, from: {self.from_}, to: {self.to_}, msg: {self.message})"
elif self.response is not None:
return f"JimMessage(response: {self.response}, time: {str_time}, encoding: {self.encoding})"
else:
return f"JimMessage(time: {str_time}, encoding: {self.encoding})"
def to_bytes(self):
dict_ = dict()
dict_["time"] = self.time
if self.action is not None:
if self.action == ACTION_AUTH:
dict_["action"] = "authenticate"
dict_["user"] = self.user
if self.action == ACTION_QUIT:
dict_["action"] = "quit"
if self.action == ACTION_PROBE:
dict_["action"] = "probe"
if self.action == ACTION_PRESENCE:
dict_["action"] = "presence"
dict_["user"] = self.user
if self.action == ACTION_MSG:
dict_["action"] = "msg"
dict_["to"] = self.to_
dict_["from"] = self.from_
dict_["encoding"] = self.encoding
dict_["message"] = self.message
if self.response is not None:
dict_["response"] = self.response
data = _encode_message(dict_, self.encoding)
return data
@staticmethod
def parse_bytes(data, encoding):
dict_ = _decode_message(data, encoding)
if dict_ is None:
return None
if "time" not in dict_:
return None
time_ = dict_["time"]
if "action" in dict_:
action = dict_["action"]
if action == "authenticate":
if "user" not in dict_:
return None
user = dict_["user"]
if "account_name" not in user or "password" not in user:
return None
jim_msg = JimMessage(encoding)
jim_msg.action = ACTION_AUTH
jim_msg.time = time_
jim_msg.user = user
return jim_msg
if action == "quit":
jim_msg = JimMessage(encoding)
jim_msg.action = ACTION_QUIT
jim_msg.time = time_
return jim_msg
if action == "probe":
jim_msg = JimMessage(encoding)
jim_msg.action = ACTION_PROBE
jim_msg.time = time_
return jim_msg
if action == "presence":
if "user" not in dict_:
return None
user = dict_["user"]
if "account_name" not in user:
return None
jim_msg = JimMessage(encoding)
jim_msg.action = ACTION_PRESENCE
jim_msg.time = time_
jim_msg.user = user
return jim_msg
if action == "msg":
if "to" not in dict_ \
or "from" not in dict_ \
or "encoding" not in dict_ \
or "message" not in dict_:
return None
jim_msg = JimMessage(encoding)
jim_msg.action = ACTION_MSG
jim_msg.time = time_
jim_msg.to_ = dict_["to"]
jim_msg.from_ = dict_["from"]
jim_msg.encoding = dict_["encoding"]
jim_msg.message = dict_["message"]
return jim_msg
return None
elif "response" in dict_:
jim_msg = JimMessage(encoding)
jim_msg.response = dict_["response"]
jim_msg.time = time_
return jim_msg
else:
return None
@staticmethod
def response_message(response, encoding):
jim_msg = JimMessage(encoding)
jim_msg.response = response
return jim_msg