server: let some boards be exempt from /recent
[rb-79.git] / rb79.h
bloba09983e7e74db2ea1ddd391b63d7cadbb9764c66
1 /*
2 * Copyright (c) 2017-2018, De Rais <derais@cock.li>
4 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all
7 * copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 /* Defines what's needed to identifiy a board['s folder] */
20 struct board {
21 /* Short name, like "a" or "b" */
22 const char *name;
24 /* Like "Anime" or "Random" */
25 const char *title;
27 /* Cooldown after you post text */
28 int text_cooldown;
30 /* Cooldown after you post textless */
31 int blank_cooldown;
33 /* How many threads are on each page */
34 unsigned int threads_per_page;
36 /* How many pages total */
37 unsigned int num_pages;
39 /* Whether posts here show up in /recent */
40 unsigned int appears_in_recent;
43 /* Things users can try to do */
44 enum action_t { NONE, REPLY, NEWTHREAD, REBUILD };
47 * A post as it will be stored in (or retrieved from) the database.
48 * As part of post_cmd, this will be built up by rb79.c, inserted
49 * into the db by db_insert_post, and retrieved for writing to
50 * filesystem in write-thread.c
52 struct prepared_post {
53 /* When it was issued */
54 time_t now;
57 * This requires db access to retrieve, so it may be 0 for
58 * most of the object's life
60 uintmax_t id;
62 /* Flags, in case this is the OP of a thread */
63 uint_fast8_t thread_closed;
64 uint_fast8_t thread_stickied;
66 /* HTML-esc'd, ready for inserting into pages */
67 char *name;
68 size_t name_len;
69 char *email;
70 size_t email_len;
71 char *tripcode;
72 size_t tripcode_len;
73 char *subject;
74 size_t subject_len;
75 char *comment;
76 size_t comment_len;
78 /* Comes from a struct filetype. A copy, needs to be freed */
79 char *ext;
80 size_t ext_len;
82 /* The name the uploader called the file (HTML-esc'd) */
83 char *file_name;
84 size_t file_name_len;
86 /* The path for the full file, like /m/src/123.jpg */
87 char *system_full_path;
88 size_t system_full_path_len;
90 /* The path for the thumbnail, like /m/src/123s.jpg */
91 char *system_thumb_path;
92 size_t system_thumb_path_len;
94 /* A string like "image/jpeg, 30KiB, 500x399" */
95 char *file_info;
96 size_t file_info_len;
98 /* The main server ignores this, but mod tools use it */
99 char *ip;
100 size_t ip_len;
102 struct post_cmd {
104 * Strings are filled in by multipart.c, where we are quite
105 * thankful that everything is guaranteed to be UTF-8.
106 * Parsing, if needed, is done by rb79.c
108 struct {
109 /* action= */
110 char *action;
111 size_t action_len;
113 /* board= */
114 char *board;
115 size_t board_len;
117 /* thread= */
118 char *thread;
119 size_t thread_len;
121 /* post= */
122 char *post;
123 size_t post_len;
125 /* name= */
126 char *name;
127 size_t name_len;
129 /* email= */
130 char *email;
131 size_t email_len;
133 /* tripcode - calculated from email */
134 char *tripcode;
135 size_t tripcode_len;
137 /* subject= */
138 char *subject;
139 size_t subject_len;
141 /* comment= */
142 char *comment;
143 size_t comment_len;
145 /* file upload */
146 char *file_name;
147 size_t file_name_len;
148 char *file_contents;
149 size_t file_contents_len;
151 /* challengeid= */
152 char *challenge_id;
153 size_t challenge_id_len;
155 /* challengeresponse= */
156 char *challenge_response;
157 size_t challenge_response_len;
158 } raw;
161 * Everything below is filled in by rb79.c as derived data
163 enum action_t action_id;
164 uintmax_t board_idx;
165 uintmax_t thread_id;
167 /* This is what goes into the database. */
168 struct prepared_post prepared;
171 * The following is intermediate data for sanitizing input
172 * comments. XXX: figure out if these can be safely removed
173 * from this object, because they're sort of messy.
176 /* A sort of normalized UTF-8 comment */
177 char *scannable_comment;
178 size_t scannable_comment_len;
180 /* see to_scannable() for this one */
181 size_t *position_map;
182 size_t position_map_len;
185 /* Filetype/thumbnailing options */
186 struct filetype {
187 /* */
188 const char *mime_type;
189 const char *ext;
190 const char *install_command;
191 const char *thumb_creation_command;
192 const char *static_thumbnail;
195 #define NUM_CHALLENGE_ANSWERS 5
197 /* wakarimasen-style CAPTCHA */
198 struct challenge {
199 /* */
200 const char *question;
201 const char *answers[NUM_CHALLENGE_ANSWERS];
204 /* regex-backed wordfilter */
205 struct wordfilter_input {
206 /* */
207 const char *pattern;
208 const char *replacement;
211 /* regex-backed forbidden input */
212 struct forbidden_input {
213 /* */
214 const char *pattern;
217 /* See config.def.h for detailed descriptions. */
218 struct configuration {
219 /* */
220 const char *static_www_folder;
221 const char *work_path;
222 const char *temp_dir_template;
223 const char *trip_salt;
224 size_t trip_salt_len;
225 const struct board *boards;
226 size_t boards_num;
227 size_t max_form_data_size;
228 size_t max_file_size;
229 size_t max_text_len;
230 const struct filetype *filetypes;
231 size_t filetypes_num;
232 const char *file_description_prog;
233 const char **headers;
234 size_t headers_num;
235 const struct challenge *challenges;
236 size_t challenges_num;
237 const struct wordfilter_input *wordfilter_inputs;
238 size_t wordfilter_inputs_num;
239 const struct forbidden_input *forbidden_inputs;
240 size_t forbidden_inputs_num;
243 /* db_writeback_ZZZ takes a callback. */
244 typedef int (*post_writeback)(struct prepared_post *p, FILE *f, uint_fast8_t
245 is_op, uint_fast8_t is_summary, uint_fast8_t
246 is_recent, const char *board_name,
247 uintmax_t in_thread, uintmax_t
248 total_posts_in_thread, uint_fast8_t hr_before);
250 /* db-ZZZ.c (currently only sqlite3, but it used to be MySQL) */
252 setup_dbs(const struct configuration *conf);
255 db_construct_post_link(const char *board, size_t board_len, const char *post,
256 size_t post_len, int *found, char **out,
257 size_t *out_len);
260 db_cull_threads(size_t board_idx, size_t *out_num_pages);
263 db_check_bans(const char *ip, size_t board_idx, time_t now, int *out_is_banned,
264 char **out_ban_until, char **out_ban_reason);
267 db_check_cooldowns(const char *ip, size_t board_idx, time_t now,
268 int *out_is_cooled, char **out_cooldown_length);
271 db_cull_and_report_threads(size_t board_idx, uintmax_t **out_thread_ids,
272 size_t *out_thread_id_num, size_t *out_num_pages);
275 db_extract_subject(size_t board_idx, uintmax_t thread, char **out_subject,
276 size_t *out_subject_len);
279 db_insert_ban(uint_fast8_t global_ban, size_t board_idx, const char *first_ip,
280 const char *last_ip, const char *message, time_t ban_start, time_t
281 ban_expiry);
284 db_insert_post(const char *ip, size_t in_thread, int cooldown, struct
285 post_cmd *pc, int *thread_dne, int *thread_closed,
286 int *thread_full,
287 uintmax_t *post_id);
290 db_is_op(size_t board_idx, uintmax_t post_id, uint_fast8_t *out_is_op);
293 db_moderate_post(size_t board_idx, uintmax_t post_id, const
294 char *moderator_comment, uint_fast8_t change_sticky,
295 uint_fast8_t sticky_status,
296 uint_fast8_t change_close, uint_fast8_t close_status);
299 db_remove_thread_and_files(size_t board_idx, uintmax_t thread_id);
302 db_remove_post_and_files(size_t board_idx, uintmax_t thread_id);
305 db_update_file_info(size_t board_idx, uintmax_t post_id, const char *info,
306 size_t info_len, const char *system_full_path, size_t
307 system_full_path_len,
308 const char *system_thumb_path, size_t
309 system_thumb_path_len);
312 db_writeback_posts_in_thread(size_t board_idx, uintmax_t thread, FILE *f,
313 post_writeback pw_function);
316 db_writeback_recent_posts(FILE *f, post_writeback pw_function);
319 db_writeback_thread_summaries(size_t board_idx, uintmax_t *thread_ids, size_t
320 thread_ids_num, FILE *f);
323 clean_dbs(void);
325 /* locks.c */
327 setup_locks(const struct configuration *conf);
330 lock_acquire(size_t board_idx);
333 lock_acquire_recent(void);
336 lock_release(size_t board_idx);
339 lock_release_recent(void);
342 clean_locks(void);
344 /* multipart.c */
346 setup_multipart(void);
349 multipart_decompose(const char *full_data, size_t full_data_len, struct
350 post_cmd *post_cmd);
353 clean_multipart(void);
355 /* preconditions.c */
357 preconditions_check(const struct configuration *conf);
359 /* sanitize-comment.c */
361 setup_sanitize_comment(const struct configuration *conf);
364 st_sanitize_text(struct post_cmd *pc, int *our_fault);
367 clean_sanitize_comment(void);
369 /* sanitize-file.c */
371 setup_sanitize_file(const struct configuration *conf);
374 sf_check_mime_type(const char *buf, size_t len, const struct
375 filetype **out_filetype);
378 sf_describe_file(const char *mimetype, const char *filepath,
379 char **out_description, size_t *out_len);
382 sf_install_files(size_t board_idx, const char *buf, size_t len, time_t *now,
383 const struct filetype *filetype, char **out_abs_path,
384 char **out_path,
385 size_t *out_path_len, char **out_thumb_path,
386 size_t *out_thumb_path_len,
387 int *our_fault);
390 clean_sanitize_file(void);
392 /* tripcodes.c */
394 setup_tripcodes(const struct configuration *conf);
397 tripcodes_calculate(struct post_cmd *p);
400 clean_tripcodes(void);
402 /* util.c */
403 char *
404 util_iso8601_from_time_t(time_t t);
407 util_normalize_ip(const char *in, char **out);
410 util_rebuild(struct configuration *conf);
412 /* write-thread.c */
414 setup_write_thread(const struct configuration *conf);
417 wt_remove_files(const char *system_full_path, size_t system_full_path_len, const
418 char *system_thumb_path, size_t system_thumb_path_len);
421 wt_remove_thread_page(size_t board_idx, uintmax_t thread_id);
424 wt_write_board(size_t board_idx, uintmax_t *thread_ids, size_t thread_ids_num,
425 size_t board_pages_num);
428 wt_write_post(struct prepared_post *p, FILE *f, uint_fast8_t is_op, uint_fast8_t
429 is_summary, uint_fast8_t is_recent, const char *board_name,
430 uintmax_t in_thread,
431 uintmax_t total_posts_in_thread, uint_fast8_t hr_before);
434 wt_write_recent_page(void);
437 wt_write_thread(size_t board_idx, uintmax_t thread);
439 void
440 clean_write_thread(void);