server: tie regex filters to db_insert_ban
[rb-79.git] / rb79-delete-post.c
blob16c08ebdb7906ae35d4d56969b67a179bbdf4695
1 /*
2 * Copyright (c) 2017-2020, 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.
18 #include <errno.h>
19 #include <locale.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include <sodium.h>
27 #include "macros.h"
28 #include "rb79.h"
30 #include "config.h"
32 const char *program_name = "rb79-delete-post";
34 /* Show usage message, do not exit */
35 void
36 usage(void)
38 size_t len = strlen(program_name);
40 printf("Usage: %s -b board-name\n", program_name);
41 printf(" %*s -p post-id\n", (int) len, "");
44 /* Do the thing */
45 int
46 main(int argc, char **argv)
48 int ret = EINVAL;
49 struct configuration conf = { 0 };
50 int opt = 0;
51 const char *b_arg = 0;
52 size_t board_idx = 0;
53 const char *p_arg = 0;
54 uintmax_t post_id = 0;
55 uint_fast8_t actually_is_op = 0;
57 setlocale(LC_ALL, "");
59 /* Parse options */
60 while ((opt = getopt(argc, argv, "b:p:")) != -1) {
61 switch (opt) {
62 case 'b':
64 if (b_arg) {
65 ERROR_MESSAGE("-b already specified");
66 goto done;
69 b_arg = optarg;
70 break;
71 case 'p':
73 if (p_arg) {
74 ERROR_MESSAGE("-p already specified");
75 goto done;
78 p_arg = optarg;
79 break;
80 default:
81 usage();
82 goto done;
86 if (!b_arg ||
87 !p_arg) {
88 usage();
89 goto done;
92 conf = (struct configuration) {
93 /* */
94 .static_www_folder = static_www_folder, /* */
95 .work_path = work_path, /* */
96 .temp_dir_template = temp_dir_template, /* */
97 .trip_salt = trip_salt, /* */
98 .trip_salt_len = strlen(trip_salt), /* */
99 .boards = boards, /* */
100 .boards_num = NUM_OF(boards), /* */
101 .max_form_data_size = max_form_data_size, /* */
102 .max_file_size = max_file_size, /* */
103 .max_text_len = max_text_len, /* */
104 .filetypes = filetypes, /* */
105 .filetypes_num = NUM_OF(filetypes), /* */
106 .file_description_prog = file_description_prog, /* */
107 .headers = headers, /* */
108 .headers_num = NUM_OF(headers), /* */
109 .challenges = challenges, /* */
110 .challenges_num = NUM_OF(challenges), /* */
111 .wordfilter_inputs = wordfilter_inputs, /* */
112 .wordfilter_inputs_num = NUM_OF(wordfilter_inputs), /* */
113 .forbidden_inputs = forbidden_inputs, /* */
114 .forbidden_inputs_num = NUM_OF(forbidden_inputs), /* */
117 /* Interpret board */
118 board_idx = (size_t) -1;
120 for (size_t j = 0; j < conf.boards_num; ++j) {
121 if (!strcmp(conf.boards[j].name, b_arg)) {
122 board_idx = j;
126 if (board_idx == (size_t) -1) {
127 ERROR_MESSAGE("No board \"%s\" known", b_arg);
128 goto done;
131 /* Interpret post */
132 errno = 0;
133 post_id = strtoull(p_arg, 0, 0);
135 if (errno) {
136 ret = errno;
137 PERROR_MESSAGE("strtoull");
138 goto done;
141 /* Set up a minimal part of the system */
142 if (setup_locks(&conf) < 0) {
143 goto done;
146 if (setup_dbs(&conf) < 0) {
147 goto done;
150 if (setup_write_thread(&conf) < 0) {
151 goto done;
154 if (db_is_op(board_idx, post_id, &actually_is_op) < 0) {
155 goto done;
158 if (lock_acquire(board_idx) < 0) {
159 goto done;
162 if (actually_is_op) {
163 if (db_remove_thread_and_files(board_idx, post_id) < 0) {
164 goto done;
166 } else {
167 if (db_remove_post_and_files(board_idx, post_id) < 0) {
168 goto done;
172 lock_release(board_idx);
173 util_rebuild(&conf);
174 ret = 0;
175 done:
176 clean_locks();
177 clean_dbs();
178 clean_write_thread();
180 return ret;