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-08-02 18:48:43 +02:00
|
|
|
"""
|
|
|
|
Provides a great number of useful utility functions IRC. Things to muck around
|
|
|
|
with hostmasks, set bold or color on strings, IRC-case-insensitive dicts, a
|
|
|
|
nick class to handle nicks (so comparisons and hashing and whatnot work in an
|
|
|
|
IRC-case-insensitive fashion), and numerous other things.
|
|
|
|
"""
|
|
|
|
|
2003-11-25 09:38:19 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2003-10-05 14:47:19 +02:00
|
|
|
import fix
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
import re
|
2003-11-04 06:50:19 +01:00
|
|
|
import copy
|
2003-08-12 21:12:12 +02:00
|
|
|
import sets
|
2003-11-15 05:37:04 +01:00
|
|
|
import time
|
|
|
|
import random
|
2004-01-02 21:50:43 +01:00
|
|
|
import socket
|
2003-03-12 07:26:59 +01:00
|
|
|
import string
|
|
|
|
import fnmatch
|
2003-04-17 12:11:34 +02:00
|
|
|
import operator
|
2003-11-15 05:37:04 +01:00
|
|
|
from itertools import imap
|
2003-12-04 00:13:07 +01:00
|
|
|
from cStringIO import StringIO as sio
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-01-16 18:33:51 +01:00
|
|
|
import utils
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def isUserHostmask(s):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns whether or not the string s is a valid User hostmask."""
|
2003-03-12 07:26:59 +01:00
|
|
|
p1 = s.find('!')
|
|
|
|
p2 = s.find('@')
|
|
|
|
if p1 < p2-1 and p1 >= 1 and p2 >= 3 and len(s) > p2+1:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def isServerHostmask(s):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns True if s is a valid server hostmask."""
|
2003-08-11 19:16:03 +02:00
|
|
|
return not isUserHostmask(s)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def nickFromHostmask(hostmask):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns the nick from a user hostmask."""
|
2003-04-03 10:17:21 +02:00
|
|
|
assert isUserHostmask(hostmask)
|
2003-08-12 21:12:12 +02:00
|
|
|
return hostmask.split('!', 1)[0]
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def userFromHostmask(hostmask):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns the user from a user hostmask."""
|
2003-04-03 10:17:21 +02:00
|
|
|
assert isUserHostmask(hostmask)
|
2003-03-12 07:26:59 +01:00
|
|
|
return hostmask.split('!', 1)[1].split('@', 1)[0]
|
|
|
|
|
|
|
|
def hostFromHostmask(hostmask):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns the host from a user hostmask."""
|
2003-04-03 10:17:21 +02:00
|
|
|
assert isUserHostmask(hostmask)
|
2003-03-12 07:26:59 +01:00
|
|
|
return hostmask.split('@', 1)[1]
|
|
|
|
|
|
|
|
def splitHostmask(hostmask):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns the nick, user, host of a user hostmask."""
|
2003-04-03 10:17:21 +02:00
|
|
|
assert isUserHostmask(hostmask)
|
2003-08-12 21:12:12 +02:00
|
|
|
nick, rest = hostmask.split('!', 1)
|
2003-03-12 07:26:59 +01:00
|
|
|
user, host = rest.split('@', 1)
|
2003-08-12 21:12:12 +02:00
|
|
|
return (nick, user, host)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def joinHostmask(nick, ident, host):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Joins the nick, ident, host into a user hostmask."""
|
2003-04-03 10:17:21 +02:00
|
|
|
assert nick and ident and host
|
2003-03-12 07:26:59 +01:00
|
|
|
return '%s!%s@%s' % (nick, ident, host)
|
|
|
|
|
2003-04-04 09:07:19 +02:00
|
|
|
_lowertrans = string.maketrans(string.ascii_uppercase + r'\[]~',
|
|
|
|
string.ascii_lowercase + r'|{}^')
|
2003-04-20 11:32:52 +02:00
|
|
|
def toLower(s):
|
|
|
|
"""Returns the string s lowered according to IRC case rules."""
|
2003-11-26 19:21:12 +01:00
|
|
|
return intern(s.translate(_lowertrans))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def nickEqual(nick1, nick2):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns True if nick1 == nick2 according to IRC case rules."""
|
2003-04-04 09:07:19 +02:00
|
|
|
return toLower(nick1) == toLower(nick2)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-02-11 06:57:34 +01:00
|
|
|
|
2003-06-02 07:31:13 +02:00
|
|
|
_nickchars = r'_[]\`^{}|-'
|
2004-02-11 06:57:34 +01:00
|
|
|
nickRe = re.compile(r'^[A-Za-z%s][0-9A-Za-z%s]*$'
|
|
|
|
% (re.escape(_nickchars), re.escape(_nickchars)))
|
2003-12-10 19:49:45 +01:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def isNick(s):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns True if s is a valid IRC nick."""
|
2004-02-24 12:21:12 +01:00
|
|
|
return bool(nickRe.match(s))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def isChannel(s):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns True if s is a valid IRC channel name."""
|
2003-04-04 09:07:19 +02:00
|
|
|
return (s and s[0] in '#&+!' and len(s) <= 50 and \
|
|
|
|
'\x07' not in s and ',' not in s and ' ' not in s)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-02-11 06:57:34 +01:00
|
|
|
def isCtcp(msg):
|
|
|
|
"""Returns whether or not msg is a CTCP message."""
|
|
|
|
return msg.command == 'PRIVMSG' and \
|
|
|
|
msg.args[1].startswith('\x01') and \
|
|
|
|
msg.args[1].endswith('\x01')
|
2003-12-04 00:13:07 +01:00
|
|
|
|
|
|
|
_patternCache = {}
|
2003-12-16 21:36:43 +01:00
|
|
|
def _hostmaskPatternEqual(pattern, hostmask):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns True if hostmask matches the hostmask pattern pattern."""
|
2003-12-04 00:13:07 +01:00
|
|
|
try:
|
2003-12-16 21:36:43 +01:00
|
|
|
return _patternCache[pattern](hostmask) is not None
|
2003-12-04 00:13:07 +01:00
|
|
|
except KeyError:
|
2004-04-09 17:30:09 +02:00
|
|
|
# We make our own regexps, rather than use fnmatch, because fnmatch's
|
|
|
|
# case-insensitivity is not IRC's case-insensitity.
|
2003-12-04 00:13:07 +01:00
|
|
|
fd = sio()
|
|
|
|
for c in pattern:
|
|
|
|
if c == '*':
|
|
|
|
fd.write('.*')
|
|
|
|
elif c == '?':
|
|
|
|
fd.write('.')
|
2004-04-09 17:30:09 +02:00
|
|
|
elif c in '[{':
|
2003-12-04 00:13:07 +01:00
|
|
|
fd.write('[[{]')
|
2004-04-09 17:30:09 +02:00
|
|
|
elif c in '}]':
|
2003-12-04 00:13:07 +01:00
|
|
|
fd.write(r'[}\]]')
|
2004-04-09 17:30:09 +02:00
|
|
|
elif c in '|\\':
|
2003-12-04 00:13:07 +01:00
|
|
|
fd.write(r'[|\\]')
|
2004-04-09 17:30:09 +02:00
|
|
|
elif c in '^~':
|
2003-12-04 00:13:07 +01:00
|
|
|
fd.write('[~^]')
|
|
|
|
else:
|
|
|
|
fd.write(re.escape(c))
|
|
|
|
fd.write('$')
|
|
|
|
f = re.compile(fd.getvalue(), re.I).match
|
|
|
|
_patternCache[pattern] = f
|
2003-12-16 21:36:43 +01:00
|
|
|
return f(hostmask) is not None
|
|
|
|
|
|
|
|
_hostmaskPatternEqualCache = {}
|
|
|
|
def hostmaskPatternEqual(pattern, hostmask):
|
|
|
|
try:
|
|
|
|
return _hostmaskPatternEqualCache[(pattern, hostmask)]
|
|
|
|
except KeyError:
|
|
|
|
b = _hostmaskPatternEqual(pattern, hostmask)
|
|
|
|
_hostmaskPatternEqualCache[(pattern, hostmask)] = b
|
|
|
|
return b
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def banmask(hostmask):
|
|
|
|
"""Returns a properly generic banning hostmask for a hostmask.
|
|
|
|
|
|
|
|
>>> banmask('nick!user@host.domain.tld')
|
|
|
|
'*!*@*.domain.tld'
|
|
|
|
|
|
|
|
>>> banmask('nick!user@10.0.0.1')
|
|
|
|
'*!*@10.0.0.*'
|
|
|
|
"""
|
2003-10-02 06:47:33 +02:00
|
|
|
assert isUserHostmask(hostmask)
|
2003-03-12 07:26:59 +01:00
|
|
|
host = hostFromHostmask(hostmask)
|
2004-01-16 18:33:51 +01:00
|
|
|
if utils.isIP(host):
|
2004-01-02 21:50:43 +01:00
|
|
|
L = host.split('.')
|
|
|
|
L[-1] = '*'
|
|
|
|
return '*!*@' + '.'.join(L)
|
2004-01-16 18:33:51 +01:00
|
|
|
elif utils.isIPV6(host):
|
2004-01-02 21:50:43 +01:00
|
|
|
L = host.split(':')
|
|
|
|
L[-1] = '*'
|
|
|
|
return '*!*@' + ':'.join(L)
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-11-13 20:00:35 +01:00
|
|
|
if '.' in host:
|
|
|
|
return '*!*@*%s' % host[host.find('.'):]
|
|
|
|
else:
|
|
|
|
return '*!*@' + host
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-02-11 07:57:35 +01:00
|
|
|
_plusRequireArguments = 'ovhblkqe'
|
2004-02-13 09:58:29 +01:00
|
|
|
_minusRequireArguments = 'ovhbkqe'
|
2004-02-11 07:57:35 +01:00
|
|
|
def separateModes(args):
|
2003-08-02 18:48:43 +02:00
|
|
|
"""Separates modelines into single mode change tuples. Basically, you
|
|
|
|
should give it the .args of a MODE IrcMsg.
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
>>> separateModes(['+ooo', 'jemfinch', 'StoneTable', 'philmes'])
|
|
|
|
[('+o', 'jemfinch'), ('+o', 'StoneTable'), ('+o', 'philmes')]
|
|
|
|
|
|
|
|
>>> separateModes(['+o-o', 'jemfinch', 'PeterB'])
|
|
|
|
[('+o', 'jemfinch'), ('-o', 'PeterB')]
|
|
|
|
|
|
|
|
>>> separateModes(['+s-o', 'test'])
|
|
|
|
[('+s', None), ('-o', 'test')]
|
|
|
|
|
|
|
|
>>> separateModes(['+sntl', '100'])
|
2004-02-16 01:51:29 +01:00
|
|
|
[('+s', None), ('+n', None), ('+t', None), ('+l', 100)]
|
2003-03-12 07:26:59 +01:00
|
|
|
"""
|
2004-02-11 07:57:35 +01:00
|
|
|
if not args:
|
|
|
|
return []
|
2003-03-12 07:26:59 +01:00
|
|
|
modes = args[0]
|
2003-04-06 14:27:34 +02:00
|
|
|
assert modes[0] in '+-', 'Invalid args: %r' % args
|
2003-03-12 07:26:59 +01:00
|
|
|
args = list(args[1:])
|
|
|
|
ret = []
|
|
|
|
length = len(modes)
|
2004-02-16 01:54:05 +01:00
|
|
|
for c in modes:
|
|
|
|
if c in '+-':
|
|
|
|
last = c
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2004-02-11 07:57:35 +01:00
|
|
|
if last == '+':
|
|
|
|
requireArguments = _plusRequireArguments
|
|
|
|
else:
|
|
|
|
requireArguments = _minusRequireArguments
|
2004-02-16 01:54:05 +01:00
|
|
|
if c in requireArguments:
|
2004-02-16 01:51:29 +01:00
|
|
|
arg = args.pop(0)
|
|
|
|
try:
|
|
|
|
arg = int(arg)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2004-02-16 01:54:05 +01:00
|
|
|
ret.append((last + c, arg))
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2004-02-16 01:54:05 +01:00
|
|
|
ret.append((last + c, None))
|
2004-02-11 07:57:35 +01:00
|
|
|
return ret
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-06 14:27:34 +02:00
|
|
|
def joinModes(modes):
|
|
|
|
"""Joins modes of the same form as returned by separateModes."""
|
|
|
|
args = []
|
|
|
|
modeChars = []
|
|
|
|
currentMode = '\x00'
|
|
|
|
for (mode, arg) in modes:
|
|
|
|
if arg is not None:
|
|
|
|
args.append(arg)
|
|
|
|
if not mode.startswith(currentMode):
|
|
|
|
currentMode = mode[0]
|
|
|
|
modeChars.append(mode[0])
|
|
|
|
modeChars.append(mode[1])
|
|
|
|
args.insert(0, ''.join(modeChars))
|
|
|
|
return args
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def bold(s):
|
2003-04-06 14:27:34 +02:00
|
|
|
"""Returns the string s, bolded."""
|
2003-09-03 11:46:06 +02:00
|
|
|
return '\x02%s\x0F' % s
|
|
|
|
|
|
|
|
def reverse(s):
|
|
|
|
"""Returns the string s, reverse-videoed."""
|
|
|
|
return '\x16%s\x0F' % s
|
|
|
|
|
|
|
|
def underline(s):
|
|
|
|
"""Returns the string s, underlined."""
|
|
|
|
return '\x1F%s\x0F' % s
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-07-23 17:13:30 +02:00
|
|
|
mircColors = {
|
|
|
|
None: '',
|
2003-07-25 06:00:38 +02:00
|
|
|
'white': 0,
|
|
|
|
'black': 1,
|
|
|
|
'blue': 2,
|
|
|
|
'green': 3,
|
|
|
|
'red': 4,
|
|
|
|
'brown': 5,
|
|
|
|
'purple': 6,
|
|
|
|
'orange': 7,
|
|
|
|
'yellow': 8,
|
|
|
|
'light green': 9,
|
|
|
|
'teal': 10,
|
|
|
|
'light blue': 11,
|
|
|
|
'dark blue': 12,
|
|
|
|
'pink': 13,
|
|
|
|
'dark grey': 14,
|
|
|
|
'light grey': 15,
|
2003-07-23 17:13:30 +02:00
|
|
|
}
|
|
|
|
|
2003-08-02 18:48:43 +02:00
|
|
|
# Offer a reverse mapping from integers to their associated colors.
|
2003-07-23 17:13:30 +02:00
|
|
|
for (k, v) in mircColors.items():
|
2003-07-25 06:00:38 +02:00
|
|
|
if k is not None: # Ignore empty string for None.
|
|
|
|
mircColors[v] = k
|
2003-07-23 07:29:16 +02:00
|
|
|
|
|
|
|
def mircColor(s, fg=None, bg=None):
|
2003-08-02 18:48:43 +02:00
|
|
|
"""Returns s with the appropriate mIRC color codes applied."""
|
2003-07-23 17:13:30 +02:00
|
|
|
if fg is None and bg is None:
|
|
|
|
return s
|
2003-07-30 13:04:59 +02:00
|
|
|
if fg is None or isinstance(fg, str):
|
|
|
|
fg = mircColors[fg]
|
2003-07-25 06:00:38 +02:00
|
|
|
if bg is None:
|
2003-09-03 11:46:06 +02:00
|
|
|
return '\x03%s%s\x0F' % (fg, s)
|
2003-07-23 17:13:30 +02:00
|
|
|
else:
|
2003-07-25 06:00:38 +02:00
|
|
|
if isinstance(bg, str):
|
|
|
|
bg = mircColors[bg]
|
2003-09-03 11:46:06 +02:00
|
|
|
return '\x03%s,%s%s\x0F' % (fg, bg, s)
|
2003-07-30 13:04:59 +02:00
|
|
|
|
2003-08-12 10:50:28 +02:00
|
|
|
def canonicalColor(s, bg=False, shift=0):
|
2003-10-04 14:36:48 +02:00
|
|
|
"""Assigns an (fg, bg) canonical color pair to a string based on its hash
|
|
|
|
value. This means it might change between Python versions. This pair can
|
|
|
|
be used as a *parameter to mircColor. The shift parameter is how much to
|
|
|
|
right-shift the hash value initially.
|
|
|
|
"""
|
2003-08-12 10:50:28 +02:00
|
|
|
h = hash(s) >> shift
|
2003-08-02 18:48:43 +02:00
|
|
|
fg = h % 14 + 2 # The + 2 is to rule out black and white.
|
|
|
|
if bg:
|
|
|
|
bg = (h >> 4) & 3 # The 5th, 6th, and 7th least significant bits.
|
|
|
|
if fg < 8:
|
|
|
|
bg += 8
|
|
|
|
else:
|
|
|
|
bg += 2
|
2003-08-12 10:50:28 +02:00
|
|
|
return (fg, bg)
|
2003-08-02 18:48:43 +02:00
|
|
|
else:
|
2003-08-12 10:50:28 +02:00
|
|
|
return (fg, None)
|
|
|
|
|
2004-04-09 05:59:12 +02:00
|
|
|
_unColorRe = re.compile(r'\x03(?:\d{1,2},\d{1,2}|\d{1,2}|,\d{1,2}|)')
|
2003-08-12 10:50:28 +02:00
|
|
|
def unColor(s):
|
2003-09-04 19:33:04 +02:00
|
|
|
"""Removes the color from a string."""
|
2003-08-12 10:50:28 +02:00
|
|
|
return _unColorRe.sub('', s)
|
2003-07-23 07:29:16 +02:00
|
|
|
|
2003-04-04 08:08:36 +02:00
|
|
|
def isValidArgument(s):
|
2004-02-10 07:19:16 +01:00
|
|
|
"""Returns whether s is strictly a valid argument for an IRC message."""
|
2003-03-12 07:26:59 +01:00
|
|
|
return '\r' not in s and '\n' not in s and '\x00' not in s
|
|
|
|
|
2003-04-03 10:31:47 +02:00
|
|
|
def safeArgument(s):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""If s is unsafe for IRC, returns a safe version."""
|
2004-02-10 07:19:16 +01:00
|
|
|
if isinstance(s, unicode):
|
|
|
|
s = s.encode('utf-8')
|
|
|
|
if isValidArgument(s):
|
2003-04-03 10:31:47 +02:00
|
|
|
return s
|
|
|
|
else:
|
|
|
|
return repr(s)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 08:59:39 +02:00
|
|
|
def replyTo(msg):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns the appropriate target to send responses to msg."""
|
2003-03-12 07:26:59 +01:00
|
|
|
if isChannel(msg.args[0]):
|
|
|
|
return msg.args[0]
|
|
|
|
else:
|
|
|
|
return msg.nick
|
|
|
|
|
2003-10-09 18:12:53 +02:00
|
|
|
def dccIP(ip):
|
|
|
|
"""Returns in IP in the proper for DCC."""
|
2004-01-16 18:33:51 +01:00
|
|
|
assert utils.isIP(ip), \
|
|
|
|
'argument must be a string ip in xxx.yyy.zzz.www format.'
|
2003-10-09 18:12:53 +02:00
|
|
|
i = 0
|
|
|
|
x = 256**3
|
|
|
|
for quad in ip.split('.'):
|
|
|
|
i += int(quad)*x
|
|
|
|
x /= 256
|
|
|
|
return i
|
|
|
|
|
|
|
|
def unDccIP(i):
|
|
|
|
"""Takes an integer DCC IP and return a normal string IP."""
|
|
|
|
assert isinstance(i, (int, long)), '%r is not an number.' % i
|
|
|
|
L = []
|
|
|
|
while len(L) < 4:
|
2003-11-15 05:37:04 +01:00
|
|
|
L.append(i % 256)
|
2003-10-09 18:12:53 +02:00
|
|
|
i /= 256
|
2003-11-15 05:37:04 +01:00
|
|
|
L.reverse()
|
|
|
|
return '.'.join(imap(str, L))
|
2003-04-10 09:29:08 +02:00
|
|
|
|
2003-12-06 15:18:10 +01:00
|
|
|
class IrcString(str):
|
2003-08-14 16:15:07 +02:00
|
|
|
"""This class does case-insensitive comparison and hashing of nicks."""
|
|
|
|
def __init__(self, s):
|
2003-12-16 19:14:48 +01:00
|
|
|
assert isinstance(s, basestring), \
|
|
|
|
'Cannot make an IrcString from %s' % type(s)
|
2003-12-06 15:16:18 +01:00
|
|
|
str.__init__(self, intern(s)) # This does nothing, I fear.
|
2003-08-14 16:15:07 +02:00
|
|
|
self.lowered = toLower(s)
|
|
|
|
|
|
|
|
def __eq__(self, s):
|
|
|
|
try:
|
|
|
|
return toLower(s) == self.lowered
|
|
|
|
except:
|
|
|
|
return False
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-09 07:46:35 +02:00
|
|
|
def __ne__(self, s):
|
2004-01-23 14:08:42 +01:00
|
|
|
return not (self == s)
|
2003-10-09 07:46:35 +02:00
|
|
|
|
2003-08-14 16:15:07 +02:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.lowered)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-10 09:29:08 +02:00
|
|
|
|
2004-02-05 08:14:35 +01:00
|
|
|
class IrcDict(utils.InsensitivePreservingDict):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Subclass of dict to make key comparison IRC-case insensitive."""
|
2004-02-05 08:14:35 +01:00
|
|
|
key = staticmethod(toLower)
|
2004-01-19 23:29:55 +01:00
|
|
|
|
|
|
|
|
2003-08-12 21:12:12 +02:00
|
|
|
class IrcSet(sets.Set):
|
2003-10-04 14:36:48 +02:00
|
|
|
"""A sets.Set using IrcStrings instead of regular strings."""
|
2003-11-04 06:50:19 +01:00
|
|
|
def __init__(self, seq=()):
|
2003-11-04 00:25:04 +01:00
|
|
|
self.__parent = super(IrcSet, self)
|
|
|
|
self.__parent.__init__()
|
2003-11-04 06:50:19 +01:00
|
|
|
for elt in seq:
|
|
|
|
self.add(elt)
|
2003-11-04 00:25:04 +01:00
|
|
|
|
2003-08-12 21:12:12 +02:00
|
|
|
def add(self, s):
|
2003-11-04 00:25:04 +01:00
|
|
|
return self.__parent.add(IrcString(s))
|
2003-08-12 21:12:12 +02:00
|
|
|
|
|
|
|
def remove(self, s):
|
2003-11-04 00:25:04 +01:00
|
|
|
return self.__parent.remove(IrcString(s))
|
2003-08-12 21:12:12 +02:00
|
|
|
|
|
|
|
def discard(self, s):
|
2003-11-04 00:25:04 +01:00
|
|
|
return self.__parent.discard(IrcString(s))
|
2003-08-12 21:12:12 +02:00
|
|
|
|
|
|
|
def __contains__(self, s):
|
2003-11-04 00:25:04 +01:00
|
|
|
return self.__parent.__contains__(IrcString(s))
|
2003-08-12 21:12:12 +02:00
|
|
|
has_key = __contains__
|
|
|
|
|
2003-11-04 06:50:19 +01:00
|
|
|
def __reduce__(self):
|
|
|
|
return (self.__class__, (list(self),))
|
|
|
|
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys, doctest
|
|
|
|
doctest.testmod(sys.modules['__main__'])
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|