From 6361b1e856ebbc8e14d399019e2c53a35f4e0063 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Sun, 22 Sep 2013 11:13:52 -0400 Subject: [PATCH] ircdb: Deduplicate hostmasks in IrcUser.addAuth If addAuth is called with a hostmask that already exists (exactly, not just equivalent patterns) in self.auth, remove the entry for the old authentication. This essentially updates the timeout for the hostmask. Signed-off-by: James McCoy --- src/ircdb.py | 15 +++++++++++++-- test/test_ircdb.py | 5 ++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/ircdb.py b/src/ircdb.py index 5f10cc107..b96cfa7ae 100644 --- a/src/ircdb.py +++ b/src/ircdb.py @@ -1,6 +1,6 @@ ### # Copyright (c) 2002-2009, Jeremiah Fincher -# Copyright (c) 2009, James McCoy +# Copyright (c) 2009,2013, James McCoy # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -291,9 +291,20 @@ class IrcUser(object): self.hostmasks.remove(hostmask) def addAuth(self, hostmask): - """Sets a user's authenticated hostmask. This times out in 1 hour.""" + """Sets a user's authenticated hostmask. This times out according to + conf.supybot.timeoutIdentification. If hostmask exactly matches an + existing, known hostmask, the previous entry is removed.""" if self.checkHostmask(hostmask, useAuth=False) or not self.secure: self.auth.append((time.time(), hostmask)) + knownHostmasks = set() + def uniqueHostmask(auth): + (_, mask) = auth + if mask not in knownHostmasks: + knownHostmasks.add(mask) + return True + return False + uniqued = filter(uniqueHostmask, reversed(self.auth)) + self.auth = list(reversed(uniqued)) else: raise ValueError, 'secure flag set, unmatched hostmask' diff --git a/test/test_ircdb.py b/test/test_ircdb.py index 8eec61771..0a505cd22 100644 --- a/test/test_ircdb.py +++ b/test/test_ircdb.py @@ -252,7 +252,7 @@ class IrcUserTestCase(IrcdbTestCase): self.failIf(u.checkHostmask('foo!bar@baz')) finally: conf.supybot.databases.users.timeoutIdentification.setValue(orig) - + def testMultipleAuth(self): orig = conf.supybot.databases.users.timeoutIdentification() try: @@ -260,6 +260,9 @@ class IrcUserTestCase(IrcdbTestCase): u = ircdb.IrcUser() u.addAuth('foo!bar@baz') self.failUnless(u.checkHostmask('foo!bar@baz')) + u.addAuth('foo!bar@baz') + self.failUnless(u.checkHostmask('foo!bar@baz')) + self.failUnless(len(u.auth) == 1) u.addAuth('boo!far@fizz') self.failUnless(u.checkHostmask('boo!far@fizz')) time.sleep(2.1)