From 8c6ab17e579cf8f4e510ec324fc98b010ff01cb0 Mon Sep 17 00:00:00 2001 From: jesopo Date: Sun, 2 Sep 2018 19:54:45 +0100 Subject: [PATCH] give an Exports object (actually, ExportsContex object) to each module, to facilitate things like !set and !channelset without using the events system --- Exports.py | 44 +++++++++++++++++++++++++++++++++++++ IRCBot.py | 6 +++-- ModuleManager.py | 14 +++++++----- modules/8ball.py | 2 +- modules/accept_invite.py | 2 +- modules/admin.py | 2 +- modules/auto_mode.py | 9 ++++---- modules/bitcoin.py | 2 +- modules/bitly.py | 2 +- modules/books.py | 2 +- modules/channel_op.py | 29 +++++++++++------------- modules/channel_save.py | 2 +- modules/check_mode.py | 2 +- modules/check_urls.py | 17 +++++++------- modules/coins.py | 2 +- modules/commands.py | 7 +++--- modules/ctcp.py | 2 +- modules/define.py | 2 +- modules/dice.py | 2 +- modules/dns.py | 2 +- modules/ducks.py | 8 +++---- modules/geoip.py | 2 +- modules/google.py | 2 +- modules/hash.py | 2 +- modules/haveibeenpwned.py | 2 +- modules/ids.py | 2 +- modules/imdb.py | 2 +- modules/in.py | 2 +- modules/join.py | 2 +- modules/karma.py | 9 ++++---- modules/lastfm.py | 7 +++--- modules/modules.py | 2 +- modules/nickname_aliases.py | 2 +- modules/nickserv.py | 2 +- modules/nr.py | 2 +- modules/perform.py | 2 +- modules/permissions.py | 2 +- modules/pong.py | 2 +- modules/print_activity.py | 2 +- modules/quit.py | 2 +- modules/quotes.py | 2 +- modules/random_number.py | 2 +- modules/sasl.py | 2 +- modules/sed.py | 16 ++++++-------- modules/seen.py | 2 +- modules/set.py | 35 ++++++++++------------------- modules/signals.py | 2 +- modules/soundcloud.py | 2 +- modules/spotify.py | 2 +- modules/stats.py | 2 +- modules/strax.py | 2 +- modules/telegram.py | 2 +- modules/tfl.py | 2 +- modules/thesaurus.py | 2 +- modules/title.py | 2 +- modules/to.py | 2 +- modules/todo.py | 2 +- modules/trakt.py | 6 ++--- modules/translate.py | 2 +- modules/twitter.py | 2 +- modules/upc.py | 2 +- modules/urbandictionary.py | 2 +- modules/weather.py | 2 +- modules/wikipedia.py | 2 +- modules/wolframalpha.py | 2 +- modules/words.py | 2 +- modules/youtube.py | 10 ++++----- 67 files changed, 172 insertions(+), 151 deletions(-) create mode 100644 Exports.py diff --git a/Exports.py b/Exports.py new file mode 100644 index 00000000..8baca50d --- /dev/null +++ b/Exports.py @@ -0,0 +1,44 @@ + + +class ExportsContext(object): + def __init__(self, parent, context): + self._parent = parent + self.context = context + + def add(self, setting, value): + self._parent._context_add(self.context, setting, value) + def get_all(self, setting): + return self._parent.get_all(setting) + +class Exports(object): + def __init__(self): + self._exports = {} + self._context_exports = {} + + def new_context(self, context): + return ExportsContext(self, context) + + def add(self, setting, value): + self._add(None, setting, value) + def _context_add(self, context, setting, value): + self._add(context, setting, value) + def _add(self, context, setting, value): + if context == None: + if not setting in self_exports: + self._exports[setting] = [] + self._exports[setting].append(value) + else: + if not context in self._context_exports: + self._context_exports[context] = {} + if not setting in self._context_exports[context]: + self._context_exports[context][setting] = [] + self._context_exports[context][setting].append(value) + + def get_all(self, setting): + return self._exports.get(setting, []) + sum([ + exports.get(setting, []) for exports in + self._context_exports.values()], []) + + def purge_context(self, context): + if context in self._context_exports: + del self._context_exports[context] diff --git a/IRCBot.py b/IRCBot.py index 75751e91..ac1dd6bd 100644 --- a/IRCBot.py +++ b/IRCBot.py @@ -1,6 +1,6 @@ import os, select, sys, threading, time, traceback, uuid -import EventManager, IRCLineHandler, IRCServer, Logging +import EventManager, Exports, IRCLineHandler, IRCServer, Logging import ModuleManager, Timer class Bot(object): @@ -14,7 +14,9 @@ class Bot(object): self.running = True self.poll = select.epoll() self._events = EventManager.EventHook(self) - self.modules = ModuleManager.ModuleManager(self, self._events) + self._exports = Exports.Exports() + self.modules = ModuleManager.ModuleManager(self, self._events, + self._exports) self.log = Logging.Log(self) self.line_handler = IRCLineHandler.LineHandler(self, self._events) self.timers = [] diff --git a/ModuleManager.py b/ModuleManager.py index a05918cb..56ad2323 100644 --- a/ModuleManager.py +++ b/ModuleManager.py @@ -1,9 +1,10 @@ import glob, imp, inspect, os, sys, uuid class ModuleManager(object): - def __init__(self, bot, events, directory="modules"): + def __init__(self, bot, events, exports, directory="modules"): self.bot = bot self.events = events + self.exports = exports self.directory = directory self.modules = {} self.waiting_requirement = {} @@ -49,12 +50,12 @@ class ModuleManager(object): if not inspect.isclass(module.Module): raise ImportError("module '%s' has a Module attribute but it is not a class.") - event_context = str(uuid.uuid4()) + context = str(uuid.uuid4()) module_object = module.Module(self.bot, self.events.new_context( - event_context)) + context), self.exports.new_context(context)) if not hasattr(module_object, "_name"): module_object._name = name.title() - module_object._event_context = event_context + module_object._context = context module_object._import_name = name assert not module_object._name in self.modules, ( @@ -87,8 +88,9 @@ class ModuleManager(object): module = self.modules[name] del self.modules[name] - event_context = module._event_context - self.events.purge_context(event_context) + context = module._context + self.events.purge_context(context) + self.exports.purge_context(context) del sys.modules[name] references = sys.getrefcount(module) diff --git a/modules/8ball.py b/modules/8ball.py index 57be2da4..39e786e0 100644 --- a/modules/8ball.py +++ b/modules/8ball.py @@ -17,7 +17,7 @@ CHOICES = [ ] class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received.command.8ball").hook( self.decide, min_args=1, diff --git a/modules/accept_invite.py b/modules/accept_invite.py index e25aea76..fc8648d7 100644 --- a/modules/accept_invite.py +++ b/modules/accept_invite.py @@ -1,7 +1,7 @@ class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("invite").hook(self.on_invite) def on_invite(self, event): diff --git a/modules/admin.py b/modules/admin.py index 5578febe..595eb1c0 100644 --- a/modules/admin.py +++ b/modules/admin.py @@ -1,7 +1,7 @@ class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("changenickname" ).hook(self.change_nickname, permission="changenickname", diff --git a/modules/auto_mode.py b/modules/auto_mode.py index 3e800591..08fd94de 100644 --- a/modules/auto_mode.py +++ b/modules/auto_mode.py @@ -1,13 +1,12 @@ import Utils class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot - events.on("postboot").on("configure").on( - "channelset").assure_call(setting="automode", - help="Disable/Enable automode", - validate=Utils.bool_or_none) + exports.add("channelset", {"setting": "automode", + "help": "Disable/Enable automode", + "validate": Utils.bool_or_none}) events.on("channel").on("mode").hook(self.on_mode) events.on("received").on("join").hook(self.on_join) diff --git a/modules/bitcoin.py b/modules/bitcoin.py index f15c5266..ac291aa0 100644 --- a/modules/bitcoin.py +++ b/modules/bitcoin.py @@ -2,7 +2,7 @@ import Utils class Module(object): _name = "BTC" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("btc").hook( self.btc, help="Get the exchange rate of bitcoins", diff --git a/modules/bitly.py b/modules/bitly.py index 05489fb0..afcc1522 100644 --- a/modules/bitly.py +++ b/modules/bitly.py @@ -7,7 +7,7 @@ URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v3/shorten" REGEX_URL = re.compile("https?://", re.I) class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot self.events = events events.on("get").on("shortlink").hook(self.shortlink) diff --git a/modules/books.py b/modules/books.py index 9c94c656..efea1670 100644 --- a/modules/books.py +++ b/modules/books.py @@ -7,7 +7,7 @@ REGEX_BOOKID = re.compile("id=([\w\-]+)") class Module(object): _name = "ISBN" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("isbn").hook( self.isbn, help="Get book information from a provided ISBN", diff --git a/modules/channel_op.py b/modules/channel_op.py index 9e20d693..7f80d5b3 100644 --- a/modules/channel_op.py +++ b/modules/channel_op.py @@ -2,7 +2,7 @@ import Utils class Module(object): _name = "Channel Op" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("kick", "k" ).hook(self.kick, channel_only=True, require_mode="o", @@ -40,21 +40,18 @@ class Module(object): events.on("received").on("message").on("channel").hook( self.highlight_spam) - events.on("postboot").on("configure").on( - "channelset").assure_call(setting="highlight-spam-threshold", - help="Set the number of nicknames in a message that " - "qualifies as spam", validate=Utils.int_or_none) - events.on("postboot").on("configure").on( - "channelset").assure_call(setting="highlight-spam-protection", - help="Enable/Disable highlight spam protection", - validate=Utils.bool_or_none) - events.on("postboot").on("configure").on( - "channelset").assure_call(setting="highlight-spam-ban", - help="Enable/Disable banning highlight spammers " - "instead of just kicking", validate=Utils.bool_or_none) - events.on("postboot").on("configure").on( - "channelset").assure_call(setting="ban-format", - help="Set ban format ($n = nick, $u = username, $h = hostname)") + exports.add("channelset", {"setting": "highlight-spam-threshold", + "help": "Set the number of nicknames in a message that " + "qualifies as spam", "validate": Utils.int_or_none}) + exports.add("channelset", {"setting": "highlight-spam-protection", + "help": "Enable/Disable highlight spam protection", + "validate": Utils.bool_or_none}) + exports.add("channelset", {"setting": "highlight-spam-ban", + "help": "Enable/Disable banning highlight spammers " + "instead of just kicking", "validate": Utils.bool_or_none}) + exports.add("channelset", {"setting": "ban-format", + "help": "Set ban format ($n = nick, $u = username, " + "$h = hostname)"}) def kick(self, event): target = event["args_split"][0] diff --git a/modules/channel_save.py b/modules/channel_save.py index afbd4200..15b3a6c2 100644 --- a/modules/channel_save.py +++ b/modules/channel_save.py @@ -1,7 +1,7 @@ class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received.numeric.001").hook(self.on_connect) events.on("self.join").hook(self.on_join) events.on("self.kick").hook(self.on_kick) diff --git a/modules/check_mode.py b/modules/check_mode.py index 4fb2b783..5819ef51 100644 --- a/modules/check_mode.py +++ b/modules/check_mode.py @@ -1,7 +1,7 @@ class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("preprocess").on("command").hook(self.preprocess_command) diff --git a/modules/check_urls.py b/modules/check_urls.py index 7d39294e..e53437b7 100644 --- a/modules/check_urls.py +++ b/modules/check_urls.py @@ -7,18 +7,17 @@ URL_VIRUSTOTAL = "https://www.virustotal.com/vtapi/v2/url/report" RE_URL = re.compile(r"https?://\S+", re.I) class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot self.events = events events.on("received.message.channel").hook(self.message) - events.on("postboot").on("configure").on( - "channelset").assure_call(setting="check-urls", - help="Enable/Disable automatically checking for malicious URLs", - validate=Utils.bool_or_none) - events.on("postboot").on("configure").on( - "channelset").assure_call(setting="check-urls-kick", - help="Enable/Disable automatically kicking users that send " - "malicious URLs", validate=Utils.bool_or_none) + + exports.add("channelset", {"setting": "check-urls", + "help": "Enable/Disable automatically checking for " + "malicious URLs", "validate": Utils.bool_or_none}) + exports.add("channelset", {"setting": "check-urls-kick", + "help": "Enable/Disable automatically kicking users that " + "send malicious URLs", "validate": Utils.bool_or_none}) def message(self, event): match = RE_URL.search(event["message"]) diff --git a/modules/coins.py b/modules/coins.py index 307f171f..15b06918 100644 --- a/modules/coins.py +++ b/modules/coins.py @@ -26,7 +26,7 @@ THIRD_COLUMN = list(range(1, 37))[2::3] REGEX_STREET = re.compile("street([1-9]|1[0-2])$") class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received.command.coins").hook(self.coins, help="Show how many coins you have") diff --git a/modules/commands.py b/modules/commands.py index 5084f690..e3c4c00e 100644 --- a/modules/commands.py +++ b/modules/commands.py @@ -45,7 +45,7 @@ class StdErr(Out): self.module_name, Utils.FONT_RESET) class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot self.events = events events.on("received").on("message").on("channel").hook( @@ -60,9 +60,8 @@ class Module(object): events.on("received").on("command").on("more").hook(self.more, help="Get more output from the last command", skip_out=True) - events.on("postboot").on("configure").on( - "channelset").assure_call(setting="command-prefix", - help="Set the command prefix used in this channel") + exports.add("channelset", {"setting": "command-prefix", + "help": "Set the command prefix used in this channel"}) events.on("new").on("user", "channel").hook(self.new) events.on("send").on("stdout").hook(self.send_stdout) diff --git a/modules/ctcp.py b/modules/ctcp.py index 5320369a..e6d7ad8f 100644 --- a/modules/ctcp.py +++ b/modules/ctcp.py @@ -1,7 +1,7 @@ import datetime class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("message").on("private").hook( self.private_message) diff --git a/modules/define.py b/modules/define.py index 6f17164c..b29520cd 100644 --- a/modules/define.py +++ b/modules/define.py @@ -5,7 +5,7 @@ import Utils URL_WORDNIK = "http://api.wordnik.com:80/v4/word.json/%s/definitions" class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("define").hook( self.define, help="Define a provided term", diff --git a/modules/dice.py b/modules/dice.py index 2829cf48..1b1fc0ab 100644 --- a/modules/dice.py +++ b/modules/dice.py @@ -2,7 +2,7 @@ import random import Utils class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received.command.roll").hook( self.roll_dice, min_args=1, diff --git a/modules/dns.py b/modules/dns.py index 43499601..112fcf43 100644 --- a/modules/dns.py +++ b/modules/dns.py @@ -2,7 +2,7 @@ import socket class Module(object): _name = "DNS" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("command").on("dns").hook( self.dns, min_args=1, help="Get all addresses for a given hostname (IPv4/IPv6)", diff --git a/modules/ducks.py b/modules/ducks.py index 83251cef..06194b85 100644 --- a/modules/ducks.py +++ b/modules/ducks.py @@ -14,7 +14,7 @@ DUCK_LIST = [ ] class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot self.events = events @@ -33,10 +33,8 @@ class Module(object): "stats!") - events.on("postboot").on("configure").on( - "channelset").assure_call(setting="ducks-enabled", - help="Toggles ducks!", - validate=Utils.bool_or_none) + exports.add("channelset", {"setting": "ducks-enabled", + "help": "Toggle ducks!", "validate": Utils.bool_or_none}) events.on("received.numeric.366").hook(self.bootstrap) diff --git a/modules/geoip.py b/modules/geoip.py index bd874d76..0228daab 100644 --- a/modules/geoip.py +++ b/modules/geoip.py @@ -4,7 +4,7 @@ URL_GEOIP = "http://ip-api.com/json/%s" class Module(object): _name = "GeoIP" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("command").on("geoip").hook( self.geoip, min_args=1, help="Get geoip data on a given IPv4/IPv6 address", diff --git a/modules/google.py b/modules/google.py index 632c5fd2..99566ebe 100644 --- a/modules/google.py +++ b/modules/google.py @@ -6,7 +6,7 @@ import Utils URL_GOOGLESEARCH = "https://www.googleapis.com/customsearch/v1" class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("google", "g").hook(self.google, help="Google feeling lucky", diff --git a/modules/hash.py b/modules/hash.py index 19f0591f..38115411 100644 --- a/modules/hash.py +++ b/modules/hash.py @@ -1,7 +1,7 @@ import hashlib class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("hash" ).hook(self.hash, min_args=2, help="Hash a string", diff --git a/modules/haveibeenpwned.py b/modules/haveibeenpwned.py index 854f69f3..0af37b46 100644 --- a/modules/haveibeenpwned.py +++ b/modules/haveibeenpwned.py @@ -4,7 +4,7 @@ URL_HAVEIBEENPWNEDAPI = "https://haveibeenpwned.com/api/v2/breachedaccount/%s" URL_HAVEIBEENPWNED = "https://haveibeenpwned.com/" class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("command").on("beenpwned").hook( self.beenpwned, min_args=1, help="Find out if a username, email or similar has appeared " diff --git a/modules/ids.py b/modules/ids.py index 0c8ba0bf..8e5dc838 100644 --- a/modules/ids.py +++ b/modules/ids.py @@ -2,7 +2,7 @@ class Module(object): _name = "IDs" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received.command.myid").hook(self.my_id, help="Show your user ID") events.on("received.command.channelid").hook( diff --git a/modules/imdb.py b/modules/imdb.py index 71458638..b145d5b4 100644 --- a/modules/imdb.py +++ b/modules/imdb.py @@ -8,7 +8,7 @@ URL_IMDBTITLE = "http://imdb.com/title/%s" class Module(object): _name = "IMDb" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("imdb").hook( self.imdb, min_args=1, diff --git a/modules/in.py b/modules/in.py index 2ac6a9bc..940a2dbb 100644 --- a/modules/in.py +++ b/modules/in.py @@ -5,7 +5,7 @@ SECONDS_MAX = Utils.SECONDS_WEEKS*8 SECONDS_MAX_DESCRIPTION = "8 weeks" class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("in").hook( self.in_command, min_args=2, diff --git a/modules/join.py b/modules/join.py index 1596e020..31a26873 100644 --- a/modules/join.py +++ b/modules/join.py @@ -1,7 +1,7 @@ class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("numeric").on("001").hook(self.do_join) def do_join(self, event): diff --git a/modules/karma.py b/modules/karma.py index 40911e1b..5f6e5178 100644 --- a/modules/karma.py +++ b/modules/karma.py @@ -5,7 +5,7 @@ REGEX_KARMA = re.compile("(.*)(\+{2,}|\-{2,})$") KARMA_DELAY_SECONDS = 3 class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot self.events = events events.on("new").on("user").hook(self.new_user) @@ -19,10 +19,9 @@ class Module(object): min_args=1, help="Reset a specified karma to 0", usage="") - events.on("postboot").on("configure").on( - "channelset").assure_call(setting="karma-verbose", - help="Disable/Enable automatically responding to karma changes", - validate=Utils.bool_or_none) + exports.add("channelset", {"setting": "karma-verbose", + "help": "Disable/Enable automatically responding to " + "karma changes", "validate": Utils.bool_or_none}) def new_user(self, event): event["user"].last_karma = None diff --git a/modules/lastfm.py b/modules/lastfm.py index 328a79a6..80204a3f 100644 --- a/modules/lastfm.py +++ b/modules/lastfm.py @@ -5,12 +5,11 @@ import Utils URL_SCROBBLER = "http://ws.audioscrobbler.com/2.0/" class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot - events.on("postboot").on("configure").on( - "set").assure_call(setting="lastfm", - help="Set username on last.fm") + exports.add("set", {"setting": "lastfm", + "help": "Set username on last.fm"}) events.on("received").on("command").on("np", "listening", "nowplaying").hook(self.np, diff --git a/modules/modules.py b/modules/modules.py index 0138be1b..8e4da8ad 100644 --- a/modules/modules.py +++ b/modules/modules.py @@ -1,7 +1,7 @@ class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received.command.loadmodule").hook(self.load, min_args=1, permission="load-module", help="Load a module", diff --git a/modules/nickname_aliases.py b/modules/nickname_aliases.py index a77cd926..a7f9768b 100644 --- a/modules/nickname_aliases.py +++ b/modules/nickname_aliases.py @@ -22,7 +22,7 @@ def del_setting(user, setting): class Module(object): _name = "Aliases" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("new").on("user").hook(self.new_user) events.on("received").on("nick").hook(self.nickname_change) diff --git a/modules/nickserv.py b/modules/nickserv.py index 779bc955..5b7e9087 100644 --- a/modules/nickserv.py +++ b/modules/nickserv.py @@ -2,7 +2,7 @@ import base64 import EventManager class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("numeric").on("001" ).hook(self.on_connect, priority=EventManager.PRIORITY_URGENT) events.on("received").on("command").on("setnickserv" diff --git a/modules/nr.py b/modules/nr.py index 59df34c6..92b9b929 100644 --- a/modules/nr.py +++ b/modules/nr.py @@ -20,7 +20,7 @@ class Module(object): PASSENGER_ACTIVITIES = ["U", "P", "R"] COLOURS = [Utils.COLOR_LIGHTBLUE, Utils.COLOR_GREEN, Utils.COLOR_RED, Utils.COLOR_CYAN, Utils.COLOR_LIGHTGREY, Utils.COLOR_ORANGE] - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot self._client = None events.on("received").on("command").on("nrtrains" diff --git a/modules/perform.py b/modules/perform.py index 224efdcb..33fb006e 100644 --- a/modules/perform.py +++ b/modules/perform.py @@ -1,7 +1,7 @@ import EventManager class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("numeric").on("001").hook( self.on_connect, priority=EventManager.PRIORITY_URGENT) diff --git a/modules/permissions.py b/modules/permissions.py index e23a230e..63012f3b 100644 --- a/modules/permissions.py +++ b/modules/permissions.py @@ -5,7 +5,7 @@ REQUIRES_IDENTIFY = ("You need to be identified to use that command " "(/msg %s register | /msg %s identify)") class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("new").on("user").hook(self.new_user) events.on("preprocess").on("command").hook( diff --git a/modules/pong.py b/modules/pong.py index 4feb35f1..72987542 100644 --- a/modules/pong.py +++ b/modules/pong.py @@ -1,7 +1,7 @@ class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received.command.ping").hook(self.pong, help="Ping pong!") def pong(self, event): diff --git a/modules/print_activity.py b/modules/print_activity.py index 48495b46..e2d3eb2f 100644 --- a/modules/print_activity.py +++ b/modules/print_activity.py @@ -2,7 +2,7 @@ import datetime import EventManager class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("message").on("channel").hook( diff --git a/modules/quit.py b/modules/quit.py index 1658aa0e..5d40a718 100644 --- a/modules/quit.py +++ b/modules/quit.py @@ -55,7 +55,7 @@ QUOTES = { } class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("get.quit-quote").hook(self.quote) def quote(self, event): diff --git a/modules/quotes.py b/modules/quotes.py index 69b1d79b..4d448fc2 100644 --- a/modules/quotes.py +++ b/modules/quotes.py @@ -1,7 +1,7 @@ import random, time class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("quoteadd", "qadd").hook(self.quote_add, min_args=1, diff --git a/modules/random_number.py b/modules/random_number.py index 14cc6da7..0e83a215 100644 --- a/modules/random_number.py +++ b/modules/random_number.py @@ -2,7 +2,7 @@ import random, uuid class Module(object): _name = "Random" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("command").on("random", "rand").hook(self.random, help="Get a random number", usage="[start] [end]") diff --git a/modules/sasl.py b/modules/sasl.py index b9219292..15a7523a 100644 --- a/modules/sasl.py +++ b/modules/sasl.py @@ -1,7 +1,7 @@ import base64 class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("preprocess.connect").hook(self.preprocess_connect) events.on("received.cap").hook(self.on_cap) diff --git a/modules/sed.py b/modules/sed.py index c276d4d1..74441c4d 100644 --- a/modules/sed.py +++ b/modules/sed.py @@ -5,20 +5,18 @@ REGEX_SPLIT = re.compile("(?", min_args=1, require_mode="o") - def _postboot_set(self, settings, event): - settings[event["setting"]] = {} - settings[event["setting"]]["validate"] = event.get( - "validate", lambda s: s) - settings[event["setting"]]["help"] = event.get("help", - "") - def postboot_set(self, event): - self._postboot_set(self.settings, event) - def postboot_channelset(self, event): - self._postboot_set(self.channel_settings, event) - def _set(self, settings, event, target): + settings_dict = dict([(setting["setting"], setting + ) for setting in settings]) if len(event["args_split"]) > 1: setting = event["args_split"][0].lower() - if setting in settings: + if setting in settings_dict: value = " ".join(event["args_split"][1:]) - value = settings[setting]["validate"](value) + value = settings_dict[setting].get("validate", + lambda x: x)(value) if not value == None: target.set_setting(setting, value) event["stdout"].write("Saved setting") @@ -59,12 +45,13 @@ class Module(object): event["stderr"].write("Please provide a value") else: event["stdout"].write("Available settings: %s" % ( - ", ".join(settings.keys()))) + ", ".join(settings_dict.keys()))) def set(self, event): - self._set(self.settings, event, event["user"]) + self._set(self.exports.get_all("set"), event, event["user"]) def channel_set(self, event): - self._set(self.channel_settings, event, event["target"]) + self._set(self.exports.get_all("channelset"), event, + event["target"]) def _get(self, event, setting, qualifier, value): if not value == None: diff --git a/modules/signals.py b/modules/signals.py index 4587575d..d1430964 100644 --- a/modules/signals.py +++ b/modules/signals.py @@ -2,7 +2,7 @@ import signal import Config class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot self.events = events signal.signal(signal.SIGINT, self.SIGINT) diff --git a/modules/soundcloud.py b/modules/soundcloud.py index c8481043..952c9938 100644 --- a/modules/soundcloud.py +++ b/modules/soundcloud.py @@ -9,7 +9,7 @@ REGEX_SOUNDCLOUD = "https?://soundcloud.com/([^/]+)/([^/]+)" class Module(object): _name = "SoundCloud" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("soundcloud", "sc" ).hook(self.soundcloud, help="Search SoundCloud") diff --git a/modules/spotify.py b/modules/spotify.py index 31040135..766e66c3 100644 --- a/modules/spotify.py +++ b/modules/spotify.py @@ -4,7 +4,7 @@ import Utils URL_SPOTIFY = "https://api.spotify.com/v1/search" class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("command").on("spotify").hook( self.spotify, help="Search for a track on spotify", min_args=1) diff --git a/modules/stats.py b/modules/stats.py index 7ba19e43..658f058e 100644 --- a/modules/stats.py +++ b/modules/stats.py @@ -2,7 +2,7 @@ import time import Utils class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.boot_time = time.time() self.bot = bot events.on("received").on("command").on("uptime" diff --git a/modules/strax.py b/modules/strax.py index 26cecd07..0a189b48 100644 --- a/modules/strax.py +++ b/modules/strax.py @@ -2,7 +2,7 @@ import random class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received.command.strax").hook( self.strax, help="Suggests a glorious method of battle for the glory of the Sontaran Empire, through IRC!") diff --git a/modules/telegram.py b/modules/telegram.py index 5639666b..a32f472c 100644 --- a/modules/telegram.py +++ b/modules/telegram.py @@ -8,7 +8,7 @@ from threading import Thread class Module(Thread): _name = "telegram" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): key = bot.config.get("telegram-api-key") if not key: return diff --git a/modules/tfl.py b/modules/tfl.py index 72862db6..cfdb5369 100644 --- a/modules/tfl.py +++ b/modules/tfl.py @@ -20,7 +20,7 @@ PLATFORM_TYPES = ["Northbound", "Southbound", "Eastbound", "Westbound", "Inner R class Module(object): _name = "TFL" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot self.result_map = {} events.on("received").on("command").on("tflbus" diff --git a/modules/thesaurus.py b/modules/thesaurus.py index 6f91cec4..25e53427 100644 --- a/modules/thesaurus.py +++ b/modules/thesaurus.py @@ -5,7 +5,7 @@ import Utils URL_THESAURUS = "http://words.bighugelabs.com/api/2/%s/%s/json" class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("synonym", "antonym").hook(self.thesaurus, min_args=1, diff --git a/modules/title.py b/modules/title.py index 7852f64c..824cc6da 100644 --- a/modules/title.py +++ b/modules/title.py @@ -4,7 +4,7 @@ import Utils REGEX_URL = re.compile("https?://\S+", re.I) class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("command").on("title", "t").hook( self.title, help="Get the title of the provided or most " "recent URL.", usage="[URL]") diff --git a/modules/to.py b/modules/to.py index 89abc477..c01c2a58 100644 --- a/modules/to.py +++ b/modules/to.py @@ -1,7 +1,7 @@ import EventManager class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("message").on("channel" ).hook(self.channel_message, priority=EventManager.PRIORITY_MEDIUM) diff --git a/modules/todo.py b/modules/todo.py index 8c1ead32..85c206e9 100644 --- a/modules/todo.py +++ b/modules/todo.py @@ -1,7 +1,7 @@ class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("todo").hook( self.todo, help="Find out what's in your todo list", diff --git a/modules/trakt.py b/modules/trakt.py index 3f8f2019..8d82896b 100644 --- a/modules/trakt.py +++ b/modules/trakt.py @@ -6,15 +6,15 @@ URL_TRAKT = "https://api-v2launch.trakt.tv/users/%s/watching" URL_TRAKTSLUG = "https://trakt.tv/%s/%s" class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("nowwatching", "nw").hook(self.now_watching, help="Get what you or another user is now watching " "on trakt.tv", usage="[username]") - events.on("postboot").on("configure").on("set" - ).assure_call(setting="trakt", help="Set username on trakt.tv") + exports.add("set", {"setting": "trakt", + "help": "Set username on trakt.tv"}) def now_watching(self, event): if event["args"]: diff --git a/modules/translate.py b/modules/translate.py index bcc8830b..cd017dde 100644 --- a/modules/translate.py +++ b/modules/translate.py @@ -6,7 +6,7 @@ URL_LANGUAGES = "https://cloud.google.com/translate/docs/languages" REGEX_LANGUAGES = re.compile("(\w+)?:(\w+)? ") class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("command").on("translate", "tr").hook( self.translate, help="Translate the provided phrase or the " "last line seen.", usage="[phrase]") diff --git a/modules/twitter.py b/modules/twitter.py index 84f63137..6d9b3dfe 100644 --- a/modules/twitter.py +++ b/modules/twitter.py @@ -11,7 +11,7 @@ REGEX_TWITTERURL = re.compile( "https?://(?:www\.)?twitter.com/[^/]+/status/(\d+)", re.I) class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("twitter", "tw" ).hook(self.twitter, help="Find a tweet", diff --git a/modules/upc.py b/modules/upc.py index 720dfa49..404fe400 100644 --- a/modules/upc.py +++ b/modules/upc.py @@ -4,7 +4,7 @@ UPCITEMDB_URL = "https://api.upcitemdb.com/prod/trial/lookup" class Module(object): _name = "UPC" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on( "upc", "ean", "gtin").hook( diff --git a/modules/urbandictionary.py b/modules/urbandictionary.py index cd5e87e2..da4098c8 100644 --- a/modules/urbandictionary.py +++ b/modules/urbandictionary.py @@ -5,7 +5,7 @@ URL_URBANDICTIONARY = "http://api.urbandictionary.com/v0/define" REGEX_DEFNUMBER = re.compile("-n(\d+) \S+") class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): events.on("received").on("command").on("urbandictionary", "ud" ).hook(self.ud, min_args=1, help="Get the definition of a provided term", diff --git a/modules/weather.py b/modules/weather.py index 20630ab4..39e3962f 100644 --- a/modules/weather.py +++ b/modules/weather.py @@ -5,7 +5,7 @@ import Utils URL_WEATHER = "http://api.openweathermap.org/data/2.5/weather" class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("weather").hook( self.weather, min_args=1, diff --git a/modules/wikipedia.py b/modules/wikipedia.py index 4d1585fe..61575822 100644 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -3,7 +3,7 @@ import Utils URL_WIKIPEDIA = "https://en.wikipedia.org/w/api.php" class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("wiki", "wi" ).hook(self.wikipedia, min_args=1) diff --git a/modules/wolframalpha.py b/modules/wolframalpha.py index 95084339..6ef56192 100644 --- a/modules/wolframalpha.py +++ b/modules/wolframalpha.py @@ -6,7 +6,7 @@ URL_WA = "https://api.wolframalpha.com/v1/result" class Module(object): _name = "Wolfram|Alpha" - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("command").on("wolframalpha", "wa" ).hook(self.wa, min_args=1, help= diff --git a/modules/words.py b/modules/words.py index 1cb432b0..49ae42ed 100644 --- a/modules/words.py +++ b/modules/words.py @@ -2,7 +2,7 @@ import time import Utils class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot events.on("received").on("message").on("channel" ).hook(self.channel_message) diff --git a/modules/youtube.py b/modules/youtube.py index 1ffd4db5..df158fcf 100644 --- a/modules/youtube.py +++ b/modules/youtube.py @@ -17,7 +17,7 @@ ARROW_UP = "▲" ARROW_DOWN = "▼" class Module(object): - def __init__(self, bot, events): + def __init__(self, bot, events, exports): self.bot = bot self.events = events events.on("received").on("command").on("yt", "youtube" @@ -26,11 +26,9 @@ class Module(object): events.on("received").on("message").on("channel").hook( self.channel_message) - events.on("postboot").on("configure").on( - "channelset").assure_call(setting="auto-youtube", - help="Disable/Enable automatically getting info from youtube URLs", - validate=Utils.bool_or_none) - + exports.add("channelset", {"setting": "auto-youtube", + "help": "Disable/Enable automatically getting info from " + "youtube URLs", "validate": Utils.bool_or_none}) def get_video_page(self, video_id, part): return Utils.get_url(URL_YOUTUBEVIDEO, get_params={"part": part,