forked from hanseklev/portfolie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
113 lines (96 loc) · 3.47 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
import socket
import utils
from utils import colors
import select
import sys
from User import Bot, User
import time, random
args = utils.getCommandLineArguments(True)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
[ PORT, IP, USERNAME, ishuman, response_limit, free_for_all ] = args
if ishuman:
user = User(USERNAME)
else:
user = Bot(USERNAME)
user.load('bot-data.json')
try:
client.connect((IP, PORT))
client.setblocking(False)
client.send(user.getname().encode())
except socket.error as e:
print(f'{colors.FAIL}Unable to connect..either the server is not running, or you have wrong port/IP' + colors.ENDC)
quit()
else:
print(colors.WARNING + f"\n******* WELCOME {USERNAME} THE BOTINATOR 2000 *******\n" + colors.ENDC)
def formatname(msg):
text = str(msg)
if (text.find(':') == -1):
return text
arr = text.split(":", 2)
return str(f"{colors.FAIL}{arr[0]}: {colors.ENDC}{arr[1]}")
def parsedata(msg):
decoded_msg = msg
try:
decoded_msg = msg.decode()
except (UnicodeDecodeError, AttributeError):
pass
return str(decoded_msg).split(utils.DELIMITER)
def prettifymessage(name, msg):
if name == 'INFO':
return str(f"{colors.WARNING}{msg}"+colors.ENDC)
if name == 'HOST':
return f"{colors.WARNING}{name}{colors.ENDC}: {msg}"
if name == USERNAME:
return f"{colors.BLUE}You{colors.ENDC}: {msg}"
else:
return f"{colors.CYAN}{name}{colors.ENDC}: {msg}"
isrunning = True
response = ''
while isrunning:
inputs = [sys.stdin, client]
read, write, _, = select.select(inputs, [], [], 10)
response_msg = ''
for s in read:
if s is client:
try:
data = s.recv(1024)
except ConnectionResetError:
print(colors.FAIL+"Something bad happened, try to reconnect"+colors.ENDC)
isrunning = False
break
if not data:
isrunning = False
break
else:
parsed_msg = parsedata(data)
if len(parsed_msg) == 2:
[name, msg] = parsed_msg
if name == 'HOST': #Bots only responds to the hosts actions by default
response_msg = msg
print(prettifymessage(name, msg))
else:
if free_for_all and name != 'INFO':
response_msg = msg
print(prettifymessage(name, msg))
if ishuman:
if select.select([sys.stdin,],[],[],0.0)[0]:
response = sys.stdin.readline().strip()
else: # If there is no input the program jumps to the top and checks for input
continue # this way the user is not blocking incoming messages while typing
else:
if response_msg != '':
time.sleep(random.randint(200, 350)/100) # Add some delay to the bot response to prevent mayhem
response = user.respond(response_msg)
if response == '':
continue
print(prettifymessage(USERNAME, response))
else:
continue
if response == 'bye':
client.send(response.encode())
client.shutdown(socket.SHUT_RDWR)
else:
client.send(response.encode())
client.close()
print('Closing app..')
quit()