From 6135a88741fcafa49bb2bd768cfc971cd7d58b5e Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Tue, 18 May 2010 12:48:36 -0500 Subject: [PATCH] Updates and tweaks to some ircutils functions. --- src/ircutils.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/ircutils.py b/src/ircutils.py index de13fbffe..c7b5408df 100644 --- a/src/ircutils.py +++ b/src/ircutils.py @@ -48,14 +48,10 @@ def debug(s, *args): """Prints a debug string. Most likely replaced by our logging debug.""" print '***', s % args +userHostmaskRe = re.compile(r'^\S+!\S+@\S+$') def isUserHostmask(s): """Returns whether or not the string s is a valid User hostmask.""" - 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 + return userHostmaskRe.match(s) is not None def isServerHostmask(s): """s => bool @@ -66,19 +62,19 @@ def nickFromHostmask(hostmask): """hostmask => nick Returns the nick from a user hostmask.""" assert isUserHostmask(hostmask) - return hostmask.split('!', 1)[0] + return splitHostmask(hostmask)[0] def userFromHostmask(hostmask): """hostmask => user Returns the user from a user hostmask.""" assert isUserHostmask(hostmask) - return hostmask.split('!', 1)[1].split('@', 1)[0] + return splitHostmask(hostmask)[1] def hostFromHostmask(hostmask): """hostmask => host Returns the host from a user hostmask.""" assert isUserHostmask(hostmask) - return hostmask.split('@', 1)[1] + return splitHostmask(hostmask)[2] def splitHostmask(hostmask): """hostmask => (nick, user, host) @@ -86,13 +82,13 @@ def splitHostmask(hostmask): assert isUserHostmask(hostmask) nick, rest = hostmask.split('!', 1) user, host = rest.split('@', 1) - return (nick, user, host) + return (intern(nick), intern(user), intern(host)) def joinHostmask(nick, ident, host): """nick, user, host => hostmask Joins the nick, ident, host into a user hostmask.""" assert nick and ident and host - return '%s!%s@%s' % (nick, ident, host) + return intern('%s!%s@%s' % (nick, ident, host)) _rfc1459trans = string.maketrans(string.ascii_uppercase + r'\[]~', string.ascii_lowercase + r'|{}^')