-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_account.mu
executable file
·162 lines (152 loc) · 7.86 KB
/
delete_account.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
#!/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 sys
from argon2.exceptions import VerificationError
from argon2 import PasswordHasher
import main
import notify
def print_fields():
print(">Account deletion")
print()
print(" Username: `B444`<username`" + username + ">`b")
if remote_identity:
print()
print("Leave this field empty if you want to use your identity for authorizing this action.")
print(" Password: `B444`<!|password`>`b")
print(" `B444`<?|delete_content|yes`>`bDelete content (posts and comments)`")
main.execute_sql(f"DELETE FROM verification_codes WHERE use_type = 'delete_account' AND use_id = '{link_id}'")
verification_code = main.get_verification_code()
main.execute_sql(f"INSERT INTO verification_codes (username, code, use_type, use_id, creation_time) VALUES ('None', '{main.encrypt(verification_code)}', 'delete_account', '{link_id}', unixepoch())")
print(f"Confirm this action by typing: '{verification_code}'")
print("Confirmation: `B444`<8|confirmation`>`b")
print(f"`Ff22`_`[Delete account`:{main.page_path}/delete_account.mu`*|source_link_id={link_id}]`_`f")
if not remote_identity:
print()
print("Verification using your identity is not available because you are not identified.")
try:
link_id, remote_identity = main.handle_ids()
main.print_header(link_id, reload=True)
username = ""
password = ""
confirmation = ""
delete_content = False
for env_variable in os.environ:
if env_variable == "field_username":
username = os.environ[env_variable]
elif env_variable == "field_password":
password = os.environ[env_variable]
elif env_variable == "field_confirmation":
confirmation = os.environ[env_variable]
elif env_variable == "field_delete_content" and os.environ[env_variable] == "yes":
delete_content = True
if username == "":
print_fields()
elif len(username) < 4:
print(">Your username must be longer than 4 characters.\n")
print_fields()
elif len(username) > 64:
print(">Your username must not be longer than 64 characters.\n")
print_fields()
elif not main.check_username(username, allow_admin=True):
print(">You entered a wrong username or password.\n")
print_fields()
elif len(password) > 512:
print(">Long passwords are good, but that is just too long.\n")
print_fields()
elif main.verify_link_id():
if len(main.query_database(f"SELECT user_id FROM users WHERE username = '{username}'")) == 0:
print(">You entered a wrong username or password.\n")
print_fields()
main.close_database(write_changes=False)
sys.exit(0)
else:
confirmed = False
query_result = main.query_database("SELECT username, remote_id FROM connections WHERE allow_login = 1 AND verified = 1")
for data in query_result:
if main.decrypt(data[1]) == remote_identity and data[0] == username:
confirmed = True
break
if len(password) >= 8:
hasher = PasswordHasher()
try:
hasher.verify(main.decrypt(main.query_database(f"SELECT password FROM users WHERE username = '{username}'")[0][0]), password)
confirmed = True
except VerificationError:
print(">You entered a wrong username or password.\n")
print_fields()
main.close_database(write_changes=False)
sys.exit(0)
if main.query_database(f"SELECT enabled FROM users WHERE username = '{username}'")[0][0] != 1:
print(">This account is disabled and cannot be deleted.")
main.close_database(write_changes=False)
sys.exit(0)
query_result = main.query_database(f"SELECT code FROM verification_codes WHERE use_type = 'delete_account' AND use_id = '{link_id}'")
if len(query_result) != 1:
print(">Verification error\n")
print_fields()
main.close_database(write_changes=False)
sys.exit(0)
else:
verification_code = main.decrypt(query_result[0][0])
if confirmation != verification_code:
print(">Please confirm the deletion by typing the code.\n")
print_fields()
main.close_database()
sys.exit(0)
if not confirmed:
print(">Verification error\n")
print_fields()
main.close_database(write_changes=False)
sys.exit(0)
elif len(main.query_database(f"SELECT value FROM settings WHERE key = 'admin_username' AND value = '{username}'")) == 1:
print(">Admin accounts can't be deleted!\n")
print_fields()
main.close_database(write_changes=False)
sys.exit(0)
else:
if main.notifications_enabled:
try:
notification_text = f"Your account {username} has been deleted. If you didn't perform this action, contact a forum admin immediately!"
destinations = main.query_database(f"SELECT remote_id FROM connections WHERE username = '{username}' 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.execute_sql(f"DELETE FROM users WHERE username = '{username}'")
main.execute_sql(f"DELETE FROM connections WHERE username = '{username}'")
main.execute_sql(f"DELETE FROM subscriptions WHERE username = '{username}'")
# modify / delete posts their comments
if delete_content:
posts = main.query_database(f"SELECT post_id FROM posts WHERE username = '{username}'")
for post_id in posts:
main.execute_sql(f"DELETE FROM posts WHERE post_id = '{post_id[0]}'")
main.execute_sql(f"DELETE FROM subscriptions WHERE post_id = '{post_id[0]}'")
else:
main.execute_sql(f"UPDATE posts SET username = '[DELETED]' WHERE username = '{username}'")
# modify / delete comment chains started by this user
if delete_content:
comments = main.query_database(f"SELECT comment_id FROM comments WHERE username = '{username}'")
main.delete_comment_chain(comments)
else:
main.execute_sql(f"UPDATE comments SET username = '[DELETED]' WHERE username = '{username}'")
print(">Your account has been deleted.\n")
# submit a dummy value in order to force a reload
print(f"`Ff22`_`[Home`:{main.page_path}/index.mu`reload=132]`_`f")
main.close_database()
except:
print("An error occured")