Files
2025-08-02 22:45:05 +02:00

155 lines
7.0 KiB
Plaintext
Executable File

#!/usr/bin/env python3
# nomadForum - a forum on the NomadNetwork
# Copyright (C) 2023-2025 AutumnSpark1226
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import uuid
import sys
import main
import notify
def edit_check_data():
if not main.check_uuid(edit_id):
print("data not found")
main.close_database(write_changes=False)
sys.exit(0)
op_username = ""
post_data = main.query_database(f"SELECT username FROM posts WHERE post_id = '{edit_id}'")
if len(post_data) != 1:
print("data not found")
main.close_database(write_changes=False)
sys.exit(0)
op_username = post_data[0][0]
if username != op_username:
print("You can only edit your own posts.")
main.close_database(write_changes=False)
sys.exit(0)
def print_fields(edit=False):
global content, title, username
if edit:
print(">Edit a post\n")
else:
print(">Create a post\n")
username = main.query_database(f"SELECT username FROM users WHERE link_id = '{link_id}'")
if len(username) != 1:
print("Congratulations, you found a bug!")
main.close_database(write_changes=False)
exit(0)
username = username[0][0]
if edit:
edit_check_data
if edit:
post_data = main.query_database(f"SELECT title, content FROM posts WHERE post_id = '{edit_id}'")[0]
title = post_data[0]
content = post_data[1]
content = content.replace("\n", "$newline$")
if content == "":
content = main.query_database(f"SELECT default_styling FROM users WHERE username = '{username}'")[0][0]
if content == 'None':
content = ""
print(f"Title: `B444`<40|post_title`{title}>`b")
print()
content = content.replace("\n", "$newline$")
print(f"Content: `B444`<70|content`{content}>`b")
print("$newline$ creates a new line. You can just use enter and it will be automatically replaced.\n")
if edit:
print(f"`Ff22`_`[Confirm`:{main.page_path}/post.mu`*|edit=true|edit_id={edit_id}|edit_confirm=true|source_link_id={link_id}]`_`f")
else:
print(f"`Ff22`_`[Post`:{main.page_path}/post.mu`*|source_link_id={link_id}]`_`f")
try:
link_id, remote_identity = main.handle_ids()
main.print_header(link_id, reload=True)
title = ""
content = ""
edit = False
edit_id = ""
for env_variable in os.environ:
if env_variable == "field_post_title":
title = os.environ[env_variable]
elif env_variable == "field_content":
content = os.environ[env_variable]
elif env_variable == "var_edit" and os.environ[env_variable] == "true":
edit = True
elif env_variable == "var_edit_id":
edit_id = os.environ[env_variable]
if len(main.query_database(f"SELECT user_id FROM users WHERE link_id = '{link_id}'")) != 1:
print(">You need to login first.")
elif title == "" or content == "":
print_fields(edit=edit)
elif len(title) <= 4 or len(title) >= 128:
print(">The title must be between 4 and 128 characters long.\n")
print_fields(edit=edit)
elif len(content) >= 10000:
print(">The content must not be more than 10000 characters.\n")
print_fields(edit=edit)
elif main.verify_link_id():
content = content.replace("$newline$", "\n")
title = main.prepare_title(title)
content = main.prepare_content(content)
username = main.query_database(f"SELECT username FROM users WHERE link_id = '{link_id}'")[0][0]
if main.query_database(f"SELECT enabled FROM users WHERE username = '{username}'")[0][0] != 1:
print(">Your account is disabled.")
main.close_database(write_changes=False)
sys.exit(0)
# simple spam protection, will be replaced by a better system in the future
elif len(main.query_database(f"SELECT numeric_id FROM posts WHERE username = '{username}' AND unixepoch() < (changed + 30)")) != 0:
print(">Spam protection triggered!")
main.close_database(write_changes=False)
sys.exit(0)
elif not edit and len(main.query_database(f"SELECT numeric_id FROM posts WHERE (title = '{title}' OR content = '{content}') AND unixepoch() < (changed + 120)")) != 0:
print(">Spam protection triggered!")
main.close_database(write_changes=False)
sys.exit(0)
if edit:
edit_check_data()
main.execute_sql(f"UPDATE posts SET title = '{title}', content = '{content}', changed = unixepoch(), last_action = unixepoch(), edited = 1 WHERE post_id = '{edit_id}' AND username = '{username}'")
print(">Your post has been edited.")
print()
print(f"`Ff22`_`[Visit`:{main.page_path}/view.mu`post_id={edit_id}]`_`f")
else:
post_id = str(uuid.uuid4())
# this should not be very probable
while len(main.query_database(f"SELECT numeric_id FROM posts WHERE post_id = '{post_id}'")) != 0:
post_id = str(uuid.uuid4())
main.execute_sql(f"INSERT INTO posts (post_id, username, title, content, created, changed, last_action) VALUES ('{post_id}', '{username}', '{title}', '{content}', unixepoch(), unixepoch(), unixepoch())")
print(">Your post has been added.")
print()
print(f"`Ff22`_`[Visit`:{main.page_path}/view.mu`post_id={post_id}]`_`f")
if main.notifications_enabled:
try:
notification_recipients = main.query_database(f"SELECT username FROM subscriptions WHERE post_id = '$new$'")
content = content.replace("\'\'", "\'")
display_name = main.remove_micron(main.query_database(f"SELECT display_name FROM users WHERE username = '{username}'")[0][0])
notification_text = f"{display_name} posted:\n{title}\n{main.remove_micron(content)}"
for recipient in notification_recipients:
destinations = main.query_database(f"SELECT remote_id FROM connections WHERE username = '{recipient[0]}' AND send_notifications = 1")
for destination in destinations:
notify.add_notification([main.decrypt(destination[0]), notification_text])
except:
print("An error occured. No notifications were sent. Please contact an admin.")
main.close_database()
except:
print("An error occured")