diff options
author | Question Box Service <qbox@andrewyu.org> | 2023-04-07 12:06:21 +0200 |
---|---|---|
committer | Question Box Service <qbox@andrewyu.org> | 2023-04-07 12:06:21 +0200 |
commit | 8772395b33a82d8eeeb5600d44e10ee14e2cdd43 (patch) | |
tree | 5086dea4524e762b71ab1a68afdb151119fc99da | |
parent | 958e0840142d113b50c30119f387b363403b45fe (diff) | |
download | qbox-8772395b33a82d8eeeb5600d44e10ee14e2cdd43.tar.gz qbox-8772395b33a82d8eeeb5600d44e10ee14e2cdd43.zip |
Allow HTML emails and decode quoted-printable transfer encodings
-rwxr-xr-x | app.py | 60 |
1 files changed, 37 insertions, 23 deletions
@@ -29,6 +29,7 @@ from subprocess import Popen, PIPE from html import escape import time, os +import quopri import json import re import mailbox @@ -41,7 +42,7 @@ MAPPING = { 'Andrew Yu (Hypfzhqiik)' ), "hypfzwmiik": ( - "box2@andrewyu.org", + "shengfei.yin@icloud.com", "hypfzwmiik.json", 'https://users.andrewyu.org/~hypfzwmiik/', 'Hypfzwmiik' @@ -205,29 +206,40 @@ def qboard(user): if part.get_content_type() == "text/plain": break else: - ts = str(time.time()) - newmsg = MIMEText( - f"Hello {MAPPING[user][3]},\n\nYour reply was in an incorrect format. Please ensure that it includes at least one subpart of MIME type ``text/plain''.\n\nQuestion Box System" - ) - newmsg["From"] = "qbox@andrewyu.org" - newmsg["To"] = msg["From"] - if msg["Subject"].startswith("Re: "): - newmsg["Subject"] = msg["Subject"] + for part in msg.walk(): + if part.get_content_type() == "text/html": + break else: - newmsg["Subject"] = "Re: " + msg["Subject"] - if "Message-ID" in msg.keys(): - newmsg["In-Reply-To"] = msg["Message-ID"] - elif "Message-Id" in msg.keys(): - newmsg["In-Reply-To"] = msg["Message-Id"] - newmsg["Message-Id"] = "<qbox-system-%s@andrewyu.org>" % ts - p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE) - p.communicate(newmsg.as_bytes()) - return + ts = str(time.time()) + newmsg = MIMEText( + f"Hello {MAPPING[user][3]},\n\nYour reply was in an incorrect format. Please ensure that it includes at least one subpart of MIME type ``text/plain'' (or ``text/html'' but that's not recommended).\n\nQuestion Box System" + ) + newmsg["From"] = "qbox@andrewyu.org" + newmsg["To"] = msg["From"] + if msg["Subject"].startswith("Re: "): + newmsg["Subject"] = msg["Subject"] + else: + newmsg["Subject"] = "Re: " + msg["Subject"] + if "Message-ID" in msg.keys(): + newmsg["In-Reply-To"] = msg["Message-ID"] + elif "Message-Id" in msg.keys(): + newmsg["In-Reply-To"] = msg["Message-Id"] + newmsg["Message-Id"] = "<qbox-system-%s@andrewyu.org>" % ts + p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE) + p.communicate(newmsg.as_bytes()) + return + + received_message_text_plain = part.get_payload() + if part["Content-Transfer-Encoding"] == "quoted-printable": + received_message_text_decoded = quopri.decodestring(received_message_text_plain).decode("utf-8", "surrogateescape") + else: + received_message_text_decoded = received_message_text_plain + if part.get_content_type() == "text/plain": + received_message_text_decoded = received_message_text_decoded.replace("\r\n", "<br />").replace("\n", "<br />") - recieved_message_text_plain = part.get_payload() db.remove(question) - question["a"] = recieved_message_text_plain + question["a"] = received_message_text_decoded db.append(question) dump_database(user, db) @@ -252,20 +264,22 @@ def qboard(user): return Response( open("templates/qboard.html", "r") .read() - .replace("{{username}}", "<a href=\"%s\">%s</a>" % (MAPPING[user][2], MAPPING[user][3])) + .replace("{{username}}", MAPPING[user][3]) .replace("{{pq}}", generate_past_questions_from_database(db)), mimetype="text/html", ) elif request.method == "POST": + if request.content_length > 1024 * 20: + return Response("Your request is too large!!!", mimetype="text/plain") ts = str(time.time()) if "text" in request.form and request.form["text"].strip(): text = request.form["text"] db = load_database(user) db.append({"q": text, "a": None, "ts": ts}) dump_database(user, db) - print(text + "\a") + print(repr(text) + "\a") msg = MIMEText( - f"Hello {MAPPING[user][3]},\n\nThe following message was received in your ({user}'s) question box at {ts}. Please reply to this in plain text email, as in the MIME type must be ``text/plain''; you may handwrite HTML in your reply. Remember to remove any quoted text if your email client adds these automatically. Attachments will be ignored.\n\n{text}\n\nQuestion Box System" + f"Hello {MAPPING[user][3]},\n\nThe following message was received in your ({user}'s) question box at server timestamp {ts}. Please reply to this in plain text email, as in the MIME type should be ``text/plain''; you may handwrite HTML in your reply; newlines will be automatically converted to ``<br />''s. Alternatively, HTML email will be accepted but are not recommended. Remember to remove any quoted text if your email client adds these automatically. Attachments will be ignored.\n\n{text}\n\nQuestion Box System" ) msg["From"] = "qbox@andrewyu.org" msg["To"] = MAPPING[user][0] |