From b48ca7ab71cd2241417a3fdc40e8d148930e4fdd Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Thu, 14 Apr 2005 01:35:35 +0000 Subject: [PATCH] Added supybot.protocols.irc.queuing.rateLimit.join, to throttle joins. --- src/conf.py | 5 +++++ src/irclib.py | 13 +++++++++++-- test/test_irclib.py | 8 ++++---- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/conf.py b/src/conf.py index 331fbbc45..70132b046 100644 --- a/src/conf.py +++ b/src/conf.py @@ -942,6 +942,11 @@ registerGlobalValue(supybot.protocols.irc.queuing, 'duplicates', multiple times; most of the time it doesn't matter, unless you're doing certain kinds of plugin hacking.""")) +registerGroup(supybot.protocols.irc.queuing, 'rateLimit') +registerGlobalValue(supybot.protocols.irc.queuing.rateLimit, 'join', + registry.Float(0, """Determines how many seconds must elapse between JOINs + sent to the server.""")) + ### # supybot.protocols.http ### diff --git a/src/irclib.py b/src/irclib.py index 9f88fbf4f..3cdaf9713 100644 --- a/src/irclib.py +++ b/src/irclib.py @@ -139,7 +139,7 @@ class IrcCallback(IrcCommandDispatcher): # later point) reorder messages based on priority or penalty calculations. ### _high = frozenset(['MODE', 'KICK', 'PONG', 'NICK', 'PASS', 'CAPAB']) -_low = frozenset(['PRIVMSG', 'PING', 'WHO', 'NOTICE']) +_low = frozenset(['PRIVMSG', 'PING', 'WHO', 'NOTICE', 'JOIN']) class IrcMsgQueue(object): """Class for a queue of IrcMsgs. Eventually, it should be smart. @@ -153,7 +153,7 @@ class IrcMsgQueue(object): the 'high priority' ones before the normal ones before the 'low priority' ones. """ - __slots__ = ('msgs', 'highpriority', 'normal', 'lowpriority') + __slots__ = ('msgs', 'highpriority', 'normal', 'lowpriority', 'lastJoin') def __init__(self, iterable=()): self.reset() for msg in iterable: @@ -161,6 +161,7 @@ class IrcMsgQueue(object): def reset(self): """Clears the queue.""" + self.lastJoin = 0 self.highpriority = smallqueue() self.normal = smallqueue() self.lowpriority = smallqueue() @@ -190,6 +191,14 @@ class IrcMsgQueue(object): msg = self.normal.dequeue() elif self.lowpriority: msg = self.lowpriority.dequeue() + if msg.command == 'JOIN': + limit = conf.supybot.protocols.irc.queuing.rateLimit.join() + now = time.time() + if self.lastJoin + limit <= now: + self.lastJoin = now + else: + self.lowpriority.enqueue(msg) + msg = None return msg def __contains__(self, msg): diff --git a/test/test_irclib.py b/test/test_irclib.py index 3ca6db872..b4fe078cb 100644 --- a/test/test_irclib.py +++ b/test/test_irclib.py @@ -158,10 +158,10 @@ class IrcMsgQueueTestCase(SupyTestCase): q.enqueue(self.who) self.assertEqual(self.join, q.dequeue()) self.assertEqual(self.who, q.dequeue()) - q.enqueue(self.who) - q.enqueue(self.join) - self.assertEqual(self.join, q.dequeue()) - self.assertEqual(self.who, q.dequeue()) +## q.enqueue(self.who) +## q.enqueue(self.join) +## self.assertEqual(self.join, q.dequeue()) +## self.assertEqual(self.who, q.dequeue()) def testTopicBeforePrivmsg(self): q = irclib.IrcMsgQueue()