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.
 
 

182 lines
4.9 KiB

# coding=UTF-8
import sys
import os
import thunderbird
import re
from subprocess import Popen, PIPE
import xml.etree.ElementTree as ET
if sys.platform == 'darwin':
from platform_macOS import *
elif sys.platform == 'win32':
from platform_Windows import *
else:
from platform_Linux import *
gpg = gpg_executable(thunderbird_base)
def command(*args):
result = []
cmd = [gpg, '--with-colons', '--batch']
cmd.extend(args)
for line in Popen(cmd, stdout=PIPE).stdout:
data = line.split(':')
result.append(data)
return result
def export_command(*args):
result = ""
cmd = [gpg, '-a', '--batch']
cmd.extend(args)
return "".join(Popen(cmd, stdout=PIPE).stdout.readlines())
def fpr_from_keyid(keyid):
for x in command('--fingerprint', keyid):
if x[0] == 'fpr':
return x[9]
raise LookupError()
uid_re = re.compile(r'(?P<name>[^<]*) \<(?P<email>[^>]*)\>')
def uid(text):
m = uid_re.match(text)
if m:
return { 'name': m.group('name'), 'email': m.group('email') }
raise LookupError()
def pgp_secret_keys():
result = []
key = {}
for x in command('-K'):
if x[0] == 'sec':
try:
key = { 'fpr': fpr_from_keyid(x[4]) , 'uids': [] }
result.append(key)
except LookupError:
key = { 'uids': [] }
elif x[0] == 'sec':
key = { 'uids': [] }
elif x[0] == 'uid':
try:
key['uids'].append(uid(x[9]))
except LookupError: pass
return result
def own_pgp_keys():
result = []
key = {}
for x in command('-K'):
if x[0] == 'sec' and x[1] == 'u':
try:
key = { 'fpr': fpr_from_keyid(x[4]) , 'uids': [] }
result.append(key)
except LookupError:
key = { 'uids': [] }
elif x[0] == 'sec':
key = { 'uids': [] }
elif x[0] == 'uid':
try:
key['uids'].append(uid(x[9]))
except LookupError: pass
return result
def comm_partner_pgp_keys():
result = []
key = {}
for x in command('--list-key'):
if x[0] == 'pub' and x[1] == '-' and x[8] != 'n':
try:
key = { 'fpr': fpr_from_keyid(x[4]), 'trust': x[8] == 'f' or x[8] == 'u', 'uids': [] }
result.append(key)
except LookupError:
key = { 'uids': [] }
elif x[0] == 'pub':
key = { 'uids': [] }
elif x[0] == 'uid':
try:
key['uids'].append(uid(x[9]))
except LookupError: pass
return result
def keydata(fpr):
return export_command('--export', fpr)
def secret(fpr):
return export_command('--export-secret-key', fpr)
def identities():
return thunderbird.identities(thunderbird_base)
def pubkey_iterator():
for x in command('--list-key'):
if x[0] == 'pub':
yield keydata(x[4])
class Rules:
def __init__(self):
try:
self.et = ET.parse(os.path.join(thunderbird.profile_path(thunderbird_base), 'pgprules.xml'))
self.rules = self.et.getroot()
except:
self.et = None
self.rules = None
def test_address(self, email):
if self.rules is None:
return True
result = True
for rule in self.rules:
if rule.attrib['email'][0] == '{' and rule.attrib['email'][-1] == '}':
if email == rule.attrib['email'][1:-1]:
result = rule.attrib['encrypt'] != '0'
if rule.attrib['keyId'] == '.':
break
elif len(rule.attrib['keyId']) > 2:
return rule.attrib['keyId'][2:]
elif rule.attrib['email'][0] == '{':
begins = rule.attrib['email'][1:]
if email[:len(begins)] == begins:
result = rule.attrib['encrypt'] != '0'
if rule.attrib['keyId'] == '.':
break
elif len(rule.attrib['keyId']) > 2:
return rule.attrib['keyId'][2:]
elif rule.attrib['email'][-1] == '}':
ends = rule.attrib['email'][:-1]
if email[len(email) - len(ends):] == ends:
result = rule.attrib['encrypt'] != '0'
if rule.attrib['keyId'] == '.':
break
elif len(rule.attrib['keyId']) > 2:
return rule.attrib['keyId'][2:]
else:
if rule.attrib['email'][:-1] in email:
result = rule.attrib['encrypt'] != '0'
if rule.attrib['keyId'] == '.':
break
elif len(rule.attrib['keyId']) > 2:
return rule.attrib['keyId'][2:]
return result
rules = Rules()