Import data from Enigmail to pEp for Thunderbird
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

166 lines
6.3 KiB

#!/usr/bin/python
# coding=UTF-8
# macOS only supports Python 2.7
import sys
import os
import pEp_JSON
import thunderbird
from base64 import b64encode
if sys.platform == 'darwin':
from platform_macOS import *
elif sys.platform == 'win32':
from platform_Windows import *
else:
from platform_Linux import *
profile = thunderbird.profile_path(thunderbird_base)
print "*** Import from Enigmail into pEp ***"
print "--- Using directory " + thunderbird_base
print "--- Using Thunderbird profile " + profile
pEp_JSON.enable_addon(profile)
# if this is an update to a pre-installed pEp extension or if Enigmail is not
# installed then do nothing and exit gracefully
enigmail_extension = os.path.join(profile,
'extensions', '{847b3a00-7ab1-11d4-8f02-006008948af5}.xpi')
if not os.path.isfile(enigmail_extension):
print "--- Enigmail extension not found: " + str(enigmail_extension)
print "*** Import from Enigmail into pEp: Nothing to do! ***"
sys.exit(0)
verbose = 0
try:
import enigmail
except GnuPGNotFound:
print "--- GnuPG not found (despite Enigmail being installed?)"
print "*** Import from Enigmail into pEp: Nothing to do! ***"
sys.exit(0)
with pEp_JSON.Connection() as call:
# import keydata from own keys
i = 0
for secret_key in enigmail.pgp_secret_keys():
keydata = enigmail.secret(secret_key['fpr'])
call('import_key', b64encode(keydata), 23, [ '42', ])
i += 1
print "--- Imported " + str(i) + " private keys"
# import keydata from comm_partners
i = 0
for keydata in enigmail.pubkey_iterator():
call('import_key', b64encode(keydata), 23, [ '42', ])
i += 1
print "--- Imported " + str(i) + " public keys"
# if enigmail is not in p≡p mode create comm_partner's identities
if not thunderbird.enigmail_juniorMode(thunderbird_base):
print "--- Enigmail is in classic mode, importing data into management.db"
# set own keys
i = 0
for own_key in enigmail.own_pgp_keys():
for uid in own_key['uids']:
me = {
'username': uid['name'],
'user_id': 'pEp_own_userId',
'address': uid['email'],
'fpr': own_key['fpr'],
}
call('set_own_key', me, own_key['fpr'])
if verbose: print 'set_own_key: ' + str(me)
i += 1
print "--- Set " + str(i) + " own keys"
# set own identities
i = 0
for tbid, own_identity in enigmail.identities().iteritems():
try:
username = own_identity['fullName']
except:
username = ''
print "--- No full name for " + own_identity['useremail']
me = {
'username': username,
'user_id': 'pEp_own_userId',
'address': own_identity['useremail'],
}
# Some identities may not have a PGP key assigned, so careful!
# In the Thunderbird data, the values are like "0x8CD2A600", so we strip the "0x".
try:
if own_identity['pgpkeyId'] and len(own_identity['pgpkeyId']) > 2:
call('set_own_key', me, own_identity['pgpkeyId'][2:])
except:
pass
call('myself', me)
if verbose: print "own identity: " + str(me)
i += 1
print "--- Set " + str(i) + " own identities"
# set comm partner's identities
if thunderbird.enigmail_assignKeysByRules(thunderbird_base) or \
thunderbird.enigmail_assignKeysByEmailAddr(thunderbird_base):
i = 0
for pgpkey in enigmail.comm_partner_pgp_keys():
for uid in pgpkey['uids']:
try:
if thunderbird.enigmail_assignKeysByRules(thunderbird_base):
result = enigmail.rules.test_address(uid['email'])
if result == False:
pass
elif result == True:
if thunderbird.enigmail_assignKeysByEmailAddr(thunderbird_base):
ident = {
'username': uid['name'],
'address': uid['email'],
}
ident = call('update_identity', ident)[u'outParams'][0]
ident[u'fpr'] = pgpkey['fpr']
call('set_identity', ident)
if verbose: print "comm partner: " + str(ident)
i += 1
else:
ident = {
'username': uid['name'],
'address': uid['email'],
}
ident = call('update_identity', ident)[u'outParams'][0]
ident[u'fpr'] = result
call('set_identity', ident)
if verbose: print "comm partner: " + str(ident)
i += 1
else:
ident = {
'username': uid['name'],
'address': uid['email'],
}
ident = call('update_identity', ident)[u'outParams'][0]
ident[u'fpr'] = pgpkey['fpr']
call('set_identity', ident)
if verbose: print "comm partner: " + str(ident)
i += 1
except:
print " Something wrong with partner '" + ident['username'] + " <" + ident['address'] + ">', skipped"
print "--- Set " + str(i) + " partner identities"
# remove Enigmail extension
try:
print "--- Removing Enigmail extension from " + enigmail_extension
os.remove(enigmail_extension)
except:
print " Removal failed :-("
print "*** Import from Enigmail into pEp: Done! ***"