import re
import io
import os
+import enum
import importlib
import email.parser
"""This list contains the abbreviated names of reply languages available to
edward."""
+class TxtType (enum.Enum):
+ text = 0
+ message = 1
+ pubkey = 2
+ detachedsig = 3
+ signature = 4
-match_types = [('message',
+
+match_types = [(TxtType.message,
'-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----'),
- ('pubkey',
+ (TxtType.pubkey,
'-----BEGIN PGP PUBLIC KEY BLOCK-----.*?-----END PGP PUBLIC KEY BLOCK-----'),
- ('detachedsig',
+ (TxtType.detachedsig,
'-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----')]
"""This list of tuples matches query names with re.search() queries used
Instances of this class are often strung together within one or more arrays
pointed to by each instance of the EddyMsg class.
- 'piece_type' refers to a string whose value describes the content of
- 'string'. Examples include "pubkey", for public keys, and "message", for
- encrypted data (or armored signatures until they are known to be such.) The
- names derive from the header and footer of each of these ascii-encoded gpg
- blocks.
+ 'piece_type' refers to an enum whose value describes the content of
+ 'string'. Examples include TxtType.pubkey, for public keys, and
+ TxtType.message, for encrypted data (or armored signatures until they are
+ known to be such.) Some of the names derive from the header and footer of
+ each of these ascii-encoded gpg blocks.
'string' contains some string of text, such as non-GPG text, an encrypted
block of text, a signature, or a public key.
"""
# don't try to re-split pieces containing gpg data
- if payload_piece.piece_type != "text":
+ if payload_piece.piece_type != TxtType.text:
return [payload_piece]
flags = re.DOTALL | re.MULTILINE
try:
payload = PayloadPiece()
payload.string = mime_decoded_bytes.decode(charset)
- payload.piece_type = 'text'
+ payload.piece_type = TxtType.text
obj.payload_pieces = [payload]
except UnicodeDecodeError:
for piece in eddymsg_obj.payload_pieces:
- if piece.piece_type == "text":
+ if piece.piece_type == TxtType.text:
# don't transform the plaintext.
pass
- elif piece.piece_type == "message":
+ elif piece.piece_type == TxtType.message:
(plaintext, sigs) = decrypt_block(piece.string, gpgme_ctx)
if plaintext:
(plaintext, sigs) = verify_sig_message(piece.string, gpgme_ctx)
if plaintext:
- piece.piece_type = "signature"
+ piece.piece_type = TxtType.signature
piece.gpg_data = GPGData()
piece.gpg_data.sigs = sigs
# recurse!
# FIXME: consider handling pubkeys first, so that signatures can be
# validated on freshly imported keys
- elif piece.piece_type == "pubkey":
+ elif piece.piece_type == TxtType.pubkey:
key_fps = add_gpg_key(piece.string, gpgme_ctx)
if key_fps != []:
piece.gpg_data = GPGData()
piece.gpg_data.keys = key_fps
- elif piece.piece_type == "detachedsig":
+ elif piece.piece_type == TxtType.detachedsig:
for prev in prev_parts:
sig_fps = verify_detached_signature(piece.string, prev.payload_bytes, gpgme_ctx)
"""
for piece in eddymsg_obj.payload_pieces:
- if piece.piece_type == "text":
+ if piece.piece_type == TxtType.text:
# don't quote the plaintext part.
pass
- elif piece.piece_type == "message":
+ elif piece.piece_type == TxtType.message:
prepare_for_reply_message(piece, replyinfo_obj)
- elif piece.piece_type == "pubkey":
+ elif piece.piece_type == TxtType.pubkey:
prepare_for_reply_pubkey(piece, replyinfo_obj)
- elif (piece.piece_type == "detachedsig") \
- or (piece.piece_type == "signature"):
+ elif (piece.piece_type == TxtType.detachedsig) \
+ or (piece.piece_type == TxtType.signature):
prepare_for_reply_sig(piece, replyinfo_obj)
"""Helper function for prepare_for_reply()
This function is called when the piece_type of a payload piece is
- "message", or GPG Message block. This should be encrypted text. If the
+ TxtType.message, or GPG Message block. This should be encrypted text. If the
encryted block is signed, a sig will be attached to .target_key unless
there is already one there.
Nothing
Pre:
- the piece.payload_piece value should be "message".
+ the piece.payload_piece value should be TxtType.message.
Post:
replyinfo_obj gets updated with decryption status, signing status and a
replyinfo_obj: a ReplyInfo object
Pre:
- piece.piece_type should be set to "pubkey".
+ piece.piece_type should be set to TxtType.pubkey .
Post:
replyinfo_obj has its fields updated.
replyinfo_obj: a ReplyInfo object
Pre:
- piece.piece_type should be set to "signature", or "detachedsig".
+ piece.piece_type should be set to TxtType.signature, or
+ TxtType.detachedsig .
Post:
replyinfo_obj has its fields updated.
for piece in eddymsg_obj.payload_pieces:
if (get_signed_part):
- if ((piece.piece_type == "detachedsig") \
- or (piece.piece_type == "signature")) \
+ if ((piece.piece_type == TxtType.detachedsig) \
+ or (piece.piece_type == TxtType.signature)) \
and (piece.gpg_data != None):
flatten_decrypted_payloads(piece.gpg_data.plainobj, replyinfo_obj, False)
replyinfo_obj.target_key = piece.gpg_data.sigs[0]
break
else:
- if piece.piece_type == "text":
+ if piece.piece_type == TxtType.text:
replyinfo_obj.msg_to_quote += piece.string