-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathboards.py
More file actions
341 lines (310 loc) · 11.4 KB
/
boards.py
File metadata and controls
341 lines (310 loc) · 11.4 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import time
import os
from flask import Blueprint, request
import pagemaker as p
import viewer as v
import tags as t
import settings as s
import utils as u
import tripcode as tr
import whitelist
boards = Blueprint("boards", __name__)
index_p = "./boards/list.txt"
with open(index_p, "r") as index:
index = index.read().splitlines()
local_b = [i.split(" ") for i in index]
# /b/
# splash()
# /b/board/
# browse(board)
# /b/board/key
# mod_board(board, key)
# /b/board/host/thread
# show_thread(board, host, thread)
# # board_index(board)
# # load_thread(board, host, thread)
def board_index(board):
# mod.txt
# 0 - hide
# 1 - normal
# 2 - lock
# 3 - sticky
# 4 - lock sticky
# 5 - permasage
threads = t.tags_threads([board]) # [host, thread]
with open("./threads/list.txt", "r") as info:
info = info.read().splitlines() # [host, thread, last, Lrep, Grep, title]
info = [i.split(" ") for i in info]
info = [i for i in info if i[:2] in threads]
info = [[*i[:5], " ".join(i[5:])] for i in info]
mfile = f"./boards/{board}/threads.txt"
to_hide = [] # 0
to_sticky = [] # 2
to_sage = [] # 3
hidden = []
normal = []
sages = []
stickies = []
with open(mfile, "r") as mod:
mod = mod.read().splitlines()
for n, m in enumerate(mod):
entry = m.split(" @ ")
entry[0] = entry[0].split(" ")
try:
if int(entry[1][0]) == 0: # hidden
to_hide.append(entry[0])
elif int(entry[1][0]) == 2: # sticky
to_sticky.append(entry[0])
elif int(entry[1][0]) == 3: # sage
to_sage.append(entry[0])
else:
normal.append(entry[0])
except:
pass
link = "<li> <a href='{0}'>{1}</a> ({2} replies)"
sticky = "<li> 📌 <a href='{0}'>{1}</a> ({2} replies)"
sage = "<li> ⚓ <a href='{0}'>{1}</a> ({2} replies)"
for n, entry in enumerate(info):
item = [entry[0], entry[1]]
url = "/".join([f"/b/{board}", *item])
link_data = [url, entry[5], entry[4]]
if item in to_sticky:
stickies.append(sticky.format(*link_data))
elif item in to_sage:
sages.append(sage.format(*link_data))
elif item in to_hide:
hidden.append(link.format(*link_data))
else:
normal.append(link.format(*link_data))
links = []
links += stickies
links += normal
links += sages
return links
@boards.route('/b/')
def splash():
with open(index_p, "r") as index:
index = index.read().splitlines()
local_b = [i.split(" ") for i in index]
boards = local_b
template = "<li><a href='/b/{0}'>{0}</a> (managed by <b><code>{1}</code></b>)"
page = """<h1>User Boards</h1><div class="info">
Boards are a work in progress system that will allow user-managed
communities to exist within the Multichan network.
<br>Register a board by visiting <code>/b/tagname/password</code> , where
<i>tagname</i> is a <a href="/tags/">tag</a> that exists and password
is a secret password. HTML can be used in <code>intro.txt</code> ;
moderating threads is done by adding new entries to
<code>threads.txt</code> and moderating comments is done by adding new
entries to <code>hide.txt</code> . One entry per line.
</div><p>"""
page += "<div><ul>" \
+ "\n".join([template.format(*i) for i in boards]) \
+ "</ul></div>"
return p.mk(page)
@boards.route('/b/<board>/', methods=['POST', 'GET'])
@boards.route('/b/<board>/list')
def browse(board):
if request.method == 'POST':
if not whitelist.approve():
return p.mk(whitelist.show_captcha(1))
user_key = tr.sec(request.form["key"])
with open(index_p, "r") as index:
index = index.read().splitlines()
local_b = [i.split(" ") for i in index]
test = [i for i in local_b if (i[0] == board and i[1] == user_key)]
if not len(test):
pass
files = ["info.txt", "hide.txt", "threads.txt", "ihosts.txt"]
try:
for f in files:
path = "./boards/" + board + "/"
data = request.form[f].strip()
with open(path+f, "w") as out:
out.write(data)
except:
print(board)
info = f"./boards/{board}/info.txt"
with open(info, "r") as about:
about = about.read()
page = ["<div>"]
page.append(f"<a href='/b'>[back]</a>")
page.append(f"<h1>/{board}/</h1>")
page.append(f"<link rel='alternative' type='application/xml' href='/atom/tag/{board}.atom'>")
page.append(about)
page.append(f"<p><hr><a href='/create/{board}'>[+] Create a new thread on /{board}/</a>")
with open(f"./boards/{board}/ihosts.txt", "r") as ihosts:
ihosts = ihosts.read().strip().splitlines()
page.append("<hr><b>Image hosts: </b>" + " ♥ ".join(ihosts))
page.append("<hr><ul>")
threads = board_index(board)
# threads = "\n".join(threads)
page += threads
page.append("</ul>")
page = "\n".join([n for n in page if (len(n) != 2)])
return p.mk(page)
def mkboard(board, key):
if not whitelist.approve():
return p.mk(whitelist.show_captcha(1))
key = tr.sec(key)
path = "./boards/" + board + "/"
try:
os.mkdir(path)
except:
pass
with open("./boards/list.txt", "a") as li:
li.write(f"{board} {key}\n")
try:
os.mkdir(path)
except:
pass
files = ["info.txt", "threads.txt", "hide.txt", "ihosts.txt"]
for f in files:
_path = path + f
with open(_path, "w") as fi:
if f == "ihosts.txt":
fi.write(s.ihost)
else:
fi.write("")
@boards.route('/b/<board>/<key>')
def mod_board(board, key):
new_local = []
if not whitelist.approve():
return p.mk(whitelist.show_captcha(1))
with open(index_p, "r") as index:
index = index.read().splitlines()
local_b = [i.split(" ") for i in index]
bs = [x[0] for x in local_b]
if board not in bs:
mkboard(board, key)
return str([board, key])
for L in local_b:
if tr.sec(key) == L[1] and L[0] == board:
new_local.append(L)
if not len(new_local):
return "0"
page = ["<style>textarea {", "width: 800px; height:120px;}</style>"]
page.append("<form action='.' method='post'>")
page.append("<input type='submit' value='Moderate Board'>")
page.append(f"<input type='hidden' name='board' value='{board}'>")
page.append(f"<input type='hidden' name='key' value='{key}'>")
mod = {}
files = ["info.txt", "threads.txt", "hide.txt", "ihosts.txt"]
for f in files:
with open(f"./boards/{board}/{f}", "r") as data:
data = data.read().strip()
mod[f] = data + "\n"
for f in mod:
page.append(f)
if f == "threads.txt":
page[-1] += " // format: host thread @ mode ; 0 hide ; 2 sticky; 3 sage"
elif f == "hide.txt":
page[-1] += " // format: host thread host reply# "
page.append(f"<textarea name='{f}'>{mod[f]}</textarea>")
return "<pre>" + "\n".join(page) + "</pre>"
def load_thread(board, host, thread):
# Board view of host:thread
# path = [] # [[host, time]]
posts = {} # {"host": [time, time, time]}
data = {} # {"host": [[host, time, author, message]] }
_thread = [] # [[host, time, author, message]]
origin = [host, thread] # 0chan, 0
path = f"./threads/{host}/{thread}/"
hide = f"./boards/{board}/hide.txt"
ihost = f"./boards/{board}/ihosts.txt"
with open(path + "list.txt", "r") as path:
path = path.read().splitlines()
path = [p.split(" ") for p in path]
with open(ihost, "r") as ihosts:
ihosts = ihosts.read().splitlines()
ihosts = [i.strip() for i in ihosts]
with open(hide, "r") as hide:
hide = hide.read().splitlines()
hide = [h.split(" ") for h in hide]
hide = [h for h in hide if h[:2] == origin]
for p in path:
# site time
if p[0] not in posts.keys():
posts[p[0]] = []
posts[p[0]].append(p[1])
# posts[host][reply, reply, reply]
data = posts.copy()
for d in data:
whereis = f"./threads/{host}/{thread}/{d}.txt"
with open(whereis, "r") as files:
files = files.read().splitlines()
files = [[d, *f.split("<>")] for f in files]
data[d] = files
# data[host][rep] = [post, goes, here]
# {"host": ["host", "time", "author", "message"]}
for h in hide:
if [host, thread] != [h[0], h[1]]:
continue
data[h[2]][int(h[3])-1] = [data[h[2]][int(h[3])-1][0],
data[h[2]][int(h[3])-1][1],
"<i>deleted</i>",
"<i>message deleted by admin</i>"]
cnt = {}
for p in path:
h = p[0]
if h not in cnt:
cnt[h] = 0
_thread.append(data[h][cnt[h]])
cnt[h] += 1
return _thread
# data["host"][cnt] = ["host", "time", "author", "message"]
# _thread = list.txt x ["host", "time", "author", "message"]
@boards.route('/b/<board>/<host>/<thread>/')
def show_thread(board, host, thread, methods=['POST', 'GET']):
# datetime = "%a, %b %d, %Y, @ %-I %p"
# datetime = time.strftime(datetime, time.localtime(int(x[1]))),
test = mod_board(board, host)
tindex = load_thread(board, host, thread) # [[host, time, author, comment]]
tindex = [[x[0], u.unix2hum(x[1]), *x[2:]] for x in tindex]
head = f"./threads/{host}/{thread}/head.txt"
with open(head, "r") as head:
head = head.read().splitlines()[0]
page = ["<div class='threads'>"]
page.append(f"{s.name} <a href='/b/{board}'>/{board}/</a>")
page.append(f"<h2>{head}</h2>")
cnt = {}
render = []
with open("./templ/post.t", "r") as reply:
reply = reply.read()
for t in tindex:
with open(f"./boards/{board}/ihosts.txt", "r") as ihosts:
ihosts = ihosts.read().splitlines()
if t[0] not in cnt:
cnt[t[0]] = 0
cnt[t[0]] += 1
b = [t[0], s.friends[t[0]]]
ref = f"{b[1]}/{cnt[b[0]]}"
link = f"<a href='#{ref}' "
link += f"onclick='quote(\"{ref}\")' id='{ref}'>"
link += f">>{b[0]}/{cnt[b[0]]}</a>"
for i in ihosts:
print([i, t[3]])
if i in t[3]:
t[3] = u.imgur(t[3], i)
break
t[3] = t[3].split("<br>")
t[3] = "<br>".join([f"<b class='quote'>{x}</b>"
if len(x) and x[0] == ">" else x
for x in t[3]])
# 0 reply, # 1 date, #2 name, #3 comment, #4 host
# 0 host, # 1 time, #2 author, #3 comment
page.append(reply.format(link, t[1], t[2], t[3], ""))
canpost = whitelist.approve()
with open("./templ/newr.t", "r") as newr:
newr = newr.read().format(host, thread)
if not canpost:
replf = whitelist.show_captcha(1, f"/threads/{host}/{thread}/")
else:
replf = newr.format(board, thread)
page.append(replf)
page.append("</div>")
return p.mk("<br>".join(page))
@boards.route('/b/<board>/0')
def front(board):
with open(f"./threads/{board}/list.txt", "r") as index:
index = index.read().splitlines()