Some slightly new helper functions and stuff.

This commit is contained in:
Jeremy Fincher 2003-04-29 07:37:54 +00:00
parent ce6b03d52f
commit 54788a643a
2 changed files with 38 additions and 23 deletions

View File

@ -146,6 +146,17 @@ class Channel(object):
self.halfops = set() self.halfops = set()
self.voices = set() self.voices = set()
def addUser(self, user):
while user[0] in '@%+':
(marker, user) = (user[0], user[1:])
if marker == '@':
self.ops.add(user)
elif marker == '%':
self.halfops.add(user)
elif marker == '+':
self.voices.add(user)
self.users.add(user)
def removeUser(self, user): def removeUser(self, user):
self.users.discard(user) self.users.discard(user)
self.ops.discard(user) self.ops.discard(user)
@ -153,15 +164,20 @@ class Channel(object):
self.voices.discard(user) self.voices.discard(user)
def __getstate__(self): def __getstate__(self):
return (self.topic, self.users, self.ops, self.halfops, self.voices) return map(lambda name: getattr(self, name), self.__slots__)
def __setstate__(self, (topic, users, ops, halfops, voices)): def __setstate__(self, t):
self.topic = topic for (name, value) in zip(self.__slots__, t):
self.users = users setattr(self, name, value)
self.ops = ops
self.halfops = halfops
self.voices = voices
def __eq__(self, other):
ret = True
for name in self.__slots__:
ret = ret and getattr(self, name) == getattr(other, name)
return ret
def __ne__(self, other):
return not self == other
class IrcState(object): class IrcState(object):
"""Maintains state of the Irc connection. Should also become smarter. """Maintains state of the Irc connection. Should also become smarter.
@ -176,14 +192,21 @@ class IrcState(object):
self.channels = ircutils.IrcDict() self.channels = ircutils.IrcDict()
def __getstate__(self): def __getstate__(self):
d = {} return map(lambda name: getattr(self, name), self.__slots__)
for s in self.__slots__:
d[s] = getattr(self, s)
def __setstate__(self, d): def __setstate__(self, t):
for (name, value) in d.iteritems(): for (name, value) in zip(self.__slots__, t):
setattr(self, name, value) setattr(self, name, value)
def __eq__(self, other):
ret = True
for name in self.__slots__:
ret = ret and getattr(self, name) == getattr(other, name)
return ret
def __ne__(self, other):
return not self == other
def copy(self): def copy(self):
ret = self.__class__() ret = self.__class__()
ret.history = copy.copy(self.history) ret.history = copy.copy(self.history)
@ -214,15 +237,7 @@ class IrcState(object):
chan = self.channels[channel.lower()] chan = self.channels[channel.lower()]
users = [ircutils.nick(user) for user in users.split()] users = [ircutils.nick(user) for user in users.split()]
for user in users: for user in users:
if user[0] in '@%+': chan.addUser(user)
(marker, user) = (user[0], user[1:])
if marker == '@':
chan.ops.add(user)
elif marker == '%':
chan.halfops.add(user)
elif marker == '+':
chan.voices.add(user)
chan.users.add(user)
elif msg.command == 'PART': elif msg.command == 'PART':
for channel in msg.args[0].split(','): for channel in msg.args[0].split(','):
channel = channel.lower() channel = channel.lower()

View File

@ -103,10 +103,10 @@ class ChannelTestCase(unittest.TestCase):
def testPickleCopy(self): def testPickleCopy(self):
c = irclib.Channel() c = irclib.Channel()
for name in c.__slots__: for name in c.__slots__:
debug.printf(getattr(c, name)) debug.printf('%s %s' % (name, getattr(c, name)))
c1 = pickle.loads(pickle.dumps(c)) c1 = pickle.loads(pickle.dumps(c))
for name in c1.__slots__: for name in c1.__slots__:
debug.printf(getattr(c1, name)) debug.printf('%s %s' % (name, getattr(c1, name)))
self.assertEqual(pickle.loads(pickle.dumps(c)), c) self.assertEqual(pickle.loads(pickle.dumps(c)), c)
def testAddUser(self): def testAddUser(self):