From 67cbd8327de4787b72cd0a0f95e060e2901601c7 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Thu, 10 Apr 2003 07:56:41 +0000 Subject: [PATCH] Closed bug 715763, 'Several JOIN commands don't allow specification of a key' --- src/AdminCommands.py | 17 ++++++++++++++--- src/ircmsgs.py | 32 ++++++++++++++++++++++++++------ test/ircmsgs_test.py | 17 +++++++++++++++++ 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/AdminCommands.py b/src/AdminCommands.py index d8c891019..7c3290b79 100755 --- a/src/AdminCommands.py +++ b/src/AdminCommands.py @@ -47,10 +47,21 @@ class AdminCommands(privmsgs.CapabilityCheckingPrivmsg): """ [ ...] Tell the bot to join the whitespace-separated list of channels - you give it. + you give it. If a channel requires a key, attach it behind the + channel name via a comma. I.e., if you need to join both #a and #b, + and #a requires a key of 'aRocks', then you'd call 'join #a,aRocks #b' """ - irc.queueMsg(ircmsgs.joins(args)) - for channel in args: + keys = [] + channels = [] + for s in args: + if ',' in s: + (channel, key) = s.split(',', 1) + channels.insert(0, channel) + keys.insert(0, key) + else: + channels.append(channel) + irc.queueMsg(ircmsgs.joins(channels, keys)) + for channel in channels: irc.queueMsg(ircmsgs.who(channel)) def nick(self, irc, msg, args): diff --git a/src/ircmsgs.py b/src/ircmsgs.py index ab13cca77..b0e754dbb 100644 --- a/src/ircmsgs.py +++ b/src/ircmsgs.py @@ -43,7 +43,8 @@ import ircutils class IrcMsg(object): """Class to represent an IRC message. """ - __slots__ = ('_args', '_command', '_host', '_nick', '_prefix', '_user') + __slots__ = ('_args', '_command', '_host', '_nick', + '_prefix', '_user', '_hash') def __init__(self, s='', command='', args=None, prefix='', msg=None): if not s and not command and not msg: raise ValueError, 'IRC messages require a command.' @@ -128,7 +129,13 @@ class IrcMsg(object): return not (self == other) def __hash__(self): - return hash(self.command) & hash(self.prefix) & hash(self.args) + try: + return self._hash + except AttributeError: + self._hash = hash(self.command) & \ + hash(self.prefix) & \ + hash(self.args) + return self._hash def __repr__(self): return '%s(prefix=%r, command=%r, args=%r)' % \ @@ -339,15 +346,28 @@ def notice(recipient, msg, prefix=''): assert (isChannel(recipient) or isNick(recipient)) and msg return IrcMsg(prefix=prefix, command='NOTICE', args=(recipient, msg)) -def join(channel, prefix=''): +def join(channel, key=None, prefix=''): """Returns a JOIN to a channel""" assert isChannel(channel) - return IrcMsg(prefix=prefix, command='JOIN', args=(channel,)) + if key is None: + return IrcMsg(prefix=prefix, command='JOIN', args=(channel,)) + else: + return IrcMsg(prefix=prefix, command='JOIN', args=(channel, key)) -def joins(channels, prefix=''): +def joins(channels, keys=None, prefix=''): """Returns a JOIN to each of channels.""" assert filter(isChannel, channels) == channels - return IrcMsg(prefix=prefix, command='JOIN', args=(','.join(channels),)) + if keys is None: + keys = [] + assert len(keys) <= len(channels) + if not keys: + return IrcMsg(prefix=prefix, + command='JOIN', + args=(','.join(channels),)) + else: + return IrcMsg(prefix=prefix, + command='JOIN', + args=(','.join(channels), ','.join(keys))) def part(channel, msg='', prefix=''): """Returns a PART from channel with the message msg.""" diff --git a/test/ircmsgs_test.py b/test/ircmsgs_test.py index 00d7b64b7..6f4177e07 100644 --- a/test/ircmsgs_test.py +++ b/test/ircmsgs_test.py @@ -137,3 +137,20 @@ class FunctionsTestCase(unittest.TestCase): self.assertEqual(ircutils.separateModes(withExceptions.args[1:]), [('+b', bans[0]), ('+b', bans[1]), ('+e', exceptions[0])]) + + def testJoin(self): + channel = '#osu' + key = 'michiganSucks' + self.assertEqual(ircmsgs.join(channel).args, ('#osu',)) + self.assertEqual(ircmsgs.join(channel, key).args, + ('#osu', 'michiganSucks')) + + def testJoins(self): + channels = ['#osu', '#umich'] + keys = ['michiganSucks', 'osuSucks'] + self.assertEqual(ircmsgs.joins(channels).args, ('#osu,#umich',)) + self.assertEqual(ircmsgs.joins(channels, keys).args, + ('#osu,#umich', 'michiganSucks,osuSucks')) + keys.pop() + self.assertEqual(ircmsgs.joins(channels, keys).args, + ('#osu,#umich', 'michiganSucks'))