build: do not clobber config.h on updates
[rb-79.git] / rb79-view-thread.c
blob9081bd49ed5af80cda0a553b9968f9bf304f9767
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-view-thread";
34 /* Options */
35 static uint_fast8_t use_color = 1;
37 /* Show usage message, do not exit */
38 void usage(void)
40 size_t len = strlen(program_name);
42 printf("Usage: %s -b board-name\n", program_name);
43 printf(" %*s -t thread-id\n", (int) len, "");
44 printf(" %*s [ -C ] # Do not use color\n", (int) len, "");
47 /* Print out enough of a post to give the mod information */
48 int print_post(struct prepared_post *p, FILE *f, uint_fast8_t is_op,
49 uint_fast8_t is_summary, const char *board_name, uintmax_t
50 total_posts_in_thread, uint_fast8_t hr_before)
52 char *time_str = util_iso8601_from_time_t(p->now);
53 unsigned char out_hash[9] = { 0 };
55 UNUSED(is_summary);
56 UNUSED(hr_before);
57 UNUSED(board_name);
58 UNUSED(total_posts_in_thread);
60 if (crypto_generichash(out_hash, 9, (const unsigned char *) p->ip,
61 p->ip_len, 0, 0)) {
62 PERROR_MESSAGE("crypto_generichash_init");
63 goto done;
66 if (is_op) {
67 fprintf(f, "--------------------------------------\n");
70 fprintf(f, " ");
72 if (use_color) {
73 fprintf(f, "\033[48;2;%d;%d;%dm ", 0xff & out_hash[0], 0xff &
74 out_hash[1], 0xff & out_hash[2]);
75 fprintf(f, "\033[48;2;%d;%d;%dm ", 0xff & out_hash[3], 0xff &
76 out_hash[4], 0xff & out_hash[5]);
77 fprintf(f, "\033[48;2;%d;%d;%dm ", 0xff & out_hash[6], 0xff &
78 out_hash[7], 0xff & out_hash[8]);
79 fprintf(f, "\033[0m");
80 } else {
81 fprintf(f, " ");
84 fprintf(f, " %s%*s", p->ip, (int) (20 > p->ip_len ? 20 - p->ip_len : 1),
85 " ");
86 fprintf(f, "%s ", UBSAFES(time_str));
87 fprintf(f, "No. %ju\n", p->id);
89 if (p->file_name) {
90 fprintf(f, " [ File: %s ]\n", p->file_name);
93 fprintf(f, " ");
95 if (p->comment_len > 40) {
96 fprintf(f, "%.*s...", 37, p->comment);
97 } else if (p->comment_len) {
98 fprintf(f, "%s", p->comment);
101 fprintf(f, "\n");
102 fprintf(f, "--------------------------------------\n");
103 done:
104 free(time_str);
106 return 0;
109 /* Do the thing */
110 int main(int argc, char **argv)
112 int ret = EINVAL;
113 struct configuration conf = { 0 };
114 int opt = 0;
115 const char *b_arg = 0;
116 size_t board_idx = 0;
117 const char *t_arg = 0;
118 uintmax_t thread_id = 0;
120 setlocale(LC_ALL, "");
122 /* Parse options */
123 while ((opt = getopt(argc, argv, "b:t:C")) != -1) {
124 switch (opt) {
125 case 'b':
127 if (b_arg) {
128 ERROR_MESSAGE("-b already specified");
129 goto done;
132 b_arg = optarg;
133 break;
134 case 't':
136 if (t_arg) {
137 ERROR_MESSAGE("-t already specified");
138 goto done;
141 t_arg = optarg;
142 break;
143 case 'C':
144 use_color = 0;
145 break;
146 default:
147 usage();
148 goto done;
152 if (!b_arg ||
153 !t_arg) {
154 usage();
155 goto done;
158 conf = (struct configuration) {
159 /* */
160 .static_www_folder = static_www_folder, /* */
161 .work_path = work_path, /* */
162 .trip_salt = trip_salt, /* */
163 .trip_salt_len = strlen(trip_salt), /* */
164 .boards = boards, /* */
165 .boards_num = NUM_OF(boards), /* */
166 .max_form_data_size = max_form_data_size, /* */
167 .max_file_size = max_file_size, /* */
168 .max_text_len = max_text_len, /* */
169 .filetypes = filetypes, /* */
170 .filetypes_num = NUM_OF(filetypes), /* */
171 .file_description_prog = file_description_prog, /* */
172 .headers = headers, /* */
173 .headers_num = NUM_OF(headers), /* */
174 .challenges = challenges, /* */
175 .challenges_num = NUM_OF(challenges), /* */
176 .wordfilter_inputs = wordfilter_inputs, /* */
177 .wordfilter_inputs_num = NUM_OF(wordfilter_inputs), /* */
180 /* Interpret board */
181 board_idx = (size_t) -1;
183 for (size_t j = 0; j < conf.boards_num; ++j) {
184 if (!strcmp(conf.boards[j].name, b_arg)) {
185 board_idx = j;
189 if (board_idx == (size_t) -1) {
190 ERROR_MESSAGE("No board \"%s\" known", b_arg);
191 goto done;
194 /* Interpret thread */
195 errno = 0;
196 thread_id = strtoull(t_arg, 0, 0);
198 if (errno) {
199 ret = errno;
200 PERROR_MESSAGE("strtoull");
201 goto done;
204 /* Set up a minimal part of the system */
205 if (setup_locks(&conf) < 0) {
206 goto done;
209 if (setup_dbs(&conf) < 0) {
210 goto done;
213 if (setup_write_thread(&conf) < 0) {
214 goto done;
217 ret = db_writeback_posts_in_thread(board_idx, thread_id, stdout,
218 print_post);
219 done:
220 clean_locks();
221 clean_dbs();
222 clean_write_thread();
224 return ret;