diff --git a/.gitignore b/.gitignore index b63f137..ed26aac 100644 --- a/.gitignore +++ b/.gitignore @@ -133,4 +133,5 @@ cython_debug/ .DS_Store /config/ /db/ -.vscode \ No newline at end of file +.vscode +/data \ No newline at end of file diff --git a/docker-compose-debug.yml b/docker-compose-debug.yml index 79677f8..b9a30ef 100644 --- a/docker-compose-debug.yml +++ b/docker-compose-debug.yml @@ -9,4 +9,5 @@ services: - 1030:1030 volumes: - ./config:/config + - ./data:/data - ./src:/app \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 0d25bfb..e36ff10 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,4 +8,5 @@ services: ports: - 1030:1030 volumes: - - ./config:/config \ No newline at end of file + - ./config:/config + - ./data:/data \ No newline at end of file diff --git a/docs/config folder.md b/docs/configuration.md similarity index 100% rename from docs/config folder.md rename to docs/configuration.md diff --git a/requirements.txt b/requirements.txt index 4a6b337..a62cfdd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Flask +gnupg git+https://github.com/jmarya/htmlpy#egg=htmlpy \ No newline at end of file diff --git a/src/index.py b/src/index.py index 419d25f..922c5c9 100644 --- a/src/index.py +++ b/src/index.py @@ -1,7 +1,10 @@ -from flask import request, session, Blueprint, Response +from flask import request, session, Blueprint, Response, redirect from htmlpy import * from config import CONFIG from os.path import exists +import os +import datetime +import gnupg main_pages = Blueprint("main", __name__) @@ -20,6 +23,75 @@ BOOTSTRAP = [ ] +def encrypt(msg): + pgp = gnupg.GPG() + pgp.import_keys(open("/config/pub.key").read()) + return str(pgp.encrypt(msg, pgp.list_keys()[0]["fingerprint"])) + + +def save_message(msg, name=""): + os.makedirs("/data/messages", exist_ok=True) + dt = datetime.datetime.now().strftime("%Y-%m-%d.%H-%M") + f = open(f"/data/messages/{name}-{dt}.asc", "w") + f.write(msg) + + +@main_pages.route("/message", methods=["GET", "POST"]) +def send_message(): + if request.method == "POST": + msg = request.form["message"] + name = request.form["msg_name"] + if msg is not None: + cipher = encrypt(msg) + save_message(cipher, name) + return redirect(request.url_root) + else: + return buildSite( + [ + Div( + [ + Heading(1, "Message"), + LineBreak(), + Form( + destination=request.url, + inner=[ + Input( + "", + placeholder="Name", + name="msg_name", + global_attr=GlobalAttributes( + css_class="form-control bg-dark text-white", + style="margin-bottom: 15px", + ), + ), + TextArea( + inner="", + placeholder="Message", + name="message", + global_attr=GlobalAttributes( + css_class="form-control bg-dark text-white", + style="margin-bottom: 15px;", + ), + ), + Input( + type=InputType.Submit, + value="Send Message", + name="submit", + global_attr=GlobalAttributes( + css_class="btn btn-danger text-white text-decoration-none" + ), + ), + ], + ), + ], + global_attr=GlobalAttributes( + css_class="container", style="margin-top: 25px" + ), + ) + ] + ).to_code() + + def buildSite(content): return Document( head=Head( @@ -54,7 +126,7 @@ def public_key(): [ Bold("To Import: "), Span( - f'curl "{request.base_url}"|gpg --import', + f'curl -sL "{request.base_url}"|gpg --import', global_attr=GlobalAttributes( style="display: block;font-family: monospace,monospace;margin-top: 10px; font-size: 20px;overflow-wrap: break-word;" ), @@ -86,7 +158,13 @@ def build_contact_block(): [ Heading(1, "Contact"), ThematicBreak(), - [Link("/public_key", "My PGP Key"), LineBreak()] + [ + Link("/public_key", "My PGP Key"), + LineBreak(), + Link("/message", "Write a message"), + LineBreak(), + LineBreak(), + ] if exists("/config/pub.key") else None, Link(f"mailto:{CONFIG['email']}", CONFIG["email"]),