add message function

This commit is contained in:
JMARyA 2022-08-25 10:15:00 +02:00
parent 5be8a35856
commit e9b84ce522
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 87 additions and 5 deletions

1
.gitignore vendored
View file

@ -134,3 +134,4 @@ cython_debug/
/config/
/db/
.vscode
/data

View file

@ -9,4 +9,5 @@ services:
- 1030:1030
volumes:
- ./config:/config
- ./data:/data
- ./src:/app

View file

@ -9,3 +9,4 @@ services:
- 1030:1030
volumes:
- ./config:/config
- ./data:/data

View file

@ -1,2 +1,3 @@
Flask
gnupg
git+https://github.com/jmarya/htmlpy#egg=htmlpy

View file

@ -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"]),