-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.mu
executable file
·165 lines (152 loc) · 7.41 KB
/
post.mu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
# nomadForum - a forum on the NomadNetwork
# Copyright (C) 2023-2024 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():
print(">Create a post\n")
print(f"Title: `B444`<40|title`{title}>`b")
print()
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]
global content
if content == "":
content = main.query_database(f"SELECT default_styling FROM users WHERE username = '{username}'")[0][0]
if content == 'None':
content = ""
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")
print(f"`Ff22`_`[Post`:{main.page_path}/post.mu`*|source_link_id={link_id}]`_`f")
def print_fields_edit():
global title, content, username
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]
edit_check_data()
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$")
print(">Edit a post\n")
print(f"Title: `B444`<40|title`{title}>`b")
print()
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")
print(f"`Ff22`_`[Post`:{main.page_path}/post.mu`*|edit=true|edit_id={edit_id}|edit_confirm=true|source_link_id={link_id}]`_`f")
print("#!c=0")
try:
link_id, remote_identity = main.handle_ids()
main.print_header(link_id)
title = ""
content = ""
edit = False
edit_id = ""
for env_variable in os.environ:
if env_variable == "field_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 == "" and content == "":
if edit:
print_fields_edit()
else:
print_fields()
elif len(title) < 4 or len(title) > 128 or len(content) >= 4096:
print(">The title must be at least 4 characters long. (Did you enter very long text? Well, that's not allowed.)\n")
content = ""
title = ""
print_fields()
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 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")