mirror of
https://github.com/simple-login/app.git
synced 2024-09-27 20:31:30 +02:00
refactor: use headers.py
This commit is contained in:
parent
d6fc132df1
commit
ee0ed7d9ec
@ -7,6 +7,8 @@ SUBJECT = "Subject"
|
|||||||
FROM = "From"
|
FROM = "From"
|
||||||
TO = "To"
|
TO = "To"
|
||||||
CONTENT_TYPE = "Content-Type"
|
CONTENT_TYPE = "Content-Type"
|
||||||
|
CONTENT_DISPOSITION = "Content-Disposition"
|
||||||
|
CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding"
|
||||||
MIME_VERSION = "Mime-Version"
|
MIME_VERSION = "Mime-Version"
|
||||||
REPLY_TO = "Reply-To"
|
REPLY_TO = "Reply-To"
|
||||||
RECEIVED = "Received"
|
RECEIVED = "Received"
|
||||||
@ -21,3 +23,17 @@ DKIM_HEADERS = [
|
|||||||
[MESSAGE_ID.encode(), DATE.encode()],
|
[MESSAGE_ID.encode(), DATE.encode()],
|
||||||
[FROM.encode()],
|
[FROM.encode()],
|
||||||
]
|
]
|
||||||
|
|
||||||
|
SL_DIRECTION = "X-SimpleLogin-Type"
|
||||||
|
SL_EMAIL_LOG_ID = "X-SimpleLogin-EmailLog-ID"
|
||||||
|
SL_ENVELOPE_FROM = "X-SimpleLogin-Envelope-From"
|
||||||
|
SL_ENVELOPE_TO = "X-SimpleLogin-Envelope-To"
|
||||||
|
|
||||||
|
MIME_HEADERS = [
|
||||||
|
MIME_VERSION,
|
||||||
|
CONTENT_TYPE,
|
||||||
|
CONTENT_DISPOSITION,
|
||||||
|
CONTENT_TRANSFER_ENCODING,
|
||||||
|
]
|
||||||
|
# convert to lowercase to facilitate header look up
|
||||||
|
MIME_HEADERS = [h.lower() for h in MIME_HEADERS]
|
||||||
|
@ -141,22 +141,6 @@ from app.utils import sanitize_email
|
|||||||
from init_app import load_pgp_public_keys
|
from init_app import load_pgp_public_keys
|
||||||
from server import create_app, create_light_app
|
from server import create_app, create_light_app
|
||||||
|
|
||||||
# forward or reply
|
|
||||||
_DIRECTION = "X-SimpleLogin-Type"
|
|
||||||
|
|
||||||
|
|
||||||
_EMAIL_LOG_ID_HEADER = "X-SimpleLogin-EmailLog-ID"
|
|
||||||
_ENVELOPE_FROM = "X-SimpleLogin-Envelope-From"
|
|
||||||
_ENVELOPE_TO = "X-SimpleLogin-Envelope-To"
|
|
||||||
|
|
||||||
_MIME_HEADERS = [
|
|
||||||
"MIME-Version",
|
|
||||||
"Content-Type",
|
|
||||||
"Content-Disposition",
|
|
||||||
"Content-Transfer-Encoding",
|
|
||||||
]
|
|
||||||
_MIME_HEADERS = [h.lower() for h in _MIME_HEADERS]
|
|
||||||
|
|
||||||
|
|
||||||
newrelic_app = None
|
newrelic_app = None
|
||||||
if NEWRELIC_CONFIG_PATH:
|
if NEWRELIC_CONFIG_PATH:
|
||||||
@ -436,13 +420,13 @@ def prepare_pgp_message(
|
|||||||
# copy all headers from original message except all standard MIME headers
|
# copy all headers from original message except all standard MIME headers
|
||||||
for i in reversed(range(len(clone_msg._headers))):
|
for i in reversed(range(len(clone_msg._headers))):
|
||||||
header_name = clone_msg._headers[i][0].lower()
|
header_name = clone_msg._headers[i][0].lower()
|
||||||
if header_name.lower() not in _MIME_HEADERS:
|
if header_name.lower() not in headers.MIME_HEADERS:
|
||||||
msg[header_name] = clone_msg._headers[i][1]
|
msg[header_name] = clone_msg._headers[i][1]
|
||||||
|
|
||||||
# Delete unnecessary headers in clone_msg except _MIME_HEADERS to save space
|
# Delete unnecessary headers in clone_msg except _MIME_HEADERS to save space
|
||||||
delete_all_headers_except(
|
delete_all_headers_except(
|
||||||
clone_msg,
|
clone_msg,
|
||||||
_MIME_HEADERS,
|
headers.MIME_HEADERS,
|
||||||
)
|
)
|
||||||
|
|
||||||
if clone_msg[headers.CONTENT_TYPE] is None:
|
if clone_msg[headers.CONTENT_TYPE] is None:
|
||||||
@ -748,17 +732,17 @@ def forward_email_to_mailbox(
|
|||||||
delete_all_headers_except(
|
delete_all_headers_except(
|
||||||
msg,
|
msg,
|
||||||
[
|
[
|
||||||
"From",
|
headers.FROM,
|
||||||
"To",
|
headers.TO,
|
||||||
"Cc",
|
headers.CC,
|
||||||
"Subject",
|
headers.SUBJECT,
|
||||||
# do not delete original message id
|
# do not delete original message id
|
||||||
headers.MESSAGE_ID,
|
headers.MESSAGE_ID,
|
||||||
# References and In-Reply-To are used for keeping the email thread
|
# References and In-Reply-To are used for keeping the email thread
|
||||||
headers.REFERENCES,
|
headers.REFERENCES,
|
||||||
headers.IN_REPLY_TO,
|
headers.IN_REPLY_TO,
|
||||||
]
|
]
|
||||||
+ _MIME_HEADERS,
|
+ headers.MIME_HEADERS,
|
||||||
)
|
)
|
||||||
|
|
||||||
# create PGP email if needed
|
# create PGP email if needed
|
||||||
@ -787,12 +771,12 @@ def forward_email_to_mailbox(
|
|||||||
return False, status.E406
|
return False, status.E406
|
||||||
|
|
||||||
# add custom header
|
# add custom header
|
||||||
add_or_replace_header(msg, _DIRECTION, "Forward")
|
add_or_replace_header(msg, headers.SL_DIRECTION, "Forward")
|
||||||
|
|
||||||
msg[_EMAIL_LOG_ID_HEADER] = str(email_log.id)
|
msg[headers.SL_EMAIL_LOG_ID] = str(email_log.id)
|
||||||
msg[_ENVELOPE_FROM] = envelope.mail_from
|
msg[headers.SL_ENVELOPE_FROM] = envelope.mail_from
|
||||||
# when an alias isn't in the To: header, there's no way for users to know what alias has received the email
|
# when an alias isn't in the To: header, there's no way for users to know what alias has received the email
|
||||||
msg[_ENVELOPE_TO] = alias.email
|
msg[headers.SL_ENVELOPE_TO] = alias.email
|
||||||
|
|
||||||
if not msg[headers.MESSAGE_ID]:
|
if not msg[headers.MESSAGE_ID]:
|
||||||
LOG.w("missing message id header, create one")
|
LOG.w("missing message id header, create one")
|
||||||
@ -987,17 +971,17 @@ def handle_reply(envelope, msg: Message, rcpt_to: str) -> (bool, str):
|
|||||||
delete_all_headers_except(
|
delete_all_headers_except(
|
||||||
msg,
|
msg,
|
||||||
[
|
[
|
||||||
"From",
|
headers.FROM,
|
||||||
"To",
|
headers.TO,
|
||||||
"Cc",
|
headers.CC,
|
||||||
"Subject",
|
headers.SUBJECT,
|
||||||
# do not delete original message id
|
# do not delete original message id
|
||||||
headers.MESSAGE_ID,
|
headers.MESSAGE_ID,
|
||||||
# References and In-Reply-To are used for keeping the email thread
|
# References and In-Reply-To are used for keeping the email thread
|
||||||
headers.REFERENCES,
|
headers.REFERENCES,
|
||||||
headers.IN_REPLY_TO,
|
headers.IN_REPLY_TO,
|
||||||
]
|
]
|
||||||
+ _MIME_HEADERS,
|
+ headers.MIME_HEADERS,
|
||||||
)
|
)
|
||||||
|
|
||||||
# replace the reverse-alias (i.e. "ra+string@simplelogin.co") by the contact email in the email body
|
# replace the reverse-alias (i.e. "ra+string@simplelogin.co") by the contact email in the email body
|
||||||
@ -1053,8 +1037,8 @@ def handle_reply(envelope, msg: Message, rcpt_to: str) -> (bool, str):
|
|||||||
LOG.w("missing date header, add one")
|
LOG.w("missing date header, add one")
|
||||||
msg[headers.DATE] = date_header
|
msg[headers.DATE] = date_header
|
||||||
|
|
||||||
msg[_DIRECTION] = "Reply"
|
msg[headers.SL_DIRECTION] = "Reply"
|
||||||
msg[_EMAIL_LOG_ID_HEADER] = str(email_log.id)
|
msg[headers.SL_EMAIL_LOG_ID] = str(email_log.id)
|
||||||
|
|
||||||
LOG.d(
|
LOG.d(
|
||||||
"send email from %s to %s, mail_options:%s,rcpt_options:%s",
|
"send email from %s to %s, mail_options:%s,rcpt_options:%s",
|
||||||
|
Loading…
Reference in New Issue
Block a user