Limnoria/src/ircdb.py

702 lines
25 KiB
Python
Raw Normal View History

2003-03-12 07:26:59 +01:00
#!/usr/bin/env python
###
# Copyright (c) 2002, Jeremiah Fincher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
2003-11-25 09:38:19 +01:00
__revision__ = "$Id$"
import fix
2003-03-12 07:26:59 +01:00
import os
2003-07-31 08:20:58 +02:00
import sets
2003-03-12 07:26:59 +01:00
import time
import string
from itertools import imap, ilen, ifilter
2003-03-12 07:26:59 +01:00
import log
2003-03-12 07:26:59 +01:00
import conf
import utils
2003-03-12 07:26:59 +01:00
import world
import ircutils
from structures import PersistentDictionary
2003-03-12 07:26:59 +01:00
def fromChannelCapability(capability):
2003-09-13 19:29:56 +02:00
"""Returns a (channel, capability) tuple from a channel capability."""
if not isChannelCapability(capability):
raise ValueError, '%s is not a channel capability' % capability
#return capability.rsplit('.', 1)
return rsplit(capability, '.', 1)
2003-03-12 07:26:59 +01:00
def isChannelCapability(capability):
2003-09-13 19:29:56 +02:00
"""Returns True if capability is a channel capability; False otherwise."""
2003-03-12 07:26:59 +01:00
if '.' in capability:
2003-04-20 23:52:53 +02:00
(channel, capability) = capability.split('.', 1)
2003-03-12 07:26:59 +01:00
return ircutils.isChannel(channel)
else:
return False
def makeChannelCapability(channel, capability):
2003-09-13 19:29:56 +02:00
"""Makes a channel capability given a channel and a capability."""
2003-03-12 07:26:59 +01:00
return '%s.%s' % (channel, capability)
def isAntiCapability(capability):
2003-09-13 19:29:56 +02:00
"""Returns True if capability is an anticapability; False otherwise."""
2003-03-12 07:26:59 +01:00
if isChannelCapability(capability):
(_, capability) = fromChannelCapability(capability)
return capability[0] == '-'
2003-03-12 07:26:59 +01:00
def makeAntiCapability(capability):
2003-09-13 19:29:56 +02:00
"""Returns the anticapability of a given capability."""
assert not isAntiCapability(capability), 'makeAntiCapability does not ' \
'work on anticapabilities; you probably want invertCapability.'
2003-03-12 07:26:59 +01:00
if '.' in capability:
(channel, capability) = fromChannelCapability(capability)
return '%s.-%s' % (channel, capability)
2003-03-12 07:26:59 +01:00
else:
return '-' + capability
2003-03-12 07:26:59 +01:00
2003-04-20 23:52:53 +02:00
def unAntiCapability(capability):
2003-09-13 19:29:56 +02:00
"""Takes an anticapability and returns the non-anti form."""
if not isAntiCapability(capability):
raise ValueError, '%s is not an anti capability' % capability
2003-04-20 23:52:53 +02:00
if isChannelCapability(capability):
(channel, capability) = fromChannelCapability(capability)
return '.'.join((channel, capability[1:]))
else:
return capability[1:]
def invertCapability(capability):
2003-09-13 19:29:56 +02:00
"""Make a capability into an anticapability and vice versa."""
2003-04-20 23:52:53 +02:00
if isAntiCapability(capability):
return unAntiCapability(capability)
else:
return makeAntiCapability(capability)
2003-03-12 07:26:59 +01:00
_normal = string.maketrans('\r\n', ' ')
2003-09-13 19:29:56 +02:00
def _normalize(s):
2003-03-12 07:26:59 +01:00
return s.translate(_normal)
_invert = invertCapability
2003-07-31 08:20:58 +02:00
class CapabilitySet(sets.Set):
2003-09-13 19:29:56 +02:00
"""A subclass of set handling basic capability stuff."""
2003-04-20 23:52:53 +02:00
def __init__(self, capabilities=()):
self.__parent = super(CapabilitySet, self)
self.__parent.__init__()
2003-04-20 23:52:53 +02:00
for capability in capabilities:
self.add(capability)
def add(self, capability):
"""Adds a capability to the set."""
2003-04-20 23:52:53 +02:00
capability = ircutils.toLower(capability)
inverted = _invert(capability)
if self.__parent.__contains__(inverted):
self.__parent.remove(inverted)
self.__parent.add(capability)
2003-04-20 23:52:53 +02:00
def remove(self, capability):
"""Removes a capability from the set."""
2003-04-20 23:52:53 +02:00
capability = ircutils.toLower(capability)
self.__parent.remove(capability)
2003-04-20 23:52:53 +02:00
def __contains__(self, capability):
capability = ircutils.toLower(capability)
if self.__parent.__contains__(capability):
2003-04-20 23:52:53 +02:00
return True
if self.__parent.__contains__(_invert(capability)):
2003-04-20 23:52:53 +02:00
return True
else:
return False
def check(self, capability):
"""Returns the appropriate boolean for whether a given capability is
'allowed' given its (or its anticapability's) presence in the set.
"""
2003-04-20 23:52:53 +02:00
capability = ircutils.toLower(capability)
if self.__parent.__contains__(capability):
2003-04-20 23:52:53 +02:00
return True
elif self.__parent.__contains__(_invert(capability)):
2003-04-20 23:52:53 +02:00
return False
else:
raise KeyError, capability
def __repr__(self):
return '%s([%s])' % (self.__class__.__name__,
', '.join(imap(repr, self)))
2003-04-20 23:52:53 +02:00
antiOwner = makeAntiCapability('owner')
2003-04-20 23:52:53 +02:00
class UserCapabilitySet(CapabilitySet):
2003-09-13 19:29:56 +02:00
"""A subclass of CapabilitySet to handle the owner capability correctly."""
def __init__(self, *args, **kwargs):
self.__parent = super(UserCapabilitySet, self)
self.__parent.__init__(*args, **kwargs)
2003-04-20 23:52:53 +02:00
def __contains__(self, capability):
capability = ircutils.toLower(capability)
if capability == 'owner' or capability == antiOwner:
return True
elif self.__parent.__contains__('owner'):
2003-04-20 23:52:53 +02:00
return True
else:
return self.__parent.__contains__(capability)
2003-04-20 23:52:53 +02:00
def check(self, capability):
"""Returns the appropriate boolean for whether a given capability is
'allowed' given its (or its anticapability's) presence in the set.
Differs from CapabilitySet in that it handles the 'owner' capability
appropriately.
"""
2003-04-20 23:52:53 +02:00
capability = ircutils.toLower(capability)
if capability == 'owner' or capability == antiOwner:
if self.__parent.__contains__('owner'):
return not isAntiCapability(capability)
2003-04-20 23:52:53 +02:00
else:
return isAntiCapability(capability)
elif self.__parent.__contains__('owner'):
2003-04-20 23:52:53 +02:00
if isAntiCapability(capability):
return False
else:
return True
else:
return self.__parent.check(capability)
2003-04-20 23:52:53 +02:00
def add(self, capability):
"""Adds a capability to the set. Just make sure it's not -owner."""
2003-04-20 23:52:53 +02:00
capability = ircutils.toLower(capability)
assert capability != '-owner', '"-owner" disallowed.'
self.__parent.add(capability)
2003-08-20 18:26:23 +02:00
2003-03-12 07:26:59 +01:00
class IrcUser(object):
"""This class holds the capabilities and authentications for a user."""
def __init__(self, ignore=False, password='', name='',
capabilities=(), hostmasks=None, secure=False, hashed=False):
self.auth = None # The (time, hostmask) a user authenticated under
self.name = name # The name of the user.
2003-03-12 07:26:59 +01:00
self.ignore = ignore # A boolean deciding if the person is ignored.
self.secure = secure # A boolean describing if hostmasks *must* match.
self.hashed = hashed # True if the password is hashed on disk.
2003-03-12 07:26:59 +01:00
self.password = password # password (plaintext? hashed?)
2003-04-20 23:52:53 +02:00
self.capabilities = UserCapabilitySet()
for capability in capabilities:
self.capabilities.add(capability)
2003-03-12 07:26:59 +01:00
if hostmasks is None:
self.hostmasks = [] # A list of hostmasks used for recognition
else:
self.hostmasks = hostmasks
def __repr__(self):
return '%s(ignore=%s, password=%r, name=%r, hashed=%r, ' \
'capabilities=%r, hostmasks=%r, secure=%r)\n' % \
(self.__class__.__name__,
self.ignore, self.password, self.name, self.hashed,
self.capabilities, self.hostmasks, self.secure)
2003-03-12 07:26:59 +01:00
def addCapability(self, capability):
"""Gives the user the given capability."""
2003-03-12 07:26:59 +01:00
self.capabilities.add(capability)
def removeCapability(self, capability):
"""Takes from the user the given capability."""
self.capabilities.remove(capability)
2003-03-12 07:26:59 +01:00
def checkCapability(self, capability):
"""Checks the user for a given capability."""
2003-04-20 23:52:53 +02:00
if self.ignore:
2003-03-12 07:26:59 +01:00
if isAntiCapability(capability):
return True
else:
return False
else:
2003-04-20 23:52:53 +02:00
return self.capabilities.check(capability)
2003-03-12 07:26:59 +01:00
def setPassword(self, password, hashed=False):
"""Sets the user's password."""
if hashed or self.hashed:
self.hashed = True
self.password = utils.saltHash(password)
else:
self.password = password
2003-03-12 07:26:59 +01:00
def checkPassword(self, password):
"""Checks the user's password."""
if self.hashed:
(salt, _) = self.password.split('|')
return (self.password == utils.saltHash(password, salt=salt))
else:
return (self.password == password)
2003-03-12 07:26:59 +01:00
def checkHostmask(self, hostmask, useAuth=True):
"""Checks a given hostmask against the user's hostmasks or current
authentication. If useAuth is False, only checks against the user's
hostmasks.
"""
if useAuth and self.auth and (hostmask == self.auth[1]):
2003-03-12 07:26:59 +01:00
return True
for pat in self.hostmasks:
if ircutils.hostmaskPatternEqual(pat, hostmask):
return True
return False
def addHostmask(self, hostmask):
"""Adds a hostmask to the user's hostmasks."""
2003-03-12 07:26:59 +01:00
self.hostmasks.append(hostmask)
def removeHostmask(self, hostmask):
"""Removes a hostmask from the user's hostmasks."""
self.hostmasks = [s for s in self.hostmasks if s != hostmask]
2003-03-12 07:26:59 +01:00
def setAuth(self, hostmask):
"""Sets a user's authenticated hostmask. This times out in 1 hour."""
if self.checkHostmask(hostmask, useAuth=False) or not self.secure:
self.auth = (time.time(), hostmask)
else:
raise ValueError, 'secure flag set, unmatched hostmask'
2003-03-12 07:26:59 +01:00
def unsetAuth(self):
"""Unsets a use's authenticated hostmask."""
2003-03-12 07:26:59 +01:00
self.auth = None
class IrcChannel(object):
"""This class holds the capabilities, bans, and ignores of a channel.
"""
2003-04-02 13:08:34 +02:00
defaultOff = ('op', 'halfop', 'voice', 'protected')
2003-03-12 07:26:59 +01:00
def __init__(self, bans=None, ignores=None, capabilities=None,
lobotomized=False, defaultAllow=True):
self.defaultAllow = defaultAllow
if bans is None:
self.bans = []
else:
self.bans = bans
if ignores is None:
self.ignores = []
else:
self.ignores = ignores
if capabilities is None:
2003-04-20 23:52:53 +02:00
self.capabilities = CapabilitySet()
2003-03-12 07:26:59 +01:00
else:
self.capabilities = capabilities
for capability in self.defaultOff:
if capability not in self.capabilities:
2003-04-02 13:08:34 +02:00
self.capabilities.add(makeAntiCapability(capability))
2003-03-12 07:26:59 +01:00
self.lobotomized = lobotomized
def __repr__(self):
return '%s(bans=%r, ignores=%r, capabilities=%r, ' \
'lobotomized=%r, defaultAllow=%s)\n' % \
2003-03-12 07:26:59 +01:00
(self.__class__.__name__, self.bans, self.ignores,
self.capabilities, self.lobotomized,
self.defaultAllow)
def addBan(self, hostmask):
"""Adds a ban to the channel banlist."""
2003-03-12 07:26:59 +01:00
self.bans.append(hostmask)
def removeBan(self, hostmask):
"""Removes a ban from the channel banlist."""
2003-03-12 07:26:59 +01:00
self.bans = [s for s in self.bans if s != hostmask]
def checkBan(self, hostmask):
"""Checks whether a given hostmask is banned by the channel banlist."""
2003-03-12 07:26:59 +01:00
for pat in self.bans:
if ircutils.hostmaskPatternEqual(pat, hostmask):
return True
return False
def addIgnore(self, hostmask):
"""Adds an ignore to the channel ignore list."""
2003-03-12 07:26:59 +01:00
self.ignores.append(hostmask)
def removeIgnore(self, hostmask):
"""Removes an ignore from the channel ignore list."""
2003-03-12 07:26:59 +01:00
self.ignores = [s for s in self.ignores if s != hostmask]
2003-04-02 13:08:34 +02:00
def addCapability(self, capability):
"""Adds a capability to the channel's default capabilities."""
2003-04-02 13:08:34 +02:00
self.capabilities.add(capability)
2003-03-12 07:26:59 +01:00
def removeCapability(self, capability):
"""Removes a capability from the channel's default capabilities."""
2003-04-02 13:08:34 +02:00
self.capabilities.remove(capability)
2003-03-12 07:26:59 +01:00
2003-04-02 13:08:34 +02:00
def setDefaultCapability(self, b):
"""Sets the default capability in the channel."""
2003-04-02 13:08:34 +02:00
self.defaultAllow = b
2003-03-12 07:26:59 +01:00
def checkCapability(self, capability):
"""Checks whether a certain capability is allowed by the channel."""
2003-03-12 07:26:59 +01:00
if capability in self.capabilities:
2003-04-20 23:52:53 +02:00
return self.capabilities.check(capability)
2003-03-12 07:26:59 +01:00
else:
2003-04-20 23:52:53 +02:00
if isAntiCapability(capability):
return not self.defaultAllow
2003-04-02 13:08:34 +02:00
else:
return self.defaultAllow
2003-03-12 07:26:59 +01:00
def checkIgnored(self, hostmask):
"""Checks whether a given hostmask is to be ignored by the channel."""
2003-03-12 07:26:59 +01:00
if self.lobotomized:
return True
for mask in self.bans:
if ircutils.hostmaskPatternEqual(mask, hostmask):
return True
for mask in self.ignores:
if ircutils.hostmaskPatternEqual(mask, hostmask):
return True
return False
class UsersDB(object):
2003-09-13 19:29:56 +02:00
"""A simple serialized-to-file User Database."""
2003-03-12 07:26:59 +01:00
def __init__(self, filename):
self.filename = filename
if os.path.exists(filename):
fd = file(filename, 'r')
s = fd.read()
fd.close()
IrcSet = ircutils.IrcSet
2003-09-13 19:29:56 +02:00
(self.nextId, self.users) = eval(_normalize(s))
2003-03-12 07:26:59 +01:00
else:
self.nextId = 1
self.users = [IrcUser(capabilities=['owner'],
password=utils.mktemp())]
self._nameCache = {}
self._hostmaskCache = {}
def reload(self):
2003-09-13 19:29:56 +02:00
"""Reloads the database from its file."""
self.__init__(self.filename)
def flush(self):
2003-09-13 19:29:56 +02:00
"""Flushes the database to its file."""
fd = file(self.filename, 'w')
fd.write(repr((self.nextId, self.users)))
fd.close()
2003-03-12 07:26:59 +01:00
def getUserId(self, s):
2003-09-13 19:29:56 +02:00
"""Returns the user ID of a given name or hostmask."""
2003-03-12 07:26:59 +01:00
if ircutils.isUserHostmask(s):
try:
return self._hostmaskCache[s]
except KeyError:
ids = []
for (id, user) in enumerate(self.users):
if user is None:
continue
if user.checkHostmask(s):
ids.append(id)
if len(ids) == 1:
id = ids[0]
self._hostmaskCache[s] = id
2003-12-01 13:04:02 +01:00
try:
self._hostmaskCache[id].add(s)
except KeyError:
self._hostmaskCache[id] = sets.Set([s])
return id
elif len(ids) == 0:
raise KeyError, s
else:
raise ValueError, 'Ids %r matched.' % ids
else: # Not a hostmask, must be a name.
2003-09-14 09:05:41 +02:00
s = s.lower()
try:
return self._nameCache[s]
except KeyError:
for (id, user) in enumerate(self.users):
if user is None:
continue
2003-09-14 09:05:41 +02:00
if s == user.name.lower():
self._nameCache[s] = id
2003-09-14 09:05:41 +02:00
self._nameCache[id] = s
return id
else:
raise KeyError, s
2003-03-12 07:26:59 +01:00
def getUser(self, id):
2003-09-13 19:29:56 +02:00
"""Returns a user given its id, name, or hostmask."""
if not isinstance(id, int):
# Must be a string. Get the UserId first.
id = self.getUserId(id)
try:
ret = self.users[id]
if ret is None:
raise KeyError, id
return ret
except IndexError:
raise KeyError, id
def hasUser(self, id):
2003-09-13 19:29:56 +02:00
"""Returns the database has a user given its id, name, or hostmask."""
try:
self.getUser(id)
return True
except KeyError:
return False
2003-03-12 07:26:59 +01:00
def __iter__(self):
x = ifilter(None, self.users)
x.next() # Skip the bot user.
return x
def numUsers(self):
return ilen(self)
def setUser(self, id, user):
2003-09-13 19:29:56 +02:00
"""Sets a user (given its id) to the IrcUser given it."""
assert isinstance(id, int), 'setUser takes an integer userId.'
2003-12-01 13:04:02 +01:00
if (not 0 <= id < len(self.users)) or self.users[id] is None:
raise KeyError, id
2003-03-31 11:26:51 +02:00
try:
if self.getUserId(user.name) != id:
2003-12-01 13:04:02 +01:00
s = '%s is already registered to someone else.' % user.name
raise ValueError, s
2003-03-31 11:26:51 +02:00
except KeyError:
pass
for hostmask in user.hostmasks:
try:
if self.getUserId(hostmask) != id:
2003-12-01 13:04:02 +01:00
s = '%s is already registered to someone else.'% hostmask
raise ValueError, s
except KeyError:
continue
if id in self._nameCache:
2003-09-14 09:05:41 +02:00
del self._nameCache[self._nameCache[id]]
del self._nameCache[id]
if id in self._hostmaskCache:
for hostmask in self._hostmaskCache[id]:
del self._hostmaskCache[hostmask]
del self._hostmaskCache[id]
self.users[id] = user
self.flush()
def delUser(self, id):
2003-09-13 19:29:56 +02:00
"""Removes a user from the database."""
if not 0 <= id < len(self.users) or self.users[id] is None:
raise KeyError, id
self.users[id] = None
2003-09-14 09:05:41 +02:00
if id in self._nameCache:
del self._nameCache[self._nameCache[id]]
del self._nameCache[id]
2003-09-18 12:25:20 +02:00
if id in self._hostmaskCache:
for hostmask in self._hostmaskCache[id]:
del self._hostmaskCache[hostmask]
del self._hostmaskCache[id]
self.flush()
def newUser(self):
2003-09-13 19:29:56 +02:00
"""Allocates a new user in the database and returns it and its id."""
user = IrcUser()
id = self.nextId
self.nextId += 1
self.users.append(user)
self.flush()
return (id, user)
2003-03-12 07:26:59 +01:00
class ChannelsDictionary(utils.IterableMap):
2003-03-12 07:26:59 +01:00
def __init__(self, filename):
self.filename = filename
Set = sets.Set
self.dict = PersistentDictionary(filename, globals(), locals())
2003-03-12 07:26:59 +01:00
def getChannel(self, channel):
2003-09-13 19:29:56 +02:00
"""Returns an IrcChannel object for the given channel."""
2003-03-12 07:26:59 +01:00
channel = channel.lower()
if channel in self.dict:
return self.dict[channel]
else:
c = IrcChannel()
self.dict[channel] = c
return c
def setChannel(self, channel, ircChannel):
2003-09-13 19:29:56 +02:00
"""Sets a given channel to the IrcChannel object given."""
2003-03-12 07:26:59 +01:00
channel = channel.lower()
self.dict[channel] = ircChannel
self.flush()
2003-03-12 07:26:59 +01:00
def flush(self):
2003-09-13 19:29:56 +02:00
"""Flushes the channel database to its file."""
self.dict.flush()
2003-03-12 07:26:59 +01:00
def reload(self):
2003-09-13 19:29:56 +02:00
"""Reloads the channel database from its file."""
2003-03-12 07:26:59 +01:00
self.__init__(self.filename)
def iteritems(self):
return self.dict.iteritems()
2003-03-12 07:26:59 +01:00
###
# Later, I might add some special handling for botnet.
###
2004-01-18 08:58:26 +01:00
confDir = conf.supybot.directories.conf()
users = UsersDB(os.path.join(confDir,
conf.supybot.databases.users.filename()))
channels = ChannelsDictionary(os.path.join(confDir,
conf.supybot.databases.channels.filename()))
2003-03-12 07:26:59 +01:00
###
# Useful functions for checking credentials.
###
def checkIgnored(hostmask, recipient='', users=users, channels=channels):
"""checkIgnored(hostmask, recipient='') -> True/False
Checks if the user is ignored by the recipient of the message.
"""
2004-01-18 08:58:26 +01:00
for ignore in conf.supybot.ignores():
2003-03-12 07:26:59 +01:00
if ircutils.hostmaskPatternEqual(ignore, hostmask):
2004-01-18 08:58:26 +01:00
log.info('Ignoring %s due to conf.supybot.ignores.', hostmask)
2003-03-12 07:26:59 +01:00
return True
try:
id = users.getUserId(hostmask)
user = users.getUser(id)
2003-03-12 07:26:59 +01:00
except KeyError:
# If there's no user...
if ircutils.isChannel(recipient):
channel = channels.getChannel(recipient)
2003-12-16 21:56:05 +01:00
if channel.checkIgnored(hostmask):
log.info('Ignoring %s due to the channel ignores.', hostmask)
return True
else:
return False
2003-03-12 07:26:59 +01:00
else:
2004-01-18 08:58:26 +01:00
if conf.supybot.defaultIgnore():
log.info('Ignoring %s due to conf.supybot.defaultIgnore',
hostmask)
2003-12-16 21:56:05 +01:00
return True
else:
return False
2003-03-12 07:26:59 +01:00
if user.checkCapability('owner'):
# Owners shouldn't ever be ignored.
return False
elif user.ignore:
2003-12-16 21:56:05 +01:00
log.info('Ignoring %s due to his IrcUser ignore flag.', hostmask)
2003-03-12 07:26:59 +01:00
return True
elif recipient:
if ircutils.isChannel(recipient):
channel = channels.getChannel(recipient)
2003-12-16 21:56:05 +01:00
if channel.checkIgnored(hostmask):
log.info('Ignoring %s due to the channel ignores.', hostmask)
return True
else:
return False
2003-03-12 07:26:59 +01:00
else:
return False
else:
return False
def _x(capability, ret):
if isAntiCapability(capability):
return not ret
else:
return ret
2003-08-20 18:26:23 +02:00
def _checkCapabilityForUnknownUser(capability, users=users, channels=channels):
if isChannelCapability(capability):
(channel, capability) = fromChannelCapability(capability)
try:
c = channels.getChannel(channel)
if capability in c.capabilities:
return c.checkCapability(capability)
else:
return _x(capability, c.defaultAllow)
except KeyError:
pass
2004-01-18 08:58:26 +01:00
if capability in conf.supybot.defaultCapabilities():
return True
2004-01-18 08:58:26 +01:00
elif invertCapability(capability) in conf.supybot.defaultCapabilities():
return False
else:
2004-01-18 08:58:26 +01:00
return _x(capability, conf.supybot.defaultAllow())
2003-03-12 07:26:59 +01:00
def checkCapability(hostmask, capability, users=users, channels=channels):
2003-09-13 19:29:56 +02:00
"""Checks that the user specified by name/hostmask has the capabilty given.
"""
2004-01-18 08:58:26 +01:00
if world.testing:
return _x(capability, True)
2003-03-12 07:26:59 +01:00
try:
2003-09-13 19:29:56 +02:00
u = users.getUser(hostmask)
if u.secure and not u.checkHostmask(hostmask, useAuth=False):
raise KeyError
2003-04-20 23:52:53 +02:00
except KeyError:
# Raised when no hostmasks match.
return _checkCapabilityForUnknownUser(capability, users=users,
channels=channels)
except ValueError, e:
# Raised when multiple hostmasks match.
log.warning('%s: %s', hostmask, e)
return _checkCapabilityForUnknownUser(capability, users=users,
channels=channels)
2003-04-20 23:52:53 +02:00
if capability in u.capabilities:
return u.checkCapability(capability)
else:
if isChannelCapability(capability):
(channel, capability) = fromChannelCapability(capability)
try:
chanop = makeChannelCapability(channel, 'op')
if u.checkCapability(chanop):
return _x(capability, True)
2003-04-20 23:52:53 +02:00
except KeyError:
pass
c = channels.getChannel(channel)
if capability in c.capabilities:
return c.checkCapability(capability)
else:
return _x(capability, c.defaultAllow)
2004-01-18 08:58:26 +01:00
defaultCapabilities = conf.supybot.defaultCapabilities()
if capability in defaultCapabilities:
2003-03-12 07:26:59 +01:00
return True
2004-01-18 08:58:26 +01:00
elif invertCapability(capability) in defaultCapabilities:
2003-04-20 23:52:53 +02:00
return False
2003-04-02 13:08:34 +02:00
else:
2004-01-18 08:58:26 +01:00
return _x(capability, conf.supybot.defaultAllow())
2003-08-20 18:26:23 +02:00
2003-03-12 07:26:59 +01:00
def checkCapabilities(hostmask, capabilities, requireAll=False):
"""Checks that a user has capabilities in a list.
requireAll is the True if *all* capabilities in the list must be had, False
if *any* of the capabilities in the list must be had.
"""
for capability in capabilities:
if requireAll:
if not checkCapability(hostmask, capability):
return False
else:
if checkCapability(hostmask, capability):
return True
if requireAll:
return True
else:
return False
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: