Use irc.isChannel instead of ircutils.isChannel when possible.

It's aware of the network's 'chantypes'.
This commit is contained in:
Valentin Lorentz 2019-08-04 18:11:28 +02:00
parent bf7db13f01
commit 696254ff1b
18 changed files with 58 additions and 51 deletions

View File

@ -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

View File

@ -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:

View File

@ -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')

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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()

View File

@ -326,7 +326,7 @@ class Google(callbacks.PluginRegexp):
Uses Google's calculator to calculate the value of <expression>.
"""
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 <phone number> 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')

View File

@ -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):

View File

@ -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))

View File

@ -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))

View File

@ -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],

View File

@ -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]')

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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:

View File

@ -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()