diff --git a/plugins/Later/config.py b/plugins/Later/config.py index eb505a8d6..e3d8803ba 100644 --- a/plugins/Later/config.py +++ b/plugins/Later/config.py @@ -51,5 +51,9 @@ conf.registerGlobalValue(Later, 'private', conf.registerGlobalValue(Later, 'tellOnJoin', registry.Boolean(True, _("""Determines whether users will be notified upon joining any channel the bot is in, or only upon sending a message."""))) +conf.registerGlobalValue(Later, 'messageExpiry', + registry.NonNegativeInteger(30, _("""Determines the maximum number of + days that a message will remain queued for a user. After this time elapses, + the message will be deleted. If this value is 0, there is no maximum."""))) # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/plugins/Later/plugin.py b/plugins/Later/plugin.py index 434a33511..56731da46 100644 --- a/plugins/Later/plugin.py +++ b/plugins/Later/plugin.py @@ -30,6 +30,7 @@ import csv import time +import datetime import supybot.log as log import supybot.conf as conf @@ -120,6 +121,31 @@ class Later(callbacks.Plugin): return nick[:-1] return nick + def _deleteExpired(self): + expiry = self.registryValue('messageExpiry') + curtime = time.time() + nickremovals=[] + for (nick, notes) in self._notes.iteritems(): + removals = [] + for (notetime, whence, text) in notes: + td = datetime.timedelta(seconds=(curtime - notetime)) + if td.days > expiry: + removals.append((notetime, whence, text)) + for note in removals: + notes.remove(note) + if len(notes) == 0: + nickremovals.append(nick) + for nick in nickremovals: + del self._notes[nick] + self._flushNotes() + + ## Note: we call _deleteExpired from 'tell'. This means that it's possible + ## for expired notes to remain in the database for longer than the maximum, + ## if no tell's are called. + ## However, the whole point of this is to avoid crud accumulation in the + ## database, so it's fine that we only delete expired notes when we try + ## adding new ones. + @internationalizeDocstring def tell(self, irc, msg, args, nick, text): """ @@ -128,6 +154,7 @@ class Later(callbacks.Plugin): contain wildcard characters, and the first matching nick will be given the note. """ + self._deleteExpired() if ircutils.strEqual(nick, irc.nick): irc.error(_('I can\'t send notes to myself.')) return diff --git a/plugins/Later/test.py b/plugins/Later/test.py index 447f088fd..5dd9d5259 100644 --- a/plugins/Later/test.py +++ b/plugins/Later/test.py @@ -28,6 +28,7 @@ ### from supybot.test import * +import time class LaterTestCase(PluginTestCase): plugins = ('Later',) @@ -52,5 +53,14 @@ class LaterTestCase(PluginTestCase): self.assertRegexp('later notes', 'foo\.') conf.supybot.protocols.irc.strictRfc.setValue(origconf) + def testNoteExpiry(self): + cb = self.irc.getCallback('Later') + # add a note 40 days in the past + cb._addNote('foo', 'test', 'some stuff', at=(time.time() - 3456000)) + self.assertRegexp('later notes', 'foo') + self.assertNotError('later tell moo stuff') + self.assertNotRegexp('later notes', 'foo') + self.assertRegexp('later notes', 'moo') + # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: