revert gpgmymail to 40b3ba3760
This commit is contained in:
parent
885532f27a
commit
61120dd4e8
1 changed files with 2 additions and 27 deletions
29
gpgmymail
29
gpgmymail
|
@ -40,13 +40,6 @@ import gnupg
|
||||||
|
|
||||||
# constants
|
# constants
|
||||||
DEFAULT_ENCODING='utf-8' # default is latin-1 which fails w some unicode chars
|
DEFAULT_ENCODING='utf-8' # default is latin-1 which fails w some unicode chars
|
||||||
CTE_TO_ENCODER_DICT = {
|
|
||||||
"7bit": email.encoders.encode_7or8bit,
|
|
||||||
"8bit": email.encoders.encode_7or8bit,
|
|
||||||
"base64": email.encoders.encode_base64,
|
|
||||||
"quoted-printable": email.encoders.encode_quopri
|
|
||||||
}
|
|
||||||
DEFAULT_ENCODER = email.encoders.encode_7or8bit
|
|
||||||
|
|
||||||
def is_message_encrypted(message: email.message.Message) -> bool:
|
def is_message_encrypted(message: email.message.Message) -> bool:
|
||||||
"""Determines whether or not an email message is encrypted.
|
"""Determines whether or not an email message is encrypted.
|
||||||
|
@ -56,24 +49,6 @@ def is_message_encrypted(message: email.message.Message) -> bool:
|
||||||
|
|
||||||
return message.get_content_subtype() == "encrypted"
|
return message.get_content_subtype() == "encrypted"
|
||||||
|
|
||||||
def get_encoder_from_msg(msg: email.message.Message) -> typing.Callable:
|
|
||||||
"""
|
|
||||||
Return a suitable encoder function from email.encoders based on an input
|
|
||||||
message. If the input message has no Content-Transfer-Encoding header,
|
|
||||||
or there is no encoder function corresponding to the CTE header, a default
|
|
||||||
encoder will be returned.
|
|
||||||
|
|
||||||
:param msg: an unencrypted email Message
|
|
||||||
:return: function from email.encoders, see
|
|
||||||
https://docs.python.org/3/library/email.encoders.html
|
|
||||||
"""
|
|
||||||
cte = msg.get("Content-Transfer-Encoding")
|
|
||||||
if cte:
|
|
||||||
encoder = CTE_TO_ENCODER_DICT.get(cte)
|
|
||||||
else:
|
|
||||||
return DEFAULT_ENCODER
|
|
||||||
return encoder if encoder else DEFAULT_ENCODER
|
|
||||||
|
|
||||||
def encrypt(
|
def encrypt(
|
||||||
message: email.message.Message,
|
message: email.message.Message,
|
||||||
recipients: typing.List[str],
|
recipients: typing.List[str],
|
||||||
|
@ -107,13 +82,13 @@ def encrypt(
|
||||||
enc = email.mime.application.MIMEApplication(
|
enc = email.mime.application.MIMEApplication(
|
||||||
_data=str(encrypted_content).encode(),
|
_data=str(encrypted_content).encode(),
|
||||||
_subtype="octet-stream",
|
_subtype="octet-stream",
|
||||||
_encoder=get_encoder_from_msg(message)
|
_encoder=email.encoders.encode_7or8bit
|
||||||
)
|
)
|
||||||
|
|
||||||
control = email.mime.application.MIMEApplication(
|
control = email.mime.application.MIMEApplication(
|
||||||
_data=b'Version: 1\n',
|
_data=b'Version: 1\n',
|
||||||
_subtype='pgp-encrypted; name="msg.asc"',
|
_subtype='pgp-encrypted; name="msg.asc"',
|
||||||
_encoder=get_encoder_from_msg(message)
|
_encoder=email.encoders.encode_7or8bit
|
||||||
)
|
)
|
||||||
control['Content-Disposition'] = 'inline; filename="msg.asc"'
|
control['Content-Disposition'] = 'inline; filename="msg.asc"'
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue