-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPchan.cpp
More file actions
234 lines (197 loc) · 6.26 KB
/
IPchan.cpp
File metadata and controls
234 lines (197 loc) · 6.26 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
#include "IPchan.h"
#include "lib/dumbstr/dumbstr.h"
#include "lib/captcha/botwall.h"
#include "lib/sqlite/sqlite3.h"
#include <iostream>
#include <string>
#include <algorithm>
#include "lib/mongoose/mongoose.h"
#include "lib/sqleasy/sqleasy.h"
char* sql_err;
size_t stoi_nchars;
void db_schema_init(sqlite3* db)
{
rows res;
const char* make_posts = "create table posts("
"tid INTEGER,"
"bid INTEGER,"
"no INTEGER,"
"tim INTEGER,"
"nam TEXT,"
"bod TEXT"
");";
const char* make_threads = "create table threads("
"bid INTEGER,"
"tid INTEGER,"
"subject TEXT,"
"replies INTEGER,"
"lastpost INTEGER"
");";
const char* make_boards = "create table boards("
"ipid INTEGER,"
"threadcount INTEGER"
");";
sqlite3_exec(db, make_threads, sqleasy_q::cb, &res, &sql_err);
sqlite3_exec(db, make_posts, sqleasy_q::cb, &res, &sql_err);
}
std::string thread::make_thread_fe(sqlite3* db, row& r)
{
std::string acc;
rows posts;
sqleasy_q{db,
dumbfmt({"select * from posts where bid=",se(r["bid"])," and tid=",se(r["tid"]),";"})
}.rexec(&posts);
for (auto iter = posts.begin(); iter != posts.end(); iter++)
{
if (iter == posts.begin() + 1 && posts.size() > 2)
acc.append("<details><summary>Unroll Thread...</summary>");
else if (iter == posts.end() - 1 && posts.size() > 2)
acc.append("</details>");
time_t ti = (time_t)stoi((*iter)["tim"], &stoi_nchars);
acc.append(
dumbfmt_file("./static/template/reply.html", {
{"no", (*iter)["no"]},
{"name", dumbfmt_html_escape((*iter)["nam"])},
{"trip", dumbfmt_html_escape("")},
{"datetime", ctime(&ti)},
{"body", dumbfmt_html_escape((*iter)["bod"])}
})
);
}
return acc;
}
bool thread::comparator(const row &l, const row &r)
{
int left = std::stoi(l.at("lastpost"), &stoi_nchars);
int right = std::stoi(r.at("lastpost"), &stoi_nchars);
return left > right;
}
std::string board::make_threadlist_fe(sqlite3* db)
{
std::string acc;
rows threads;
sqleasy_q{db,
dumbfmt({"select * from threads where bid=",se(std::to_string(this->ipid)), ";"})
}.rexec(&threads);
std::sort(threads.begin(), threads.end(), thread::comparator);
for (auto iter = threads.begin(); iter != threads.end(); iter++)
{
acc.append(
dumbfmt({
"<a href=\"#", (*iter)["tid"], "\">",
(*iter)["subject"], " (" ,(*iter)["replies"], ")", "</a> "
})
);
}
return acc;
}
std::string board::make_board_fe(sqlite3* db, std::string captcha, std::string capt_token)
{
std::string acc;
rows threads;
sqleasy_q{db,
dumbfmt({"select * from threads where bid=", se(std::to_string(this->ipid)), ";"})
}.rexec(&threads);
std::sort(threads.begin(), threads.end(), thread::comparator);
for (auto iter = threads.begin(); iter != threads.end(); iter++)
{
acc.append(
dumbfmt_file("./static/template/thread.html", {
{"subject", (*iter)["subject"]},
{"replycount", (*iter)["replies"]},
{"replies", thread::make_thread_fe(db, (*iter))},
{"bid", (*iter)["bid"]},
{"tid", (*iter)["tid"]},
{"captcha_token", capt_token},
{"challenge", captcha}
})
);
}
return acc;
}
#define buffetchhttpvar(key) char buffer_##key[10000]; \
mg_http_get_var(&querystring, #key, buffer_##key, sizeof(buffer_##key)); \
const char* key = buffer_##key;
std::string thread::add_post(sqlite3* db, mg_str& querystring, std::string secret)
{
buffetchhttpvar(token);
buffetchhttpvar(guess);
buffetchhttpvar(bid);
if (!botwall::check_captcha(bid, guess, token, secret))
return "ur shits fucked mayne, try that captcha again";
rows res;
buffetchhttpvar(tid);
time_t tim = time(0);
rows threads;
sqleasy_q{db,
dumbfmt({"select * from threads where bid=",se(bid)," and tid=",se(tid),";"})
}.rexec(&threads);
const char* replies = threads.at(0)["replies"].c_str();
int replycount = std::stoi(replies, &stoi_nchars) + 1;
sqleasy_q{db,
dumbfmt({"update threads set replies=",se(std::to_string(replycount))," where bid=",se(bid)," and tid=",se(tid),";"})
}.rexec(&threads);
sqleasy_q{db,
dumbfmt({"update threads set lastpost=",se(std::to_string(tim))," where bid=",se(bid)," and tid=",se(tid),";"})
}.rexec(&threads);
buffetchhttpvar(postname);
buffetchhttpvar(postbody);
sqleasy_q{db,
dumbfmt(
{
"insert into posts values(",
se(tid), ",",
se(bid), ",",
se(std::to_string(replycount)), ",",
se(std::to_string(tim)), ",\"",
se(postname), "\",\"",
se(postbody), "\");"
}
)
}.rexec(&res);
return "done (hopefully.......)";
}
std::string board::add_thread(sqlite3* db, mg_str& querystring, std::string secret)
{
buffetchhttpvar(token);
buffetchhttpvar(guess);
buffetchhttpvar(bid);
if (!botwall::check_captcha(bid, guess, token, secret))
return "ur shits fucked mayne, try that captcha again";
rows res;
time_t tim = time(0);
rows threads;
sqleasy_q{db,
dumbfmt({"select * from threads where bid=", se(bid), ";"})
}.rexec(&threads);
int tid = threads.size();
buffetchhttpvar(postname);
buffetchhttpvar(postsub);
buffetchhttpvar(postbody);
sqleasy_q{db,
dumbfmt(
{
"insert into threads values(",
se(bid), ",",
se(std::to_string(tid)), ",\"",
se(postsub), "\",",
"1,",
se(std::to_string(tim)), ");"
}
)
}.rexec(&res);
sqleasy_q{db,
dumbfmt(
{
"insert into posts values(",
se(std::to_string(tid)), ",",
se(bid), ",",
"1", ",",
se(std::to_string(tim)), ",\"",
se(postname), "\",\"",
se(postbody), "\");"
}
)
}.rexec(&res);
return "done (hopefully.......)";
}