-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaib.py
588 lines (478 loc) · 21 KB
/
yaib.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/env python
"""
YAIB - Yet Another IRC Bot
https://github.com/collingreen/yaib
Collin "Keeyai" Green - collingreen.com
You can see the original yaib in #ludumdare on irc.afternet.org.
The official yaib channel is #yaib on irc.afternet.org.
Yaib is built using Twisted:
http://twistedmatrix.com/documents/14.0.2/api/twisted.words.protocols.irc.IRCClient.html # NOQA
"""
import sys
import os
import time
import imp
import json
import logging
import traceback
from pubsub import pub
from tools import util
from modules import settings, connections, persistence
from modules.admin.admin_manager import AdminManager
CONFIG_FILE_PATH = 'config.json'
PRIVATE_CONFIG_FILE_PATH = 'private_config.json'
# add path to make importing plugins work
sys.path.insert(
0,
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
)
# TODO: configure from command line or config.json
logging.basicConfig(level=logging.DEBUG)
class Yaib(object):
def __init__(self, *args, **kwargs):
# TODO: support multiple IRC connections at once
self.channels = []
self.plugins = []
self.shutup_until = None
self.DONT_NOTIFY_PLUGINS_FLAG = '**does_not_notify_plugins**'
# load configuration
try:
with open(CONFIG_FILE_PATH, 'r') as f:
config_content = f.read()
except: # TODO: Catch the real exceptions here
logging.error(
"Could not open configuration file {}. Quitting.".format(
CONFIG_FILE_PATH
)
)
sys.exit(1)
config = {}
try:
config = json.loads(config_content)
logging.info("Loaded configuration from %s" % CONFIG_FILE_PATH)
except ValueError as e:
logging.error(
"Could not load configuration file {}. Exiting. {}".format(
CONFIG_FILE_PATH,
repr(e)
)
)
sys.exit(1)
# try loading private config file
private_config_content = ''
try:
with open(PRIVATE_CONFIG_FILE_PATH, 'r') as private_config_file:
private_config_content = private_config_file.read()
except: # TODO: catch real exceptions
logging.warning("Could not open private config file {}.".format(
PRIVATE_CONFIG_FILE_PATH
))
private_config = {}
try:
private_config = json.loads(private_config_content)
logging.info(
'Loaded private configuration from {}'.format(
PRIVATE_CONFIG_FILE_PATH
)
)
except ValueError as e:
logging.error(
'Could not parse private config file {}.'.format(
PRIVATE_CONFIG_FILE_PATH,
)
)
# merge private and public config and convert to object
config.update(private_config)
self.config = util.dictToObject(config)
# get required fields from config
self.command_prefix = self.config.connection.command_prefix
self.nick = self.config.nick
# load settings module based on configuration
# TODO: move this config check to settings module itself
if self.config.settings.module == 'json':
self.settings = settings.json(self.config)
self.settings.loadSettings()
else:
logging.error(
"Unsupported settings module {}. Exiting.".format(
self.config.settings.module
)
)
sys.exit(1)
# load admin module based on configuration
self.adminManager = AdminManager(self.config)
# subscribe to events
self.subscribeToEvents()
# ensure default settings are all available
self.createDefaultSettings()
# create persistence layer
self.persistence = persistence.default(self.config)
# load plugins
self.loadPlugins()
def subscribeToEvents(self):
# subscribe to events from the server connections
pub.subscribe(self.onConnected, 'connection:connected')
pub.subscribe(self.onMessageOfTheDay, 'connection:messageOfTheDay')
pub.subscribe(self.onUserAction, 'connection:userAction')
pub.subscribe(self.onNotification, 'connection:notification')
pub.subscribe(self.onPrivateMessage, 'connection:privateMessage')
pub.subscribe(self.onDirectMessage, 'connection:directMessage')
pub.subscribe(self.onCommand, 'connection:command')
pub.subscribe(self.onMessage, 'connection:message')
pub.subscribe(self.onJoined, 'connection:joined')
pub.subscribe(self.onLeave, 'connection:left')
pub.subscribe(self.onKicked, 'connection:kicked')
pub.subscribe(self.onTopicChanged, 'connection:topicChanged')
pub.subscribe(self.onUserJoined, 'connection:userJoined')
pub.subscribe(self.onUserLeft, 'connection:userLeft')
pub.subscribe(self.onUserQuit, 'connection:userQuit')
pub.subscribe(self.onUserKicked, 'connection:userKicked')
pub.subscribe(self.onUserRenamed, 'connection:userRenamed')
pub.subscribe(self.onUserList, 'connection:userList')
pub.subscribe(self.onPong, 'connection:pong')
pub.subscribe(self.onIRCUnknown, 'connection:IRCUnknown')
def start(self):
"""
Called after initialization. Connects to the servers in the settings.
"""
# create a connection
connection = connections.irc
self.connection_factory = connection.connectToServer(
self.config.connection,
self.nick
)
connection.start()
def createDefaultSettings(self):
"""Ensure the default settings exist from the config file."""
# TODO: just load everything from config without explicitly listing
if isinstance(self.config.default_channels, list):
default_channels = self.config.default_channels
else:
default_channels = [
c.strip() for c in
self.config.default_channels.split(',')
]
self.settings.setMulti({
'nick': self.config.nick,
'default_channels': default_channels,
'shutup_duration': 30
},
initial=True
)
def loadPlugins(self):
"""
Loads plugins from the plugins folder. Plugins
should be in folders with the same name as their main
script and expose a class Plugin that inherits from
baseplugin. Example: plugins/baseplugin.py
"""
logging.info("loading plugins")
# load each plugin and put in self.plugins
self.plugins = []
for path in os.listdir(self.config.plugins.root):
self.loadPlugin(path)
# notify listeners that the plugins have finished loading
pub.sendMessage('core:pluginsLoaded')
self.callInPlugins('onPluginsLoaded')
def loadPlugin(self, path):
"""Load a plugin from the given path"""
logging.debug("looking for plugin in %s" % path)
# if path is a folder
if os.path.isdir(os.path.join(self.config.plugins.root, path)):
# if found plugin in folder
plugin_file_path = os.path.join(
self.config.plugins.root, path, "%s.py" % path
)
if os.path.isfile(plugin_file_path):
try:
# try to import it
logging.debug("- importing plugin %s module" % path)
plugin_module = imp.load_source(
path,
plugin_file_path
)
logging.debug("Imported plugin module %s" % path)
# TODO: change to only catch import errors here
except Exception as e:
logging.error(
"Error importing plugin %s: %s" % (path, repr(e))
)
traceback.print_exc()
return False
if hasattr(plugin_module, 'Plugin'):
try:
logging.debug("Creating plugin")
plugin = plugin_module.Plugin(
self,
self.config
)
logging.debug("Loaded plugin %s" % plugin.name)
# loading external code - catch all exceptions
except Exception as e:
logging.error("Error loading plugin %s: %s" % (
plugin_file_path, repr(e))
)
return False
# TODO: what does the comment below mean? does this work?
# this probably doesnt work singly bc it sticks the same
# plugin on the end of the list
for p in self.plugins:
if p.name == plugin.name:
self.plugins.remove(p)
self.plugins.append(plugin)
return True
return False
def getPluginSettings(self, pluginName):
"""Create a wrapper around the settings object that namespaces
the getters and setters based on the plugin name."""
yaibSettings = self.settings
class SettingsWrapper(object):
def getKey(self, key):
return '%s.%s' % (pluginName, key)
def set(self, key, *args, **kwargs):
return yaibSettings.set(self.getKey(key), *args, **kwargs)
def setMulti(self, settingsDict, initial=False):
settingsDict = dict([
(self.getKey(k), v) for k, v in settingsDict.items()
])
return yaibSettings.setMulti(settingsDict, initial=initial)
def get(self, key, default=None):
return yaibSettings.get(self.getKey(key), default=default)
def getMulti(self, keys, default=None):
keys = [self.getKey(k) for k in keys]
return yaibSettings.getMulti(keys, default=default)
return SettingsWrapper()
def callInPlugins(self, command, *args, **kwargs):
for p in self.plugins:
if hasattr(p, command):
try:
getattr(p, command)(*args, **kwargs)
# running plugin command - catch everything
except Exception as e:
logging.error(
"Exception running {} in plugin {}: {}".format(
command,
p.name,
repr(e)
)
)
def formatDoc(self, message):
"""
Formats the given message by replacing {nick} and {command_prefix}
with their current values and stripping any control flags."""
doc = message.replace(self.DONT_NOTIFY_PLUGINS_FLAG, '')
return doc.format(
nick=self.nick,
command_prefix=self.command_prefix
)
def callLater(self, delay, func, *args, **kwargs):
"""Wait for the given delay then call the function with the args."""
self.server_connection.callLater(delay, func, *args, **kwargs)
# connection functionality
def onConnected(self, connection):
# save the active server connection
# (refactor when supporting multiple servers)
self.server_connection = connection
# set nick
self.setNick(self.settings.get('nick'))
# join default channels
default_channels = self.settings.get('default_channels')
for channel in default_channels:
self.server_connection.join(str(channel))
# call in plugins
self.callInPlugins('onConnected')
def onMessageOfTheDay(self, message):
self.callInPlugins('onMessageOfTheDay', message)
def onNotification(self, user, nick, channel, message):
self.callInPlugins('onNotification', user, nick, channel, message)
def onUserAction(self, user, nick, channel, action):
self.callInPlugins('onUserAction', user, nick, channel, action)
def onPrivateMessage(self, user, nick, message):
# split it
command, x, more = message.lstrip(' ').partition(' ')
# strip command prefix if there is one
if command.startswith(self.command_prefix):
command = command[len(self.command_prefix):]
# try to call this like a command
result = self.findAndCall(command, user, nick, self.nick, more)
# didnt find command, pass on to plugins
if result is None:
self.callInPlugins('onPrivateMessage', user, nick, message)
def onDirectMessage(self, user, nick, channel, message):
# remove nick from front
processed = message[len(self.nick):]
processed = processed.lstrip(
self.config.connection.nick_command_delimiters
)
# split it into command name and the arguments
command, x, more = processed.partition(' ')
# check if first word is a command
found = self.findAndCall(command, user, nick, channel, more)
if not found:
self.callInPlugins(
'onMessage', user, nick, channel, message, highlight=True
)
def onMessage(self, user, nick, channel, message, highlight):
self.callInPlugins(
'onMessage',
user,
nick,
channel,
message,
highlight
)
def onCommand(self, user, nick, channel, command, more):
found = self.findAndCall(command, user, nick, channel, more)
if not found:
self.callInPlugins(
'onMessage', user, nick, channel, more, highlight=True
)
def findAndCall(self, command, user, nick, channel, more):
"""Searches for the specified command and calls it if possible. Returns
False if didn't have permission, None if not found, True if found and
executed. If multiple plugins provide the same command, whichever one
is found first will be executed."""
searchables = [self] + self.plugins
for searchable in searchables:
func, is_admin_command = \
self.findCommand(searchable, user, nick, channel, command)
# found it, execute it
if func:
command_event_name = 'onCommand'
func(user, nick, channel, more)
# if admin command, publish and notify plugins
if is_admin_command:
command_event_name = 'onAdminCommand'
pub.sendMessage(
'core:adminCommand',
user=user,
nick=nick,
channel=channel,
command=command,
more=more
)
# special cases not to send to plugins
if (not func.__doc__ or
self.DONT_NOTIFY_PLUGINS_FLAG not in func.__doc__):
self.callInPlugins(
command_event_name, user, nick, channel, command, more
)
return True
# never found it or didn't have permission, return None
return None
def findCommand(self, obj, user, nick, channel, command):
# look for admin command if user is admin
if self.isAdmin(user, nick):
f = 'admin_%s' % command
if hasattr(obj, f):
return getattr(obj, f), True
# look for op command if user is op
if self.isOp(nick, channel):
f = 'op_%s' % command
if hasattr(obj, f):
return getattr(obj, f), False
# look for regular handler for this command - None otherwise
func = getattr(obj, 'command_' + command, None)
return func, False
# TODO: implement this correctly
def quit(self):
# shutdown all the plugins
self.callInPlugins('onShutdown')
# shutdown all the modules
pub.sendMessage('core:shutdown')
# save settings
self.settings.saveSettings()
# TODO: support multiple connections
self.server_connection.quit()
def setNick(self, nick):
old_nick = self.nick if hasattr(self, 'nick') else ''
# TODO: support multiple connections
if hasattr(self, 'server_connection'):
self.server_connection.setNick(nick)
self.nick = nick
self.callInPlugins('onNickChange', nick, old_nick)
def sendMessage(self, channel, message):
"""
Sends a message to the specified channel on the current
server connection.
"""
# do nothing if in 'shutup' mode
if self.shutup_until and time.time() < self.shutup_until:
return False
# TODO: support multiple connections
self.server_connection.sendMessage(channel, message)
# send to plugins
self.callInPlugins('onSend', channel, message)
def action(self, channel, action):
"""Sends an action in the specified channel (or nick!)."""
self.server_connection.describe(channel, action)
self.callInPlugins('onAction', channel, action)
def isAdmin(self, user, nick):
"""Returns true if the user is currently an admin."""
return self.adminManager.isAdmin(user, nick)
def isOp(self, nick, channel):
# TODO: implement this
return False
# IRC Commands
def onJoined(self, channel):
logging.info("Joined %s" % channel)
# add to list of channels
self.channels.append(channel)
# add to settings
channels = self.settings.get('default_channels')
if channel not in channels:
channels.append(channel)
self.settings.set('default_channels', channels)
# notify plugins
self.callInPlugins('onJoined', channel)
def onLeave(self, channel):
logging.info("Left %s" % channel)
self.channels.remove(channel)
# remove from settings
channels = self.settings.get('default_channels')
if channel in channels:
channels.remove(channel)
self.settings.set('default_channels', channels)
# call in plugins
self.callInPlugins('onLeave', channel)
def onKicked(self, kicker_user, kicker, channel, message):
"""Called when kicked from a channel."""
self.onLeave(channel)
self.callInPlugins('onKicked', kicker_user, kicker, channel, message)
def onTopicChanged(self, user, nick, channel, topic):
self.callInPlugins('onTopicChanged', user, nick, channel, topic)
def onUserJoined(self, user, nick, channel):
"""Called when another user joins the channel"""
self.callInPlugins('onUserJoined', user, nick, channel)
def onUserLeft(self, user, nick, channel):
"""Called when another user leaves the channel"""
self.callInPlugins('onUserLeft', user, nick, channel)
def onUserQuit(self, user, nick, quitMessage):
"""Called when another user disconnections from the server."""
self.callInPlugins('onUserQuit', user, nick, quitMessage)
def onUserKicked(self, kickee, channel, kicker_user, kicker, message):
"""Called when a user is kicked from the channel"""
# TODO: onUserLeft expects user, nick channel
# self.onUserLeft(kickee, channel)
self.callInPlugins(
'onUserKicked', kickee, channel, kicker_user, kicker, message
)
def onUserRenamed(self, user, old_nick, new_nick):
"""
Called when another user changes their nick from old_nick to new_nick.
NOTE: User is the old user string before the chaneg.
"""
if old_nick != self.settings.get('nick'):
self.callInPlugins('onUserRenamed', user, old_nick, new_nick)
def onUserList(self, channel_type, channel, user_list):
self.callInPlugins('onUserList', channel_type, channel, user_list)
def onPong(self, user, nick, channel, seconds):
self.sendMessage(
channel,
"%s pong! Round trip time: %.2f seconds" % (nick, seconds)
)
def onIRCUnknown(self, prefix, command, params):
self.callInPlugins('irc_%s' % command, params)
if __name__ == '__main__':
yaib = Yaib()
yaib.start()