-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmychat.py
532 lines (477 loc) · 20.6 KB
/
cmychat.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#!/usr/bin/env python3
# coding=UTF-8
'''
@Author: cenmmy
@LastEditors: cenmmy
@Description:
@Date: 2019-04-19 15:19:01
@LastEditTime: 2019-05-01 15:05:07
'''
import curses
from frdls import FrdLs
from msgls import Msg, MsgLs
from input import Input
from command import Command
from statusbar import StatusBar
import time
import socketio
import locale
import os
import threading
from config import Config
# const variable
MODE_NORMAL = 0b000
MODE_FRDLS = 0b001
MODE_MSGLS = 0b010
MODE_INPUT = 0b011
MODE_COMMAND = 0b100
COMMAND_IS_EMPTY = -1
COMMAND_IS_NOT_EMPTY = 0
URL = 'http://localhost:1234'
class WinInfo(object):
def __init__(self, sminrow, smincol, smaxrow, smaxcol):
self.sminrow = sminrow
self.smincol = smincol
self.smaxrow = smaxrow
self.smaxcol = smaxcol
class CmyChat(object):
def __init__(self, stdscr, nlines, ncols, begin_y, begin_x):
self.stdscr = stdscr
self.nlines = nlines
self.ncols = ncols
self.begin_y = begin_y
self.begin_x = begin_x
self.init_variable()
self.init_pair()
self.cal_wininfo()
self.set_win_color(self._frdls_info, 1)
self.set_win_color(self._msgls_info, 2)
self.set_win_color(self._input_info, 3)
self.set_win_color(self._command_info, 4)
self.create_win()
# send file path
self.filepath = ''
self.file_sender = ''
self.file_receiver = ''
self.sio = socketio.Client()
def init_variable(self):
self.friends = []
self.text = ''
self.command = ''
self.mode = MODE_NORMAL
self.user = {'username':''}
self.current_receiver = 'no_receiver'
self.msgls_set = {}
self.logged_in = False
def init_pair(self):
frdls_color = Config['color']['frdls']
msgls_color = Config['color']['msgls']
input_color = Config['color']['input']
command_color = Config['color']['command']
curses.init_pair(1, frdls_color['fgcolor'], frdls_color['bgcolor'])
curses.init_pair(2, msgls_color['fgcolor'], msgls_color['bgcolor'])
curses.init_pair(3, input_color['fgcolor'], input_color['bgcolor'])
curses.init_pair(4, command_color['fgcolor'], command_color['bgcolor'])
def cal_wininfo(self):
self._frdls_info = WinInfo(self.begin_y + 1, self.begin_x + 1, (self.begin_y + 1) + (self.nlines - 2 - 4 - 1), (self.begin_x + 1) + (int((self.ncols - 4) * 0.25) - 1))
self._msgls_info = WinInfo(self.begin_y + 1, self._frdls_info.smaxcol + 1 + 1 + 1, (self.begin_y + 1) + (int((self.nlines - 2 - 2 - 4) * 0.7) - 1), (self.begin_x + self.ncols - 1 - 1))
self._input_info = WinInfo(self._msgls_info.smaxrow + 1 + 1 + 1, self._frdls_info.smaxcol + 1 + 1 + 1, self.begin_y + self.nlines - 1 - 4 - 1, self.begin_x + self.ncols - 1 - 1)
self._command_info = WinInfo(self.begin_y + self.nlines - 1 - 1 - 1, self.begin_x + 1, self.begin_y + self.nlines - 1 - 1 - 1, self.begin_x + self.ncols -1 -1)
self._statusbar_info = WinInfo(self.begin_y + self.nlines - 1, self.begin_x, self.begin_y + self.nlines - 1, self.begin_x + self.ncols - 1)
def set_win_color(self, wininfo, colorpair):
line = wininfo.sminrow
while line <= wininfo.smaxrow:
self.stdscr.chgat(line, wininfo.smincol, wininfo.smaxcol - wininfo.smincol + 1, curses.color_pair(colorpair))
line = line + 1
def create_win(self):
self.frdls = FrdLs(self.stdscr, self._frdls_info.smaxcol - self._frdls_info.smincol + 1, self._frdls_info.sminrow, self._frdls_info.smincol, self._frdls_info.smaxrow, self._frdls_info.smaxcol, self.friends, Config['color']['frdls']['fgcolor'], Config['color']['frdls']['bgcolor'])
self.msgls_set[self.current_receiver] = MsgLs(self.stdscr, self._msgls_info.smaxcol - self._msgls_info.smincol + 1, self._msgls_info.sminrow, self._msgls_info.smincol, self._msgls_info.smaxrow, self._msgls_info.smaxcol, [], fgcolor=Config['color']['msgls']['fgcolor'], bgcolor=Config['color']['msgls']['bgcolor'])
self.input = Input(self.stdscr, self._input_info.smaxcol - self._input_info.smincol + 1, self._input_info.sminrow, self._input_info.smincol, self._input_info.smaxrow, self._input_info.smaxcol, self.text, Config['color']['input']['fgcolor'], Config['color']['input']['bgcolor'])
self.command = Command(self.stdscr, self._command_info.smaxrow - self._command_info.sminrow + 1, self._command_info.sminrow, self._command_info.smincol, self._command_info.smaxrow, self._command_info.smaxcol, self.command, Config['color']['command']['fgcolor'], Config['color']['command']['bgcolor'])
self.statusbar = StatusBar(self.stdscr, 1, self._statusbar_info.smaxcol - self._statusbar_info.smincol + 1, self._statusbar_info.sminrow, self._statusbar_info.smincol, self._statusbar_info.smaxrow, self._statusbar_info.smaxcol, "NORMAL", "xxxxxx", fgcolor=curses.COLOR_BLACK, bgcolor=curses.COLOR_WHITE)
def resize(self, nlines, ncols, begin_y=0, begin_x=0):
self.nlines = nlines
self.ncols = ncols
self.begin_y = begin_y
self.begin_x = begin_x
def draw(self):
self.frdls.draw()
self.msgls_set[self.current_receiver].draw()
self.input.draw()
self.command.draw()
self.statusbar.draw()
def refresh(self):
self.stdscr.refresh()
self.frdls.refresh()
self.msgls_set[self.current_receiver].refresh()
self.input.refresh()
self.command.refresh()
self.statusbar.refresh()
def clear(self):
self.stdscr.clear()
self.frdls.clear()
self.msgls_set[self.current_receiver].clear()
self.input.clear()
self.command.clear()
self.statusbar.clear()
def run(self):
self.connect(URL)
self.event_listenning()
while True:
ch = self.stdscr.getch()
if ch == curses.KEY_RESIZE:
self.on_key_resize()
elif ch == 27:
self.on_key_esc()
elif self.mode == MODE_NORMAL:
if ch == ord('q'):
if self.logged_in:
self.sio.emit('logout', {}, callback=lambda res: self.sio.disconnect())
else:
self.sio.emit('quit', {}, callback=lambda: self.sio.disconnect())
break
else:
self.on_normal(ch)
elif self.mode == MODE_FRDLS:
self.on_frdls(ch)
elif self.mode == MODE_MSGLS:
self.on_msgls(ch)
elif self.mode == MODE_INPUT:
self.on_input(ch)
elif self.mode == MODE_COMMAND:
self.on_command(ch)
def on_key_resize(self):
nlines, ncols = self.stdscr.getmaxyx()
if nlines >= 25 and ncols >= 55:
self.clear()
self.friends = self.frdls.friends
self.msgs = self.msgls_set[self.current_receiver].msgs
self.text = self.input.text
self.command = self.command.command
self.resize(nlines, ncols)
self.cal_wininfo()
self.set_win_color(self._frdls_info, 1)
self.set_win_color(self._msgls_info, 2)
self.set_win_color(self._input_info, 3)
self.set_win_color(self._command_info, 4)
self.create_win()
self.draw()
self.refresh()
else:
self.stdscr.clear()
try:
curses.init_pair(6, curses.COLOR_RED, curses.COLOR_BLACK)
self.stdscr.addstr(int(nlines / 2), int((ncols - len("TOO SMALL")) / 2), "TOO SMALL", curses.color_pair(6))
except curses.error:
pass
self.stdscr.refresh()
def on_key_esc(self):
self.mode = MODE_NORMAL
self.statusbar.mode = "NORMAL"
self.statusbar.content()
self.statusbar.refresh()
def on_normal(self, ch):
if ch == ord('f'):
self.mode = MODE_FRDLS
self.statusbar.mode = "FRDLS"
self.statusbar.content()
self.statusbar.refresh()
elif ch == ord('m'):
self.mode = MODE_MSGLS
self.statusbar.mode = "MSGLS"
self.statusbar.content()
self.statusbar.refresh()
elif ch == ord('i'):
self.mode = MODE_INPUT
self.statusbar.mode = "INPUT"
self.statusbar.content()
self.statusbar.refresh()
elif ch == ord('c'):
self.mode = MODE_COMMAND
self.statusbar.mode = "COMMAND"
self.statusbar.content()
self.statusbar.refresh()
elif ch == ord('r'):
# reconnect
self.connect(URL)
def on_frdls(self, ch):
if ch == curses.KEY_UP:
self.frdls.current_row_style(curses.color_pair(self.frdls.color_pair))
self.frdls.onkeyup()
self.frdls.current_row_style(curses.A_REVERSE)
self.refresh()
elif ch == curses.KEY_DOWN:
self.frdls.current_row_style(curses.color_pair(self.frdls.color_pair))
self.frdls.onkeydown()
self.frdls.current_row_style(curses.A_REVERSE)
self.refresh()
elif ch == ord('\n'):
receiver = self.frdls.friends[self.frdls.current_row - 1]
if receiver != self.user['username']:
self.msgls_set[self.current_receiver].clear()
self.msgls_set[self.current_receiver].refresh()
self.current_receiver = receiver
self.msgls_set[self.current_receiver].content()
self.msgls_set[self.current_receiver].refresh()
self.mode = MODE_INPUT
self.statusbar.mode = "INPUT"
self.statusbar.content()
self.statusbar.refresh()
def on_msgls(self, ch):
if ch == curses.KEY_UP:
self.msgls_set[self.current_receiver].current_row_style(self.msgls_set[self.current_receiver].color_pair)
self.msgls_set[self.current_receiver].onkeyup()
self.msgls_set[self.current_receiver].current_row_style(curses.A_REVERSE)
self.msgls_set[self.current_receiver].refresh()
elif ch == curses.KEY_DOWN:
self.msgls_set[self.current_receiver].current_row_style(self.msgls_set[self.current_receiver].color_pair)
self.msgls_set[self.current_receiver].onkeydown()
self.msgls_set[self.current_receiver].current_row_style(curses.A_REVERSE)
self.msgls_set[self.current_receiver].refresh()
def on_input(self, ch):
if ch == curses.KEY_BACKSPACE:
self.input.delete()
self.input.clear()
self.input.refresh()
self.input.content()
self.input.ondelete()
self.set_win_color(self._input_info, 3)
self.stdscr.refresh()
self.input.refresh()
elif ch == curses.KEY_UP:
self.input.onkeyup()
self.input.refresh()
elif ch == curses.KEY_DOWN:
self.input.onkeydown()
self.input.refresh()
elif ch == ord('\n'):
msg = Msg(self.user['username'], self.current_receiver, self.input.text, time.strftime('%H:%M:%S', time.localtime(time.time())))
self.sio.emit('message', {'receiver': self.current_receiver, 'message': self.input.text}, callback=lambda res: self.onmessage(res, msg))
self.input.text = ''
self.input.clear()
self.input.refresh()
self.input.content()
self.set_win_color(self._input_info, 3)
self.stdscr.refresh()
self.input.refresh()
else:
self.input.read(ch)
self.input.content()
self.input.oninsert()
self.input.refresh()
def on_command(self, ch):
if ch == curses.KEY_LEFT:
self.command.onkeyleft()
self.command.refresh()
elif ch == curses.KEY_RIGHT:
self.command.onkeyright()
self.command.refresh()
elif ch == curses.KEY_BACKSPACE:
self.command.delete()
self.command.clear()
self.command.refresh()
self.command.content()
self.command.ondelete()
self.set_win_color(self._command_info, 4)
self.stdscr.refresh()
self.command.refresh()
elif ch == ord('\n'):
res = self.command.reslove()
self.exec_cmd(res)
self.command.command = ''
self.command.clear()
self.command.refresh()
self.command.content()
self.set_win_color(self._command_info, 4)
self.stdscr.refresh()
self.command.refresh()
else:
self.command.read(ch)
self.command.content()
self.command.oninsert()
self.command.refresh()
def exec_cmd(self, cmd):
if cmd['status'] == COMMAND_IS_EMPTY:
self.prompt('command is empty')
else:
if cmd['cmd'] == 'login':
self.login(cmd['parms'])
elif cmd['cmd'] == 'register':
self.register(cmd['parms'])
elif cmd['cmd'] == 'logout':
self.logout(cmd['parms'])
elif cmd['cmd'] == 'sendfile':
self.sendfile(cmd['parms'])
else:
self.prompt('command undefined')
def connect(self, url):
try:
self.sio.connect(url)
except socketio.exceptions.ConnectionError as e:
self.prompt(str(e))
except ValueError as e:
self.prompt(str(e))
else:
self.prompt('successful connection')
def login(self, parms):
if len(parms) != 2:
self.prompt('parms error')
else:
username = parms[0]
password = parms[1]
try:
self.sio.emit('login', {'username': username, 'password': password}, callback=lambda res: self.onlogin(username, res))
except socketio.exceptions.ConnectionError as e:
self.prompt(str(e))
def register(self, parms):
if len(parms) != 2:
self.prompt('parms error')
else:
username = parms[0]
password = parms[1]
try:
self.sio.emit('register', {'username': username, 'password': password}, callback=lambda res: self.onregister(res))
except socketio.exceptions.ConnectionError as e:
self.prompt(str(e))
def logout(self, parms):
if len(parms) != 0:
self.prompt('parms error')
else:
self.sio.emit('logout', '', callback=lambda res: self.onlogout(res))
def sendfile(self, parms):
if len(parms) != 1:
self.prompt('parms error')
else:
filepath = parms[0]
if (not os.path.exists(filepath)) or (not os.path.isfile(filepath)):
self.prompt('can not found file {}'.format(filepath))
else:
self.filepath = filepath
self.file_receiver = self.current_receiver
try:
self.sio.emit('fconnect', {'name': self.user['username'], 'filename': os.path.split(filepath)[0], 'filesize': os.path.getsize(filepath)})
except socketio.exceptions.ConnectionError as e:
self.prompt(str(e))
def prompt(self, prompt):
self.statusbar.other = prompt
self.statusbar.clear()
self.statusbar.refresh()
self.statusbar.content()
self.statusbar.refresh()
def event_listenning(self):
@self.sio.on('connect')
def connect():
pass
@self.sio.on('disconnect')
def disconnect():
pass
@self.sio.on('friend_login')
def friend_login(friend):
self.frdls.friends.append(friend)
self.frdls.content()
self.frdls.refresh()
self.msgls_set[friend] = MsgLs(self.stdscr, self._msgls_info.smaxcol - self._msgls_info.smincol + 1, self._msgls_info.sminrow, self._msgls_info.smincol, self._msgls_info.smaxrow, self._msgls_info.smaxcol, [], receiver=friend)
@self.sio.on('friend_logout')
def friend_logout(friend):
self.frdls.clear()
self.frdls.refresh()
self.frdls.friends.remove(friend)
self.frdls.content()
self.frdls.refresh()
self.msgls_set.pop(friend)
@self.sio.on('user_online')
def user_online(users):
for friend in users['users']:
self.frdls.friends.append(friend)
self.msgls_set[friend] = MsgLs(self.stdscr, self._msgls_info.smaxcol - self._msgls_info.smincol + 1, self._msgls_info.sminrow, self._msgls_info.smincol, self._msgls_info.smaxrow, self._msgls_info.smaxcol, [], receiver=friend)
self.frdls.content()
self.frdls.refresh()
@self.sio.on('message')
def message(msg):
sender = msg['sender']
message = msg['message']
msg = Msg(sender, self.user['username'], message, time.strftime('%H:%M:%S', time.localtime()))
self.msgls_set[sender].add_msg(msg)
if sender == self.current_receiver:
self.msgls_set[self.current_receiver].content()
self.msgls_set[self.current_receiver].move2bottom()
self.msgls_set[self.current_receiver].refresh()
self.prompt("Receive messages from " + sender)
@self.sio.on('connect_ok')
def connect_ok(data):
th = threading.Thread(target=self._sendfile)
th.start()
th.join()
self.prompt('file has sended')
@self.sio.on('fconnect')
def fconnect(data):
self.file_sender = data['name']
self.sio.emit('connect_ok', {'name': self.file_sender})
th = threading.Thread(target=self._recvfile, args=(data['filename'], data['filesize']))
th.start()
th.join()
self.prompt('file has recved')
def onlogin(self, username, res):
if res['code'] == 0 or res['code'] == 1:
self.logged_in = True
self.user['username'] = username
self.prompt(res['text'])
def onmessage(self, res, msg):
if res['code'] == 1:
self.prompt(res['text'])
self.msgls_set[self.current_receiver].add_msg(msg)
self.msgls_set[self.current_receiver].content()
self.msgls_set[self.current_receiver].move2bottom()
self.msgls_set[self.current_receiver].refresh()
else:
self.prompt(res['text'])
def onlogout(self, res):
if res['code'] == 1:
self.logged_in = False
self.init_variable()
self.create_win()
self.draw()
self.refresh()
self.prompt(res['text'])
def onregister(self, res):
self.prompt(res['text'])
def _sendfile(self):
fsio = socketio.Client()
fsio.connect('http://localhost:1234')
@fsio.on('end')
def end(data):
fsio.disconnect()
@fsio.on('recv_ok')
def recv_ok(data):
self.prompt(str(data['has']))
with open(self.filepath, 'rb') as f:
buffer = f.read(10)
block = 1
while buffer:
fsio.emit('send_file', {'block': block, 'data': buffer})
buffer = f.read(10)
block = block + 1
fsio.wait()
def _recvfile(self, filename, filesize):
fsio = socketio.Client()
fsio.connect('http://localhost:1234')
file = {
'recv_bytes': 0,
'file_buffer': []
}
@fsio.on('end')
def end(data):
fsio.disconnect()
@fsio.on('recv_file')
def recv_file(data):
file['recv_bytes'] = file['recv_bytes'] + len(data['data'])
file['file_buffer'].append((data['block'], data['data']))
fsio.emit('recv_ok', {'has': '[{:.2f}%]'.format((file['recv_bytes'] / filesize) * 100)})
self.prompt('[{:.2f}%]'.format((file['recv_bytes'] / filesize) * 100))
if file['recv_bytes'] == filesize:
file['file_buffer'].sort(key=lambda data: data[0])
with open('xxx.txt', 'ab+') as f:
for block in file['file_buffer']:
f.write(block[1])
fsio.emit('end', {})
fsio.wait()