/
client.py
158 lines (136 loc) · 4.5 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
import argparse
import ctypes
import os
import socket
import threading
import time
import unicurses as u
parser = argparse.ArgumentParser(description="Open a client connected to an ip.")
parser.add_argument('host', default='localhost', nargs='?', help='server host address')
parser.add_argument('port', default='80', type=int, nargs='?', help='server host post')
parser.add_argument('-c', '--channel', default='lobby')
parser.add_argument('-u', '--user', default='anon')
args = parser.parse_args()
kill_thread = False
HOST, PORT = args.host, args.port
channel = args.channel
os.system("mode con: cols=120 lines=30")
m_to_display = 22
def draw_boxes():
u.box(chat_win)
u.box(input_win)
u.box(people_win)
def reset():
u.wclear(chat_win)
u.wclear(input_win)
u.wclear(people_win)
def refresh():
draw_boxes()
u.wrefresh(chat_win)
u.wrefresh(input_win)
u.wrefresh(people_win)
def get_latest(l):
if len(l) < m_to_display:
return l
else:
return l[-m_to_display:]
def get_messages():
old_messages = ''
while True:
if kill_thread:
break
time.sleep(0.05)
while True:
try:
sock = socket.socket()
sock.connect((HOST, PORT))
sock.sendall(bytes(channel + chr(0) + chr(2), "utf-8"))
recvd = sock.recv(1024).decode().split('||')
count = recvd[0]
users = recvd[1].split('|')
ctypes.windll.kernel32.SetConsoleTitleA(bytes(channel + ": " + str(count) + " user(s) online", "utf-8"))
u.wclear(people_win)
u.box(people_win)
i = 1
u.mvwaddstr(people_win, i, 1, ' Users in #%s:' % channel)
for user in users:
i += 1
u.mvwaddstr(people_win, i, 1, ' - ' + user)
u.wrefresh(people_win)
sock = socket.socket()
sock.connect((HOST, PORT))
break
except (ConnectionRefusedError, ConnectionResetError):
u.mvwaddstr(chat_win, 1, 1, "Can't connect.")
time.sleep(1)
sock.sendall(bytes(channel + chr(0) + chr(1), "utf-8"))
incoming_messages = str(sock.recv(4096), "utf-8")
if not incoming_messages == old_messages:
i = 0
u.wclear(chat_win)
draw_boxes()
for message in get_latest(incoming_messages.split('\n')):
i += 1
u.mvwaddstr(chat_win, i, 1, message[:78])
u.wrefresh(chat_win)
old_messages = incoming_messages
time.sleep(0.1)
message_refresh_thread = threading.Thread(target=get_messages)
err = []
sock = socket.socket()
while True:
try:
sock.connect((HOST, PORT))
except WindowsError:
print("Can't connect: " + HOST + ':' + str(PORT))
continue
break
sock.sendall(bytes(channel + chr(0) + chr(3) + chr(0) + args.user, "utf-8"))
stdscr = u.initscr()
u.curs_set(0)
chat_win = u.newwin(25, 90, 0, 0)
input_win = u.newwin(5, 90, 25, 0)
people_win = u.newwin(30, 30, 0, 90)
message_refresh_thread.start()
while True:
sock = socket.socket()
u.wclear(input_win)
u.mvwaddstr(input_win, 1, 1, '> ')
refresh()
u.flushinp()
data = u.mvwgetstr(input_win, 1, 3)
send = True
# if len(data.split()) > 1:
# if data.split()[0] == ":send":
# file = data.split()[1]
# if os.path.exists(file) and not os.path.isdir(file):
# bdata = open(file, 'rb').read()
# sock.connect((HOST, PORT))
# sock.sendall(bytes(channel + chr(0) + chr(5) + chr(0) + file + chr(0) + str(bdata) + "\n", "utf-8"))
# sock.close()
# send = False
if data in [":exit", "^C", ":quit", ":q"]:
break
if send:
try:
sock.connect((HOST, PORT))
sock.sendall(bytes(channel + chr(0) + data + "\n", "utf-8"))
except (ConnectionRefusedError, ConnectionResetError):
err.append("Can't connect.")
finally:
sock.close()
if len(data.split()) > 1:
if data.split()[0] == ":join":
try:
channel = data.split()[1]
except IndexError:
pass
refresh()
sock = socket.socket()
sock.connect((HOST, PORT))
sock.sendall(bytes(channel + chr(0) + chr(4) + "\n", "utf-8"))
u.endwin()
# noinspection PyRedeclaration
kill_thread = True
time.sleep(0.1)
os.system('cls')