From 696254ff1b742b033f9311738a1d192c6fa5c98f Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sun, 4 Aug 2019 18:11:28 +0200 Subject: [PATCH] Use irc.isChannel instead of ircutils.isChannel when possible. It's aware of the network's 'chantypes'. --- plugins/Aka/plugin.py | 16 ++++++++-------- plugins/Alias/plugin.py | 8 ++++---- plugins/BadWords/plugin.py | 2 +- plugins/Channel/plugin.py | 2 +- plugins/ChannelStats/plugin.py | 10 +++++----- plugins/Config/plugin.py | 18 +++++++++--------- plugins/Factoids/plugin.py | 2 +- plugins/Google/plugin.py | 4 ++-- plugins/Misc/plugin.py | 12 +++++++----- plugins/NickCapture/plugin.py | 2 +- plugins/Note/plugin.py | 12 ++++++------ plugins/RSS/plugin.py | 2 +- plugins/Reply/test.py | 2 +- plugins/Services/plugin.py | 2 +- plugins/Success/plugin.py | 2 +- src/callbacks.py | 6 +++--- src/irclib.py | 2 +- test/test_irclib.py | 5 +++++ 18 files changed, 58 insertions(+), 51 deletions(-) diff --git a/plugins/Aka/plugin.py b/plugins/Aka/plugin.py index edcea4a62..63e23c4c1 100644 --- a/plugins/Aka/plugin.py +++ b/plugins/Aka/plugin.py @@ -555,7 +555,7 @@ class Aka(callbacks.Plugin): channel = 'global' for (option, arg) in optlist: if option == 'channel': - if not ircutils.isChannel(arg): + if not irc.isChannel(arg): irc.error(_('%r is not a valid channel.') % arg, Raise=True) channel = arg @@ -587,7 +587,7 @@ class Aka(callbacks.Plugin): channel = 'global' for (option, arg) in optlist: if option == 'channel': - if not ircutils.isChannel(arg): + if not irc.isChannel(arg): irc.error(_('%r is not a valid channel.') % arg, Raise=True) channel = arg @@ -618,7 +618,7 @@ class Aka(callbacks.Plugin): channel = 'global' for (option, arg) in optlist: if option == 'channel': - if not ircutils.isChannel(arg): + if not irc.isChannel(arg): irc.error(_('%r is not a valid channel.') % arg, Raise=True) channel = arg @@ -650,7 +650,7 @@ class Aka(callbacks.Plugin): channel = 'global' for (option, arg) in optlist: if option == 'channel': - if not ircutils.isChannel(arg): + if not irc.isChannel(arg): irc.error(_('%r is not a valid channel.') % arg, Raise=True) channel = arg @@ -673,7 +673,7 @@ class Aka(callbacks.Plugin): channel = 'global' for (option, arg) in optlist: if option == 'channel': - if not ircutils.isChannel(arg): + if not irc.isChannel(arg): irc.error(_('%r is not a valid channel.') % arg, Raise=True) channel = arg @@ -696,7 +696,7 @@ class Aka(callbacks.Plugin): channel = 'global' for (option, arg) in optlist: if option == 'channel': - if not ircutils.isChannel(arg): + if not irc.isChannel(arg): irc.error(_('%r is not a valid channel.') % arg, Raise=True) channel = arg @@ -742,7 +742,7 @@ class Aka(callbacks.Plugin): filterlocked = filterunlocked = False for (option, arg) in optlist: if option == 'channel': - if not ircutils.isChannel(arg): + if not irc.isChannel(arg): irc.error(_('%r is not a valid channel.') % arg, Raise=True) channel = arg @@ -785,7 +785,7 @@ class Aka(callbacks.Plugin): channel = 'global' for (option, arg) in optlist: if option == 'channel': - if not ircutils.isChannel(arg): + if not irc.isChannel(arg): irc.error(_('%r is not a valid channel.') % arg, Raise=True) channel = arg diff --git a/plugins/Alias/plugin.py b/plugins/Alias/plugin.py index c833e06e3..8ea30d205 100644 --- a/plugins/Alias/plugin.py +++ b/plugins/Alias/plugin.py @@ -43,13 +43,13 @@ from supybot.i18n import PluginInternationalization, internationalizeDocstring _ = PluginInternationalization('Alias') # Copied from the old privmsgs.py. -def getChannel(msg, args=()): +def getChannel(irc, msg, args): """Returns the channel the msg came over or the channel given in args. If the channel was given in args, args is modified (the channel is removed). """ - if args and ircutils.isChannel(args[0]): + if args and irc.isChannel(msg.args[0]): if conf.supybot.reply.requireChannelCommandsToBeSentInChannel(): if args[0] != msg.args[0]: s = 'Channel commands must be sent in the channel to which ' \ @@ -60,7 +60,7 @@ def getChannel(msg, args=()): 'to False.' raise callbacks.Error(s) return args.pop(0) - elif ircutils.isChannel(msg.args[0]): + elif irc.isChannel(msg.args[0]): return msg.args[0] else: raise callbacks.Error('Command must be sent in a channel or ' \ @@ -173,7 +173,7 @@ def makeNewAlias(name, alias): def f(self, irc, msg, args): alias = original.replace('$nick', msg.nick) if '$channel' in original: - channel = getChannel(msg, args) + channel = getChannel(irc, msg, args) alias = alias.replace('$channel', channel) tokens = callbacks.tokenize(alias) if biggestDollar or biggestAt: diff --git a/plugins/BadWords/plugin.py b/plugins/BadWords/plugin.py index 54c511030..e2f5c4c8f 100644 --- a/plugins/BadWords/plugin.py +++ b/plugins/BadWords/plugin.py @@ -75,7 +75,7 @@ class BadWords(callbacks.Privmsg): channel = msg.args[0] self.updateRegexp(channel) s = ircutils.stripFormatting(msg.args[1]) - if ircutils.isChannel(channel) and self.registryValue('kick', channel): + if irc.isChannel(channel) and self.registryValue('kick', channel): if self.regexp.search(s): c = irc.state.channels[channel] cap = ircdb.makeChannelCapability(channel, 'op') diff --git a/plugins/Channel/plugin.py b/plugins/Channel/plugin.py index 1581b65b1..0b83eba7c 100644 --- a/plugins/Channel/plugin.py +++ b/plugins/Channel/plugin.py @@ -912,7 +912,7 @@ class Channel(callbacks.Plugin): if 's' in irc.state.channels[channel].modes and \ msg.args[0] != channel and \ not ircdb.checkCapability(msg.prefix, capability) and \ - (ircutils.isChannel(msg.args[0]) or \ + (irc.isChannel(msg.args[0]) or \ msg.nick not in irc.state.channels[channel].users): irc.error(_('You don\'t have access to that information.'), Raise=True) diff --git a/plugins/ChannelStats/plugin.py b/plugins/ChannelStats/plugin.py index 2aa4eb5bd..13e3e8b5a 100644 --- a/plugins/ChannelStats/plugin.py +++ b/plugins/ChannelStats/plugin.py @@ -141,8 +141,8 @@ class StatsDB(plugins.ChannelUserDB): else: return UserStat(*L) - def addMsg(self, msg, id=None): - if msg.args and ircutils.isChannel(msg.args[0]): + def addMsg(self, irc, msg, id=None): + if msg.args and irc.isChannel(msg.args[0]): channel = plugins.getChannel(msg.args[0]) if (channel, 'channelStats') not in self: self[channel, 'channelStats'] = ChannelStat() @@ -181,16 +181,16 @@ class ChannelStats(callbacks.Plugin): self.__parent.die() def __call__(self, irc, msg): - self.db.addMsg(msg) + self.db.addMsg(irc, msg) super(ChannelStats, self).__call__(irc, msg) def outFilter(self, irc, msg): if msg.command == 'PRIVMSG': - if ircutils.isChannel(msg.args[0]): + if irc.isChannel(msg.args[0]): if self.registryValue('selfStats', msg.args[0]): try: self.outFiltering = True - self.db.addMsg(msg, 0) + self.db.addMsg(irc, msg, 0) finally: self.outFiltering = False return msg diff --git a/plugins/Config/plugin.py b/plugins/Config/plugin.py index c3780766f..a5505bf7a 100644 --- a/plugins/Config/plugin.py +++ b/plugins/Config/plugin.py @@ -63,7 +63,7 @@ def getWrapper(name): raise registry.InvalidRegistryName(name) return group -def getCapability(name): +def getCapability(irc, name): capability = 'owner' # Default to requiring the owner capability. if not name.startswith('supybot') and not name.startswith('users'): name = 'supybot.' + name @@ -74,7 +74,7 @@ def getCapability(name): group = group.get(part) if not getattr(group, '_opSettable', True): return 'owner' - if ircutils.isChannel(part): + if irc.isChannel(part): # If a registry value has a channel in it, it requires a # 'channel,op' capability, or so we assume. We'll see if we're # proven wrong. @@ -148,15 +148,15 @@ class Config(callbacks.Plugin): except registry.InvalidRegistryValue as e: irc.error(str(e)) - def _list(self, group): + def _list(self, irc, group): L = [] for (vname, v) in group._children.items(): if hasattr(group, 'channelValue') and group.channelValue and \ - ircutils.isChannel(vname) and not v._children: + irc.isChannel(vname) and not v._children: continue if hasattr(v, 'channelValue') and v.channelValue: vname = '#' + vname - if v._added and not all(ircutils.isChannel, v._added): + if v._added and not all(irc.isChannel, v._added): vname = '@' + vname L.append(vname) utils.sortBy(str.lower, L) @@ -172,7 +172,7 @@ class Config(callbacks.Plugin): it can be separately configured for each channel using the 'channel' command in this plugin, it is preceded by an '#' sign. """ - L = self._list(group) + L = self._list(irc, group) if L: irc.reply(format('%L', L)) else: @@ -190,7 +190,7 @@ class Config(callbacks.Plugin): for (name, x) in conf.supybot.getValues(getChildren=True): if word in name.lower(): possibleChannel = registry.split(name)[-1] - if not ircutils.isChannel(possibleChannel): + if not irc.isChannel(possibleChannel): L.append(name) if L: irc.reply(format('%L', L)) @@ -207,7 +207,7 @@ class Config(callbacks.Plugin): if not group._private: return (value, None) else: - capability = getCapability(group._name) + capability = getCapability(irc, group._name) if ircdb.checkCapability(msg.prefix, capability): return (value, True) else: @@ -222,7 +222,7 @@ class Config(callbacks.Plugin): irc.error(_("This configuration variable is not writeable " "via IRC. To change it you have to: 1) use the 'flush' command 2) edit " "the config file 3) use the 'config reload' command."), Raise=True) - capability = getCapability(group._name) + capability = getCapability(irc, group._name) if ircdb.checkCapability(msg.prefix, capability): # I think callCommand catches exceptions here. Should it? group.set(value) diff --git a/plugins/Factoids/plugin.py b/plugins/Factoids/plugin.py index 18bded0ab..1028d8fb6 100644 --- a/plugins/Factoids/plugin.py +++ b/plugins/Factoids/plugin.py @@ -128,7 +128,7 @@ class FactoidsCallback(httpserver.SupyHTTPServerCallback): self.write(httpserver.get_template('factoids/index.html')) elif len(parts) == 2: channel = utils.web.urlunquote(parts[0]) - if not ircutils.isChannel(channel): + if not irc.isChannel(channel): self.send_response(404) self.send_header('Content-type', 'text/html; charset=utf-8') self.end_headers() diff --git a/plugins/Google/plugin.py b/plugins/Google/plugin.py index ccf18647e..42a6f1a0f 100644 --- a/plugins/Google/plugin.py +++ b/plugins/Google/plugin.py @@ -326,7 +326,7 @@ class Google(callbacks.PluginRegexp): Uses Google's calculator to calculate the value of . """ channel = msg.args[0] - if not ircutils.isChannel(channel): + if not irc.isChannel(channel): channel = None url = self._googleUrl(expr, channel) h = {"User-Agent":"Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"} @@ -360,7 +360,7 @@ class Google(callbacks.PluginRegexp): Looks up on Google. """ channel = msg.args[0] - if not ircutils.isChannel(channel): + if not irc.isChannel(channel): channel = None url = self._googleUrl(phonenumber, channel) html = utils.web.getUrl(url).decode('utf8') diff --git a/plugins/Misc/plugin.py b/plugins/Misc/plugin.py index 70ad0e3c8..bfc587315 100644 --- a/plugins/Misc/plugin.py +++ b/plugins/Misc/plugin.py @@ -34,6 +34,7 @@ import imp import sys import json import time +import functools import supybot @@ -402,10 +403,10 @@ class Misc(callbacks.Plugin): irc.error(_('That\'s all, there is no more.')) more = wrap(more, [additional('seenNick')]) - def _validLastMsg(self, msg): + def _validLastMsg(self, irc, msg): return msg.prefix and \ msg.command == 'PRIVMSG' and \ - ircutils.isChannel(msg.args[0]) + irc.isChannel(msg.args[0]) @internationalizeDocstring def last(self, irc, msg, args, optlist): @@ -422,7 +423,7 @@ class Misc(callbacks.Plugin): predicates = {} nolimit = False skipfirst = True - if ircutils.isChannel(msg.args[0]): + if irc.isChannel(msg.args[0]): predicates['in'] = lambda m: ircutils.strEqual(m.args[0], msg.args[0]) else: @@ -469,7 +470,8 @@ class Misc(callbacks.Plugin): predicates.setdefault('regexp', []).append(f) elif option == 'nolimit': nolimit = True - iterable = filter(self._validLastMsg, reversed(irc.state.history)) + iterable = filter(functools.partial(self._validLastMsg, irc), + reversed(irc.state.history)) if skipfirst: # Drop the first message only if our current channel is the same as # the channel we've been instructed to look at. @@ -535,7 +537,7 @@ class Misc(callbacks.Plugin): irc.error('This command cannot be nested.', Raise=True) if target.lower() == 'me': target = msg.nick - if ircutils.isChannel(target): + if irc.isChannel(target): irc.error(_('Hey, just give the command. No need for the tell.')) return if not ircutils.isNick(target): diff --git a/plugins/NickCapture/plugin.py b/plugins/NickCapture/plugin.py index b9a0d40ba..0e6061724 100644 --- a/plugins/NickCapture/plugin.py +++ b/plugins/NickCapture/plugin.py @@ -120,7 +120,7 @@ class NickCapture(callbacks.Plugin): def do437(self, irc, msg): """Nick/channel is temporarily unavailable""" - if ircutils.isChannel(msg.args[1]): + if irc.isChannel(msg.args[1]): return self.log.info('Nick %s is unavailable; attempting NickServ release ' 'on %s.' % (msg.args[1], irc.network)) diff --git a/plugins/Note/plugin.py b/plugins/Note/plugin.py index ad41f75e8..b89ae2a7a 100644 --- a/plugins/Note/plugin.py +++ b/plugins/Note/plugin.py @@ -270,8 +270,8 @@ class Note(callbacks.Plugin): self.db.setRead(id) note = wrap(note, ['user', ('id', 'note')]) - def _formatNoteId(self, msg, note, sent=False): - if note.public or not ircutils.isChannel(msg.args[0]): + def _formatNoteId(self, irc, msg, note, sent=False): + if note.public or not irc.isChannel(msg.args[0]): if sent: sender = plugins.getUserName(note.to) return format('#%i to %s', note.id, sender) @@ -315,7 +315,7 @@ class Note(callbacks.Plugin): irc.reply('No matching notes were found.') else: utils.sortBy(operator.attrgetter('id'), notes) - ids = [self._formatNoteId(msg, note) for note in notes] + ids = [self._formatNoteId(irc, msg, note) for note in notes] ids = self._condense(ids) irc.reply(format('%L', ids)) search = wrap(search, @@ -357,7 +357,7 @@ class Note(callbacks.Plugin): irc.reply('You have no unread notes.') else: utils.sortBy(operator.attrgetter('id'), notes) - ids = [self._formatNoteId(msg, note) for note in notes] + ids = [self._formatNoteId(irc, msg, note) for note in notes] ids = self._condense(ids) irc.reply(format('%L.', ids)) list = wrap(list, ['user', getopts({'old': '', 'sent': '', @@ -421,7 +421,7 @@ class Note(callbacks.Plugin): else: utils.sortBy(operator.attrgetter('id'), notes) notes.reverse() # Most recently sent first. - ids = [self._formatNoteId(msg, note, sent=True) for note in notes] + ids = [self._formatNoteId(irc, msg, note, sent=True) for note in notes] ids = self._condense(ids) irc.reply(format('%L.', ids)) @@ -444,7 +444,7 @@ class Note(callbacks.Plugin): else: utils.sortBy(operator.attrgetter('id'), notes) notes.reverse() - ids = [self._formatNoteId(msg, note) for note in notes] + ids = [self._formatNoteId(irc, msg, note) for note in notes] ids = self._condense(ids) irc.reply(format('%L.', ids)) diff --git a/plugins/RSS/plugin.py b/plugins/RSS/plugin.py index a79442654..020e853bc 100644 --- a/plugins/RSS/plugin.py +++ b/plugins/RSS/plugin.py @@ -63,7 +63,7 @@ else: from urllib.request import ProxyHandler def get_feedName(irc, msg, args, state): - if ircutils.isChannel(args[0]): + if irc.isChannel(args[0]): state.errorInvalid('feed name', args[0], 'must not be channel names.') if not registry.isValidRegistryName(args[0]): state.errorInvalid('feed name', args[0], diff --git a/plugins/Reply/test.py b/plugins/Reply/test.py index 5d9a184bd..525f8835f 100644 --- a/plugins/Reply/test.py +++ b/plugins/Reply/test.py @@ -34,7 +34,7 @@ class ReplyTestCase(ChannelPluginTestCase): plugins = ('Reply',) def testPrivate(self): m = self.getMsg('private [list]') - self.failIf(ircutils.isChannel(m.args[0])) + self.failIf(self.irc.isChannel(m.args[0])) def testNotice(self): m = self.getMsg('notice [list]') diff --git a/plugins/Services/plugin.py b/plugins/Services/plugin.py index b3aa27ab7..d268af47d 100644 --- a/plugins/Services/plugin.py +++ b/plugins/Services/plugin.py @@ -276,7 +276,7 @@ class Services(callbacks.Plugin): chanTypes = irc.state.supported['CHANTYPES'] if re.match(r'^\[[%s]' % re.escape(chanTypes), s): self.log.debug('Got entrymsg from ChanServ %s.', on) - elif ircutils.isChannel(msg.args[0]): + elif irc.isChannel(msg.args[0]): # Atheme uses channel-wide notices for alerting channel access # changes if the FANTASY or VERBOSE setting is on; we can suppress # these 'unexpected notice' warnings since they're not really diff --git a/plugins/Success/plugin.py b/plugins/Success/plugin.py index 68c68a048..602a90df6 100644 --- a/plugins/Success/plugin.py +++ b/plugins/Success/plugin.py @@ -62,7 +62,7 @@ class Success(plugins.ChannelIdDatabasePlugin): return ret def get(self, attr): - if ircutils.isChannel(attr): + if irc.isChannel(attr): pluginSelf.target = attr return self conf.supybot.replies.success.__class__ = MySuccessClass diff --git a/src/callbacks.py b/src/callbacks.py index 7e8b52f30..6d1676e97 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -470,7 +470,7 @@ class RichReplyMethods(object): joiner = joiner.join to = self._getTarget(kwargs.get('to')) if oneToOne is None: # Can be True, False, or None - if ircutils.isChannel(to): + if self.irc.isChannel(to): oneToOne = conf.get(conf.supybot.reply.oneToOne, to) else: oneToOne = conf.supybot.reply.oneToOne() @@ -665,7 +665,7 @@ class NestedCommandsIrcProxy(ReplyIrcProxy): self.notice = None self.private = None self.noLengthCheck = None - if ircutils.isChannel(self.msg.args[0]): + if self.irc.isChannel(self.msg.args[0]): self.prefixNick = conf.get(conf.supybot.reply.withNickPrefix, self.msg.args[0]) else: @@ -976,7 +976,7 @@ class NestedCommandsIrcProxy(ReplyIrcProxy): pass # We'll leave it as it is. mask = prefix.split('!', 1)[1] self._mores[mask] = msgs - public = ircutils.isChannel(msg.args[0]) + public = self.irc.isChannel(msg.args[0]) private = self.private or not public self._mores[msg.nick] = (private, msgs) m = reply(msg, response, to=self.to, diff --git a/src/irclib.py b/src/irclib.py index 0fc8dca08..744e0a5b5 100644 --- a/src/irclib.py +++ b/src/irclib.py @@ -569,7 +569,7 @@ class IrcState(IrcCommandDispatcher, log.Firewalled): def doMode(self, irc, msg): channel = msg.args[0] - if ircutils.isChannel(channel): # There can be user modes, as well. + if irc.isChannel(channel): # There can be user modes, as well. try: chan = self.channels[channel] except KeyError: diff --git a/test/test_irclib.py b/test/test_irclib.py index 99d6e4353..f320b6cd8 100644 --- a/test/test_irclib.py +++ b/test/test_irclib.py @@ -35,6 +35,7 @@ import pickle import supybot.conf as conf import supybot.irclib as irclib import supybot.ircmsgs as ircmsgs +import supybot.ircutils as ircutils # The test framework used to provide these, but not it doesn't. We'll add # messages to as we find bugs (if indeed we find bugs). @@ -229,6 +230,10 @@ class IrcStateTestCase(SupyTestCase): class FakeIrc: nick = 'nick' prefix = 'nick!user@host' + + def isChannel(self, s): + return ircutils.isChannel(s) + irc = FakeIrc() def testKickRemovesChannel(self): st = irclib.IrcState()