forked from CloudBotIRC/CloudBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
710 lines (575 loc) · 24.1 KB
/
plugin.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
import asyncio
import glob
import importlib
import inspect
import logging
import os
import re
import sqlalchemy
from cloudbot.event import Event
from cloudbot.util import botvars
logger = logging.getLogger("cloudbot")
def find_hooks(parent, module):
"""
:type parent: Plugin
:type module: object
:rtype: (list[CommandHook], list[RegexHook], list[RawHook], list[SieveHook], List[EventHook], list[OnloadHook])
"""
# set the loaded flag
module._cloudbot_loaded = True
command = []
regex = []
raw = []
sieve = []
event = []
onload = []
type_lists = {"command": command, "regex": regex, "irc_raw": raw, "sieve": sieve, "event": event, "onload": onload}
for name, func in module.__dict__.items():
if hasattr(func, "_cloudbot_hook"):
# if it has cloudbot hook
func_hooks = func._cloudbot_hook
for hook_type, func_hook in func_hooks.items():
type_lists[hook_type].append(_hook_name_to_plugin[hook_type](parent, func_hook))
# delete the hook to free memory
del func._cloudbot_hook
return command, regex, raw, sieve, event, onload
def find_tables(code):
"""
:type code: object
:rtype: list[sqlalchemy.Table]
"""
tables = []
for name, obj in code.__dict__.items():
if isinstance(obj, sqlalchemy.Table) and obj.metadata == botvars.metadata:
# if it's a Table, and it's using our metadata, append it to the list
tables.append(obj)
return tables
class PluginManager:
"""
PluginManager is the core of CloudBot plugin loading.
PluginManager loads Plugins, and adds their Hooks to easy-access dicts/lists.
Each Plugin represents a file, and loads hooks onto itself using find_hooks.
Plugins are the lowest level of abstraction in this class. There are four different plugin types:
- CommandPlugin is for bot commands
- RawPlugin hooks onto irc_raw irc lines
- RegexPlugin loads a regex parameter, and executes on irc lines which match the regex
- SievePlugin is a catch-all sieve, which all other plugins go through before being executed.
:type bot: cloudbot.bot.CloudBot
:type plugins: dict[str, Plugin]
:type commands: dict[str, CommandHook]
:type raw_triggers: dict[str, list[RawHook]]
:type catch_all_triggers: list[RawHook]
:type event_type_hooks: dict[cloudbot.event.EventType, list[EventHook]]
:type regex_hooks: list[(re.__Regex, RegexHook)]
:type sieves: list[SieveHook]
"""
def __init__(self, bot):
"""
Creates a new PluginManager. You generally only need to do this from inside cloudbot.bot.CloudBot
:type bot: cloudbot.bot.CloudBot
"""
self.bot = bot
self.plugins = {}
self.commands = {}
self.raw_triggers = {}
self.catch_all_triggers = []
self.event_type_hooks = {}
self.regex_hooks = []
self.sieves = []
self._hook_waiting_queues = {}
@asyncio.coroutine
def load_all(self, plugin_dir):
"""
Load a plugin from each *.py file in the given directory.
Won't load any plugins listed in "disabled_plugins".
:type plugin_dir: str
"""
path_list = glob.iglob(os.path.join(plugin_dir, '*.py'))
# Load plugins asynchronously :O
yield from asyncio.gather(*[self.load_plugin(path) for path in path_list], loop=self.bot.loop)
@asyncio.coroutine
def load_plugin(self, path):
"""
Loads a plugin from the given path and plugin object, then registers all hooks from that plugin.
Won't load any plugins listed in "disabled_plugins".
:type path: str
"""
file_path = os.path.abspath(path)
file_name = os.path.basename(path)
title = os.path.splitext(file_name)[0]
if "plugin_loading" in self.bot.config:
pl = self.bot.config.get("plugin_loading")
if pl.get("use_whitelist", False):
if title not in pl.get("whitelist", []):
logger.info('Not loading plugin module "{}": plugin not whitelisted'.format(file_name))
return
else:
if title in pl.get("blacklist", []):
logger.info('Not loading plugin module "{}": plugin blacklisted'.format(file_name))
return
# make sure to unload the previously loaded plugin from this path, if it was loaded.
if file_name in self.plugins:
yield from self.unload_plugin(file_path)
module_name = "plugins.{}".format(title)
try:
plugin_module = importlib.import_module(module_name)
# if this plugin was loaded before, reload it
if hasattr(plugin_module, "_cloudbot_loaded"):
importlib.reload(plugin_module)
except Exception:
logger.exception("Error loading {}:".format(file_name))
return
# create the plugin
plugin = Plugin(file_path, file_name, title, plugin_module)
# proceed to register hooks
# create database tables
yield from plugin.create_tables(self.bot)
# run onload hooks
for onload_hook in plugin.run_on_load:
success = yield from self.launch(onload_hook, Event(bot=self.bot, hook=onload_hook))
if not success:
logger.warning("Not registering hooks from plugin {}: onload hook errored".format(plugin.title))
# unregister databases
plugin.unregister_tables(self.bot)
return
self.plugins[plugin.file_name] = plugin
# register commands
for command_hook in plugin.commands:
for alias in command_hook.aliases:
if alias in self.commands:
logger.warning(
"Plugin {} attempted to register command {} which was already registered by {}. "
"Ignoring new assignment.".format(plugin.title, alias, self.commands[alias].plugin.title))
else:
self.commands[alias] = command_hook
self._log_hook(command_hook)
# register raw hooks
for raw_hook in plugin.raw_hooks:
if raw_hook.is_catch_all():
self.catch_all_triggers.append(raw_hook)
else:
for trigger in raw_hook.triggers:
if trigger in self.raw_triggers:
self.raw_triggers[trigger].append(raw_hook)
else:
self.raw_triggers[trigger] = [raw_hook]
self._log_hook(raw_hook)
# register events
for event_hook in plugin.events:
for event_type in event_hook.types:
if event_type in self.event_type_hooks:
self.event_type_hooks[event_type].append(event_hook)
else:
self.event_type_hooks[event_type] = [event_hook]
self._log_hook(event_hook)
# register regexps
for regex_hook in plugin.regexes:
for regex_match in regex_hook.regexes:
self.regex_hooks.append((regex_match, regex_hook))
self._log_hook(regex_hook)
# register sieves
for sieve_hook in plugin.sieves:
self.sieves.append(sieve_hook)
self._log_hook(sieve_hook)
# we don't need this anymore
del plugin.run_on_load
@asyncio.coroutine
def unload_plugin(self, path):
"""
Unloads the plugin from the given path, unregistering all hooks from the plugin.
Returns True if the plugin was unloaded, False if the plugin wasn't loaded in the first place.
:type path: str
:rtype: bool
"""
file_name = os.path.basename(path)
title = os.path.splitext(file_name)[0]
if "disabled_plugins" in self.bot.config and title in self.bot.config['disabled_plugins']:
# this plugin hasn't been loaded, so no need to unload it
return False
# make sure this plugin is actually loaded
if not file_name in self.plugins:
return False
# get the loaded plugin
plugin = self.plugins[file_name]
# unregister commands
for command_hook in plugin.commands:
for alias in command_hook.aliases:
if alias in self.commands and self.commands[alias] == command_hook:
# we need to make sure that there wasn't a conflict, so we don't delete another plugin's command
del self.commands[alias]
# unregister raw hooks
for raw_hook in plugin.raw_hooks:
if raw_hook.is_catch_all():
self.catch_all_triggers.remove(raw_hook)
else:
for trigger in raw_hook.triggers:
assert trigger in self.raw_triggers # this can't be not true
self.raw_triggers[trigger].remove(raw_hook)
if not self.raw_triggers[trigger]: # if that was the last hook for this trigger
del self.raw_triggers[trigger]
# unregister events
for event_hook in plugin.events:
for event_type in event_hook.types:
assert event_type in self.event_type_hooks # this can't be not true
self.event_type_hooks[event_type].remove(event_hook)
if not self.event_type_hooks[event_type]: # if that was the last hook for this event type
del self.event_type_hooks[event_type]
# unregister regexps
for regex_hook in plugin.regexes:
for regex_match in regex_hook.regexes:
self.regex_hooks.remove((regex_match, regex_hook))
# unregister sieves
for sieve_hook in plugin.sieves:
self.sieves.remove(sieve_hook)
# unregister databases
plugin.unregister_tables(self.bot)
# remove last reference to plugin
del self.plugins[plugin.file_name]
if self.bot.config.get("logging", {}).get("show_plugin_loading", True):
logger.info("Unloaded all plugins from {}.py".format(plugin.title))
return True
def _log_hook(self, hook):
"""
Logs registering a given hook
:type hook: Hook
"""
if self.bot.config.get("logging", {}).get("show_plugin_loading", True):
logger.info("Loaded {}".format(hook))
logger.debug("Loaded {}".format(repr(hook)))
def _prepare_parameters(self, hook, event):
"""
Prepares arguments for the given hook
:type hook: cloudbot.plugin.Hook
:type event: cloudbot.event.Event
:rtype: list
"""
parameters = []
for required_arg in hook.required_args:
if hasattr(event, required_arg):
value = getattr(event, required_arg)
parameters.append(value)
else:
logger.error("Plugin {} asked for invalid argument '{}', cancelling execution!"
.format(hook.description, required_arg))
logger.debug("Valid arguments are: {} ({})".format(dir(event), event))
return None
return parameters
def _execute_hook_threaded(self, hook, event):
"""
:type hook: Hook
:type event: cloudbot.event.Event
"""
event.prepare_threaded()
parameters = self._prepare_parameters(hook, event)
if parameters is None:
return None
try:
return hook.function(*parameters)
finally:
event.close_threaded()
@asyncio.coroutine
def _execute_hook_sync(self, hook, event):
"""
:type hook: Hook
:type event: cloudbot.event.Event
"""
yield from event.prepare()
parameters = self._prepare_parameters(hook, event)
if parameters is None:
return None
try:
return (yield from hook.function(*parameters))
finally:
yield from event.close()
@asyncio.coroutine
def _execute_hook(self, hook, event):
"""
Runs the specific hook with the given bot and event.
Returns False if the hook errored, True otherwise.
:type hook: cloudbot.plugin.Hook
:type event: cloudbot.event.Event
:rtype: bool
"""
try:
# _internal_run_threaded and _internal_run_coroutine prepare the database, and run the hook.
# _internal_run_* will prepare parameters and the database session, but won't do any error catching.
if hook.threaded:
out = yield from self.bot.loop.run_in_executor(None, self._execute_hook_threaded, hook, event)
else:
out = yield from self._execute_hook_sync(hook, event)
except Exception:
logger.exception("Error in hook {}".format(hook.description))
return False
if out is not None:
# if there are multiple items in the response, return them on multiple lines
if isinstance(out, (list, tuple)):
event.reply(out[0])
for line in out[1:]:
event.message(line)
else:
event.reply(str(out))
return True
@asyncio.coroutine
def _sieve(self, sieve, event, hook):
"""
:type sieve: cloudbot.plugin.Hook
:type event: cloudbot.event.Event
:type hook: cloudbot.plugin.Hook
:rtype: cloudbot.event.Event
"""
try:
if sieve.threaded:
result = yield from self.bot.loop.run_in_executor(None, sieve.function, self.bot, event, hook)
else:
result = yield from sieve.function(self.bot, event, hook)
except Exception:
logger.exception("Error running sieve {} on {}:".format(sieve.description, hook.description))
return None
else:
return result
@asyncio.coroutine
def launch(self, hook, event):
"""
Dispatch a given event to a given hook using a given bot object.
Returns False if the hook didn't run successfully, and True if it ran successfully.
:type event: cloudbot.event.Event | cloudbot.event.CommandEvent
:type hook: cloudbot.plugin.Hook | cloudbot.plugin.CommandHook
:rtype: bool
"""
if hook.type != "onload": # we don't need sieves on onload hooks.
for sieve in self.bot.plugin_manager.sieves:
event = yield from self._sieve(sieve, event, hook)
if event is None:
return False
if hook.type == "command" and hook.auto_help and not event.text and hook.doc is not None:
event.notice_doc()
return False
if hook.single_thread:
# There should only be one running instance of this hook, so let's wait for the last event to be processed
# before starting this one.
key = (hook.plugin.title, hook.function_name)
if key in self._hook_waiting_queues:
queue = self._hook_waiting_queues[key]
if queue is None:
# there's a hook running, but the queue hasn't been created yet, since there's only one hook
queue = asyncio.Queue()
self._hook_waiting_queues[key] = queue
assert isinstance(queue, asyncio.Queue)
# create a future to represent this task
future = asyncio.Future()
queue.put_nowait(future)
# wait until the last task is completed
yield from future
else:
# set to None to signify that this hook is running, but there's no need to create a full queue
# in case there are no more hooks that will wait
self._hook_waiting_queues[key] = None
# Run the plugin with the message, and wait for it to finish
result = yield from self._execute_hook(hook, event)
queue = self._hook_waiting_queues[key]
if queue is None or queue.empty():
# We're the last task in the queue, we can delete it now.
del self._hook_waiting_queues[key]
else:
# set the result for the next task's future, so they can execute
next_future = yield from queue.get()
next_future.set_result(None)
else:
# Run the plugin with the message, and wait for it to finish
result = yield from self._execute_hook(hook, event)
# Return the result
return result
class Plugin:
"""
Each Plugin represents a plugin file, and contains loaded hooks.
:type file_path: str
:type file_name: str
:type title: str
:type commands: list[CommandHook]
:type regexes: list[RegexHook]
:type raw_hooks: list[RawHook]
:type sieves: list[SieveHook]
:type events: list[EventHook]
:type tables: list[sqlalchemy.Table]
"""
def __init__(self, filepath, filename, title, code):
"""
:type filepath: str
:type filename: str
:type code: object
"""
self.file_path = filepath
self.file_name = filename
self.title = title
self.commands, self.regexes, self.raw_hooks, self.sieves, self.events, self.run_on_load = find_hooks(self, code)
# we need to find tables for each plugin so that they can be unloaded from the global metadata when the
# plugin is reloaded
self.tables = find_tables(code)
@asyncio.coroutine
def create_tables(self, bot):
"""
Creates all sqlalchemy Tables that are registered in this plugin
:type bot: cloudbot.bot.CloudBot
"""
if self.tables:
# if there are any tables
logger.info("Registering tables for {}".format(self.title))
for table in self.tables:
if not (yield from bot.loop.run_in_executor(None, table.exists, bot.db_engine)):
yield from bot.loop.run_in_executor(None, table.create, bot.db_engine)
def unregister_tables(self, bot):
"""
Unregisters all sqlalchemy Tables registered to the global metadata by this plugin
:type bot: cloudbot.bot.CloudBot
"""
if self.tables:
# if there are any tables
logger.info("Unregistering tables for {}".format(self.title))
for table in self.tables:
bot.db_metadata.remove(table)
class Hook:
"""
Each hook is specific to one function. This class is never used by itself, rather extended.
:type type; str
:type plugin: Plugin
:type function: callable
:type function_name: str
:type required_args: list[str]
:type threaded: bool
:type ignore_bots: bool
:type permissions: list[str]
:type single_thread: bool
"""
def __init__(self, _type, plugin, func_hook):
"""
:type _type: str
:type plugin: Plugin
:type func_hook: hook._Hook
"""
self.type = _type
self.plugin = plugin
self.function = func_hook.function
self.function_name = self.function.__name__
self.required_args = inspect.getargspec(self.function)[0]
if self.required_args is None:
self.required_args = []
if asyncio.iscoroutine(self.function) or asyncio.iscoroutinefunction(self.function):
self.threaded = False
else:
self.threaded = True
self.ignore_bots = func_hook.kwargs.pop("ignorebots", False)
self.permissions = func_hook.kwargs.pop("permissions", [])
self.single_thread = func_hook.kwargs.pop("singlethread", False)
if func_hook.kwargs:
# we should have popped all the args, so warn if there are any left
logger.warning("Ignoring extra args {} from {}".format(func_hook.kwargs, self.description))
@property
def description(self):
return "{}:{}".format(self.plugin.title, self.function_name)
def __repr__(self):
return "type: {}, plugin: {}, ignore_bots: {}, permissions: {}, single_thread: {}, threaded: {}".format(
self.type, self.plugin.title, self.ignore_bots, self.permissions, self.single_thread, self.threaded
)
class CommandHook(Hook):
"""
:type name: str
:type aliases: list[str]
:type doc: str
:type auto_help: bool
"""
def __init__(self, plugin, cmd_hook):
"""
:type plugin: Plugin
:type cmd_hook: cloudbot.util.hook._CommandHook
"""
self.auto_help = cmd_hook.kwargs.pop("autohelp", True)
self.name = cmd_hook.main_alias
self.aliases = list(cmd_hook.aliases) # turn the set into a list
self.aliases.remove(self.name)
self.aliases.insert(0, self.name) # make sure the name, or 'main alias' is in position 0
self.doc = cmd_hook.doc
super().__init__("command", plugin, cmd_hook)
def __repr__(self):
return "Command[name: {}, aliases: {}, {}]".format(self.name, self.aliases[1:], Hook.__repr__(self))
def __str__(self):
return "command {} from {}".format("/".join(self.aliases), self.plugin.file_name)
class RegexHook(Hook):
"""
:type regexes: set[re.__Regex]
"""
def __init__(self, plugin, regex_hook):
"""
:type plugin: Plugin
:type regex_hook: cloudbot.util.hook._RegexHook
"""
self.regexes = regex_hook.regexes
super().__init__("regex", plugin, regex_hook)
def __repr__(self):
return "Regex[regexes: [{}], {}]".format(", ".join(regex.pattern for regex in self.regexes),
Hook.__repr__(self))
def __str__(self):
return "regex {} from {}".format(self.function_name, self.plugin.file_name)
class RawHook(Hook):
"""
:type triggers: set[str]
"""
def __init__(self, plugin, irc_raw_hook):
"""
:type plugin: Plugin
:type irc_raw_hook: cloudbot.util.hook._RawHook
"""
super().__init__("irc_raw", plugin, irc_raw_hook)
self.triggers = irc_raw_hook.triggers
def is_catch_all(self):
return "*" in self.triggers
def __repr__(self):
return "Raw[triggers: {}, {}]".format(list(self.triggers), Hook.__repr__(self))
def __str__(self):
return "irc raw {} ({}) from {}".format(self.function_name, ",".join(self.triggers), self.plugin.file_name)
class SieveHook(Hook):
def __init__(self, plugin, sieve_hook):
"""
:type plugin: Plugin
:type sieve_hook: cloudbot.util.hook._SieveHook
"""
# We don't want to thread sieves by default - this is retaining old behavior for compatibility
super().__init__("sieve", plugin, sieve_hook)
def __repr__(self):
return "Sieve[{}]".format(Hook.__repr__(self))
def __str__(self):
return "sieve {} from {}".format(self.function_name, self.plugin.file_name)
class EventHook(Hook):
"""
:type types: set[cloudbot.event.EventType]
"""
def __init__(self, plugin, event_hook):
"""
:type plugin: Plugin
:type event_hook: cloudbot.util.hook._EventHook
"""
super().__init__("event", plugin, event_hook)
self.types = event_hook.types
def __repr__(self):
return "Event[types: {}, {}]".format(list(self.types), Hook.__repr__(self))
def __str__(self):
return "event {} ({}) from {}".format(self.function_name, ",".join(str(t) for t in self.types),
self.plugin.file_name)
class OnloadHook(Hook):
def __init__(self, plugin, on_load_hook):
"""
:type plugin: Plugin
:type on_load_hook: cloudbot.util.hook._OnLoadHook
"""
super().__init__("onload", plugin, on_load_hook)
def __repr__(self):
return "Onload[{}]".format(Hook.__repr__(self))
def __str__(self):
return "onload {} from {}".format(self.function_name, self.plugin.file_name)
_hook_name_to_plugin = {
"command": CommandHook,
"regex": RegexHook,
"irc_raw": RawHook,
"sieve": SieveHook,
"event": EventHook,
"onload": OnloadHook
}