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> """<key id> <key server>
Add a GPG key to your account.""" 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) result = gpg.keyring.recv_keys(keyserver, keyid)
count = len(result.fingerprints) reply = format(_('%n imported, %i unchanged, %i not imported.'),
if count: (result.imported, _('key')),
result.unchanged,
result.not_imported,
[x['fingerprint'] for x in result.results])
if result.imported == 1:
user.gpgkeys.append(keyid) user.gpgkeys.append(keyid)
irc.reply(format(_('Successful import of %n: %L'), irc.reply(reply)
(count, _('key')),
[x.fingerprint for x in result.results]))
else: else:
irc.error(_('GPG key not found on the key server.')) irc.error(reply)
add = wrap(add, ['user', 'somethingWithoutSpaces', add = wrap(add, ['user', 'somethingWithoutSpaces',
'somethingWithoutSpaces']) 'somethingWithoutSpaces'])
@internationalizeDocstring @internationalizeDocstring
def remove(self, irc, msg, args, user, keyid): def remove(self, irc, msg, args, user, fingerprint):
"""<key id> """<fingerprint>
Remove a GPG key from your account.""" Remove a GPG key from your account."""
try: 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() irc.replySuccess()
except KeyError: except ValueError:
irc.error(_('GPG key not associated with your account.')) irc.error(_('GPG key not associated with your account.'))
remove = wrap(remove, ['user', 'somethingWithoutSpaces']) remove = wrap(remove, ['user', 'somethingWithoutSpaces'])

View File

@ -27,6 +27,7 @@
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
### ###
import supybot.gpg as gpg
from supybot.test import * from supybot.test import *
import supybot.world as world import supybot.world as world
@ -36,6 +37,11 @@ class UserTestCase(PluginTestCase):
plugins = ('User',) plugins = ('User',)
prefix1 = 'somethingElse!user@host.tld' prefix1 = 'somethingElse!user@host.tld'
prefix2 = 'EvensomethingElse!user@host.tld' prefix2 = 'EvensomethingElse!user@host.tld'
def setUp(self):
super(UserTestCase, self).setUp()
gpg.loadKeyring()
def testHostmaskList(self): def testHostmaskList(self):
self.assertError('hostmask list') self.assertError('hostmask list')
original = self.prefix original = self.prefix
@ -136,5 +142,19 @@ class UserTestCase(PluginTestCase):
self.assertNotError('load Seen') self.assertNotError('load Seen')
self.assertResponse('user list', 'Foo') 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: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -60,9 +60,11 @@ def fallback(default_return=None):
@fallback() @fallback()
def loadKeyring(): def loadKeyring():
global keyring 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): 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) keyring = gnupg.GPG(gnupghome=path)
loadKeyring() loadKeyring()