-
Notifications
You must be signed in to change notification settings - Fork 1
/
generic_bot.py
221 lines (180 loc) · 6.24 KB
/
generic_bot.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
#
# Copyright (C) 2017 Polshakov Dmitry (Diadlo) <polsha3@gmail.com>
# All Rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
import inspect
import pickle
import random
import sys
from pytox import Tox
from time import sleep
from os.path import exists
class ToxServer():
def __init__(self, ip, port, pk):
self.ip = ip
self.port = port
self.pk = pk
class ToxOptions():
def __init__(self):
self.ipv6_enabled = True
self.udp_enabled = True
self.proxy_type = 0 # 1=http, 2=socks
self.proxy_host = ''
self.proxy_port = 0
self.start_port = 0
self.end_port = 0
self.tcp_port = 0
self.savedata_type = 0 # 1=toxsave, 2=secretkey
self.savedata_data = b''
self.savedata_length = 0
def load_profile(self, profile):
self.savedata_data = open(profile, 'rb').read()
self.savedata_length = len(self.savedata_data)
self.savedata_type = Tox.SAVEDATA_TYPE_TOX_SAVE
class CommandInfo():
def __init__(self, object, func):
# Remove 'cmd_'
self.name = func[4:]
cmd = getattr(object, func)
doc = cmd.__doc__
if doc is None:
doc = ''
temp = doc.split(' ', 1)
try:
self._order = int(temp[0])
self.doc = temp[1].strip()
except:
self._order = 0
self.doc = doc.strip()
# Skip 'self' and 'friendId'
self.vars = inspect.getargspec(cmd)[0][2:]
def order(self):
return self._order
def __str__(self):
vars = ''
for v in self.vars:
vars += '<%s> ' % v
return '%s %s: %s' % (self.name, vars, self.doc)
class GenericBot(Tox):
def __init__(self, name, profile, servers, config_name, opts=None):
if opts is not None:
super(GenericBot, self).__init__(opts)
self.profile = profile
self.servers = servers
self.to_save = []
self.config_name = config_name
self.self_set_name(name)
self.self_set_status_message("Send me the message 'help' for full"
" list of commands")
self.connect()
def __enter__(self):
self.load_settings(self.config_name)
return self
def __exit__(self, type, value, traceback):
self.save_settings(self.config_name)
def save_settings(self, conf):
with open(conf, 'wb') as f:
for save in self.to_save:
pickle.dump(getattr(self, save), f)
def load_settings(self, conf):
if not exists(conf):
return
with open(conf, 'rb') as f:
for save in self.to_save:
try:
setattr(self, save, pickle.load(f))
except:
pass
def save_profile(self):
with open(self.profile, 'wb') as f:
f.write(self.get_savedata())
def connect(self):
server = random.choice(self.servers)
self.bootstrap(server.ip, server.port, server.pk)
def start(self):
checked = False
self.save_profile()
try:
while True:
status = self.self_get_connection_status()
if not checked and status:
print('Connected to DHT.')
checked = True
if checked and not status:
print('Disconnected from DHT.')
self.connect()
checked = False
self.iterate()
sleep(0.01)
except KeyboardInterrupt:
self.save_profile()
def on_friend_request(self, pk, message):
self.friend_add_norequest(pk)
self.save_profile()
def on_conference_invite(self, friendId, type, cookie):
self.conference_join(friendId, cookie)
def answer(self, friendId, text):
self.friend_send_message(friendId, Tox.MESSAGE_TYPE_NORMAL, text)
def ganswer(self, groupId, text):
self.conference_send_message(groupId, Tox.MESSAGE_TYPE_NORMAL, text)
def cmd_help(self, friendId):
'''00 Print this text '''
functions = filter(lambda s: s.startswith('cmd_'), dir(self))
commands = []
for f in functions:
commands.append(CommandInfo(self, f))
commands.sort(key=CommandInfo.order)
text = 'Usage:\n'
for cmd in commands:
text += ' %s\n' % str(cmd)
self.answer(friendId, text)
def handle_gcommand(self, groupId, message):
''' Handle command in group chat '''
temp = message.strip().split(' ')
name = temp[0]
params = temp[1:]
if name[0] != '!':
return
name = name[1:]
try:
method = getattr(self, 'gcmd_' + name)
except AttributeError as e:
return
try:
method(groupId, *params)
except Exception as e:
print(str(e))
def handle_command(self, friendId, message):
''' Handle command in privat chat '''
temp = message.split(' ')
name = temp[0]
params = temp[1:]
try:
method = getattr(self, 'cmd_' + name)
except AttributeError:
try:
self.answer(friendId, '%s is unsupported command' % name)
self.cmd_help(friendId)
except Exception as e:
print(str(e))
finally:
return
try:
method(friendId, *params)
except Exception as e:
error = 'Error while handle %s (%s)' % (name, str(e))
self.answer(friendId, error)