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.

167 lines
4.5 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. # coding=UTF-8
  2. import sys
  3. import os
  4. import thunderbird
  5. import re
  6. from subprocess import Popen, PIPE
  7. import xml.etree.ElementTree as ET
  8. if sys.platform == 'darwin':
  9. from platform_macOS import *
  10. elif sys.platform == 'win32':
  11. from platform_Windows import *
  12. elif sys.platform == 'linux':
  13. from platform_Linux import *
  14. else:
  15. sys.stderr.write("platform not supported\n")
  16. sys.exit(1)
  17. gpg = gpg_executable(thunderbird_base)
  18. def command(*args):
  19. result = []
  20. cmd = [gpg, '--with-colons', '--batch']
  21. cmd.extend(args)
  22. for line in Popen(cmd, stdout=PIPE).stdout:
  23. data = line.split(':')
  24. result.append(data)
  25. return result
  26. def export_command(*args):
  27. result = ""
  28. cmd = [gpg, '-a', '--batch']
  29. cmd.extend(args)
  30. return "".join(Popen(cmd, stdout=PIPE).stdout.readlines())
  31. def fpr_from_keyid(keyid):
  32. for x in command('--fingerprint', keyid):
  33. if x[0] == 'fpr':
  34. return x[9]
  35. raise LookupError()
  36. uid_re = re.compile(r'(?P<name>[^<]*) \<(?P<email>[^>]*)\>')
  37. def uid(text):
  38. m = uid_re.match(text)
  39. if m:
  40. return { 'name': m.group('name'), 'email': m.group('email') }
  41. raise LookupError()
  42. def own_pgp_keys():
  43. result = []
  44. key = {}
  45. for x in command('-K'):
  46. if x[0] == 'sec' and x[1] == 'u':
  47. try:
  48. key = { 'fpr': fpr_from_keyid(x[4]) , 'uids': [] }
  49. result.append(key)
  50. except LookupError:
  51. key = { 'uids': [] }
  52. elif x[0] == 'sec':
  53. key = { 'uids': [] }
  54. elif x[0] == 'uid':
  55. try:
  56. key['uids'].append(uid(x[9]))
  57. except LookupError: pass
  58. return result
  59. def comm_partner_pgp_keys():
  60. result = []
  61. key = {}
  62. for x in command('--list-key'):
  63. if x[0] == 'pub' and x[1] == '-' and x[8] != 'n':
  64. try:
  65. key = { 'fpr': fpr_from_keyid(x[4]), 'trust': x[8] == 'f' or x[8] == 'u', 'uids': [] }
  66. result.append(key)
  67. except LookupError:
  68. key = { 'uids': [] }
  69. elif x[0] == 'pub':
  70. key = { 'uids': [] }
  71. elif x[0] == 'uid':
  72. try:
  73. key['uids'].append(uid(x[9]))
  74. except LookupError: pass
  75. return result
  76. def keydata(fpr):
  77. return export_command('--export', fpr)
  78. def secret(fpr):
  79. return export_command('--export-secret-key', fpr)
  80. def identities():
  81. return thunderbird.identities(thunderbird_base)
  82. def pubkey_iterator():
  83. for x in command('--list-key'):
  84. if x[0] == 'pub':
  85. yield keydata(x[4])
  86. class Rules:
  87. def __init__(self):
  88. try:
  89. self.et = ET.parse(os.path.join(thunderbird.profile_path(thunderbird_base), 'pgprules.xml'))
  90. self.rules = self.et.getroot()
  91. except:
  92. self.et = None
  93. self.rules = None
  94. def test_address(self, email):
  95. if self.rules is None:
  96. return True
  97. result = True
  98. for rule in self.rules:
  99. if rule.attrib['email'][0] == '{' and rule.attrib['email'][-1] == '}':
  100. if email == rule.attrib['email'][1:-1]:
  101. result = rule.attrib['encrypt'] != '0'
  102. if rule.attrib['keyId'] == '.':
  103. break
  104. elif len(rule.attrib['keyId']) > 2:
  105. return rule.attrib['keyId'][2:]
  106. elif rule.attrib['email'][0] == '{':
  107. begins = rule.attrib['email'][1:]
  108. if email[:len(begins)] == begins:
  109. result = rule.attrib['encrypt'] != '0'
  110. if rule.attrib['keyId'] == '.':
  111. break
  112. elif len(rule.attrib['keyId']) > 2:
  113. return rule.attrib['keyId'][2:]
  114. elif rule.attrib['email'][-1] == '}':
  115. ends = rule.attrib['email'][:-1]
  116. if email[len(email) - len(ends):] == ends:
  117. result = rule.attrib['encrypt'] != '0'
  118. if rule.attrib['keyId'] == '.':
  119. break
  120. elif len(rule.attrib['keyId']) > 2:
  121. return rule.attrib['keyId'][2:]
  122. else:
  123. if rule.attrib['email'][:-1] in email:
  124. result = rule.attrib['encrypt'] != '0'
  125. if rule.attrib['keyId'] == '.':
  126. break
  127. elif len(rule.attrib['keyId']) > 2:
  128. return rule.attrib['keyId'][2:]
  129. return result
  130. rules = Rules()