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
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.
32 const char *program_name
= "rb79-view-thread";
35 static uint_fast8_t use_color
= 1;
37 /* Show usage message, do not exit */
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 };
58 UNUSED(total_posts_in_thread
);
60 if (crypto_generichash(out_hash
, 9, (const unsigned char *) p
->ip
,
62 PERROR_MESSAGE("crypto_generichash_init");
67 fprintf(f
, "--------------------------------------\n");
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");
84 fprintf(f
, " %s%*s", p
->ip
, (int) (20 > p
->ip_len
? 20 - p
->ip_len
: 1),
86 fprintf(f
, "%s ", UBSAFES(time_str
));
87 fprintf(f
, "No. %ju\n", p
->id
);
90 fprintf(f
, " [ File: %s ]\n", p
->file_name
);
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
);
102 fprintf(f
, "--------------------------------------\n");
110 int main(int argc
, char **argv
)
113 struct configuration conf
= { 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
, "");
123 while ((opt
= getopt(argc
, argv
, "b:t:C")) != -1) {
128 ERROR_MESSAGE("-b already specified");
137 ERROR_MESSAGE("-t already specified");
158 conf
= (struct configuration
) {
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
)) {
189 if (board_idx
== (size_t) -1) {
190 ERROR_MESSAGE("No board \"%s\" known", b_arg
);
194 /* Interpret thread */
196 thread_id
= strtoull(t_arg
, 0, 0);
200 PERROR_MESSAGE("strtoull");
204 /* Set up a minimal part of the system */
205 if (setup_locks(&conf
) < 0) {
209 if (setup_dbs(&conf
) < 0) {
213 if (setup_write_thread(&conf
) < 0) {
217 ret
= db_writeback_posts_in_thread(board_idx
, thread_id
, stdout
,
222 clean_write_thread();