-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwriter.py
182 lines (163 loc) · 5.81 KB
/
writer.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
import os
import time
import re
from flask import Blueprint, request
import refresh
import tags
import whitelist
import tripcode
import settings as s
import pagemaker as p
writer = Blueprint("writer", __name__)
tdir = "threads"
with open("templ/newt.t", "r") as newtt:
newtt = newtt.read()
def nametrip(name):
if "#" in name:
name = name.split("#")[:3]
print(name)
if len(name) > 2:
if len(name[1]):
name[1] = tripcode.mk(name[1])
name[1] += " !!" + tripcode.sec(name[2])
else:
name[1] = "!" + tripcode.sec(name[2])
name = name[:2]
else:
name[1] = tripcode.mk(name[1])
if not len(name[0]):
name[0] = "Anonymous"
name = " <b><a>!".join(name) + "</a></b>"
return name
def log(host, thread, postnum, reply):
ip = whitelist.get_ip()
line = " ".join([host, thread, postnum, ip, reply])
iplog = ""
with open(s.log, "r") as logger:
postlog = logger.read()
postlog += line
with open(s.log, "w") as logger:
logger.write(postlog)
def mk_op(title="", tag="random", author="Anonymous", msg=""):
title = title[:s._short]
tag = tag[:s._short].lower()
author = author[:s._short]
msg = msg[:s._long]
title = title.replace("&", "&").replace("<", "<")
author = author.replace("&", "&").replace("<", "<")
msg = msg.replace("&", "&").replace("<", "<")
msg = msg.replace("\n","<br>").replace("\r","")
if not title.strip() or not msg.strip():
return "Please write a message to create a new conversation."
if not author:
author = "Anonymous"
author = nametrip(author)
pat = re.compile(r'^[ A-Za-z0-9_-]*$')
tag = " ".join(list(set(re.findall(r'\w+', tag))))
if len(tag) == 0:
tag = "random"
# author, tstamp, msg
tnow = str(int(time.time()))
t_loc = ["local", tnow]
b_pat = f"./{tdir}/local/"
t_pat = b_pat + tnow + "/"
os.mkdir(t_pat)
head = [title, tag]
files = {"head": t_pat + "head.txt",
"list": t_pat + "list.txt",
"op": t_pat + "local.txt"}
with open(files["head"], "w") as headf:
headf.write("\n".join(head))
if os.path.isfile(files["list"]):
with open(files["list"], "r") as listf:
li = li.read().splitlines()
else:
li = []
li.append(f"local {tnow}")
li = "\n".join(li) + "\n"
with open(files["list"], "w") as listf:
listf.write(li)
rline = "<>".join([tnow, author, msg]) + "\n"
with open(files["op"], "w") as opf:
opf.write(rline)
with open(b_pat + "list.txt", "r") as bind:
bind = bind.read().splitlines()
upd = [t_loc[1], t_loc[1], "1 1", title]
bindex = [b for b in bind if len(b) > 4]
bindex = [" ".join(upd)] + bindex
bindex = "\n".join(bindex)
with open(b_pat + "list.txt", "w") as bind:
bind.write(bindex)
log("local", tnow, "1", rline)
refresh.mksite()
tags.mkhost("local")
tags.mksite()
def rep_t(host, thread, now, author, msg):
# open host/thread/local
# append post json
# update list.txt
# update host/list
author = author.replace("&", "&").replace("<", "<")
if not author:
author = "Anonymous"
else:
author = nametrip(author)
msg = msg[:s._long]
tdir = f"./threads/{host}/{thread}/"
if os.path.isfile(tdir + "lock"):
return "Error: thread is locked!"
tnow = now
msg = msg.replace("&", "&").replace("<", "<")
msg = msg.replace("\n","<br>").replace("\r","")
rline = "<>".join([tnow, author, msg]) + "\n"
cnt = 0
with open(tdir + "local.txt", "a") as t:
t.write(rline)
with open(tdir + "local.txt", "r") as t:
t = t.read().splitlines()
cnt = len(t)
with open(tdir + "list.txt", "a") as tlist:
tlist.write(f"local {tnow}\n")
log(host, thread, str(cnt), rline)
def update_host(host, thread, now, wr=1):
tpath = f"./threads/{host}/list.txt"
with open(tpath, "r") as tf:
tf = tf.read().splitlines()
tnum = thread
tf = [t.split(" ") for t in tf]
tfd = {t[0]: t[1:] for t in tf}
tfd[tnum][0] = str(now)
tfd[tnum][1] = str(int(tfd[tnum][1]) +1) # local
tfd[tnum][2] = str(int(tfd[tnum][2]) +1) # total
newl = []
for t in tfd:
newl.append([t, *tfd[t]])
newl.sort(key=lambda x:x[1], reverse=True)
newl = "\n".join([" ".join(t) for t in newl])
if wr:
with open(tpath, "w") as tpath:
tpath.write(newl)
refresh.mksite()
@writer.route('/create/', methods=['POST', 'GET'])
@writer.route('/create/<t>', methods=['POST', 'GET'])
def new_thread(t="random"):
if request.method == 'POST':
if not whitelist.approve():
return "You need to solve <a href='/captcha/'>the " \
+ "captcha</a> before you can post."
if request.form['sub'] == "Create chat":
flood = whitelist.flood(s.thread, "thread")
if flood: return flood
mk_op(title=request.form['title'],
tag=request.form['tag'],
author=request.form['author'],
msg=request.form['message'])
return "<center><h1>" \
+ "Posting thread.....<p>" \
+ "<a href='/threads/'>(back)</a></h1></center>"
# if not t=tag , t = random
if not len(t):
t = "random"
if not whitelist.approve():
return(p.mk(whitelist.show_captcha(1) + newtt.format(t)))
return p.mk(newtt.format(t))