userscript: userscript hides links to itself
[rb-79.git] / rb79-delete-post.c
blob68cbbdff974a8409c66df5583c0b83d9f4ba1eac
1 /*
2 * Copyright (c) 2017, 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 usage(void)
37 size_t len = strlen(program_name);
39 printf("Usage: %s -b board-name\n", program_name);
40 printf(" %*s -p post-id\n", (int) len, "");
43 /* Do the thing */
44 int main(int argc, char **argv)
46 int ret = EINVAL;
47 struct configuration conf = { 0 };
48 int opt = 0;
49 const char *b_arg = 0;
50 size_t board_idx = 0;
51 const char *p_arg = 0;
52 uintmax_t post_id = 0;
53 uint_fast8_t actually_is_op = 0;
55 setlocale(LC_ALL, "");
57 /* Parse options */
58 while ((opt = getopt(argc, argv, "b:p:")) != -1) {
59 switch (opt) {
60 case 'b':
62 if (b_arg) {
63 ERROR_MESSAGE("-b already specified");
64 goto done;
67 b_arg = optarg;
68 break;
69 case 'p':
71 if (p_arg) {
72 ERROR_MESSAGE("-p already specified");
73 goto done;
76 p_arg = optarg;
77 break;
78 default:
79 usage();
80 goto done;
84 if (!b_arg ||
85 !p_arg) {
86 usage();
87 goto done;
90 conf = (struct configuration) {
91 /* */
92 .static_www_folder = static_www_folder, /* */
93 .work_path = work_path, /* */
94 .trip_salt = trip_salt, /* */
95 .trip_salt_len = strlen(trip_salt), /* */
96 .boards = boards, /* */
97 .boards_num = NUM_OF(boards), /* */
98 .max_form_data_size = max_form_data_size, /* */
99 .max_file_size = max_file_size, /* */
100 .max_text_len = max_text_len, /* */
101 .filetypes = filetypes, /* */
102 .filetypes_num = NUM_OF(filetypes), /* */
103 .file_description_prog = file_description_prog, /* */
104 .headers = headers, /* */
105 .headers_num = NUM_OF(headers), /* */
106 .challenges = challenges, /* */
107 .challenges_num = NUM_OF(challenges), /* */
108 .wordfilter_inputs = wordfilter_inputs, /* */
109 .wordfilter_inputs_num = NUM_OF(wordfilter_inputs), /* */
112 /* Interpret board */
113 board_idx = (size_t) -1;
115 for (size_t j = 0; j < conf.boards_num; ++j) {
116 if (!strcmp(conf.boards[j].name, b_arg)) {
117 board_idx = j;
121 if (board_idx == (size_t) -1) {
122 ERROR_MESSAGE("No board \"%s\" known", b_arg);
123 goto done;
126 /* Interpret post */
127 errno = 0;
128 post_id = strtoull(p_arg, 0, 0);
130 if (errno) {
131 ret = errno;
132 PERROR_MESSAGE("strtoull");
133 goto done;
136 /* Set up a minimal part of the system */
137 if (setup_locks(&conf) < 0) {
138 goto done;
141 if (setup_dbs(&conf) < 0) {
142 goto done;
145 if (setup_write_thread(&conf) < 0) {
146 goto done;
149 if (db_is_op(board_idx, post_id, &actually_is_op) < 0) {
150 goto done;
153 if (lock_acquire(board_idx) < 0) {
154 goto done;
157 if (actually_is_op) {
158 if (db_remove_thread_and_files(board_idx, post_id) < 0) {
159 goto done;
161 } else {
162 if (db_remove_post_and_files(board_idx, post_id) < 0) {
163 goto done;
167 lock_release(board_idx);
168 util_rebuild(&conf);
169 ret = 0;
170 done:
171 clean_locks();
172 clean_dbs();
173 clean_write_thread();
175 return ret;