✨ add message function
This commit is contained in:
parent
5be8a35856
commit
e9b84ce522
6 changed files with 87 additions and 5 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -134,3 +134,4 @@ cython_debug/
|
|||
/config/
|
||||
/db/
|
||||
.vscode
|
||||
/data
|
|
@ -9,4 +9,5 @@ services:
|
|||
- 1030:1030
|
||||
volumes:
|
||||
- ./config:/config
|
||||
- ./data:/data
|
||||
- ./src:/app
|
|
@ -9,3 +9,4 @@ services:
|
|||
- 1030:1030
|
||||
volumes:
|
||||
- ./config:/config
|
||||
- ./data:/data
|
|
@ -1,2 +1,3 @@
|
|||
Flask
|
||||
gnupg
|
||||
git+https://github.com/jmarya/htmlpy#egg=htmlpy
|
84
src/index.py
84
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"]),
|
||||
|
|
Loading…
Add table
Reference in a new issue