Move plugins.Channel.banmask to protocols.irc.banmask

Also add protocols.irc.banmask.makeBanmask as a common function for
getting a banmask from a hostmask.
This commit is contained in:
James Vega 2009-01-19 21:12:36 +00:00
parent a84a0423ad
commit 775fe6fd4c
3 changed files with 69 additions and 56 deletions

View File

@ -1,5 +1,6 @@
### ###
# Copyright (c) 2004-2005, Jeremiah Fincher # Copyright (c) 2004-2005, Jeremiah Fincher
# Copyright (c) 2009, James Vega
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -40,42 +41,10 @@ def configure(advanced):
from supybot.questions import expect, anything, something, yn from supybot.questions import expect, anything, something, yn
conf.registerPlugin('Channel', True) conf.registerPlugin('Channel', True)
class BanmaskStyle(registry.SpaceSeparatedSetOfStrings):
validStrings = ('exact', 'nick', 'user', 'host')
def __init__(self, *args, **kwargs):
assert self.validStrings, 'There must be some valid strings. ' \
'This is a bug.'
registry.SpaceSeparatedSetOfStrings.__init__(self, *args, **kwargs)
self.__doc__ = format('Valid values include %L.',
map(repr, self.validStrings))
def help(self):
strings = [s for s in self.validStrings if s]
return format('%s Valid strings: %L.', self._help, strings)
def normalize(self, s):
lowered = s.lower()
L = list(map(str.lower, self.validStrings))
try:
i = L.index(lowered)
except ValueError:
return s # This is handled in setValue.
return self.validStrings[i]
def setValue(self, v):
v = map(self.normalize, v)
for s in v:
if s not in self.validStrings:
self.error()
registry.SpaceSeparatedSetOfStrings.setValue(self, self.List(v))
Channel = conf.registerPlugin('Channel') Channel = conf.registerPlugin('Channel')
conf.registerChannelValue(Channel, 'alwaysRejoin', conf.registerChannelValue(Channel, 'alwaysRejoin',
registry.Boolean(True, """Determines whether the bot will always try to registry.Boolean(True, """Determines whether the bot will always try to
rejoin a channel whenever it's kicked from the channel.""")) rejoin a channel whenever it's kicked from the channel."""))
conf.registerChannelValue(Channel, 'banmask',
BanmaskStyle(['user', 'host'], """Determines what will be used as the
default banmask style."""))
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -1,5 +1,6 @@
### ###
# Copyright (c) 2002-2005, Jeremiah Fincher # Copyright (c) 2002-2005, Jeremiah Fincher
# Copyright (c) 2009, James Vega
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -283,30 +284,8 @@ class Channel(callbacks.Plugin):
except KeyError: except KeyError:
irc.error(format('I haven\'t seen %s.', bannedNick), Raise=True) irc.error(format('I haven\'t seen %s.', bannedNick), Raise=True)
capability = ircdb.makeChannelCapability(channel, 'op') capability = ircdb.makeChannelCapability(channel, 'op')
def makeBanmask(bannedHostmask, options): banmaskstyle = conf.supybot.protocols.irc.banmask
(nick, user, host) = ircutils.splitHostmask(bannedHostmask) banmask = banmaskstyle.makeBanmask(bannedHostmask, [o[0] for o in optlist])
self.log.debug('*** nick: %s', nick)
self.log.debug('*** user: %s', user)
self.log.debug('*** host: %s', host)
bnick = '*'
buser = '*'
bhost = '*'
for option in options:
if option == 'nick':
bnick = nick
elif option == 'user':
buser = user
elif option == 'host':
bhost = host
elif option == 'exact':
(bnick, buser, bhost) = \
ircutils.splitHostmask(bannedHostmask)
return ircutils.joinHostmask(bnick, buser, bhost)
if optlist:
banmask = makeBanmask(bannedHostmask, [o[0] for o in optlist])
else:
banmask = makeBanmask(bannedHostmask,
self.registryValue('banmask', channel))
# Check (again) that they're not trying to make us kickban ourself. # Check (again) that they're not trying to make us kickban ourself.
if ircutils.hostmaskPatternEqual(banmask, irc.prefix): if ircutils.hostmaskPatternEqual(banmask, irc.prefix):
if ircutils.hostmaskPatternEqual(banmask, irc.prefix): if ircutils.hostmaskPatternEqual(banmask, irc.prefix):

View File

@ -900,6 +900,71 @@ registerGroup(supybot, 'protocols')
# supybot.protocols.irc # supybot.protocols.irc
### ###
registerGroup(supybot.protocols, 'irc') registerGroup(supybot.protocols, 'irc')
class Banmask(registry.SpaceSeparatedSetOfStrings):
validStrings = ('exact', 'nick', 'user', 'host')
def __init__(self, *args, **kwargs):
assert self.validStrings, 'There must be some valid strings. ' \
'This is a bug.'
self.__parent = super(Banmask, self)
self.__parent.__init__(*args, **kwargs)
self.__doc__ = format('Valid values include %L.',
map(repr, self.validStrings))
def help(self):
strings = [s for s in self.validStrings if s]
return format('%s Valid strings: %L.', self._help, strings)
def normalize(self, s):
lowered = s.lower()
L = list(map(str.lower, self.validStrings))
try:
i = L.index(lowered)
except ValueError:
return s # This is handled in setValue.
return self.validStrings[i]
def setValue(self, v):
v = map(self.normalize, v)
for s in v:
if s not in self.validStrings:
self.error()
self.__parent.setValue(self.List(v))
def makeBanmask(self, hostmask, options=None):
"""Create a banmask from the given hostmask. If a style of banmask
isn't specified via options, the value of
conf.supybot.protocols.irc.banmask is used.
A variable named 'channel' (defining the channel the ban is taking
place in) is expected to be in the environment of the caller of this
function.
options - A list specifying which parts of the hostmask should
explicitly be matched: nick, user, host. If 'exact' is given, then
only the exact hostmask will be used."""
assert ircutils.isChannel(dynamic.channel)
(nick, user, host) = ircutils.splitHostmask(hostmask)
bnick = '*'
buser = '*'
bhost = '*'
if not options:
options = get(supybot.protocols.irc.banmask, dynamic.channel)
for option in options:
if option == 'nick':
bnick = nick
elif option == 'user':
buser = user
elif option == 'host':
bhost = host
elif option == 'exact':
return hostmask
return ircutils.joinHostmask(bnick, buser, bhost)
registerChannelValue(supybot.protocols.irc, 'banmask',
Banmask(['user', 'host'], """Determines what will be used as the
default banmask style."""))
registerGlobalValue(supybot.protocols.irc, 'strictRfc', registerGlobalValue(supybot.protocols.irc, 'strictRfc',
registry.Boolean(False, """Determines whether the bot will strictly follow registry.Boolean(False, """Determines whether the bot will strictly follow
the RFC; currently this only affects what strings are considered to be the RFC; currently this only affects what strings are considered to be