aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQuestion Box Service <qbox@andrewyu.org>2023-04-09 02:42:59 +0200
committerQuestion Box Service <qbox@andrewyu.org>2023-04-09 02:42:59 +0200
commit0dfcd014b20935821f7f061ce4123e1aabd88b6a (patch)
tree014f86def7e08fe4e28691a344d5d1d1730ab3ae
parenta815834345eebd4d1498222028989db1a2bcca43 (diff)
downloadqbox-0dfcd014b20935821f7f061ce4123e1aabd88b6a.tar.gz
qbox-0dfcd014b20935821f7f061ce4123e1aabd88b6a.zip
Configuration file
-rw-r--r--.gitignore1
-rwxr-xr-xapp.py64
-rw-r--r--config.def.py14
-rw-r--r--templates/home.html5
4 files changed, 44 insertions, 40 deletions
diff --git a/.gitignore b/.gitignore
index a6c57f5..6aa9b34 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
*.json
+config.py
diff --git a/app.py b/app.py
index 29fadd5..599ccb9 100755
--- a/app.py
+++ b/app.py
@@ -34,25 +34,13 @@ import json
import re
import mailbox
-MAPPING = {
- "andrew": (
- "andrew@andrewyu.org",
- "andrew.json",
- 'https://www.andrewyu.org/',
- 'Andrew Yu (Hypfzhqiik)'
- ),
- "hypfzwmiik": (
- "shengfei.yin@icloud.com",
- "hypfzwmiik.json",
- 'https://users.andrewyu.org/~hypfzwmiik/',
- 'Hypfzwmiik'
- ),
-}
+import config
+
def load_database(user):
try:
- db_file = open("%s" % MAPPING[user][1], "r+")
+ db_file = open("%s" % config.MAPPING[user][1], "r+")
except FileNotFoundError:
db = []
else:
@@ -99,9 +87,9 @@ def generate_past_questions_from_database(db):
return generated_questions_html_listing
def dump_database(user, db):
- #with open(MAPPING[user][1], "w") as db_file:
+ #with open(config.MAPPING[user][1], "w") as db_file:
# I've replaced this so that if the write fails it doesn't corrupt the JSON file
- fn = MAPPING[user][1]
+ fn = config.MAPPING[user][1]
with open(fn + '.tmp', 'w') as db_file:
json.dump(db, db_file, indent=4)
os.replace(fn + '.tmp', fn)
@@ -132,7 +120,7 @@ def parse_address(s): # Returns name, user, host.
@app.route("/<user>", methods=["GET", "POST"])
def qboard(user):
- if user not in MAPPING:
+ if user not in config.MAPPING:
return render_template("unknown_user.html", faulty_username=user)
elif request.method == "GET":
global db
@@ -152,13 +140,13 @@ def qboard(user):
mbox.unlock()
parsed_address = parse_address(msg["From"])
from_address = parsed_address[1] + "@" + parsed_address[2]
- if from_address != MAPPING[user][0]: continue
+ if from_address != config.MAPPING[user][0]: continue
if 'In-Reply-To' not in msg.keys() or not msg['In-Reply-To']:
ts = str(time.time())
newmsg = MIMEText(
- f"Hello {MAPPING[user][3]},\n\nI cannot understand this unsolicited message. If you are trying to reply to a message, be sure to use the ``reply'' feature in your email client to indicate that you are reply to that specific notification of mine. If something sounds wrong, contact your server administrator.\n\nQuestion Box System"
+ f"Hello {config.MAPPING[user][3]},\n\nI cannot understand this unsolicited message. If you are trying to reply to a message, be sure to use the ``reply'' feature in your email client to indicate that you are reply to that specific notification of mine. If something sounds wrong, contact your server administrator.\n\nQuestion Box System"
)
- newmsg["From"] = "qbox@andrewyu.org"
+ newmsg["From"] = config.MAIL_ADDR
newmsg["To"] = msg["From"]
if msg["Subject"].startswith("Re: "):
newmsg["Subject"] = msg["Subject"]
@@ -168,7 +156,7 @@ def qboard(user):
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
+ newmsg["Message-Id"] = "<qbox-system-%s@%s>" % (ts, config.MAIL_HOST)
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(newmsg.as_bytes())
return # This return was missing
@@ -180,9 +168,9 @@ def qboard(user):
else:
ts = str(time.time())
newmsg = MIMEText(
- f"Hello {MAPPING[user][3]},\n\nYou sent me a message, which was supposedly a reply to one of my notifications. However, I do not recognize your In-Reply-To header as one of my Message-IDs, so I don't know how to handle your message. Maybe you could check if you are using your email client's ``reply'' feature correctly, or if the database has been modified to remove the notified post?\n\nThings are getting weird. Contact your server administrator if confused.\n\nQuestion Box System"
+ f"Hello {config.MAPPING[user][3]},\n\nYou sent me a message, which was supposedly a reply to one of my notifications. However, I do not recognize your In-Reply-To header as one of my Message-IDs, so I don't know how to handle your message. Maybe you could check if you are using your email client's ``reply'' feature correctly, or if the database has been modified to remove the notified post?\n\nThings are getting weird. Contact your server administrator if confused.\n\nQuestion Box System"
)
- newmsg["From"] = "qbox@andrewyu.org"
+ newmsg["From"] = config.MAIL_ADDR
newmsg["To"] = msg["From"]
if msg["Subject"].startswith("Re: "):
newmsg["Subject"] = msg["Subject"]
@@ -192,7 +180,7 @@ def qboard(user):
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
+ newmsg["Message-Id"] = "<qbox-system-%s@%s>" % (ts, config.MAIL_HOST)
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(newmsg.as_bytes())
return
@@ -207,9 +195,9 @@ def qboard(user):
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'' (or ``text/html'' but that's not recommended).\n\nQuestion Box System"
+ f"Hello {config.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["From"] = config.MAIL_ADDR
newmsg["To"] = msg["From"]
if msg["Subject"].startswith("Re: "):
newmsg["Subject"] = msg["Subject"]
@@ -219,7 +207,7 @@ def qboard(user):
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
+ newmsg["Message-Id"] = "<qbox-system-%s@%s>" % (ts, config.MAIL_HOST)
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(newmsg.as_bytes())
return
@@ -240,9 +228,9 @@ def qboard(user):
ts = str(time.time())
newmsg = MIMEText(
- f"Hello {MAPPING[user][3]},\n\nI have received your message and I added it to the question board.\n\nQuestion Box System"
+ f"Hello {config.MAPPING[user][3]},\n\nI have received your message and I added it to the question board.\n\nQuestion Box System"
)
- newmsg["From"] = "qbox@andrewyu.org"
+ newmsg["From"] = config.MAIL_ADDR
newmsg["To"] = msg["From"]
if msg["Subject"].startswith("Re: "):
newmsg["Subject"] = msg["Subject"]
@@ -252,14 +240,14 @@ def qboard(user):
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
+ newmsg["Message-Id"] = "<qbox-system-%s@%s>" % (ts, config.MAIL_HOST)
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(newmsg.as_bytes())
return Response(
open("templates/qboard.html", "r")
.read()
- .replace("{{username}}", MAPPING[user][3])
+ .replace("{{username}}", config.MAPPING[user][3])
.replace("{{pq}}", generate_past_questions_from_database(db)),
mimetype="text/html",
)
@@ -273,12 +261,12 @@ def qboard(user):
db.append({"q": text, "a": None, "ts": ts})
dump_database(user, db)
msg = MIMEText(
- 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"
+ f"Hello {config.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]
+ msg["From"] = config.MAIL_ADDR
+ msg["To"] = config.MAPPING[user][0]
msg["Subject"] = "Question Box Message"
- msg["Message-Id"] = "<qbox-%s@andrewyu.org>" % ts
+ msg["Message-Id"] = "<qbox-%s@%s>" % (ts, config.MAIL_HOST)
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(msg.as_bytes())
else:
@@ -295,11 +283,11 @@ def index():
return Response(
open("templates/home.html", "r")
.read()
- .replace("{{userlist}}", generate_user_list_from_mapping(MAPPING)),
+ .replace("{{userlist}}", generate_user_list_from_mapping(config.MAPPING)),
mimetype="text/html",
)
if __name__ == "__main__":
- app.run(port=5728)
+ app.run(port=config.PORT)
diff --git a/config.def.py b/config.def.py
new file mode 100644
index 0000000..624cab3
--- /dev/null
+++ b/config.def.py
@@ -0,0 +1,14 @@
+# Copy this to config.py
+
+MAPPING = {
+ "username": (
+ "email@example.tld",
+ "path-to-config.json",
+ 'https://homepage.example.tld/',
+ 'Full Name'
+ ),
+}
+
+PORT = 5728
+MAIL_HOST = "andrewyu.org"
+MAIL_ADDR = "qbox@andrewyu.org"
diff --git a/templates/home.html b/templates/home.html
index 1dd1320..c5babf6 100644
--- a/templates/home.html
+++ b/templates/home.html
@@ -5,6 +5,7 @@
<link rel="stylesheet" href="https://www.andrewyu.org/plain.css" />
<link rel="shortcut icon" href="https://www.andrewyu.org/favicon.ico" type="image/x-icon" />
<meta charset="utf-8" />
+ <style>details {border: solid 1px; padding-left: 5px; padding-right: 5px;} details p {margin-bottom: 0ex;} details p+p {margin-bottom: 0.5ex;}</style>
</head>
<body>
<h1>Question Boards on <code>andrewyu.org</code></h1>
@@ -13,7 +14,7 @@
The following is a list of users on this question board server. Click their name to access their particular question board.
</p>
<ul>
- {{userlist}}
+ (The administrator of this server has disabled user listings.)
</ul>
<details>
@@ -34,7 +35,7 @@
It must be actually a <em>reply</em> to the notification email. That is, your reply's <code>In-Reply-To</code> header must match the <code>Message-ID</code> header in the notification email. In most clients, using the ``reply'' button should set this header correctly automatically.
</li>
<li>
- Plain text replies of MIME type <code>text/plain<code> are preferred and are prioritized in <code>multipart/alternative</code>s. You may hand-write HTML segments in your plain email. All line breaks in the plain text email will be replaced with <code>&lt;br /&gt;</code> when added to the server's database.
+ Plain text replies of MIME type <code>text/plain</code> are preferred and are prioritized in <code>multipart/alternative</code>s. You may hand-write HTML segments in your plain email. All line breaks in the plain text email will be replaced with <code>&lt;br /&gt;</code> when added to the server's database.
</li>
<li>
HTML replies of MIME type <code>text/html</code> are accepted when no <code>text/plain</code> is found, as a last resort. The entirety of the HTML email is included in the database and thus the final Web page, which means that many tags and such will likely be messed up as we're directly including complete HTML code inside an HTML page. Therefore, it is not recommended to be using HTML replies.