diff --git a/plugins/User/plugin.py b/plugins/User/plugin.py index c28681123..dd6d3045f 100644 --- a/plugins/User/plugin.py +++ b/plugins/User/plugin.py @@ -396,27 +396,39 @@ class User(callbacks.Plugin): """ Add a GPG key to your account.""" + if keyid in user.gpgkeys: + irc.error(_('This key is already associated with your ' + 'account.')) + return result = gpg.keyring.recv_keys(keyserver, keyid) - count = len(result.fingerprints) - if count: + reply = format(_('%n imported, %i unchanged, %i not imported.'), + (result.imported, _('key')), + result.unchanged, + result.not_imported, + [x['fingerprint'] for x in result.results]) + if result.imported == 1: user.gpgkeys.append(keyid) - irc.reply(format(_('Successful import of %n: %L'), - (count, _('key')), - [x.fingerprint for x in result.results])) + irc.reply(reply) else: - irc.error(_('GPG key not found on the key server.')) + irc.error(reply) add = wrap(add, ['user', 'somethingWithoutSpaces', 'somethingWithoutSpaces']) @internationalizeDocstring - def remove(self, irc, msg, args, user, keyid): - """ + def remove(self, irc, msg, args, user, fingerprint): + """ Remove a GPG key from your account.""" try: - user.gpgkeys.remove(keyid) + keyids = [x['keyid'] for x in gpg.keyring.list_keys() + if x['fingerprint'] == fingerprint] + if len(keyids) == 0: + raise ValueError + for keyid in keyids: + user.gpgkeys.remove(keyid) + gpg.keyring.delete_keys(fingerprint) irc.replySuccess() - except KeyError: + except ValueError: irc.error(_('GPG key not associated with your account.')) remove = wrap(remove, ['user', 'somethingWithoutSpaces']) diff --git a/plugins/User/test.py b/plugins/User/test.py index a9526987a..108bd5fa6 100644 --- a/plugins/User/test.py +++ b/plugins/User/test.py @@ -27,6 +27,7 @@ # POSSIBILITY OF SUCH DAMAGE. ### +import supybot.gpg as gpg from supybot.test import * import supybot.world as world @@ -36,6 +37,11 @@ class UserTestCase(PluginTestCase): plugins = ('User',) prefix1 = 'somethingElse!user@host.tld' prefix2 = 'EvensomethingElse!user@host.tld' + + def setUp(self): + super(UserTestCase, self).setUp() + gpg.loadKeyring() + def testHostmaskList(self): self.assertError('hostmask list') original = self.prefix @@ -136,5 +142,19 @@ class UserTestCase(PluginTestCase): self.assertNotError('load Seen') self.assertResponse('user list', 'Foo') + if network: + def testGpgAddRemove(self): + self.assertNotError('register foo bar') + self.assertError('user gpg add 51E516F0B0C5CE6A pgp.mit.edu') + self.assertResponse('user gpg add EB17F1E0CEB63930 pgp.mit.edu', + '1 key imported, 0 unchanged, 0 not imported.') + self.assertNotError( + 'user gpg remove F88ECDE235846FA8652DAF5FEB17F1E0CEB63930') + self.assertResponse('user gpg add EB17F1E0CEB63930 pgp.mit.edu', + '1 key imported, 0 unchanged, 0 not imported.') + self.assertResponse('user gpg add EB17F1E0CEB63930 pgp.mit.edu', + 'Error: This key is already associated with your account.') + + # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/src/gpg.py b/src/gpg.py index 2305bfc00..a72a73464 100644 --- a/src/gpg.py +++ b/src/gpg.py @@ -60,9 +60,11 @@ def fallback(default_return=None): @fallback() def loadKeyring(): global keyring - path = conf.supybot.directories.data.dirize('GPGkeyring') + path = os.path.abspath(conf.supybot.directories.data.dirize('GPGkeyring')) if not os.path.isdir(path): - os.mkdir(path) + log.info('Creating directory %s' % path) + os.mkdir(path, 0700) + assert os.path.isdir(path) keyring = gnupg.GPG(gnupghome=path) loadKeyring()