nomadnet-website/pages/chat.mu

56 lines
1.6 KiB
Plaintext
Executable File

#!/usr/bin/env python3
import hswro
from os import environ
from redis import Redis
from datetime import datetime
import json
from uuid import uuid4
from typing import Optional
CHAT_NAME = "chat_test"
toast: Optional[str] = None
l = hswro.get_login_info()
r = Redis(host='localhost', port=6379, db=0)
msg_to_post = environ.get('field_message', None)
if msg_to_post is not None and msg_to_post.strip() != "" and l[0] is not None:
msg_to_post = hswro.sanitize_input(msg_to_post[0:255].strip())
r.rpush(CHAT_NAME, str(json.dumps({
"when": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
"who": l[0],
"msg": msg_to_post,
"id": uuid4().hex
})))
r.ltrim(CHAT_NAME, -20, -1)
#print(environ)
msg_to_del = environ.get('var_delete', None)
if msg_to_del is not None and hswro.is_admin():
for msg in r.lrange(CHAT_NAME, 0, -1):
m = json.loads(msg.decode('utf-8'))
if m['id'] == msg_to_del:
toast = f"\nDeleted a post by {m['who']}"
r.lrem(CHAT_NAME, 0, msg)
break
hswro.header("Shoutbox")
if toast is not None:
print(f"`c{toast}")
print("`l")
if l[0] is not None:
print(f"{l[0]}: `BFFF`F222`<message`>{hswro.BGCOLOR} [`[Send`:/page/chat.mu`*]]")
for msg in reversed(r.lrange(CHAT_NAME, 0, -1)):
m = json.loads(msg.decode('utf-8'))
delbtn = ""
if hswro.is_admin():
delbtn = f"`B400`FAAA`!`[DEL`:/page/chat.mu`delete={m['id']}]`!"
print(f"{delbtn}`BCCC`F333{m['when']}`BAAA`F444{'<'+m['who']+'>':>16}`B888`F000{m['msg']}")
hswro.reset_colors()
hswro.footer()