Skip to content

Commit

Permalink
Fix a few things about how keys are attached
Browse files Browse the repository at this point in the history
  • Loading branch information
BjarniRunar committed Oct 14, 2014
1 parent 784d747 commit bd8b818
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions mailpile/mailutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def CleanMessage(config, msg):

# Remove headers we don't want to expose
if (lkey.startswith('x-mp-internal-') or
lkey in ('bcc', 'encryption')):
lkey in ('bcc', 'encryption', 'attach-pgp-pubkey')):
replacements.append((key, None))

# Strip the #key part off any e-mail addresses:
Expand Down Expand Up @@ -221,6 +221,8 @@ def PrepareMessage(config, msg, sender=None, rcpts=None, events=None):
msg,
events)

attach_pgp_pubkey = False
attached_pubkey = False
crypto_policy = config.prefs.crypto_policy.lower()
rcpts = rcpts or []

Expand All @@ -232,6 +234,8 @@ def PrepareMessage(config, msg, sender=None, rcpts=None, events=None):
sender = sender or val
elif lhdr == 'encryption':
crypto_policy = val.lower()
elif lhdr == 'attach-pgp-pubkey':
attach_pgp_pubkey = val.lower()
elif need_rcpts and lhdr in ('to', 'cc', 'bcc'):
rcpts += ExtractEmails(val, strip_keys=False)

Expand Down Expand Up @@ -277,29 +281,8 @@ def PrepareMessage(config, msg, sender=None, rcpts=None, events=None):
msg["OpenPGP"] = "id=%s; preference=%s" % (sender_keyid,
config.prefs.openpgp_header)

# Should be 'openpgp', but there is no point in being precise
if 'pgp' in crypto_policy or 'gpg' in crypto_policy:
wrapper = None
if 'sign' in crypto_policy and 'encrypt' in crypto_policy:
wrapper = OpenPGPMimeSignEncryptWrapper
elif 'sign' in crypto_policy:
wrapper = OpenPGPMimeSigningWrapper
elif 'encrypt' in crypto_policy:
wrapper = OpenPGPMimeEncryptingWrapper
elif 'none' not in crypto_policy:
raise ValueError(_('Unknown crypto policy: %s') % crypto_policy)
if wrapper:
cpi = config.prefs.inline_pgp
msg = wrapper(config,
sender=sender,
cleaner=lambda m: CleanMessage(config, m),
recipients=rcpts
).wrap(msg, prefer_inline=cpi)
elif crypto_policy and crypto_policy != 'none':
raise ValueError(_('Unknown crypto policy: %s') % crypto_policy)

# Do we want to attach a key to outgoing messages?
if str(msg['Attach-PGP-Pubkey']).lower() in ['yes', 'true']:
if str(attach_pgp_pubkey).lower() in ['yes', 'true']:
g = GnuPG(config)
keys = g.address_to_keys(ExtractEmails(sender)[0])
for fp, key in keys.iteritems():
Expand All @@ -312,7 +295,7 @@ def PrepareMessage(config, msg, sender=None, rcpts=None, events=None):

try:
from_name = key["uids"][0]["name"]
filename = _('%s\'s encryption key.asc') % from_name
filename = _('Encryption key for %s.asc') % from_name
except:
filename = _('My encryption key.asc')

Expand All @@ -325,11 +308,32 @@ def PrepareMessage(config, msg, sender=None, rcpts=None, events=None):
att.signature_info = SignatureInfo(parent=msg.signature_info)
att.encryption_info = EncryptionInfo(parent=msg.encryption_info)
msg.attach(att)
del(msg['Attach-PGP-Pubkey'])
msg['x-mp-internal-pubkeys-attached'] = "Yes"
attached_pubkey = True

# Should be 'openpgp', but there is no point in being precise
if 'pgp' in crypto_policy or 'gpg' in crypto_policy:
wrapper = None
if 'sign' in crypto_policy and 'encrypt' in crypto_policy:
wrapper = OpenPGPMimeSignEncryptWrapper
elif 'sign' in crypto_policy:
wrapper = OpenPGPMimeSigningWrapper
elif 'encrypt' in crypto_policy:
wrapper = OpenPGPMimeEncryptingWrapper
elif 'none' not in crypto_policy:
raise ValueError(_('Unknown crypto policy: %s') % crypto_policy)
if wrapper:
cpi = config.prefs.inline_pgp
msg = wrapper(config,
sender=sender,
cleaner=lambda m: CleanMessage(config, m),
recipients=rcpts
).wrap(msg, prefer_inline=cpi)
elif crypto_policy and crypto_policy != 'none':
raise ValueError(_('Unknown crypto policy: %s') % crypto_policy)

rcpts = set([r.rsplit('#', 1)[0] for r in rcpts])
if attached_pubkey:
msg['x-mp-internal-pubkeys-attached'] = "Yes"
msg['x-mp-internal-readonly'] = str(int(time.time()))
msg['x-mp-internal-sender'] = sender
msg['x-mp-internal-rcpts'] = ', '.join(rcpts)
Expand Down

0 comments on commit bd8b818

Please sign in to comment.