Make gpg key adding/removal work.

This commit is contained in:
Valentin Lorentz 2012-08-03 23:22:53 +02:00
parent fa67967b09
commit cd0bfe411a
3 changed files with 46 additions and 12 deletions

View File

@ -396,27 +396,39 @@ class User(callbacks.Plugin):
"""<key id> <key server>
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):
"""<key id>
def remove(self, irc, msg, args, user, fingerprint):
"""<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'])

View File

@ -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:

View File

@ -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()