-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
258 lines (201 loc) · 6.63 KB
/
main.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import os
import util
import json
import flask
import asyncio
from mod_repltalk import client
from mod_repltalk import repltalk
from flask import request
from flask import render_template
app = flask.Flask(__name__)
DENY = "You are not a ReplTalk moderator!"
UNLIST = "{target} has been unlisted."
DELETE = "{target} has been deleted."
WARN = "{target} has been warned."
BAN = "{target} has been banned."
ADMINBAN = "{name} has now been banned and all posts/comments were deleted."
LOGIN = "You are not logged with your Replit account!"
INVALID = "The given {value} is invalid."
with open("responses/infractions.json", "r") as f:
infractions = json.load(f)
with open("responses/comments.json", "r") as f:
comments = json.load(f)
with open("responses/warns.json", "r") as f:
warns = json.load(f)
modSid = os.getenv("modsid")
commentSid = os.getenv("commentsid")
@app.route("/")
def main():
head = request.headers
flag = util.verify_headers(head)
if flag:
return render_template("mod.html")
elif flag == False:
return DENY, 403
else:
return render_template("index.html")
@app.route("/admin-ban", methods=["POST"])
def ban():
form = request.form
head = request.headers
flag = util.verify_headers(head)
if flag:
reason = form["reason"]
username = form["username"]
result = asyncio.run(util._ban(username, reason))
if not result:
return "User not found.", 404
if not reason:
return "Must provide a valid reason.", 400
return ADMINBAN.format(name=username)
elif flag == False:
return DENY, 403
else:
return render_template("index.html")
@app.route("/audits", methods=["GET", "POST"])
def audits():
form = request.form
head = request.headers
flag = util.verify_headers(head)
if flag:
if flask.request.method == "POST":
settings = {
"creator": form.get("creator", None),
"actionType": form.get("actionType", None),
"model": form.get("model", None),
"page": form.get("page", 1),
"order": form.get("order", "NEWEST"),
}
settings["page"] = int(settings["page"]) if settings["page"] != "" else 1
audits = asyncio.run(
client.get_audits(
creator=settings["creator"] if settings["creator"] != "" else None,
model=settings["model"] if settings["model"] != "ALL" else None,
actionType=settings["actionType"] if settings["actionType"] != "ALL" else None,
page=settings["page"],
order=settings["order"],
)
)
else:
settings = {"page": 1}
audits = asyncio.run(client.get_audits())
for audit in range(len(audits)):
auditData = asyncio.run(client.get_audit(audits[audit]["id"]))
if "resolved" in auditData:
if auditData["resolved"]:
audits[audit]["type"] = "RESOLVED"
elif not auditData["resolved"]:
audits[audit]["type"] = "UNRESOLVED"
elif "is_hidden" in auditData:
if auditData["is_hidden"]:
audits[audit]["type"] = "UNLISTED"
elif not auditData["is_hidden"]:
audits[audit]["type"] = "RELISTED"
elif "is_locked" in auditData:
if auditData["is_locked"]:
audits[audit]["type"] = "LOCKED"
elif not auditData["is_locked"]:
audits[audit]["type"] = "UNLOCKED"
elif "is_pinned" in auditData:
if auditData["is_pinned"]:
audits[audit]["type"] = "PINNED"
elif not auditData["is_pinned"]:
audits[audit]["type"] = "UNPINNED"
elif "is_announcement" in auditData:
if auditData["is_announcement"]:
audits[audit]["type"] = "MARKED AS ANNOUNCEMENT"
elif not auditData["is_announcement"]:
audits[audit]["type"] = "UNMARKED AS ANNOUNCEMENT"
elif "board_id" in auditData:
audits[audit]["type"] = "CHANGED BOARD"
elif auditData["Model"] == "BannedBoardUsers":
if auditData["Type"] == "CREATE":
audits[audit]["type"] = "BANNED"
if auditData["Type"] == "DELETE":
audits[audit]["type"] = "UNBANNED"
elif auditData["Model"] == "Warning":
if auditData["Type"] == "CREATE":
audits[audit]["type"] = "WARNED"
if auditData["Type"] == "DELETE":
audits[audit]["type"] = "REMOVED WARNING"
if (
not str(audits[audit]["attached"]).isdigit()
and audits[audit]["model"] in ["Posts", "Comments"]
): audits[audit]["attached"] = audits[audit]["attached"].url
elif (
not str(audits[audit]["attached"]).isdigit()
and audits[audit]["model"] == "BannedBoardUsers"
): audits[audit]["attached"] = audits[audit]["attached"].name
elif (
not str(audits[audit]["attached"]).isdigit()
and audits[audit]["model"] == "Warning"
): audits[audit]["attached"] = audits[audit]["attached"].name
audits[audit]["creator"] = audits[audit]["creator"].name
return flask.render_template(
"audits.html", audits=audits, settings=settings, str=lambda i: str(i)
)
elif flag == False:
return DENY, 403
else:
return flask.render_template("index.html")
@app.route("/auto-act", methods=["POST"])
def auto_act():
form = request.form
head = request.headers
flag = util.verify_headers(head)
if flag:
type = form.get("type").lower()
if type not in infractions:
return INVALID.format(value=type)
target = form[type + "-input"]
if type in ["post", "comment"]:
try:
target = int(target)
except ValueError:
return INVALID.format(value="ID")
method = getattr(client, f"get_{type}")
if method:
target = asyncio.run(method(target))
else:
return flask.abort(400)
infraction = flask.request.form[type + "-infraction-input"].lower()
if infraction not in infractions[type]:
return "The infraction type is invalid.", 400
level = infractions[type][infraction]
if level == 1:
url = target.url
asyncio.run(target.update(is_hidden=True))
client.sid = commentSid
try: asyncio.run(target.post_comment(comments[infraction]))
except: None
client.sid = modSid
return UNLIST.format(target=url)
elif level == 2:
url = target.url
asyncio.run(target.delete())
return DELETE.format(target=url)
elif level == 3:
author = target.author if type in ["post", "comment"] else target
asyncio.run(author.warn(warns[infraction]))
extra = ""
if type in ["post", "comment"]:
url = target.url
asyncio.run(target.delete())
extra = DELETE.format(target=url)
return WARN.format(target=author.name) + extra
elif level == 4:
author = target.author if type in ["post", "comment"] else target
asyncio.run(author.ban(infraction))
extra = ""
if type in ["post", "comment"]:
url = target.url
asyncio.run(target.delete())
extra = DELETE.format(target=url)
return BAN.format(target=author.name) + extra
else:
return flask.abort(400)
elif flag == False:
return DENY
else:
return flask.render_template("index.html")
app.run(host="0.0.0.0", port=8080, threaded=True)