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.
|
|
|
|
###
|
|
|
|
|
|
|
|
from fix import *
|
|
|
|
|
|
|
|
import re
|
|
|
|
import string
|
|
|
|
import fnmatch
|
2003-04-17 12:11:34 +02:00
|
|
|
import operator
|
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-03-12 07:26:59 +01:00
|
|
|
return (not isUserHostmask(s) and s.find('!') == -1 and s.find('@') == -1)
|
|
|
|
|
|
|
|
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-03-12 07:26:59 +01:00
|
|
|
return nick(hostmask.split('!', 1)[0])
|
|
|
|
|
|
|
|
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-03-12 07:26:59 +01:00
|
|
|
nck, rest = hostmask.split('!', 1)
|
|
|
|
user, host = rest.split('@', 1)
|
|
|
|
return (nick(nck), user, host)
|
|
|
|
|
|
|
|
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."""
|
|
|
|
return 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
|
|
|
|
2003-06-02 07:31:13 +02:00
|
|
|
_nickchars = r'_[]\`^{}|-'
|
|
|
|
_nickre = re.compile(r'^[A-Za-z%s][0-9A-Za-z%s]+$' % (re.escape(_nickchars),
|
2003-04-10 09:29:08 +02:00
|
|
|
re.escape(_nickchars)))
|
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."""
|
2003-03-12 07:26:59 +01:00
|
|
|
if re.match(_nickre, s):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def hostmaskPatternEqual(pattern, hostmask):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns True if hostmask matches the hostmask pattern pattern."""
|
2003-04-04 09:07:19 +02:00
|
|
|
return fnmatch.fnmatch(toLower(hostmask), toLower(pattern))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
_ipchars = string.digits + '.'
|
|
|
|
def isIP(s):
|
|
|
|
"""Not quite perfect, but close enough until I can find the regexp I want.
|
|
|
|
|
|
|
|
>>> isIP('255.255.255.255')
|
|
|
|
1
|
|
|
|
|
|
|
|
>>> isIP('abc.abc.abc.abc')
|
|
|
|
0
|
|
|
|
"""
|
2003-04-10 09:36:49 +02:00
|
|
|
if s.translate(string.ascii, _ipchars) == '':
|
|
|
|
quads = s.split('.')
|
|
|
|
if len(quads) <= 4:
|
|
|
|
for quad in quads:
|
|
|
|
if int(quad) >= 256:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
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.*'
|
|
|
|
"""
|
|
|
|
host = hostFromHostmask(hostmask)
|
|
|
|
if isIP(host):
|
|
|
|
return ('*!*@%s.*' % host[:host.rfind('.')])
|
|
|
|
else:
|
|
|
|
return ('*!*@*%s' % host[host.find('.'):])
|
|
|
|
|
|
|
|
_argModes = 'ovhblkqe'
|
|
|
|
def separateModes(args):
|
|
|
|
"""Separates modelines into single mode change tuples.
|
|
|
|
|
|
|
|
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'])
|
|
|
|
[('+s', None), ('+n', None), ('+t', None), ('+l', '100')]
|
|
|
|
"""
|
|
|
|
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 = []
|
|
|
|
index = 0
|
|
|
|
length = len(modes)
|
|
|
|
while index < length:
|
|
|
|
if modes[index] in '+-':
|
|
|
|
last = modes[index]
|
|
|
|
index += 1
|
|
|
|
else:
|
|
|
|
if modes[index] in _argModes:
|
|
|
|
ret.append((last + modes[index], args.pop(0)))
|
|
|
|
else:
|
|
|
|
ret.append((last + modes[index], None))
|
|
|
|
index += 1
|
|
|
|
return ret
|
|
|
|
|
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-03-12 07:26:59 +01:00
|
|
|
return "\x02%s\x02" % s
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-07-23 17:13:30 +02:00
|
|
|
"""Returns s, with the appropriate mIRC color codes applied."""
|
|
|
|
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:
|
|
|
|
return '\x03%s%s\x03' % (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]
|
|
|
|
return '\x03%s,%s%s\x03' % (fg, bg, s)
|
2003-07-30 13:04:59 +02:00
|
|
|
|
|
|
|
def canonicalColor(s):
|
|
|
|
return mircColor(s, hash(s) % 14 + 2)
|
2003-07-23 07:29:16 +02:00
|
|
|
|
2003-04-04 08:08:36 +02:00
|
|
|
def isValidArgument(s):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Returns if 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-17 12:11:34 +02:00
|
|
|
notFunky = string.ascii[32:]+'\x02'
|
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."""
|
2003-04-04 08:08:36 +02:00
|
|
|
if isValidArgument(s) and s.translate(string.ascii, notFunky) == '':
|
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-04-20 11:20:49 +02:00
|
|
|
def privmsgPayload(L, sep, limit=425):
|
2003-04-17 12:11:34 +02:00
|
|
|
"""Returns a valid privmsg payload given a list of strings and a separator.
|
|
|
|
|
|
|
|
Items are popped from the back of the list until the payload is small
|
|
|
|
enough to fit into a single PRIVMSG payload.
|
|
|
|
"""
|
|
|
|
shrinkList(L, sep, limit)
|
|
|
|
return sep.join(L)
|
|
|
|
|
2003-04-20 11:20:49 +02:00
|
|
|
def shrinkList(L, sep='', limit=425):
|
2003-04-17 12:11:34 +02:00
|
|
|
"""Shrinks a list of strings to a given combined length of limit."""
|
|
|
|
length = len(sep)
|
|
|
|
while reduce(operator.add, map(length.__add__, map(len, L)), 0)> limit:
|
|
|
|
L.pop()
|
|
|
|
|
2003-04-10 09:29:08 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
class nick(str):
|
|
|
|
"""This class does case-insensitive comparisons of nicks."""
|
|
|
|
def __init__(self, s):
|
2003-06-04 05:56:59 +02:00
|
|
|
self.original = s
|
2003-04-04 09:07:19 +02:00
|
|
|
self.lowered = toLower(s)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-06-04 05:56:59 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return repr(self.original)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.original)
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def __eq__(self, s):
|
|
|
|
try:
|
2003-04-04 09:07:19 +02:00
|
|
|
return toLower(s) == self.lowered
|
2003-03-12 07:26:59 +01:00
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.lowered)
|
|
|
|
|
2003-04-10 09:29:08 +02:00
|
|
|
|
|
|
|
class IrcDict(dict):
|
2003-04-20 11:32:52 +02:00
|
|
|
"""Subclass of dict to make key comparison IRC-case insensitive."""
|
2003-04-10 09:29:08 +02:00
|
|
|
def __contains__(self, s):
|
|
|
|
return dict.__contains__(self, toLower(s))
|
2003-04-11 22:44:29 +02:00
|
|
|
has_key = __contains__
|
|
|
|
|
2003-04-10 09:29:08 +02:00
|
|
|
def __setitem__(self, s, v):
|
|
|
|
dict.__setitem__(self, toLower(s), v)
|
2003-04-11 22:44:29 +02:00
|
|
|
|
2003-04-10 09:29:08 +02:00
|
|
|
def __getitem__(self, s):
|
|
|
|
return dict.__getitem__(self, toLower(s))
|
2003-04-11 22:44:29 +02:00
|
|
|
|
|
|
|
def __delitem__(self, s):
|
|
|
|
dict.__delitem__(self, toLower(s))
|
|
|
|
|
2003-04-10 09:29:08 +02:00
|
|
|
|
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:
|