-
Notifications
You must be signed in to change notification settings - Fork 1
/
sjBot.py
318 lines (251 loc) · 9.34 KB
/
sjBot.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
#!/usr/bin/env python3
import datetime
import time
import imp
import json
import os
import sys
import urllib.request
import inspect
import simple_ui
import threads
import base
commands = {}
plugins = {}
realpath = os.path.dirname(os.path.realpath(__file__))
settings = {}
connections = {}
def ui_message(ui, message):
network = ui.network
channel = ui.channel
if message.startswith('/refresh'):
ui.refresh_all()
return None
if message.startswith('/join'):
connections[ui.network].join(message.split(' ')[1])
return None
if message.startswith('/channel'):
ui.channel = message.split(' ')[1]
return None
if message.startswith('/server'):
ui.network = message.split(' ')[1]
return None
# I would rather use the real value None but for some reason
# it was not working here. So it's a string 'None'.
if network == 'None':
return
if channel != 'None':
message = 'PRIVMSG {} :{}'.format(channel, message)
connection = connections[network]
connection.send(message)
return None
def r_PING(con, *params):
con.send('PONG {}'.format(params[0]))
return None
def r_422(con, *params):
channels = settings['connections'][con.network]['channels']
for channel in channels:
con.join(channel)
return None
def r_433(con, *junk):
con.set_nickname(con.nickname + '_')
return None
def r_376(con, *junk):
info = settings['connections'][con.network]
con.privmsg('NickServ', 'Identify {} {}'.format(info['nickname'],
info['nickserv_password']))
return None
def r_396(con, *junk):
channels = settings['connections'][con.network]['channels']
for channel in channels:
con.join(channel)
return None
def r_ERROR(con, *information):
lethal = ['Closing Link', 'Ping Timeout']
if any(err_type in ' '.join(information) for err_type in lethal):
connected = con.reconnect()
if not connected:
con.close_connection()
return None
def r_ALL(con, *message):
for plugin in plugins:
source = plugins[plugin]
params = list(message)
try:
if hasattr(source, 'r_' + message[0]):
getattr(source, 'r_' + message[0])(con, globals(),
*message[1:])
if hasattr(source, 'r_' + message[1]):
getattr(source, 'r_' + message[1])(con, globals(),
*[params[0]] + params[2:])
except Exception as e:
print('Error {} in plugin {}'.format(e, plugin))
return None
@threads.asthread()
def r_PRIVMSG(con, user, channel, *message):
triggers = settings['connections'][con.network]['trigger']
if '__botname__' in triggers:
triggers[con.nickname] = triggers['__botname__']
del triggers['__botname__']
if channel not in triggers:
trigger = triggers['default']
else:
trigger = triggers[channel]
if len(' '.join(message)) == len(trigger) or ' '.join(message)[len(trigger)] == ' ' or len(' '.join(message)) == len(trigger)-1:
return None
print(' '.join(message)[len(trigger)])
if channel in settings['connections'][con.network]['ignore_suffix'] and ' '.join(message)[len(trigger)] in settings['connections'][con.network]['ignore_suffix'][channel]:
return
if channel == con.nickname:
channel = user.split('!')[0][1:]
m_string = ' '.join(message)[1:]
for t in trigger:
if m_string.startswith(t):
cmd_string = m_string[len(t):]
command = cmd_string.split(' ')[0]
if len(cmd_string.split(' ')) > 1:
params = [x for x in cmd_string.split(' ')[1:] if x != '']
else:
params = []
command_list = build_commandlist(channel, con.network, user)
for cmd in command_list:
aliases = getattr(command_list[cmd], 'aliases', None)
if aliases is None:
continue
for alias in aliases:
if alias == command:
command = cmd
if command not in command_list:
if channel in settings['connections'][con.network]['default_commands']:
command = settings['connections'][con.network]['default_commands'][channel]
func = getattr(command_list[command], command)
params = [x for x in cmd_string.split(' ')]
else:
con.privmsg(channel, '\x034Could not find that command.\x03')
return None
else:
func = getattr(command_list[command], command)
sig = inspect.signature(func)
prms = sig.parameters
optional = 0
required = 0
for i, p in enumerate(prms):
if i < 6:
continue
if (prms[p].default == prms[p].empty and
prms[p].kind != prms[p].VAR_POSITIONAL):
required += 1
else:
optional += 1
if (len(params) > (required+optional) and not any(prms[p].kind ==
prms[p].VAR_POSITIONAL for p in prms) or
len(params) < required):
output = ('Invalid params: Command "{}{}" has {} required '
'params and {} optional params. You gave {} '
'params.').format(t, command, required, optional,
len(params))
else:
try:
output = func(con, globals(), command_list, t, user,
channel, *params)
except Exception as error:
output = str(error)
if output is None:
return None
if isinstance(output, list):
for line in output:
con.privmsg(channel, line)
else:
con.privmsg(channel, output)
return None
def build_commandlist(channel, network, host):
cmdlist = {}
for cl in commands:
for ch in cl.split(','):
if ch == '__all__':
for c in commands[cl]:
cmdlist[c] = commands[cl][c]
owners = settings['connections'][network]['owners']
if ch == 'owner' and any(o in host for o in owners):
for c in commands[cl]:
cmdlist[c] = commands[cl][c]
if not ch.startswith('!') and ch == channel or not channel.startswith('#'):
for cmd in commands[cl]:
cmdlist[cmd] = commands[cl][cmd]
if ch.startswith('!') and ch[1:] != channel or not channel.startswith('#'):
for cmd in commands[cl]:
cmdlist[cmd] = commands[cl][cmd]
return cmdlist
def load_commands():
global commands
channels = os.listdir('commands/')
for channel in channels:
if not os.path.isdir('commands/{}'.format(channel)):
continue
files = os.listdir('commands/{}'.format(channel))
commands[channel] = {}
for f in files:
if not f.endswith('.py'):
continue
if os.path.isfile('commands/{}/{}'.format(channel, f)):
name = f.replace('.py', '')
src = imp.load_source('{}_{}'.format(channel, name),
'commands/{}/{}'.format(channel, f))
commands[channel][name] = src
return None
def load_plugins():
global plugins
files = os.listdir('plugins/')
for filename in files:
if not filename.endswith('.py'):
continue
name = filename.split('.py')[0]
src = imp.load_source('plugin_{}'.format(filename), 'plugins/{}'.format(filename))
plugins[filename] = src
return None
def load_settings():
global settings
with open('settings.json') as sfile:
settings = json.loads(sfile.read())
return None
@threads.asthread()
def reload_loop(delay=60):
while True:
load_commands()
load_settings()
load_plugins()
time.sleep(delay)
return None
def url_download(url):
request = urllib.request.urlopen(url)
response = request.read()
try:
decoded = response.decode('utf-8')
except UnicodeDecodeError:
return None
return decoded
def main():
global connections
load_commands()
load_settings()
load_plugins()
reload_loop()
for connection in settings['connections']:
# Checks if the user has specified to run the connection.
if not settings['connections'][connection]['running']:
continue
irc_connection = base.Connection(connection, settings['connections']
[connection]['port'])
irc_connection.set_ident('sjBot')
irc_connection.set_handler(base.handler, globals())
#rc_connection.display_func = interface.display
connected = irc_connection.connect()
if connected:
connections[irc_connection.network] = irc_connection
irc_connection.identify()
irc_connection.receive_loop()
return None
if __name__ == '__main__':
#interface = simple_ui.Interface(ui_message)
#interface.handle_input()
main()