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$"
|
|
|
|
|
2004-02-08 10:25:14 +01:00
|
|
|
from __future__ import division
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.fix as fix
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-03-27 07:57:09 +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
|
2003-12-02 00:17:26 +01:00
|
|
|
from itertools import imap, ilen, ifilter
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.log as log
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.world as world
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.unpreserve as unpreserve
|
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."""
|
2004-01-20 22:53:13 +01:00
|
|
|
assert isChannelCapability(capability)
|
|
|
|
return capability.split(',', 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."""
|
2004-01-20 22:53:13 +01:00
|
|
|
if ',' in capability:
|
|
|
|
(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."""
|
2004-01-20 22:53:13 +01:00
|
|
|
return '%s,%s' % (channel, capability)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
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)
|
2004-01-20 22:53:13 +01:00
|
|
|
return capability and 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.'
|
2004-01-20 22:53:13 +01:00
|
|
|
if isChannelCapability(capability):
|
2003-03-12 07:26:59 +01:00
|
|
|
(channel, capability) = fromChannelCapability(capability)
|
2004-01-20 22:53:13 +01:00
|
|
|
return makeChannelCapability(channel, '-' + capability)
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-10-05 13:13:20 +02:00
|
|
|
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."""
|
2003-09-06 20:32:10 +02:00
|
|
|
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)
|
2004-01-20 22:53:13 +01:00
|
|
|
return ','.join((channel, capability[1:]))
|
2003-04-20 23:52:53 +02:00
|
|
|
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)
|
|
|
|
|
2004-02-08 10:25:14 +01:00
|
|
|
def unWildcardHostmask(hostmask):
|
|
|
|
return hostmask.translate(string.ascii, '!@*?')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-11-04 00:10:29 +01:00
|
|
|
_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=()):
|
2003-11-04 00:23:28 +01:00
|
|
|
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):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Adds a capability to the set."""
|
2003-04-20 23:52:53 +02:00
|
|
|
capability = ircutils.toLower(capability)
|
2003-11-04 00:10:29 +01:00
|
|
|
inverted = _invert(capability)
|
2003-11-04 00:23:28 +01:00
|
|
|
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):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Removes a capability from the set."""
|
2003-04-20 23:52:53 +02:00
|
|
|
capability = ircutils.toLower(capability)
|
2003-11-04 00:23:28 +01:00
|
|
|
self.__parent.remove(capability)
|
2003-04-20 23:52:53 +02:00
|
|
|
|
|
|
|
def __contains__(self, capability):
|
|
|
|
capability = ircutils.toLower(capability)
|
2003-11-04 00:23:28 +01:00
|
|
|
if self.__parent.__contains__(capability):
|
2003-04-20 23:52:53 +02:00
|
|
|
return True
|
2003-11-04 00:23:28 +01:00
|
|
|
if self.__parent.__contains__(_invert(capability)):
|
2003-04-20 23:52:53 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def check(self, capability):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""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)
|
2003-11-04 00:23:28 +01:00
|
|
|
if self.__parent.__contains__(capability):
|
2003-04-20 23:52:53 +02:00
|
|
|
return True
|
2003-11-04 00:23:28 +01:00
|
|
|
elif self.__parent.__contains__(_invert(capability)):
|
2003-04-20 23:52:53 +02:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
raise KeyError, capability
|
|
|
|
|
2003-10-04 14:00:56 +02:00
|
|
|
def __repr__(self):
|
2003-10-05 13:42:58 +02:00
|
|
|
return '%s([%s])' % (self.__class__.__name__,
|
2003-11-15 05:46:09 +01:00
|
|
|
', '.join(imap(repr, self)))
|
2003-04-20 23:52:53 +02:00
|
|
|
|
2003-10-05 13:42:58 +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."""
|
2003-11-04 00:23:28 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.__parent = super(UserCapabilitySet, self)
|
|
|
|
self.__parent.__init__(*args, **kwargs)
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2003-04-20 23:52:53 +02:00
|
|
|
def __contains__(self, capability):
|
|
|
|
capability = ircutils.toLower(capability)
|
2003-10-05 13:42:58 +02:00
|
|
|
if capability == 'owner' or capability == antiOwner:
|
|
|
|
return True
|
2003-11-04 00:23:28 +01:00
|
|
|
elif self.__parent.__contains__('owner'):
|
2003-04-20 23:52:53 +02:00
|
|
|
return True
|
|
|
|
else:
|
2003-11-04 00:23:28 +01:00
|
|
|
return self.__parent.__contains__(capability)
|
2003-04-20 23:52:53 +02:00
|
|
|
|
|
|
|
def check(self, capability):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""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)
|
2003-10-05 13:42:58 +02:00
|
|
|
if capability == 'owner' or capability == antiOwner:
|
2003-11-04 00:23:28 +01:00
|
|
|
if self.__parent.__contains__('owner'):
|
2003-10-05 13:42:58 +02:00
|
|
|
return not isAntiCapability(capability)
|
2003-04-20 23:52:53 +02:00
|
|
|
else:
|
2003-10-05 13:42:58 +02:00
|
|
|
return isAntiCapability(capability)
|
2003-11-04 00:23:28 +01:00
|
|
|
elif self.__parent.__contains__('owner'):
|
2003-04-20 23:52:53 +02:00
|
|
|
if isAntiCapability(capability):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
else:
|
2003-11-04 00:23:28 +01:00
|
|
|
return self.__parent.check(capability)
|
2003-04-20 23:52:53 +02:00
|
|
|
|
|
|
|
def add(self, capability):
|
2003-10-05 13:13:20 +02:00
|
|
|
"""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)
|
2003-10-05 13:13:20 +02:00
|
|
|
assert capability != '-owner', '"-owner" disallowed.'
|
2003-11-04 00:23:28 +01:00
|
|
|
self.__parent.add(capability)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
class IrcUser(object):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""This class holds the capabilities and authentications for a user."""
|
2003-09-12 22:06:58 +02:00
|
|
|
def __init__(self, ignore=False, password='', name='',
|
2003-10-28 16:13:53 +01:00
|
|
|
capabilities=(), hostmasks=None, secure=False, hashed=False):
|
2003-09-12 22:06:58 +02:00
|
|
|
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.
|
2003-10-03 11:57:52 +02:00
|
|
|
self.secure = secure # A boolean describing if hostmasks *must* match.
|
2003-10-28 16:13:53 +01:00
|
|
|
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):
|
2004-01-09 00:03:48 +01:00
|
|
|
return '%s(ignore=%s, password=%r, name=%r, hashed=%r, ' \
|
|
|
|
'capabilities=%r, hostmasks=%r, secure=%r)\n' % \
|
2003-10-28 16:13:53 +01:00
|
|
|
(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):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Gives the user the given capability."""
|
2003-03-12 07:26:59 +01:00
|
|
|
self.capabilities.add(capability)
|
|
|
|
|
|
|
|
def removeCapability(self, capability):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Takes from the user the given capability."""
|
2003-09-23 19:04:53 +02:00
|
|
|
self.capabilities.remove(capability)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def checkCapability(self, capability):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""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
|
|
|
|
2003-10-28 16:13:53 +01:00
|
|
|
def setPassword(self, password, hashed=False):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Sets the user's password."""
|
2003-10-28 16:13:53 +01:00
|
|
|
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):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Checks the user's password."""
|
2003-10-28 16:13:53 +01:00
|
|
|
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
|
|
|
|
2003-10-03 11:57:52 +02:00
|
|
|
def checkHostmask(self, hostmask, useAuth=True):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Checks a given hostmask against the user's hostmasks or current
|
|
|
|
authentication. If useAuth is False, only checks against the user's
|
|
|
|
hostmasks.
|
|
|
|
"""
|
2004-03-25 13:16:37 +01:00
|
|
|
if useAuth:
|
|
|
|
if self.auth:
|
|
|
|
i = conf.supybot.databases.users.timeoutIdentification()
|
|
|
|
if i > 0 and self.auth[0] < time.time() - i:
|
|
|
|
self.unsetAuth()
|
|
|
|
elif hostmask == self.auth[1]:
|
|
|
|
return True
|
2003-03-12 07:26:59 +01:00
|
|
|
for pat in self.hostmasks:
|
|
|
|
if ircutils.hostmaskPatternEqual(pat, hostmask):
|
2004-02-08 10:25:14 +01:00
|
|
|
return pat
|
2003-03-12 07:26:59 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
def addHostmask(self, hostmask):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Adds a hostmask to the user's hostmasks."""
|
2004-02-08 10:25:14 +01:00
|
|
|
assert ircutils.isUserHostmask(hostmask)
|
|
|
|
if len(unWildcardHostmask(hostmask)) < 8:
|
|
|
|
raise ValueError, \
|
|
|
|
'Hostmask must contain at least 8 non-wildcard characters.'
|
2003-03-12 07:26:59 +01:00
|
|
|
self.hostmasks.append(hostmask)
|
|
|
|
|
|
|
|
def removeHostmask(self, hostmask):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Removes a hostmask from the user's hostmasks."""
|
2003-11-19 16:35:01 +01:00
|
|
|
self.hostmasks = [s for s in self.hostmasks if s != hostmask]
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def setAuth(self, hostmask):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Sets a user's authenticated hostmask. This times out in 1 hour."""
|
2003-10-11 12:20:15 +02:00
|
|
|
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):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Unsets a use's authenticated hostmask."""
|
2004-04-08 12:47:32 +02:00
|
|
|
if self.auth is not None:
|
|
|
|
users.invalidateCache(hostmask=self.auth[1])
|
|
|
|
self.auth = None
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-02-02 03:56:16 +01:00
|
|
|
def preserve(self, fd, indent=''):
|
|
|
|
def write(s):
|
|
|
|
fd.write(indent)
|
|
|
|
fd.write(s)
|
|
|
|
fd.write(os.linesep)
|
|
|
|
write('name %s' % self.name)
|
|
|
|
write('ignore %s' % self.ignore)
|
|
|
|
write('secure %s' % self.secure)
|
|
|
|
write('hashed %s' % self.hashed)
|
|
|
|
write('password %s' % self.password)
|
|
|
|
for capability in self.capabilities:
|
|
|
|
write('capability %s' % capability)
|
|
|
|
for hostmask in self.hostmasks:
|
|
|
|
write('hostmask %s' % hostmask)
|
|
|
|
fd.write(os.linesep)
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
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')
|
2004-03-25 13:16:37 +01:00
|
|
|
def __init__(self, bans=None, silences=None, exceptions=None, ignores=None,
|
|
|
|
capabilities=None, lobotomized=False, defaultAllow=True):
|
2003-03-12 07:26:59 +01:00
|
|
|
self.defaultAllow = defaultAllow
|
|
|
|
if bans is None:
|
|
|
|
self.bans = []
|
|
|
|
else:
|
|
|
|
self.bans = bans
|
2004-03-25 13:16:37 +01:00
|
|
|
if exceptions is None:
|
|
|
|
self.exceptions = []
|
|
|
|
else:
|
|
|
|
self.exceptions = exceptions
|
|
|
|
if silences is None:
|
|
|
|
self.silences = []
|
|
|
|
else:
|
|
|
|
self.silences = silences
|
2003-03-12 07:26:59 +01:00
|
|
|
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):
|
2004-01-09 00:03:48 +01:00
|
|
|
return '%s(bans=%r, ignores=%r, capabilities=%r, ' \
|
2004-03-25 13:16:37 +01:00
|
|
|
'lobotomized=%r, defaultAllow=%s, ' \
|
|
|
|
'silences=%r, exceptions=%r)\n' % \
|
2003-03-12 07:26:59 +01:00
|
|
|
(self.__class__.__name__, self.bans, self.ignores,
|
|
|
|
self.capabilities, self.lobotomized,
|
2004-03-25 13:16:37 +01:00
|
|
|
self.defaultAllow, self.silences, self.exceptions)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def addBan(self, hostmask):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Adds a ban to the channel banlist."""
|
2003-03-12 07:26:59 +01:00
|
|
|
self.bans.append(hostmask)
|
|
|
|
|
|
|
|
def removeBan(self, hostmask):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""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):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""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):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""Adds an ignore to the channel ignore list."""
|
2003-03-12 07:26:59 +01:00
|
|
|
self.ignores.append(hostmask)
|
|
|
|
|
|
|
|
def removeIgnore(self, hostmask):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""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):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""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):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""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):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""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):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""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):
|
2003-10-04 14:00:56 +02:00
|
|
|
"""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
|
|
|
|
|
2004-02-02 03:56:16 +01:00
|
|
|
def preserve(self, fd, indent=''):
|
|
|
|
def write(s):
|
|
|
|
fd.write(indent)
|
|
|
|
fd.write(s)
|
|
|
|
fd.write(os.linesep)
|
|
|
|
write('lobotomized %s' % self.lobotomized)
|
|
|
|
write('defaultAllow %s' % self.defaultAllow)
|
2004-03-25 13:16:37 +01:00
|
|
|
for capability in self.capabilities:
|
|
|
|
write('capability ' + capability)
|
2004-02-02 03:56:16 +01:00
|
|
|
for ban in self.bans:
|
2004-03-25 13:16:37 +01:00
|
|
|
write('ban ' + ban)
|
|
|
|
for silence in self.silences:
|
|
|
|
write('silence ' + silence)
|
|
|
|
for exception in self.exceptions:
|
|
|
|
write('exception ' + exception)
|
2004-02-02 03:56:16 +01:00
|
|
|
for ignore in self.ignores:
|
2004-03-25 13:16:37 +01:00
|
|
|
write('ignore ' + ignore)
|
2004-02-02 03:56:16 +01:00
|
|
|
fd.write(os.linesep)
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-02-02 03:56:16 +01:00
|
|
|
|
|
|
|
class Creator(object):
|
2004-02-02 04:24:03 +01:00
|
|
|
def badCommand(self, command, rest, lineno):
|
2004-02-02 03:56:16 +01:00
|
|
|
raise ValueError, 'Invalid command on line %s: %s' % (lineno, command)
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-02-02 03:56:16 +01:00
|
|
|
class IrcUserCreator(Creator):
|
|
|
|
id = None
|
2004-02-07 23:23:12 +01:00
|
|
|
def __init__(self, users):
|
2004-02-02 03:56:16 +01:00
|
|
|
self.u = IrcUser()
|
2004-02-07 23:23:12 +01:00
|
|
|
self.users = users
|
2004-02-02 03:56:16 +01:00
|
|
|
|
|
|
|
def user(self, rest, lineno):
|
|
|
|
if self.id is not None:
|
|
|
|
raise ValueError, 'Unexpected user command on line %s.' % lineno
|
|
|
|
IrcUserCreator.id = int(rest)
|
|
|
|
|
|
|
|
def name(self, rest, lineno):
|
|
|
|
if self.id is None:
|
|
|
|
raise ValueError, 'Unexpected user description without id.'
|
|
|
|
self.u.name = rest
|
|
|
|
|
|
|
|
def ignore(self, rest, lineno):
|
|
|
|
if self.id is None:
|
|
|
|
raise ValueError, 'Unexpected user description without id.'
|
|
|
|
self.u.ignore = bool(eval(rest))
|
|
|
|
|
|
|
|
def secure(self, rest, lineno):
|
|
|
|
if self.id is None:
|
|
|
|
raise ValueError, 'Unexpected user description without id.'
|
|
|
|
self.u.secure = bool(eval(rest))
|
|
|
|
|
|
|
|
def hashed(self, rest, lineno):
|
|
|
|
if self.id is None:
|
|
|
|
raise ValueError, 'Unexpected user description without id.'
|
|
|
|
self.u.hashed = bool(eval(rest))
|
|
|
|
|
|
|
|
def password(self, rest, lineno):
|
|
|
|
if self.id is None:
|
|
|
|
raise ValueError, 'Unexpected user description without id.'
|
|
|
|
self.u.password = rest
|
|
|
|
|
|
|
|
def hostmask(self, rest, lineno):
|
|
|
|
if self.id is None:
|
|
|
|
raise ValueError, 'Unexpected user description without id.'
|
|
|
|
self.u.hostmasks.append(rest)
|
|
|
|
|
|
|
|
def capability(self, rest, lineno):
|
|
|
|
if self.id is None:
|
|
|
|
raise ValueError, 'Unexpected user description without id.'
|
|
|
|
self.u.capabilities.add(rest)
|
|
|
|
|
|
|
|
def finish(self):
|
|
|
|
if self.u.name:
|
2004-02-07 23:23:12 +01:00
|
|
|
self.users.setUser(self.id, self.u)
|
2004-02-02 03:56:16 +01:00
|
|
|
IrcUserCreator.id = None
|
|
|
|
|
|
|
|
class IrcChannelCreator(Creator):
|
|
|
|
name = None
|
2004-02-07 23:23:12 +01:00
|
|
|
def __init__(self, channels):
|
2004-02-02 03:56:16 +01:00
|
|
|
self.c = IrcChannel()
|
2004-02-07 23:23:12 +01:00
|
|
|
self.channels = channels
|
2004-02-02 03:56:16 +01:00
|
|
|
self.hadChannel = bool(self.name)
|
|
|
|
|
|
|
|
def channel(self, rest, lineno):
|
|
|
|
if self.name is not None:
|
|
|
|
raise ValueError, 'Unexpected channel command on line %s' % lineno
|
|
|
|
IrcChannelCreator.name = rest
|
|
|
|
|
|
|
|
def lobotomized(self, rest, lineno):
|
|
|
|
if self.name is None:
|
|
|
|
raise ValueError, 'Unexpected channel description without channel.'
|
|
|
|
self.c.lobotomized = bool(eval(rest))
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-02-02 03:56:16 +01:00
|
|
|
def defaultallow(self, rest, lineno):
|
|
|
|
if self.name is None:
|
|
|
|
raise ValueError, 'Unexpected channel description without channel.'
|
|
|
|
self.c.defaultAllow = bool(eval(rest))
|
|
|
|
|
|
|
|
def capability(self, rest, lineno):
|
|
|
|
if self.name is None:
|
|
|
|
raise ValueError, 'Unexpected channel description without channel.'
|
|
|
|
self.c.capabilities.add(rest)
|
|
|
|
|
|
|
|
def ban(self, rest, lineno):
|
|
|
|
if self.name is None:
|
|
|
|
raise ValueError, 'Unexpected channel description without channel.'
|
|
|
|
self.c.bans.append(rest)
|
|
|
|
|
|
|
|
def ignore(self, rest, lineno):
|
|
|
|
if self.name is None:
|
|
|
|
raise ValueError, 'Unexpected channel description without channel.'
|
|
|
|
self.c.ignores.append(rest)
|
|
|
|
|
2004-03-25 13:16:37 +01:00
|
|
|
def silence(self, rest, lineno):
|
|
|
|
if self.name is None:
|
|
|
|
raise ValueError, 'Unexpected channel description without channel.'
|
|
|
|
self.c.silences.append(rest)
|
|
|
|
|
|
|
|
def exception(self, rest, lineno):
|
|
|
|
if self.name is None:
|
|
|
|
raise ValueError, 'Unexpected channel description without channel.'
|
|
|
|
self.c.exceptions.append(rest)
|
|
|
|
|
2004-02-02 03:56:16 +01:00
|
|
|
def finish(self):
|
|
|
|
if self.hadChannel:
|
2004-02-07 23:23:12 +01:00
|
|
|
self.channels.setChannel(self.name, self.c)
|
2004-02-02 03:56:16 +01:00
|
|
|
IrcChannelCreator.name = None
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-02-02 03:56:16 +01:00
|
|
|
|
|
|
|
class UsersDictionary(utils.IterableMap):
|
2003-09-13 19:29:56 +02:00
|
|
|
"""A simple serialized-to-file User Database."""
|
2004-02-02 03:56:16 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.filename = None
|
|
|
|
self.users = {}
|
2004-02-07 23:23:12 +01:00
|
|
|
self.nextId = 0
|
2003-09-12 22:06:58 +02:00
|
|
|
self._nameCache = {}
|
|
|
|
self._hostmaskCache = {}
|
|
|
|
|
2004-02-04 01:39:52 +01:00
|
|
|
# This is separate because the Creator has to access our instance.
|
2004-02-02 03:56:16 +01:00
|
|
|
def open(self, filename):
|
|
|
|
self.filename = filename
|
2004-02-07 23:23:12 +01:00
|
|
|
reader = unpreserve.Reader(IrcUserCreator, self)
|
2004-02-14 02:02:53 +01:00
|
|
|
try:
|
|
|
|
reader.readFile(filename)
|
|
|
|
except Exception, e:
|
|
|
|
log.error('Invalid user dictionary file, starting from scratch.')
|
|
|
|
log.error('Exact error: %s', utils.exnToString(e))
|
2004-02-02 03:56:16 +01:00
|
|
|
|
2003-09-12 22:06:58 +02:00
|
|
|
def reload(self):
|
2003-09-13 19:29:56 +02:00
|
|
|
"""Reloads the database from its file."""
|
2004-02-13 10:01:21 +01:00
|
|
|
self.nextId = 0
|
|
|
|
self.users.clear()
|
|
|
|
self._nameCache.clear()
|
|
|
|
self._hostmaskCache.clear()
|
2004-02-02 03:56:16 +01:00
|
|
|
if self.filename is not None:
|
2004-02-03 04:07:54 +01:00
|
|
|
try:
|
|
|
|
self.open(self.filename)
|
|
|
|
except EnvironmentError, e:
|
|
|
|
log.warning('UsersDictionary.reload failed: %s', e)
|
2004-02-02 03:56:16 +01:00
|
|
|
else:
|
2004-02-04 01:39:52 +01:00
|
|
|
log.warning('UsersDictionary.reload called with no filename.')
|
2003-09-12 22:06:58 +02:00
|
|
|
|
|
|
|
def flush(self):
|
2003-09-13 19:29:56 +02:00
|
|
|
"""Flushes the database to its file."""
|
2004-02-02 03:56:16 +01:00
|
|
|
if self.filename is not None:
|
|
|
|
L = self.users.items()
|
|
|
|
L.sort()
|
2004-08-01 14:46:03 +02:00
|
|
|
fd = utils.transactionalFile(self.filename)
|
2004-02-02 03:56:16 +01:00
|
|
|
for (id, u) in L:
|
|
|
|
fd.write('user %s' % id)
|
|
|
|
fd.write(os.linesep)
|
|
|
|
u.preserve(fd, indent=' ')
|
|
|
|
fd.close()
|
|
|
|
else:
|
2004-02-04 01:39:52 +01:00
|
|
|
log.warning('UsersDictionary.flush called with no filename.')
|
2004-02-02 03:56:16 +01:00
|
|
|
|
2004-02-07 23:23:12 +01:00
|
|
|
def close(self):
|
|
|
|
self.flush()
|
|
|
|
if self.flush in world.flushers:
|
|
|
|
world.flushers.remove(self.flush)
|
|
|
|
self.users.clear()
|
|
|
|
|
2004-02-02 03:56:16 +01:00
|
|
|
def iteritems(self):
|
|
|
|
return self.users.iteritems()
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-09-12 22:06:58 +02: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):
|
2003-04-08 09:28:45 +02:00
|
|
|
try:
|
2003-09-12 22:06:58 +02:00
|
|
|
return self._hostmaskCache[s]
|
2003-04-08 09:28:45 +02:00
|
|
|
except KeyError:
|
2004-02-08 10:25:14 +01:00
|
|
|
ids = {}
|
2004-02-02 03:56:16 +01:00
|
|
|
for (id, user) in self.users.iteritems():
|
2004-02-08 10:25:14 +01:00
|
|
|
x = user.checkHostmask(s)
|
|
|
|
if x:
|
|
|
|
ids[id] = x
|
2003-09-12 22:06:58 +02:00
|
|
|
if len(ids) == 1:
|
2004-02-08 10:25:14 +01:00
|
|
|
id = ids.keys()[0]
|
2003-09-12 22:06:58 +02:00
|
|
|
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])
|
2003-09-12 22:06:58 +02:00
|
|
|
return id
|
|
|
|
elif len(ids) == 0:
|
|
|
|
raise KeyError, s
|
|
|
|
else:
|
2004-02-08 10:25:14 +01:00
|
|
|
log.error('Multiple matches found in user database. '
|
|
|
|
'Removing the offending hostmasks.')
|
|
|
|
for (id, hostmask) in ids.iteritems():
|
|
|
|
log.error('Removing %r from user %s.', hostmask, id)
|
|
|
|
self.users[id].removeHostmask(hostmask)
|
2003-09-12 22:06:58 +02:00
|
|
|
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()
|
2003-09-12 22:06:58 +02:00
|
|
|
try:
|
|
|
|
return self._nameCache[s]
|
|
|
|
except KeyError:
|
2004-02-02 03:56:16 +01:00
|
|
|
for (id, user) in self.users.items():
|
2003-09-14 09:05:41 +02:00
|
|
|
if s == user.name.lower():
|
2003-09-12 22:06:58 +02:00
|
|
|
self._nameCache[s] = id
|
2003-09-14 09:05:41 +02:00
|
|
|
self._nameCache[id] = s
|
2003-09-12 22:06:58 +02:00
|
|
|
return id
|
|
|
|
else:
|
|
|
|
raise KeyError, s
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-09-12 22:06:58 +02:00
|
|
|
def getUser(self, id):
|
2003-09-13 19:29:56 +02:00
|
|
|
"""Returns a user given its id, name, or hostmask."""
|
2003-09-12 22:06:58 +02:00
|
|
|
if not isinstance(id, int):
|
|
|
|
# Must be a string. Get the UserId first.
|
|
|
|
id = self.getUserId(id)
|
2004-02-02 03:56:16 +01:00
|
|
|
return self.users[id]
|
2003-09-12 22:06:58 +02:00
|
|
|
|
|
|
|
def hasUser(self, id):
|
2003-09-13 19:29:56 +02:00
|
|
|
"""Returns the database has a user given its id, name, or hostmask."""
|
2003-09-12 22:06:58 +02:00
|
|
|
try:
|
|
|
|
self.getUser(id)
|
|
|
|
return True
|
|
|
|
except KeyError:
|
|
|
|
return False
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-12-02 00:17:26 +01:00
|
|
|
def numUsers(self):
|
2004-02-02 03:56:16 +01:00
|
|
|
return len(self.users)
|
2003-12-02 00:17:26 +01:00
|
|
|
|
2004-03-25 13:16:37 +01:00
|
|
|
def invalidateCache(self, id=None, hostmask=None, name=None):
|
|
|
|
if hostmask is not None:
|
|
|
|
if hostmask in self._hostmaskCache:
|
|
|
|
id = self._hostmaskCache.pop(hostmask)
|
|
|
|
self._hostmaskCache[id].remove(hostmask)
|
|
|
|
if not self._hostmaskCache[id]:
|
|
|
|
del self._hostmaskCache[id]
|
|
|
|
if name is not None:
|
|
|
|
del self._nameCache[self._nameCache[id]]
|
|
|
|
del self._nameCache[id]
|
|
|
|
if id is not None:
|
|
|
|
if id in self._nameCache:
|
|
|
|
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]
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2003-09-12 22:06:58 +02:00
|
|
|
def setUser(self, id, user):
|
2003-09-13 19:29:56 +02:00
|
|
|
"""Sets a user (given its id) to the IrcUser given it."""
|
2003-09-12 22:06:58 +02:00
|
|
|
assert isinstance(id, int), 'setUser takes an integer userId.'
|
2004-02-02 03:56:16 +01:00
|
|
|
self.nextId = max(self.nextId, id)
|
2003-03-31 11:26:51 +02:00
|
|
|
try:
|
2003-09-12 22:06:58 +02:00
|
|
|
if self.getUserId(user.name) != id:
|
2004-02-08 10:25:14 +01:00
|
|
|
s = '%s is someone else\'s hostmask.' % user.name
|
2003-12-01 13:04:02 +01:00
|
|
|
raise ValueError, s
|
2003-03-31 11:26:51 +02:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2003-09-12 22:06:58 +02:00
|
|
|
for hostmask in user.hostmasks:
|
2004-02-08 10:25:14 +01:00
|
|
|
for (i, u) in self.iteritems():
|
|
|
|
if i == id:
|
|
|
|
continue
|
|
|
|
elif u.checkHostmask(hostmask):
|
|
|
|
s = '%s is someone else\'s hostmask.' % hostmask
|
2003-12-01 13:04:02 +01:00
|
|
|
raise ValueError, s
|
2004-02-08 10:25:14 +01:00
|
|
|
for otherHostmask in u.hostmasks:
|
|
|
|
if ircutils.hostmaskPatternEqual(hostmask, otherHostmask):
|
|
|
|
s = '%s is someone else\'s hostmask.' % hostmask
|
|
|
|
raise ValueError, s
|
2004-03-25 13:16:37 +01:00
|
|
|
self.invalidateCache(id)
|
2003-09-12 22:06:58 +02:00
|
|
|
self.users[id] = user
|
2003-11-24 23:32:33 +01:00
|
|
|
self.flush()
|
2003-09-12 22:06:58 +02:00
|
|
|
|
|
|
|
def delUser(self, id):
|
2003-09-13 19:29:56 +02:00
|
|
|
"""Removes a user from the database."""
|
2004-02-02 03:56:16 +01:00
|
|
|
del self.users[id]
|
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]
|
2003-11-24 23:32:33 +01:00
|
|
|
self.flush()
|
2003-09-12 22:06:58 +02:00
|
|
|
|
|
|
|
def newUser(self):
|
2003-09-13 19:29:56 +02:00
|
|
|
"""Allocates a new user in the database and returns it and its id."""
|
2004-04-05 11:23:40 +02:00
|
|
|
hashed = conf.supybot.databases.users.hash()
|
|
|
|
user = IrcUser(hashed=hashed)
|
2003-09-12 22:06:58 +02:00
|
|
|
self.nextId += 1
|
2004-02-07 23:23:12 +01:00
|
|
|
id = self.nextId
|
2004-02-02 03:56:16 +01:00
|
|
|
self.users[id] = user
|
2003-11-24 23:32:33 +01:00
|
|
|
self.flush()
|
2003-09-12 22:06:58 +02:00
|
|
|
return (id, user)
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-12-03 06:26:14 +01:00
|
|
|
class ChannelsDictionary(utils.IterableMap):
|
2004-02-02 03:56:16 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.filename = None
|
|
|
|
self.channels = ircutils.IrcDict()
|
|
|
|
|
|
|
|
def open(self, filename):
|
2003-03-12 07:26:59 +01:00
|
|
|
self.filename = filename
|
2004-02-07 23:23:12 +01:00
|
|
|
reader = unpreserve.Reader(IrcChannelCreator, self)
|
2004-02-14 02:02:53 +01:00
|
|
|
try:
|
|
|
|
reader.readFile(filename)
|
|
|
|
except Exception, e:
|
|
|
|
log.error('Invalid channel database, starting from scratch.')
|
|
|
|
log.error('Exact error: %s', utils.exnToString(e))
|
2004-02-02 03:56:16 +01:00
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
"""Flushes the channel database to its file."""
|
2004-02-04 01:39:52 +01:00
|
|
|
if self.filename is not None:
|
2004-08-01 14:46:03 +02:00
|
|
|
fd = utils.transactionalFile(self.filename)
|
2004-02-02 03:56:16 +01:00
|
|
|
for (channel, c) in self.channels.iteritems():
|
|
|
|
fd.write('channel %s' % channel)
|
|
|
|
fd.write(os.linesep)
|
|
|
|
c.preserve(fd, indent=' ')
|
|
|
|
fd.close()
|
|
|
|
else:
|
2004-02-03 04:07:54 +01:00
|
|
|
log.warning('ChannelsDictionary.flush without self.filename.')
|
2004-02-02 03:56:16 +01:00
|
|
|
|
2004-02-07 23:23:12 +01:00
|
|
|
def close(self):
|
|
|
|
self.flush()
|
|
|
|
if self.flush in world.flushers:
|
|
|
|
world.flushers.remove(self.flush)
|
|
|
|
self.channels.clear()
|
|
|
|
|
2004-02-02 03:56:16 +01:00
|
|
|
def reload(self):
|
|
|
|
"""Reloads the channel database from its file."""
|
2004-02-04 01:39:52 +01:00
|
|
|
if self.filename is not None:
|
2004-02-02 03:56:16 +01:00
|
|
|
self.channels.clear()
|
2004-02-03 04:07:54 +01:00
|
|
|
try:
|
|
|
|
self.open(self.filename)
|
|
|
|
except EnvironmentError, e:
|
|
|
|
log.warning('ChannelsDictionary.reload failed: %s', e)
|
2004-02-02 03:56:16 +01:00
|
|
|
else:
|
2004-02-03 04:07:54 +01:00
|
|
|
log.warning('ChannelsDictionary.reload without self.filename.')
|
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()
|
2004-02-02 03:56:16 +01:00
|
|
|
if channel in self.channels:
|
|
|
|
return self.channels[channel]
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
|
|
|
c = IrcChannel()
|
2004-02-02 03:56:16 +01:00
|
|
|
self.channels[channel] = c
|
2003-03-12 07:26:59 +01:00
|
|
|
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()
|
2004-02-02 03:56:16 +01:00
|
|
|
self.channels[channel] = ircChannel
|
2003-11-24 23:32:33 +01:00
|
|
|
self.flush()
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-12-03 06:26:14 +01:00
|
|
|
def iteritems(self):
|
2004-02-02 03:56:16 +01:00
|
|
|
return self.channels.iteritems()
|
2003-12-03 06:26:14 +01:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-02-04 01:39:52 +01:00
|
|
|
class IgnoresDB(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.filename = None
|
|
|
|
self.hostmasks = sets.Set()
|
|
|
|
|
|
|
|
def open(self, filename):
|
|
|
|
self.filename = filename
|
|
|
|
fd = file(self.filename)
|
2004-02-04 05:56:37 +01:00
|
|
|
for line in utils.nonCommentNonEmptyLines(fd):
|
2004-02-04 01:39:52 +01:00
|
|
|
self.hostmasks.add(line.rstrip('\r\n'))
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
if self.filename is not None:
|
2004-08-01 14:46:03 +02:00
|
|
|
fd = utils.transactionalFile(self.filename)
|
2004-02-04 01:39:52 +01:00
|
|
|
for hostmask in self.hostmasks:
|
|
|
|
fd.write(hostmask)
|
|
|
|
fd.write(os.linesep)
|
|
|
|
fd.close()
|
|
|
|
else:
|
|
|
|
log.warning('IgnoresDB.flush called without self.filename.')
|
|
|
|
|
2004-02-08 10:25:14 +01:00
|
|
|
def close(self):
|
|
|
|
if self.flush in world.flushers:
|
|
|
|
world.flushers.remove(self.flush)
|
|
|
|
self.flush()
|
|
|
|
self.hostmasks.clear()
|
|
|
|
|
2004-02-04 01:39:52 +01:00
|
|
|
def reload(self):
|
|
|
|
if self.filename is not None:
|
|
|
|
self.hostmasks.clear()
|
|
|
|
try:
|
|
|
|
self.open(self.filename)
|
|
|
|
except EnvironmentError, e:
|
|
|
|
log.warning('IgnoresDB.reload failed: %s', e)
|
|
|
|
else:
|
|
|
|
log.warning('IgnoresDB.reload called without self.filename.')
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-02-04 01:39:52 +01:00
|
|
|
def checkIgnored(self, prefix):
|
|
|
|
for hostmask in self.hostmasks:
|
|
|
|
if ircutils.hostmaskPatternEqual(hostmask, prefix):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def addHostmask(self, hostmask):
|
2004-02-08 10:25:14 +01:00
|
|
|
assert ircutils.isUserHostmask(hostmask)
|
2004-02-04 01:39:52 +01:00
|
|
|
self.hostmasks.add(hostmask)
|
|
|
|
|
|
|
|
def removeHostmask(self, hostmask):
|
|
|
|
self.hostmasks.remove(hostmask)
|
|
|
|
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
confDir = conf.supybot.directories.conf()
|
2004-02-02 03:56:16 +01:00
|
|
|
try:
|
2004-02-04 01:39:52 +01:00
|
|
|
userFile = os.path.join(confDir, conf.supybot.databases.users.filename())
|
|
|
|
users = UsersDictionary()
|
|
|
|
users.open(userFile)
|
2004-02-02 03:56:16 +01:00
|
|
|
except EnvironmentError, e:
|
|
|
|
log.warning('Couldn\'t open user database: %s', e)
|
|
|
|
|
|
|
|
try:
|
2004-02-04 01:39:52 +01:00
|
|
|
channelFile = os.path.join(confDir,
|
|
|
|
conf.supybot.databases.channels.filename())
|
|
|
|
channels = ChannelsDictionary()
|
|
|
|
channels.open(channelFile)
|
2004-02-02 03:56:16 +01:00
|
|
|
except EnvironmentError, e:
|
|
|
|
log.warning('Couldn\'t open channel database: %s', e)
|
2004-02-03 05:10:22 +01:00
|
|
|
|
2004-02-04 01:39:52 +01:00
|
|
|
try:
|
|
|
|
ignoreFile = os.path.join(confDir,
|
|
|
|
conf.supybot.databases.ignores.filename())
|
|
|
|
ignores = IgnoresDB()
|
|
|
|
ignores.open(ignoreFile)
|
|
|
|
except EnvironmentError, e:
|
|
|
|
log.warning('Couldn\'t open ignore database: %s', e)
|
|
|
|
|
|
|
|
|
2004-02-03 05:10:22 +01:00
|
|
|
world.flushers.append(users.flush)
|
2004-02-04 01:39:52 +01:00
|
|
|
world.flushers.append(ignores.flush)
|
2004-02-03 05:10:22 +01:00
|
|
|
world.flushers.append(channels.flush)
|
2004-07-21 21:36:35 +02:00
|
|
|
|
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-02-04 01:39:52 +01:00
|
|
|
if ignores.checkIgnored(hostmask):
|
|
|
|
log.debug('Ignoring %s due to ignore database.', hostmask)
|
|
|
|
return True
|
2003-03-12 07:26:59 +01:00
|
|
|
try:
|
2003-09-12 22:06:58 +02:00
|
|
|
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):
|
2004-01-19 21:26:20 +01:00
|
|
|
log.debug('Ignoring %s due to the channel ignores.', hostmask)
|
2003-12-16 21:56:05 +01:00
|
|
|
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():
|
2004-01-19 21:26:20 +01:00
|
|
|
log.debug('Ignoring %s due to conf.supybot.defaultIgnore',
|
2004-01-18 08:58:26 +01:00
|
|
|
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:
|
2004-01-19 21:26:20 +01:00
|
|
|
log.debug('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):
|
2004-01-19 21:26:20 +01:00
|
|
|
log.debug('Ignoring %s due to the channel ignores.', hostmask)
|
2003-12-16 21:56:05 +01:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2003-04-21 07:38:13 +02:00
|
|
|
def _x(capability, ret):
|
|
|
|
if isAntiCapability(capability):
|
|
|
|
return not ret
|
|
|
|
else:
|
|
|
|
return ret
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-10-04 00:19:22 +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-07-31 07:00:43 +02:00
|
|
|
defaultCapabilities = conf.supybot.capabilities()
|
|
|
|
if capability in defaultCapabilities:
|
2003-10-04 00:19:22 +02:00
|
|
|
return True
|
2004-07-31 07:00:43 +02:00
|
|
|
elif invertCapability(capability) in defaultCapabilities:
|
2003-10-04 00:19:22 +02:00
|
|
|
return False
|
|
|
|
else:
|
2004-07-31 07:00:43 +02:00
|
|
|
return _x(capability, conf.supybot.capabilities.default())
|
2003-10-04 00:19:22 +02:00
|
|
|
|
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:
|
2003-04-21 07:38:13 +02:00
|
|
|
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)
|
2003-10-03 11:57:52 +02:00
|
|
|
if u.secure and not u.checkHostmask(hostmask, useAuth=False):
|
|
|
|
raise KeyError
|
2003-04-20 23:52:53 +02:00
|
|
|
except KeyError:
|
2003-10-04 00:19:22 +02:00
|
|
|
# Raised when no hostmasks match.
|
|
|
|
return _checkCapabilityForUnknownUser(capability, users=users,
|
|
|
|
channels=channels)
|
|
|
|
except ValueError, e:
|
|
|
|
# Raised when multiple hostmasks match.
|
2003-11-26 19:21:12 +01:00
|
|
|
log.warning('%s: %s', hostmask, e)
|
2003-10-04 00:19:22 +02:00
|
|
|
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:
|
2003-09-13 14:59:22 +02:00
|
|
|
chanop = makeChannelCapability(channel, 'op')
|
|
|
|
if u.checkCapability(chanop):
|
|
|
|
return _x(capability, True)
|
2003-04-20 23:52:53 +02:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2003-09-13 14:59:22 +02:00
|
|
|
c = channels.getChannel(channel)
|
|
|
|
if capability in c.capabilities:
|
|
|
|
return c.checkCapability(capability)
|
|
|
|
else:
|
|
|
|
return _x(capability, c.defaultAllow)
|
2004-07-31 07:00:43 +02:00
|
|
|
defaultCapabilities = conf.supybot.capabilities()
|
2004-01-18 08:58:26 +01:00
|
|
|
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-07-31 07:00:43 +02:00
|
|
|
return _x(capability, conf.supybot.capabilities.default())
|
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
|
|
|
|
|
2003-03-28 08:38:16 +01:00
|
|
|
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|