From b8bbcd49710a5b78a2d5595b5ff8ac868b4a4df8 Mon Sep 17 00:00:00 2001 From: iou1name Date: Sun, 27 May 2018 14:16:50 -0400 Subject: [PATCH] modeChanged() works properly, small changes to module loading --- bot.py | 91 +++++++++++++++++++++++------------------------ loader.py | 3 +- modules/url.py | 4 ++- tools/__init__.py | 5 ++- 4 files changed, 52 insertions(+), 51 deletions(-) diff --git a/bot.py b/bot.py index 55bbad2..edb469d 100755 --- a/bot.py +++ b/bot.py @@ -120,34 +120,33 @@ class Fulvia(irc.IRCClient): print(f"{failed} modules failed to load") - def register_callable(self, callables): + def register_callable(self, func): """ Registers all callable functions loaded from modules into one convenient table. """ - for func in callables: - if hasattr(func, 'commands'): - self._callables[func.__name__] = func - for cmd in func.commands: - self.commands[cmd] = tools.Command(cmd) - self.commands[cmd]._func_name = func.__name__ - self.commands[cmd].priv = func.priv - self.commands[cmd].doc = func._docs - if cmd in func.aliases: - self.commands[cmd].canonical = False - aliases = [a for a in func.commands if a != cmd] - self.commands[cmd].aliases = aliases + if hasattr(func, 'commands'): + self._callables[func.__name__] = func + for cmd in func.commands: + self.commands[cmd] = tools.Command(cmd) + self.commands[cmd]._func_name = func.__name__ + self.commands[cmd].priv = func.priv + self.commands[cmd].doc = func._docs + if cmd in func.aliases: + self.commands[cmd].canonical = False + aliases = [a for a in func.commands if a != cmd] + self.commands[cmd].aliases = aliases - if func.hook: - self._callables[func.__name__] = func - self._hooks.append(func.__name__) + if func.hook: + self._callables[func.__name__] = func + self._hooks.append(func.__name__) - if func.rate or func.channel_rate or func.global_rate: - self._times[func.__name__] = {} + if func.rate or func.channel_rate or func.global_rate: + self._times[func.__name__] = {} - if hasattr(func, 'url_callback'): - for url in func.url_callback: - self.url_callbacks[url] = func + if hasattr(func, 'url_callback'): + for url in func.url_callback: + self.url_callbacks[url] = func def unregister_callable(self, func): @@ -239,7 +238,7 @@ class Fulvia(irc.IRCClient): self._times[func_name]["global"] = time.time() if func.thread == True: - t = threading.Thread(target=self.call, args=(func, bot, trigger)) + t = threading.Thread(target=self.call,args=(func, bot, trigger)) t.start() else: self.call(func, bot, trigger) @@ -260,17 +259,26 @@ class Fulvia(irc.IRCClient): def modeChanged(self, user, channel, add_mode, modes, args): """Called when users or channel's modes are changed.""" - # TODO: do this function the right way - # TODO: channel modes - # modes[0] is lazy, but ought to work in most cases - if modes[0] in tools.op_level.keys(): - for n, name in enumerate(args): + if not channel.startswith("#"): # server level + return + + for n, mode in enumerate(modes): + if args[n] == None: # channel mode if add_mode: - mode = tools.op_level[modes[n]] - self.channels[channel].privileges[name] = mode + self.channels[channel].modes.add(mode) else: - # this is extremely lazy and won't work in a lot of cases - self.channels[channel].privileges[name] = 0 + self.channels[channel].modes.remove(mode) + + elif mode in tools.op_level.keys(): # user mode, op_level mode + nick = args[n] + op_level = tools.op_level[mode] + if add_mode: + self.channels[channel].privileges[nick] += op_level + else: + self.channels[channel].privileges[nick] -= op_level + + else: # user mode, non-op_level mode + continue def signedOn(self): @@ -348,19 +356,10 @@ class Fulvia(irc.IRCClient): ## User commands, from client->server - def msg(self, user, message, length=None): - """ - Send a message to a user or channel. See irc.IRCClient.msg for more - information. - Provided solely for documentation and clarity's sake. - """ - irc.IRCClient.msg(self, user, message, length=None) - - def reply(self, text, dest, reply_to, notice=False): """ - For compatibility with most of the sopel modules. Will phase it out - in favor of self.msg eventually. + Sends a message to 'dest' prefixed with 'reply_to' and a colon, + ala "reply_to: text". More useful with the wrapper class. """ text = reply_to + ": " + text self.msg(dest, text) @@ -392,11 +391,11 @@ class Fulvia(irc.IRCClient): class FulviaWrapper(): + """ + A wrapper class for Fulvia to provide default destinations for msg + methods and so forth. + """ def __init__(self, fulvia, trigger): - """ - A wrapper class for Fulvia to provide default destinations for - msg methods and so forth. - """ object.__setattr__(self, '_bot', fulvia) object.__setattr__(self, '_trigger', trigger) # directly setting these values would cause a recursion loop diff --git a/loader.py b/loader.py index 950fb53..c57f52c 100755 --- a/loader.py +++ b/loader.py @@ -15,7 +15,8 @@ def load_module(bot, path): if hasattr(module, 'setup'): module.setup(bot) relevant_parts = process_module(module, bot.config) - bot.register_callable(relevant_parts) + for part in relevant_parts: + bot.register_callable(part) def unload_module(bot, name): diff --git a/modules/url.py b/modules/url.py index c04d1ca..7e97abd 100755 --- a/modules/url.py +++ b/modules/url.py @@ -42,7 +42,9 @@ def title_auto(bot, trigger): res.raise_for_status() except: continue - if not res.headers["Content-Type"].startswith("text/html"): + if not res.headers.get("Content-Type"): + continue + if not res.headers.get("Content-Type").startswith("text/html"): continue if res.text.find("") == -1: continue diff --git a/tools/__init__.py b/tools/__init__.py index 32a8619..d4cdf33 100755 --- a/tools/__init__.py +++ b/tools/__init__.py @@ -100,9 +100,8 @@ class Channel(object): self.privileges = {} """The op levels of the users in the channel.""" - self.modes = "" - """The mode of the channel.""" - # NOTE: this doesn't work yet + self.modes = set() + """The current modes on the channel.""" def remove_user(self, nick): """