AmendHub

Download

jcs

/

subtext

/

sysop.c

 

(View History)

jcs   sysop: Plug small leak on retry Latest amendment: 654 on 2026-09-22

1 /*
2 * Copyright (c) 2022 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <string.h>
18 #include <ShutDown.h>
19
20 #include "subtext.h"
21 #include "ansi.h"
22 #include "binkp.h"
23 #include "board.h"
24 #include "logger.h"
25 #include "serial_local.h"
26 #include "session.h"
27 #include "sysop.h"
28 #include "telnet.h"
29
30 #define USERS_PER_PAGE 20
31
32 void sysop_edit_boards(struct session *s);
33 void sysop_edit_folders(struct session *s);
34 void sysop_edit_motd(struct session *s);
35 void sysop_edit_users(struct session *s);
36 size_t sysop_find_user_ids(size_t nall_user_ids,
37 unsigned long *all_user_ids, unsigned long **user_ids, size_t offset,
38 size_t limit);
39 void sysop_edit_user(struct session *s, unsigned long id);
40
41 void
42 sysop_menu(struct session *s)
43 {
44 static const struct session_menu_option opts[] = {
45 { 'm', "Mm", "Edit Message of the Day" },
46 { 'i', "Ii", "Trigger FTN Binkp poll" },
47 { 'h', "Hh", "Hang up modem" },
48 { 'b', "Bb", "Manage boards" },
49 { 'f', "Ff", "Manage file folders" },
50 { 'u', "Uu", "Manage users" },
51 { 's', "Ss", "Change BBS settings" },
52 { 'v', "Vv", "Reload views" },
53 { 'x', "Xx", "Quit Subtext" },
54 { 'r', "Rr", "Reboot system" },
55 { 'q', "Qq", "Return to main menu" },
56 { '?', "?", "List menu options" },
57 };
58 static const char prompt_help[] =
59 "M:MOTD I:Binkp H:Hang Up B:Boards F:Folders U:Users S:Settings V:Views";
60 char c;
61 bool show_help = true;
62 bool done = false;
63
64 if (!s->user || !s->user->is_sysop)
65 return;
66
67 session_logf(s, "Entered sysop menu");
68
69 while (!done && !s->ending) {
70 c = session_menu(s, "Sysop Menu", "Sysop", (char *)prompt_help,
71 opts, nitems(opts), show_help, NULL, NULL);
72 show_help = false;
73
74 switch (c) {
75 case 'b':
76 sysop_edit_boards(s);
77 break;
78 case 'f':
79 sysop_edit_folders(s);
80 break;
81 case 'h':
82 session_printf(s, "Hanging up modem...\r\n");
83 session_flush(s);
84 serial_hangup();
85 serial_init();
86 break;
87 case 'i':
88 session_printf(s, "Requesting binkp poll...\r\n");
89 session_flush(s);
90 binkp_next_poll = 0;
91 binkp_last_poll_error = false;
92 uthread_wakeup(periodic_job_thread);
93 break;
94 case 'm':
95 sysop_edit_motd(s);
96 break;
97 case 'r': {
98 IOParam pb;
99 long m;
100 short sc;
101
102 session_printf(s,
103 "Are you sure you want to reboot the system? [y/N] ");
104 session_flush(s);
105 sc = session_input_char(s);
106 if (sc != 'Y' && sc != 'y') {
107 session_output(s, "\r\n", 2);
108 session_flush(s);
109 break;
110 }
111 session_printf(s, "%c\r\n", sc == 'Y' ? 'Y' : 'y');
112 session_flush(s);
113
114 _exiting(0); /* call atexit list, from atexit.c */
115
116 /* flush the disk and give it a couple seconds */
117 memset(&pb, 0, sizeof(pb));
118 PBFlushVol(&pb, false);
119 Delay(120, &m);
120
121 ShutDwnStart();
122 /* we should never get here */
123 ExitToShell();
124 break;
125 }
126 case 's':
127 sysop_edit_settings(s);
128 break;
129 case 'u':
130 sysop_edit_users(s);
131 break;
132 case 'v':
133 session_printf(s, "Reloading views...\r\n");
134 session_flush(s);
135 db_cache_views(db);
136 break;
137 case 'x': {
138 short sc;
139
140 session_printf(s,
141 "Are you sure you want to quit Subtext? [y/N] ");
142 session_flush(s);
143 sc = session_input_char(s);
144 if (sc != 'Y' && sc != 'y') {
145 session_output(s, "\r\n", 2);
146 session_flush(s);
147 break;
148 }
149 session_printf(s, "%c\r\n", sc == 'Y' ? 'Y' : 'y');
150 session_flush(s);
151
152 ExitToShell();
153 break;
154 }
155 case '?':
156 show_help = true;
157 break;
158 default:
159 done = true;
160 break;
161 }
162 }
163 }
164
165 void
166 sysop_edit_settings(struct session *s)
167 {
168 struct config *new_config;
169 const struct struct_field *sf;
170 char *new_config_data, *cur_config_data;
171 short ret, n, nsval, osval;
172 unsigned long nlval, olval;
173 unsigned short reinits = 0;
174
175 if (!s->user || !s->user->is_sysop)
176 return;
177
178 ret = struct_editor(s, config_fields, nconfig_fields, NULL, 0,
179 &db->config, sizeof(struct config), (void *)&new_config,
180 "BBS Settings", "Sysop:Settings");
181 if (ret != 0 || new_config == NULL) {
182 session_printf(s, "No changes made\r\n");
183 return;
184 }
185
186 for (n = 0; n < nconfig_fields; n++) {
187 sf = &config_fields[n];
188
189 cur_config_data = (char *)&db->config + sf->off;
190 new_config_data = (char *)new_config + sf->off;
191
192 switch (sf->type) {
193 case CONFIG_TYPE_STRING:
194 case CONFIG_TYPE_PASSWORD:
195 if (memcmp(cur_config_data, new_config_data, sf->max) == 0)
196 goto next_field;
197
198 if (sf->type == CONFIG_TYPE_PASSWORD)
199 session_logf(s, "Changed BBS setting \"%s\" (password)",
200 sf->name);
201 else
202 session_logf(s, "Changed BBS setting \"%s\" from \"%s\" "
203 "to \"%s\"", sf->name, cur_config_data, new_config_data);
204
205 memcpy(cur_config_data, new_config_data, sf->max);
206 break;
207 case CONFIG_TYPE_SHORT:
208 osval = CHARS_TO_SHORT(cur_config_data[0], cur_config_data[1]);
209 nsval = CHARS_TO_SHORT(new_config_data[0], new_config_data[1]);
210
211 if (nsval == osval)
212 goto next_field;
213
214 session_logf(s, "Changed BBS setting \"%s\" from %d to %d",
215 sf->name, osval, nsval);
216
217 memcpy(cur_config_data, new_config_data, sizeof(short));
218 break;
219 case CONFIG_TYPE_LONG:
220 olval = CHARS_TO_LONG(cur_config_data[0], cur_config_data[1],
221 cur_config_data[2], cur_config_data[3]);
222 nlval = CHARS_TO_LONG(new_config_data[0], new_config_data[1],
223 new_config_data[2], new_config_data[3]);
224 if (olval == nlval)
225 goto next_field;
226
227 session_logf(s, "Changed BBS setting \"%s\" from %lu to %lu",
228 sf->name, olval, nlval);
229
230 memcpy(cur_config_data, new_config_data, sizeof(long));
231 break;
232 case CONFIG_TYPE_BOOLEAN:
233 if (cur_config_data[0] == new_config_data[0])
234 goto next_field;
235
236 session_logf(s, "Changed BBS setting \"%s\" from %s to %s",
237 sf->name, cur_config_data[0] ? "true" : "false",
238 new_config_data[0] ? "true" : "false");
239
240 memcpy(cur_config_data, new_config_data, 1);
241 break;
242 case CONFIG_TYPE_IP: {
243 char cur_ip_str[16], new_ip_str[16];
244
245 olval = CHARS_TO_LONG(cur_config_data[0], cur_config_data[1],
246 cur_config_data[2], cur_config_data[3]);
247 nlval = CHARS_TO_LONG(new_config_data[0], new_config_data[1],
248 new_config_data[2], new_config_data[3]);
249 if (olval == nlval)
250 goto next_field;
251
252 if (olval == 0)
253 snprintf(cur_ip_str, sizeof(cur_ip_str), "(None)");
254 else
255 long2ip(olval, cur_ip_str);
256
257 if (nlval == 0)
258 snprintf(new_ip_str, sizeof(new_ip_str), "(None)");
259 else
260 long2ip(nlval, new_ip_str);
261
262 session_logf(s, "Changed BBS setting \"%s\" from %s to %s",
263 sf->name, cur_ip_str, new_ip_str);
264
265 memcpy(cur_config_data, new_config_data, sizeof(long));
266 break;
267 }
268 }
269
270 reinits |= (1 << sf->reinit);
271
272 next_field:
273 continue;
274 }
275
276 db_config_save(db);
277 bile_flush(db->bile, true);
278
279 if (reinits & (1 << CONFIG_REQUIRES_SERIAL_REINIT))
280 serial_reinit();
281 if (reinits & (1 << CONFIG_REQUIRES_TELNET_REINIT))
282 telnet_reinit();
283 if (reinits & (1 << CONFIG_REQUIRES_BINKP_REINIT))
284 binkp_reinit();
285 if (reinits & (1 << CONFIG_REQUIRES_IPDB_REINIT)) {
286 if (db->ipdb)
287 ipdb_close(&db->ipdb);
288 if (db->config.ipdb_path[0])
289 db->ipdb = ipdb_open(db->config.ipdb_path);
290 }
291 if (reinits & (1 << CONFIG_REQUIRES_SYSLOG_REINIT))
292 syslog_reinit();
293
294 xfree(&new_config);
295
296 session_printf(s, "Successfully saved changes to BBS Settings\r\n");
297 }
298
299 void
300 sysop_edit_boards(struct session *s)
301 {
302 static const struct session_menu_option opts[] = {
303 { '#', "", "Enter board to edit" },
304 { 'l', "Ll", "List boards" },
305 { 'n', "Nn", "Create new board" },
306 { 'q', "QqXx", "Return to sysop menu" },
307 { '?', "?", "List menu options" },
308 };
309 static const struct session_menu_option edit_opts[] = {
310 { 'd', "Dd", "Delete board" },
311 { 'i', "Ii", "Re-index posts" },
312 };
313 static const char prompt_help[] =
314 "#:Edit Board L:List N:New Q:Return ?:Help";
315 char prompt[30];
316 struct board *board, *new_board;
317 struct bile *new_board_bile;
318 size_t n, size;
319 short bn, ret, sc;
320 char c, *data = NULL;
321 bool done, show_list, show_help;
322
323 show_list = true;
324 show_help = false;
325 done = false;
326
327 while (!done && !s->ending) {
328 if (show_list) {
329 session_printf(s, "{{B}}Boards{{/B}}\r\n");
330 session_printf(s, "%s# Name Description%s\r\n",
331 ansi(s, ANSI_BOLD, ANSI_END), ansi(s, ANSI_RESET, ANSI_END));
332 session_flush(s);
333
334 for (n = 0; n < db->nboards; n++) {
335 session_printf(s, "%2ld %- 10.10s %s\r\n",
336 n + 1,
337 db->boards[n].name,
338 db->boards[n].description);
339 }
340 session_flush(s);
341
342 show_list = false;
343 }
344
345 c = session_menu(s, "Board Editor", "Sysop:Boards",
346 (char *)prompt_help, opts, nitems(opts), show_help, "Board #",
347 &bn);
348 show_help = false;
349
350 switch (c) {
351 case 'l':
352 show_list = true;
353 break;
354 case 'n':
355 board = xmalloczero(sizeof(struct board));
356 if (board == NULL)
357 continue;
358 board->id = bile_next_id(db->bile, DB_BOARD_RTYPE);
359 board->restricted_posting = false;
360 board->restricted_viewing = false;
361 ret = struct_editor(s, board_fields, nboard_fields, NULL, 0,
362 board, sizeof(struct board), (void *)&new_board,
363 "New Board", "Sysop:Boards:New");
364 if (ret != 0) {
365 xfree(&board);
366 continue;
367 }
368
369 new_board_bile = db_board_create(db, new_board, false);
370 if (new_board_bile == NULL) {
371 xfree(&board);
372 continue;
373 }
374 bile_close(new_board_bile);
375 bile_flush(db->bile, true);
376 db_cache_boards(db);
377
378 session_logf(s, "Created new board %ld: %s", new_board->id,
379 new_board->name);
380 xfree(&board);
381 xfree(&new_board);
382 break;
383 case '#':
384 if (bn < 1 || bn > db->nboards) {
385 session_printf(s, "Invalid board ID\r\n");
386 session_flush(s);
387 break;
388 }
389
390 board = &db->boards[bn - 1];
391 snprintf(prompt, sizeof(prompt), "Sysop:Boards:%ld",
392 board->id);
393 edit_board:
394 ret = struct_editor(s, board_fields, nboard_fields, edit_opts,
395 nitems(edit_opts), board, sizeof(struct board),
396 (void *)&new_board, "Edit Board", prompt);
397 switch (ret) {
398 case 'i':
399 board_index_sorted_post_ids(board, NULL);
400 goto edit_board;
401 case 'd':
402 session_printf(s,
403 "Really delete board %s? [y/N] ", board->name);
404 session_flush(s);
405
406 sc = session_input_char(s);
407 if (s->ending)
408 return;
409 if (sc != 'Y' && sc != 'y') {
410 session_output(s, "\r\n", 2);
411 session_flush(s);
412 break;
413 }
414 session_printf(s, "%c\r\n", sc == 'Y' ? 'Y' : 'y');
415 session_flush(s);
416
417 session_logf(s, "Deleting board %ld (%s)!",
418 board->id, board->name);
419
420 db_board_delete(db, board);
421 bile_flush(db->bile, true);
422 db_cache_boards(db);
423 break;
424 case 0:
425 ret = bile_marshall_object(db->bile, board_object_fields,
426 nboard_object_fields, new_board, &data, &size);
427 if (ret != 0 || size == 0)
428 panic("board: failed to marshall object");
429
430 if (bile_write(db->bile, DB_BOARD_RTYPE, new_board->id,
431 data, size) != size)
432 panic("save of board failed: %d", bile_error(db->bile));
433 xfree(&data);
434 bile_flush(db->bile, true);
435
436 session_logf(s, "Saved changes to board %ld: %s",
437 new_board->id, new_board->name);
438 xfree(&new_board);
439
440 db_cache_boards(db);
441 break;
442 }
443 break;
444 case '?':
445 show_help = true;
446 break;
447 default:
448 done = true;
449 break;
450 }
451 }
452 }
453
454 void
455 sysop_edit_folders(struct session *s)
456 {
457 static const struct session_menu_option opts[] = {
458 { '#', "", "Enter folder to edit" },
459 { 'l', "Ll", "List folders" },
460 { 'n', "Nn", "Create new folder" },
461 { 'q', "QqXx", "Return to sysop menu" },
462 { '?', "?", "List menu options" },
463 };
464 static const struct session_menu_option edit_opts[] = {
465 { 'd', "Dd", "Delete folder" },
466 };
467 static const char prompt_help[] =
468 "#:Edit Folder L:List N:New Q:Return ?:Help";
469 char prompt[30];
470 struct folder *folder, *new_folder;
471 struct bile *new_folder_bile;
472 size_t n, size;
473 short ret, fn, sc;
474 char c, *data = NULL;
475 bool done, show_list, show_help;
476
477 show_list = true;
478 show_help = false;
479 done = false;
480
481 while (!done && !s->ending) {
482 if (show_list) {
483 session_printf(s, "{{B}}Folders{{/B}}\r\n");
484 session_printf(s, "%s# Name Description%s\r\n",
485 ansi(s, ANSI_BOLD, ANSI_END), ansi(s, ANSI_RESET, ANSI_END));
486 session_flush(s);
487
488 for (n = 0; n < db->nfolders; n++) {
489 session_printf(s, "%2ld %- 15.15s %s\r\n",
490 n + 1,
491 db->folders[n].name,
492 db->folders[n].description);
493 }
494 session_flush(s);
495
496 show_list = false;
497 }
498
499 c = session_menu(s, "Folder Editor", "Sysop:Folders",
500 (char *)prompt_help, opts, nitems(opts), show_help, "Folder #",
501 &fn);
502 show_help = false;
503
504 switch (c) {
505 case 'l':
506 show_list = true;
507 break;
508 case 'n':
509 folder = xmalloczero(sizeof(struct folder));
510 if (folder == NULL)
511 continue;
512 folder->id = bile_next_id(db->bile, DB_FOLDER_RTYPE);
513 ret = struct_editor(s, folder_fields, nfolder_fields, NULL, 0,
514 folder, sizeof(struct folder), (void *)&new_folder,
515 "New Folder", "Sysop:Folders:New");
516 if (ret != 0) {
517 xfree(&folder);
518 continue;
519 }
520
521 new_folder_bile = db_folder_create(db, new_folder, false);
522 if (new_folder_bile == NULL) {
523 xfree(&folder);
524 xfree(&new_folder);
525 continue;
526 }
527 bile_close(new_folder_bile);
528 bile_flush(db->bile, true);
529 db_cache_folders(db);
530
531 session_logf(s, "Created new folder %ld: %s", new_folder->id,
532 new_folder->name);
533 xfree(&folder);
534 xfree(&new_folder);
535 break;
536 case '#':
537 if (fn < 1 || fn > db->nfolders) {
538 session_printf(s, "Invalid folder ID\r\n");
539 session_flush(s);
540 break;
541 }
542
543 folder = &db->folders[fn - 1];
544 snprintf(prompt, sizeof(prompt), "Sysop:Folders:%ld",
545 folder->id);
546 edit_folder:
547 ret = struct_editor(s, folder_fields, nfolder_fields,
548 edit_opts, nitems(edit_opts), folder, sizeof(struct folder),
549 (void *)&new_folder, "Edit Folder", prompt);
550 switch (ret) {
551 case 'd':
552 session_printf(s,
553 "Really delete folder %s? [y/N] ", folder->name);
554 session_flush(s);
555
556 sc = session_input_char(s);
557 if (s->ending)
558 return;
559 if (sc != 'Y' && sc != 'y') {
560 session_output(s, "\r\n", 2);
561 session_flush(s);
562 break;
563 }
564 session_printf(s, "%c\r\n", sc == 'Y' ? 'Y' : 'y');
565 session_flush(s);
566
567 session_logf(s, "Deleting folder %ld (%s)!",
568 folder->id, folder->name);
569
570 db_folder_delete(db, folder);
571 bile_flush(db->bile, true);
572 db_cache_folders(db);
573 break;
574 case 0:
575 ret = bile_marshall_object(db->bile, folder_object_fields,
576 nfolder_object_fields, new_folder, &data, &size);
577 if (ret != 0 || size == 0)
578 panic("folder: failed to marshall object");
579
580 if (bile_write(db->bile, DB_FOLDER_RTYPE, new_folder->id,
581 data, size) != size)
582 panic("save of folder failed: %d", bile_error(db->bile));
583 bile_flush(db->bile, true);
584 xfree(&data);
585
586 session_logf(s, "Saved changes to folder %ld: %s",
587 new_folder->id, new_folder->name);
588 xfree(&new_folder);
589
590 db_cache_folders(db);
591 break;
592 }
593 break;
594 case '?':
595 show_help = true;
596 break;
597 default:
598 done = true;
599 break;
600 }
601 }
602 }
603
604 void
605 sysop_edit_motd(struct session *s)
606 {
607 static const struct session_menu_option opts[] = {
608 { 'n', "Nn", "Create new MOTD" },
609 { 's', "Ss", "Show latest MOTD" },
610 { 'q', "QqXx", "Return to sysop menu" },
611 { '?', "?", "List menu options" },
612 };
613 static const char prompt_help[] =
614 "N:New S:Show Q:Return ?:Help";
615 char c;
616 bool show_help = true;
617 bool done = false;
618
619 while (!done && !s->ending) {
620 c = session_menu(s, "MOTD Editor", "Sysop:MOTD", (char *)prompt_help,
621 opts, nitems(opts), show_help, NULL, NULL);
622 show_help = false;
623
624 switch (c) {
625 case 's': {
626 session_print_motd(s, true);
627 break;
628 }
629 case 'n': {
630 char *motd = NULL, *tmp = NULL;
631 struct bile_object *old_motd;
632 unsigned long new_id;
633
634 motd_write:
635 session_printf(s, "MOTD Text:\r\n");
636 session_flush(s);
637
638 tmp = session_field_input(s, 2048, s->terminal_columns - 1,
639 motd, true, 0);
640 if (tmp == NULL) {
641 if (motd)
642 xfree(&motd);
643 break;
644 }
645 session_output(s, "\r\n", 2);
646 session_flush(s);
647
648 rtrim(tmp, "\r\n\t ");
649
650 if (motd)
651 xfree(&motd);
652 motd = tmp;
653
654 session_printf(s, "\r\n{{B}}(S){{/B}}ave MOTD, "
655 "{{B}}(E){{/B}}dit again, or {{B}}(C){{/B}}ancel? ");
656 session_flush(s);
657
658 c = session_input_char(s);
659 if (c == 0 && s->obuflen > 0) {
660 s->node_funcs->output(s);
661 uthread_yield();
662 xfree(&motd);
663 break;
664 }
665
666 session_printf(s, "%c\r\n", c);
667 session_flush(s);
668
669 switch (c) {
670 case 's':
671 case 'S':
672 case 'y':
673 case '\n':
674 case '\r':
675 /* save */
676 session_printf(s, "Saving MOTD...");
677 session_flush(s);
678
679 new_id = bile_next_id(db->bile, DB_MOTD_RTYPE);
680 old_motd = bile_get_nth_of_type(db->bile, 0,
681 DB_MOTD_RTYPE);
682 if (old_motd) {
683 bile_delete(db->bile, DB_MOTD_RTYPE, old_motd->id,
684 BILE_DELETE_FLAG_ZERO | BILE_DELETE_FLAG_PURGE);
685 xfree(&old_motd);
686 }
687 bile_write(db->bile, DB_MOTD_RTYPE, new_id, motd,
688 strlen(motd) + 1);
689 bile_flush(db->bile, true);
690
691 session_printf(s, " saved!\r\n");
692 session_flush(s);
693 break;
694 case 'e':
695 case 'E':
696 goto motd_write;
697 break;
698 case 'c':
699 case 'C':
700 case CONTROL_C:
701 xfree(&motd);
702 break;
703 }
704 break;
705 }
706 case '?':
707 show_help = true;
708 break;
709 default:
710 done = true;
711 break;
712 }
713 }
714 }
715
716 void
717 sysop_edit_users(struct session *s)
718 {
719 static const struct session_menu_option opts[] = {
720 { '#', "#", "Edit user id [#]" },
721 { 'u', "Uu", "Edit user by username" },
722 { '<', "<", "Previous page of users" },
723 { 'l', "Ll", "List users" },
724 { '>', ">", "Next page of users" },
725 { 'q', "QqXx", "Return to sysop menu" },
726 { '?', "?", "List menu options" },
727 };
728 static const char prompt_help[] =
729 "#:Edit User <:Prev >:Next L:List Q:Return ?:Help";
730 unsigned long *all_user_ids = NULL, *user_ids = NULL;
731 struct user user, *fuser;
732 size_t pages, page, n, size, nall_user_ids, nuser_ids;
733 short suser_id, upp;
734 char c, time[30], *username;
735 bool show_help = false;
736 bool show_list = true;
737 bool done = false;
738 bool find_user_ids = true;
739
740 nall_user_ids = bile_sorted_ids_by_type(db->bile, DB_USER_RTYPE,
741 &all_user_ids);
742 page = 0;
743
744 while (!done && !s->ending) {
745 if (find_user_ids) {
746 upp = USERS_PER_PAGE;
747 if (s->terminal_lines < upp + 3)
748 upp = BOUND(upp, 5, s->terminal_lines - 3);
749 nuser_ids = sysop_find_user_ids(nall_user_ids, all_user_ids,
750 &user_ids, page * upp, upp);
751 /* ceil(nall_user_ids / upp) */
752 pages = (nall_user_ids + upp - 1) / upp;
753
754 if (page >= pages)
755 page = pages - 1;
756
757 find_user_ids = false;
758 }
759
760 if (show_list) {
761 session_printf(s, "{{B}}Users (Page %ld of %ld){{/B}}\r\n",
762 page + 1, pages);
763 session_printf(s, "%s # Username Active Sysop Last Login%s\r\n",
764 ansi(s, ANSI_BOLD, ANSI_END), ansi(s, ANSI_RESET, ANSI_END));
765 session_flush(s);
766
767 for (n = 0; n < nuser_ids; n++) {
768 size = bile_read(db->bile, DB_USER_RTYPE, user_ids[n],
769 (char *)&user, sizeof(struct user));
770 if (size == 0)
771 continue;
772 if (user.last_seen_at)
773 strftime(time, sizeof(time), "%Y-%m-%d %H:%M",
774 localtime(&user.last_seen_at));
775 else
776 snprintf(time, sizeof(time), "Never");
777 session_printf(s, "%3ld %-14s %c %c %s\r\n",
778 user.id,
779 user.username,
780 user.is_enabled ? 'Y' : '-',
781 user.is_sysop ? 'Y' : '-',
782 time);
783 }
784 session_flush(s);
785 show_list = false;
786 }
787
788 c = session_menu(s, "Edit Users", "Sysop:Users", (char *)prompt_help,
789 opts, nitems(opts), show_help, "User ID", &suser_id);
790 show_help = false;
791
792 handle_opt:
793 switch (c) {
794 case 'l':
795 show_list = true;
796 break;
797 case '>':
798 case '<':
799 if (c == '>' && page == pages - 1) {
800 session_printf(s, "You are at the last page of posts\r\n");
801 session_flush(s);
802 break;
803 }
804 if (c == '<' && page == 0) {
805 session_printf(s, "You are already at the first page\r\n");
806 session_flush(s);
807 break;
808 }
809 if (c == '>')
810 page++;
811 else
812 page--;
813 find_user_ids = true;
814 show_list = true;
815 break;
816 case '#':
817 sysop_edit_user(s, suser_id);
818 break;
819 case 'u':
820 session_printf(s, "Username: ");
821 session_flush(s);
822
823 username = session_field_input(s, 30, 30, NULL, false, 0);
824 session_output(s, "\r\n", 2);
825 session_flush(s);
826 if (username == NULL)
827 break;
828 if (username[0] == '\0') {
829 xfree(&username);
830 break;
831 }
832
833 fuser = user_find_by_username(username);
834 xfree(&username);
835 if (!fuser) {
836 session_printf(s, "Error: No such user.\r\n");
837 session_flush(s);
838 break;
839 }
840
841 sysop_edit_user(s, fuser->id);
842 xfree(&fuser);
843 break;
844 case '?':
845 show_help = true;
846 break;
847 default:
848 done = true;
849 break;
850 }
851 }
852
853 if (all_user_ids != NULL)
854 xfree(&all_user_ids);
855 }
856
857 size_t
858 sysop_find_user_ids(size_t nall_user_ids, unsigned long *all_user_ids,
859 unsigned long **user_ids, size_t offset, size_t limit)
860 {
861 size_t nuser_ids, n;
862
863 if (nall_user_ids < offset)
864 return 0;
865
866 nuser_ids = nall_user_ids - offset;
867 if (nuser_ids > limit)
868 nuser_ids = limit;
869
870 *user_ids = xcalloc(sizeof(unsigned long), nuser_ids);
871 if (*user_ids == NULL)
872 return 0;
873
874 for (n = 0; n < nuser_ids; n++) {
875 (*user_ids)[n] = all_user_ids[offset + n];
876 }
877
878 return nuser_ids;
879 }
880
881 void
882 sysop_edit_user(struct session *s, unsigned long id)
883 {
884 static const struct session_menu_option opts[] = {
885 { 'p', "Pp", "Change password" },
886 { 'e', "Ee", "Enable/disable account" },
887 { 'd', "Dd", "Delete account" },
888 { 's', "Ss", "Toggle sysop flag" },
889 { 'q', "QqXx", "Return to users menu" },
890 { '?', "?", "List menu options" },
891 };
892 static const char prompt_help[] =
893 "P:Password E:Enable/Disable D:Delete S:Sysop Q:Return ?:Help";
894 char title[50];
895 char prompt[25];
896 struct user user;
897 size_t size;
898 bool done = false;
899 bool show_help = true;
900 char c;
901 short cc;
902
903 size = bile_read(db->bile, DB_USER_RTYPE, id, (char *)&user,
904 sizeof(struct user));
905 if (!size) {
906 session_printf(s, "Error: Failed to find user %ld\r\n", id);
907 session_flush(s);
908 return;
909 }
910
911 snprintf(title, sizeof(title), "Edit User %s", user.username);
912 snprintf(prompt, sizeof(prompt), "Sysop:Users:%ld", user.id);
913
914 while (!done && !s->ending) {
915 c = session_menu(s, title, prompt, (char *)prompt_help, opts,
916 nitems(opts), show_help, NULL, NULL);
917 show_help = false;
918
919 handle_opt:
920 switch (c) {
921 case 'd':
922 session_printf(s, "Are you sure you want to permanently "
923 "delete {{B}}%s{{/B}}? [y/N] ", user.username);
924 session_flush(s);
925
926 cc = session_input_char(s);
927 if (cc == 'y' || cc == 'Y') {
928 session_printf(s, "%c\r\n", cc);
929 session_flush(s);
930 user_delete(&user);
931 session_printf(s, "\r\n{{B}}User deleted!{{/B}}\r\n");
932 done = true;
933 } else {
934 session_printf(s, "\r\nUser not deleted.\r\n");
935 session_flush(s);
936 }
937 break;
938 case 'e':
939 user.is_enabled = !user.is_enabled;
940 user_save(&user);
941 session_printf(s, "User %s is now %sabled.\r\n", user.username,
942 user.is_enabled ? "en" : "dis");
943 session_flush(s);
944 break;
945 case 'p':
946 user_change_password(s, &user);
947 break;
948 case 's':
949 if (strcmp(s->user->username, user.username) == 0) {
950 session_printf(s, "You cannot remove your own sysop "
951 "flag.\r\n");
952 session_flush(s);
953 break;
954 }
955 user.is_sysop = !user.is_sysop;
956 user_save(&user);
957 session_printf(s, "User %s is now %sa sysop.\r\n",
958 user.username, user.is_sysop ? "" : "no longer ");
959 session_flush(s);
960 break;
961 case '?':
962 show_help = true;
963 break;
964 default:
965 done = true;
966 break;
967 }
968 }
969 }