-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkiwibot.py
215 lines (180 loc) · 5.81 KB
/
kiwibot.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
import logging
import os
import datetime
import socket
import json
import time
import re
## GLOBAL DEBUG FLAG ##
debug_mode = False
#######################
# logging configuration
def configure_logger():
logfile_path = 'logs'
if not os.path.exists(logfile_path):
os.makedirs(logfile_path)
logfile_timestamp = int(datetime.datetime.now().timestamp())
logfile_name = f'{os.path.splitext(os.path.basename(__file__))[0]}_{logfile_timestamp}.log'
logging.basicConfig(
filename=f'{logfile_path}/{logfile_name}',
encoding='utf-8',
level=logging.DEBUG,
format='%(asctime)s [%(levelname)s] %(message)s',
datefmt='%m-%d-%Y %H:%M:%S%z'
)
logging.info(f'START')
def prettyPrint(msg):
print(f'[>] {msg}')
logging.info(msg)
def stayAlive(socket, action):
if action == 'jiggle':
sendMsg(socket, '>')
time.sleep(1)
sendMsg(socket, '<')
def readMsg(socket):
message = b''
while True:
data = socket.recv(1)
if not data:
pass
if data == b'\n':
return message
message += data
def sendMsg(socket, msg):
print_exclusions = [
'>',
'<',
'm 1',
'm 3',
'm 7',
'm 9',
'lie',
'sit',
'stand'
]
if type(msg) == str:
if msg not in print_exclusions:
print(f'[SEND] {msg}')
logging.info(msg)
msg = msg.encode('iso-8859-1')
socket.send(msg + b'\n')
def parseWhispers(socket, string):
quitting = False
whisperer = re.search(r'\<name[^\>]+\>([^\<]+)\<\/name\>', string)
whisperer = whisperer.group(1)
message = re.search(r'\"([^\"]+)\"', string)
message = message.group(1)
formatted = f'{whisperer} (whisper) {message}'
prettyPrint(formatted)
if whisperer == owner:
cmd = re.match('cmd\:(.*)', message)
if cmd:
cmd_string = cmd.group(1)
if cmd_string == 'quit':
sendMsg(socket, f'\"Disconnecting...')
time.sleep(5)
quitting = True
if quitting:
sendMsg(socket, cmd_string)
socket.close()
else:
sendMsg(socket, cmd_string)
move = re.match('move\:(.*)', message)
if move:
move_string = move.group(1).split(',')
for m in move_string:
sendMsg(socket, m)
time.sleep(0.75)
say = re.match('say:(.*)', message)
if say:
say_string = say.group(1)
say_string = f'\"{say_string}'
sendMsg(socket, say_string)
return
def parseSaying(socket, string):
saying = re.search(r'\(\<name\sshortname\=\'[^\']+\'\>([^\<]+)\<\/name\>\:\s(.*)', string)
sayer = saying.group(1)
said = saying.group(2)
formatted = f'{sayer}: {said}'
prettyPrint(formatted)
def parseEmotes(socket, string):
emote = re.search(r'\(\<font\scolor\=\'emote\'\>\<name\sshortname\=\'[^\']+\'\>([^\<]+)\<\/name\>\s(.*)\<\/font\>', string)
emoter = emote.group(1)
emoted = emote.group(2)
formatted = f'{emoter} {emoted}'
prettyPrint(formatted)
def removeTags(string):
tag_re = re.compile(r'<[^>]+>')
cleaned_string = tag_re.sub('', string)
return cleaned_string
def removeParen(string):
paren_re = re.compile(r'\(')
cleaned_string = paren_re.sub('', string)
return cleaned_string
def parseFurc(socket, msg):
if msg == 'Dragonroar':
sendMsg(socket, f'account {email} {character} {password}')
sendMsg(socket, f'color {colors}')
sendMsg(socket, f'desc {desc}')
if msg == '&&&&&&&&&&&&&':
sendMsg(socket, 'vascodagama')
# SUMMONING - would need to figure out to format the owner name appropriately; this could also be hardcoded.
# summoned = re.compile(r'\(<font color=\'query\'><name shortname=\'owner_name\'>owner_name<\/name> asks you to join their company.*')
# if summoned.match(msg):
# sendMsg(socket, 'join')
load_dream = re.compile(r'^]q')
if load_dream.match(msg):
sendMsg(socket, 'vascodagama')
whispers = re.compile(r'\(<font color=\'whisper\'>')
if whispers.match(msg):
parseWhispers(socket, msg)
emotes = re.compile(r'\(<font color=\'emote\'>')
if emotes.match(msg):
parseEmotes(socket, msg)
saying = re.compile(r'\(\<name\sshortname\=\'[^\']+\'\>[^\<]+\<\/name\>\:\s(.*)')
if saying.match(msg):
parseSaying(socket, msg)
configure_logger()
# load configuration
conf = open('bot.conf', "r")
conf = json.load(conf)
if debug_mode:
logging.debug(conf)
# global declarations
hostname = conf['connection'][0]['server']
port = conf['connection'][0]['port']
app_name = os.path.splitext(os.path.basename(__file__))[0]
app_vers = '0.6.0-alpha'
# account info
email = conf['account'][0]['email']
character = conf['account'][0]['character']
password = conf['account'][0]['password']
colors = conf['account'][0]['colors']
desc = conf['account'][0]['desc']
owner = conf['account'][0]['owner']
# append desc with script info
desc = desc + f' [{app_name} {app_vers}]'
# socket declarations
furc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
furc.connect((hostname, port))
connected = True
logging.info(f'Connected to {hostname}:{port}')
except:
logging.error(f'Failed connection to {hostname}:{port}')
t0 = time.time()
while furc._closed == False:
t1 = time.time()
if (t1 - t0) >= 300.0:
stayAlive(furc, 'jiggle')
t0 = t1
try:
msg = readMsg(furc).decode('iso-8859-1')
if debug_mode:
print(f'[>] {msg}')
parseFurc(furc, msg)
except TimeoutError:
furc.close()
logging.warning(f'Connection has timed out.')
if furc._closed == True:
logging.info(f'Connection has closed.')