7or8bit-decode #5
1 changed files with 4 additions and 26 deletions
28
gpgmymail
28
gpgmymail
|
@ -36,14 +36,12 @@ import email.mime.message
|
||||||
import typing
|
import typing
|
||||||
# for decode_email:
|
# for decode_email:
|
||||||
import quopri
|
import quopri
|
||||||
import base64
|
|
||||||
|
|
||||||
# see: https://gnupg.readthedocs.io/en/latest/
|
# see: https://gnupg.readthedocs.io/en/latest/
|
||||||
import gnupg
|
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
|
||||||
CTES_TO_BE_DECODED = ("quoted-printable", "base64")
|
|
||||||
|
|
||||||
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.
|
||||||
|
@ -61,39 +59,19 @@ def decode_email(message: email.message.Message) -> email.message.Message:
|
||||||
:return: decoded email.message.Message"""
|
:return: decoded email.message.Message"""
|
||||||
# this is a kinda hacky way to do this by manipulating the message as a
|
# this is a kinda hacky way to do this by manipulating the message as a
|
||||||
# string but i couldn't get it to work any other way
|
# string but i couldn't get it to work any other way
|
||||||
|
|
||||||
msg_ctes = message.get_all("Content-Transfer-Encoding")
|
|
||||||
|
|
||||||
# this list will be populated with any encoding that needs to be decoded,
|
|
||||||
# e.g. base64
|
|
||||||
# empty if no decoding needed
|
|
||||||
# set used to avoid dupes
|
|
||||||
decodes_needed = set()
|
|
||||||
# check if any of the parts of the message need decoding
|
|
||||||
for cte in CTES_TO_BE_DECODED:
|
|
||||||
if cte in msg_ctes:
|
|
||||||
decodes_needed.add(cte)
|
|
||||||
# no decoding needed, go ahead with message
|
|
||||||
if not decodes_needed:
|
|
||||||
return message
|
|
||||||
|
|
||||||
# decoding needed:
|
# decoding needed:
|
||||||
# as_string() gives us str, encode() gives us bytes
|
# as_string() gives us str, encode() gives us bytes
|
||||||
decoded_bytes = message.as_string().encode()
|
decoded_bytes = message.as_string().encode()
|
||||||
if "quoted-printable" in decodes_needed:
|
|
||||||
decoded_bytes = quopri.decodestring(decoded_bytes)
|
decoded_bytes = quopri.decodestring(decoded_bytes)
|
||||||
if "base64" in decodes_needed:
|
|
||||||
decoded_bytes = base64.b64decode(decoded_bytes)
|
|
||||||
|
|
||||||
# replace any instances of the Content-Transfer-Encoding header
|
# replace any instances of the Content-Transfer-Encoding header
|
||||||
decoded_bytes = decoded_bytes.replace(
|
decoded_bytes = decoded_bytes.replace(
|
||||||
b'Content-Transfer-Encoding: quoted-printable',
|
b'Content-Transfer-Encoding: quoted-printable',
|
||||||
b'Content-Transfer-Encoding: 7bit'
|
b'Content-Transfer-Encoding: 7bit'
|
||||||
)
|
)
|
||||||
decoded_bytes = decoded_bytes.replace(
|
|
||||||
b'Content-Transfer-Encoding: base64',
|
# TODO: base64 decoding, which is more difficult due to the need to not
|
||||||
b'Content-Transfer-Encoding: 7bit'
|
# treat the whole email like it's base64
|
||||||
)
|
|
||||||
|
|
||||||
return email.message_from_bytes(decoded_bytes)
|
return email.message_from_bytes(decoded_bytes)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue