1 // Main source module for the client program.
3 // Copyright (c) 1987-2022 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure is subject to the GNU General Public License version 3.
7 #include "textclient.h"
9 #define IFEXPERT if (userflags&US_EXPERT)
10 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
11 #define IFAIDE if (axlevel>=AxAideU)
12 #define IFNAIDE if (axlevel<AxAideU)
14 int rordercmp(struct ctdlroomlisting *r1, struct ctdlroomlisting *r2);
15 march *marchptr = NULL;
16 extern char *moreprompt;
18 /* globals associated with the client program */
19 char temp[PATH_MAX]; /* Name of general-purpose temp file */
20 char temp2[PATH_MAX]; /* Name of general-purpose temp file */
21 char tempdir[PATH_MAX]; /* Name of general-purpose temp directory */
22 char printcmd[SIZ]; /* print command */
23 int editor_pid = (-1);
24 char fullname[USERNAME_SIZE];
28 char room_name[ROOMNAMELEN];
29 char *uglist[UGLISTLEN]; /* size of the ungoto list */
30 long uglistlsn[UGLISTLEN]; /* current read position for all the ungoto's. Not going to make any friends with this one. */
32 char is_mail = 0; /* nonzero when we're in a mail room */
33 char axlevel = AxDeleted; /* access level */
34 char is_room_aide = 0; /* boolean flag, 1 if room admin */
36 long usernum = 0L; /* user number */
37 time_t lastcall = 0L; /* Date/time of previous login */
39 long highest_msg_read; /* used for <A>bandon room cmd */
40 long maxmsgnum; /* used for <G>oto */
42 char rc_username[USERNAME_SIZE];
48 char curr_floor = 0; /* number of current floor */
49 char floorlist[128][SIZ]; /* names of floors */
50 int termn8 = 0; /* Set to nonzero to cause a logoff */
51 int secure; /* Set to nonzero when wire is encrypted */
53 extern char instant_msgs; /* instant messages waiting! */
54 extern int rc_ansi_color; /* ansi color value from citadel.rc */
55 extern int rc_sixel; /* sixel graphics value from citadel.rc */
56 extern int next_lazy_cmd;
58 CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
59 int enable_syslog = 0;
62 // Here is our 'clean up gracefully and exit' routine
63 void ctdl_logoff(char *file, int line, CtdlIPC * ipc, int code) {
66 if (editor_pid > 0) { /* kill the editor if it's running */
67 kill(editor_pid, SIGHUP);
70 // Free the ungoto list
71 for (lp = 0; lp < uglistsize; lp++) {
75 // Shut down the server connection ... but not if the logoff code is 3,
76 // because that means we're exiting because we already lost the server.
81 // now clean up various things
86 // Violently kill off any child processes if Citadel is the login shell.
88 kill(0 - getpgrp(), SIGTERM);
90 kill(0 - getpgrp(), SIGKILL);
92 color(ORIGINAL_PAIR); // Restore the old color settings
93 stty_ctdl(SB_RESTORE); // return the old terminal settings
94 exit(code); // exit with the proper exit code
100 * signal catching function for hangups...
102 void dropcarr(int signum) {
103 logoff(NULL, 3); /* No IPC when server's already gone! */
109 * catch SIGCONT to reset terminal modes when were are put back into the
112 void catch_sigcont(int signum) {
114 signal(SIGCONT, catch_sigcont);
118 /* general purpose routines */
121 void formout(CtdlIPC * ipc, char *name) {
122 int r; /* IPC return code */
126 r = CtdlIPCSystemMessage(ipc, name, &text, buf);
128 scr_printf("%s\n", buf);
132 fmout(screenwidth, NULL, text, NULL, 1);
138 void userlist(CtdlIPC * ipc, char *patn) {
140 char fl[64]; // a buffer this small will prevent it overrunning the column
143 int r; // IPC response code
144 char *listing = NULL;
146 r = CtdlIPCUserListing(ipc, patn, &listing, buf);
148 scr_printf("%s\n", buf);
152 scr_printf("User Name Last Login\n");
153 scr_printf("---------------------------------------------------------------- ----------\n");
155 while (!IsEmptyStr(listing)) {
156 extract_token(buf, listing, 0, '\n', sizeof buf);
157 remove_token(listing, 0, '\n');
159 if (sigcaught == 0) {
160 extract_token(fl, buf, 0, '|', sizeof fl);
161 if (pattern(fl, patn) >= 0) {
162 scr_printf("%-64s ", fl);
163 lc = extract_long(buf, 3);
164 localtime_r(&lc, &tmbuf);
165 scr_printf("%02d/%02d/%04d\n", (tmbuf.tm_mon + 1), tmbuf.tm_mday, (tmbuf.tm_year + 1900));
175 // grab assorted info about the user...
176 void load_user_info(char *params) {
177 extract_token(fullname, params, 0, '|', sizeof fullname);
178 axlevel = extract_int(params, 1);
179 userflags = extract_int(params, 4);
180 usernum = extract_long(params, 5);
181 lastcall = extract_long(params, 6);
185 // Remove a room from the march list. 'floornum' is ignored unless
186 // 'roomname' is set to _FLOOR_, in which case all rooms on the requested
187 // floor will be removed from the march list.
188 void remove_march(char *roomname, int floornum) {
189 struct march *mptr, *mptr2;
191 if (marchptr == NULL)
194 if ((!strcasecmp(marchptr->march_name, roomname))
195 || ((!strcasecmp(roomname, "_FLOOR_")) && (marchptr->march_floor == floornum))) {
196 mptr = marchptr->next;
202 for (mptr = marchptr; mptr != NULL; mptr = mptr->next) {
204 if ((!strcasecmp(mptr->march_name, roomname))
205 || ((!strcasecmp(roomname, "_FLOOR_"))
206 && (mptr->march_floor == floornum))) {
208 mptr2->next = mptr->next;
219 // Locate the room on the march list which we most want to go to. Each room
220 // is measured given a "weight" of preference based on various factors.
221 char *pop_march(int desired_floor, struct march *_march) {
222 static char TheRoom[ROOMNAMELEN];
225 struct march *mptr = NULL;
227 strcpy(TheRoom, "_BASEROOM_");
231 for (mptr = _march; mptr != NULL; mptr = mptr->next) {
233 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
234 weight = weight + 10000;
235 if (mptr->march_floor == desired_floor)
236 weight = weight + 5000;
238 weight = weight + ((128 - (mptr->march_floor)) * 128);
239 weight = weight + (128 - (mptr->march_order));
241 if (weight > TheWeight) {
243 strcpy(TheRoom, mptr->march_name);
250 // jump directly to a room
251 void dotgoto(CtdlIPC * ipc, char *towhere, int display_name, int fromungoto) {
252 char aaa[SIZ], bbb[SIZ];
254 int partial_match, best_match;
256 int ugpos = uglistsize;
257 int r; /* IPC result code */
258 struct ctdlipcroom *room = NULL;
261 /* store ungoto information */
262 if (fromungoto == 0) {
263 /* sloppy slide them all down, hey it's the client, who cares. :-) */
264 if (uglistsize >= (UGLISTLEN - 1)) {
267 for (lp = 0; lp < (UGLISTLEN - 1); lp++) {
268 uglist[lp] = uglist[lp + 1];
269 uglistlsn[lp] = uglistlsn[lp + 1];
277 uglist[ugpos] = malloc(strlen(room_name) + 1);
278 strcpy(uglist[ugpos], room_name);
279 uglistlsn[ugpos] = ls;
282 /* first try an exact match */
283 r = CtdlIPCGotoRoom(ipc, towhere, "", &room, aaa);
285 newprompt("Enter room password: ", bbb, 9);
286 r = CtdlIPCGotoRoom(ipc, towhere, bbb, &room, aaa);
288 scr_printf("Wrong password.\n");
294 * If a match is not found, try a partial match.
295 * Partial matches anywhere in the string carry a weight of 1,
296 * left-aligned matches carry a weight of 2. Pick the room that
297 * has the highest-weighted match. Do not match on forgotten
301 struct march *march = NULL;
306 r = CtdlIPCKnownRooms(ipc, SubscribedRooms, AllFloors, &march, aaa);
308 /* Run the roomlist; free the data as we go */
309 struct march *mp = march; /* Current */
313 if (pattern(mp->march_name, towhere) >= 0) {
316 if (!strncasecmp(mp->march_name, towhere, strlen(towhere))) {
319 if (partial_match > best_match) {
320 strcpy(bbb, mp->march_name);
321 best_match = partial_match;
323 /* Both pointers are NULL at end of list */
330 if (IsEmptyStr(bbb)) {
331 scr_printf("No room '%s'.\n", towhere);
334 r = CtdlIPCGotoRoom(ipc, bbb, "", &room, aaa);
336 if (r / 100 != 1 && r / 100 != 2) {
337 scr_printf("%s\n", aaa);
340 strncpy(room_name, room->RRname, ROOMNAMELEN);
341 room_flags = room->RRflags;
342 room_flags2 = room->RRflags2;
343 from_floor = curr_floor;
344 curr_floor = room->RRfloor;
346 // Determine, based on the room's default view, whether an <E>nter message command will be valid here.
347 switch (room->RRdefaultview) {
350 entmsg_ok = ENTMSG_OK_YES;
353 entmsg_ok = ENTMSG_OK_BLOG;
356 entmsg_ok = ENTMSG_OK_NO;
360 remove_march(room_name, 0);
361 if (!strcasecmp(towhere, "_BASEROOM_"))
362 remove_march(towhere, 0);
364 next_lazy_cmd = 5; /* Don't read new if no new msgs */
365 if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
366 if (floorlist[(int) curr_floor][0] == 0)
368 scr_printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
370 if (display_name == 1) {
372 scr_printf("%s ", room_name);
376 if (display_name != 2) {
377 color(BRIGHT_YELLOW);
378 scr_printf("%d ", room->RRunread);
380 scr_printf("new of ");
381 color(BRIGHT_YELLOW);
382 scr_printf("%d ", room->RRtotal);
384 scr_printf("messages.\n");
386 highest_msg_read = room->RRlastread;
387 maxmsgnum = room->RRhighest;
388 is_mail = room->RRismailbox;
389 is_room_aide = room->RRaide;
390 ls = room->RRlastread;
392 /* read info file if necessary */
393 if (room->RRinfoupdated > 0)
396 /* check for newly arrived mail if we can FIXME use BIFF command for this
397 if (newmailcount > 0) {
399 if (newmailcount == 1) {
400 scr_printf("*** A new mail message has arrived.\n");
403 scr_printf("*** %d new mail messages have arrived.\n", newmailcount);
406 if (!IsEmptyStr(rc_gotmail_cmd)) {
407 rv = system(rc_gotmail_cmd);
409 scr_printf("*** failed to check for mail calling %s Reason %d.\n", rc_gotmail_cmd, rv);
417 snprintf(&status_line[1], screenwidth - 1, "%s | %s | %s | %s | %d new mail |", (secure ? "Encrypted" : "Unencrypted"), ipc->ServInfo.humannode, ipc->ServInfo.site_location, room_name, 0); // FIXME use BIFF
421 /* Goto next room having unread messages.
422 * We want to skip over rooms that the user has already been to, and take the
423 * user back to the lobby when done. The room we end up in is placed in
424 * newroom - which is set to 0 (the lobby) initially.
426 void gotonext(CtdlIPC * ipc) {
428 struct march *mptr, *mptr2;
429 char next_room[ROOMNAMELEN];
431 // Check to see if the march-mode list is already allocated.
432 // If it is, pop the first room off the list and go there.
433 if (marchptr == NULL) {
434 CtdlIPCKnownRooms(ipc, SubscribedRoomsWithNewMessages, AllFloors, &marchptr, buf);
436 // Add _BASEROOM_ to the end of the march list, so the user will end up
437 // in the system base room (usually the Lobby>) at the end of the loop.
438 mptr = (struct march *) malloc(sizeof(struct march));
440 mptr->march_order = 0;
441 mptr->march_floor = 0;
442 strcpy(mptr->march_name, "_BASEROOM_");
443 if (marchptr == NULL) {
448 while (mptr2->next != NULL)
453 // ...and remove the room we're currently in, so a <G>oto doesn't make us walk around in circles
454 remove_march(room_name, 0);
456 if (marchptr != NULL) {
457 strcpy(next_room, pop_march(curr_floor, marchptr));
460 strcpy(next_room, "_BASEROOM_");
462 remove_march(next_room, 0);
463 dotgoto(ipc, next_room, 1, 0);
467 // forget all rooms on a given floor
468 void forget_all_rooms_on(CtdlIPC * ipc, int ffloor) {
470 struct march *flist = NULL;
471 struct march *fptr = NULL;
472 struct ctdlipcroom *room = NULL;
473 int r; // IPC response code
475 scr_printf("Forgetting all rooms on %s...\n", &floorlist[ffloor][0]);
476 remove_march("_FLOOR_", ffloor);
477 r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, ffloor, &flist, buf);
479 scr_printf("Error %d: %s\n", r, buf);
483 r = CtdlIPCGotoRoom(ipc, flist->march_name, "", &room, buf);
485 r = CtdlIPCForgetRoom(ipc, buf);
487 scr_printf("Error %d: %s\n", r, buf);
500 // routine called by gotofloor() to move to a new room on a new floor
501 void gf_toroom(CtdlIPC * ipc, char *towhere, int mode) {
502 int floor_being_left;
504 floor_being_left = curr_floor;
506 if (mode == GF_GOTO) { /* <;G>oto mode */
508 dotgoto(ipc, towhere, 1, 0);
510 else if (mode == GF_SKIP) { /* <;S>kip mode */
511 dotgoto(ipc, towhere, 1, 0);
512 remove_march("_FLOOR_", floor_being_left);
514 else if (mode == GF_ZAP) { /* <;Z>ap mode */
515 dotgoto(ipc, towhere, 1, 0);
516 remove_march("_FLOOR_", floor_being_left);
517 forget_all_rooms_on(ipc, floor_being_left);
523 void gotofloor(CtdlIPC * ipc, char *towhere, int mode) {
525 int r; /* IPC response code */
527 char buf[SIZ], targ[SIZ];
529 if (floorlist[0][0] == 0)
532 for (a = 0; a < 128; ++a)
533 if (!strcasecmp(&floorlist[a][0], towhere))
537 for (a = 0; a < 128; ++a) {
538 if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
544 for (a = 0; a < 128; ++a)
545 if (pattern(towhere, &floorlist[a][0]) > 0)
549 scr_printf("No floor '%s'.\n", towhere);
552 for (mptr = marchptr; mptr != NULL; mptr = mptr->next) {
553 if ((mptr->march_floor) == tofloor) {
554 gf_toroom(ipc, mptr->march_name, mode);
559 /* Find first known room on the floor */
563 r = CtdlIPCKnownRooms(ipc, SubscribedRooms, tofloor, &mptr, buf);
565 struct march *tmp = mptr;
567 /*. . . according to room order */
569 strcpy(targ, pop_march(tofloor, mptr));
576 if (!IsEmptyStr(targ)) {
577 gf_toroom(ipc, targ, mode);
581 /* No known rooms on the floor; unzap the first one then */
585 r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, tofloor, &mptr, buf);
587 struct march *tmp = mptr;
589 /*. . . according to room order */
591 strcpy(targ, pop_march(tofloor, mptr));
598 if (!IsEmptyStr(targ)) {
599 gf_toroom(ipc, targ, mode);
602 scr_printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
608 * Indexing mechanism for a room list, called by gotoroomstep()
610 void room_tree_list_query(struct ctdlroomlisting *rp, char *findrmname, int findrmslot, char *rmname, int *rmslot, int *rmtotal) {
611 char roomname[ROOMNAMELEN];
612 static int cur_rmslot = 0;
619 if (rp->lnext != NULL) {
620 room_tree_list_query(rp->lnext, findrmname, findrmslot, rmname, rmslot, rmtotal);
623 if (sigcaught == 0) {
624 strcpy(roomname, rp->rlname);
626 if (rmname != NULL) {
627 if (cur_rmslot == findrmslot) {
628 strcpy(rmname, roomname);
631 if (rmslot != NULL) {
632 if (!strcmp(roomname, findrmname)) {
633 *rmslot = cur_rmslot;
639 if (rp->rnext != NULL) {
640 room_tree_list_query(rp->rnext, findrmname, findrmslot, rmname, rmslot, rmtotal);
643 if ((rmname == NULL) && (rmslot == NULL))
646 if (rmtotal != NULL) {
647 *rmtotal = cur_rmslot;
652 * step through rooms on current floor
654 void gotoroomstep(CtdlIPC * ipc, int direction, int mode) {
655 struct march *listing = NULL;
657 int r; /* IPC response code */
659 struct ctdlroomlisting *rl = NULL;
660 struct ctdlroomlisting *rp;
661 struct ctdlroomlisting *rs;
663 char rmname[ROOMNAMELEN];
667 /* Ask the server for a room list */
668 r = CtdlIPCKnownRooms(ipc, SubscribedRooms, (-1), &listing, buf);
675 for (mptr = listing; mptr != NULL; mptr = mptr->next) {
678 if (floor_mode && (mptr->march_floor != curr_floor))
682 rp = malloc(sizeof(struct ctdlroomlisting));
683 strncpy(rp->rlname, mptr->march_name, ROOMNAMELEN);
684 rp->rlflags = mptr->march_flags;
685 rp->rlfloor = mptr->march_floor;
686 rp->rlorder = mptr->march_order;
696 if (rordercmp(rp, rs) < 0) {
697 if (rs->lnext == NULL) {
706 if (rs->rnext == NULL) {
719 /* Find position of current room */
720 room_tree_list_query(NULL, NULL, 0, NULL, NULL, NULL);
721 room_tree_list_query(rl, room_name, 0, NULL, &rmslot, &rmtotal);
723 if (direction == 0) { /* Previous room */
724 /* If we're at the first room, wrap to the last room */
726 rmslot = rmtotal - 1;
732 else { /* Next room */
733 /* If we're at the last room, wrap to the first room */
734 if (rmslot == rmtotal - 1) {
742 /* Get name of next/previous room */
743 room_tree_list_query(NULL, NULL, 0, NULL, NULL, NULL);
744 room_tree_list_query(rl, NULL, rmslot, rmname, NULL, NULL);
747 room_tree_list_query(rl, NULL, 0, NULL, NULL, NULL);
749 if (mode == 0) { /* not skipping */
753 /* Free the room list */
755 mptr = listing->next;
760 dotgoto(ipc, rmname, 1, 0);
765 * step through floors on system
767 void gotofloorstep(CtdlIPC * ipc, int direction, int mode) {
770 if (floorlist[0][0] == 0)
775 if (direction == 0) { /* Previous floor */
777 tofloor = curr_floor - 1;
781 while (!floorlist[tofloor][0])
784 else { /* Next floor */
785 if (curr_floor < 127)
786 tofloor = curr_floor + 1;
790 while (!floorlist[tofloor][0] && tofloor < 127)
792 if (!floorlist[tofloor][0])
795 /* ;g works when not in floor mode so . . . */
797 scr_printf("(%s)\n", floorlist[tofloor]);
800 gotofloor(ipc, floorlist[tofloor], mode);
801 if (curr_floor != tofloor) { /* gotofloor failed */
802 curr_floor = tofloor;
803 goto empty_keep_going;
809 * Display user 'preferences'.
811 extern int rc_prompt_control;
812 void read_config(CtdlIPC * ipc) {
815 int r; /* IPC response code */
816 char _fullname[USERNAME_SIZE];
820 struct ctdluser *user = NULL;
822 /* get misc user info */
823 r = CtdlIPCGetBio(ipc, fullname, &resp, buf);
825 scr_printf("%s\n", buf);
828 extract_token(_fullname, buf, 1, '|', sizeof fullname);
829 _usernum = extract_long(buf, 2);
830 _axlevel = extract_int(buf, 3);
831 _lastcall = extract_long(buf, 4);
835 /* get preferences */
836 r = CtdlIPCGetConfig(ipc, &user, buf);
838 scr_printf("%s\n", buf);
843 /* show misc user info */
844 if (_lastcall > 0L) {
845 scr_printf("Last login: %s", asctime(localtime(&_lastcall)));
849 /* show preferences */
850 scr_printf("Are you an experienced Citadel user: ");
852 scr_printf("%s\n", (user->flags & US_EXPERT) ? "Yes" : "No");
854 scr_printf("Print last old message on New message request: ");
856 scr_printf("%s\n", (user->flags & US_LASTOLD) ? "Yes" : "No");
858 scr_printf("Prompt after each message: ");
860 scr_printf("%s\n", (!(user->flags & US_NOPROMPT)) ? "Yes" : "No");
862 if ((user->flags & US_NOPROMPT) == 0) {
863 scr_printf("Use 'disappearing' prompts: ");
865 scr_printf("%s\n", (user->flags & US_DISAPPEAR) ? "Yes" : "No");
868 scr_printf("Pause after each screenful of text: ");
870 scr_printf("%s\n", (user->flags & US_PAGINATOR) ? "Yes" : "No");
872 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR)) {
873 scr_printf("<N>ext and <S>top work at paginator prompt: ");
875 scr_printf("%s\n", (user->flags & US_PROMPTCTL) ? "Yes" : "No");
878 if (rc_floor_mode == RC_DEFAULT) {
879 scr_printf("View rooms by floor: ");
881 scr_printf("%s\n", (user->flags & US_FLOORS) ? "Yes" : "No");
884 if (rc_ansi_color == 3) {
885 scr_printf("Enable color support: ");
887 scr_printf("%s\n", (user->flags & US_COLOR) ? "Yes" : "No");
890 scr_printf("Be unlisted in userlog: ");
892 scr_printf("%s\n", (user->flags & US_UNLISTED) ? "Yes" : "No");
894 if (!IsEmptyStr(editor_path)) {
895 scr_printf("Always enter messages with the full-screen editor: ");
897 scr_printf("%s\n", (user->flags & US_EXTEDIT) ? "Yes" : "No");
905 * Display system statistics.
907 void system_info(CtdlIPC * ipc) {
911 int mrtg_users, mrtg_active_users;
912 char mrtg_server_uptime[40];
915 /* get #users, #active & server uptime */
916 CtdlIPCGenericCommand(ipc, "MRTG|users", NULL, 0, &resp, &bytes, buf);
917 mrtg_users = extract_int(resp, 0);
918 remove_token(resp, 0, '\n');
919 mrtg_active_users = extract_int(resp, 0);
920 remove_token(resp, 0, '\n');
921 extract_token(mrtg_server_uptime, resp, 0, '\n', sizeof mrtg_server_uptime);
925 /* get high message# */
926 CtdlIPCGenericCommand(ipc, "MRTG|messages", NULL, 0, &resp, &bytes, buf);
927 mrtg_himessage = extract_long(resp, 0);
931 /* refresh server info just in case */
932 CtdlIPCServerInfo(ipc, buf);
934 scr_printf("You are connected to %s (%s) @%s\n", ipc->ServInfo.nodename, ipc->ServInfo.humannode, ipc->ServInfo.fqdn);
935 scr_printf("running %s with text client v%.2f,\n", ipc->ServInfo.software, (float) CLIENT_VERSION / 100);
936 scr_printf("server build %s,\n", ipc->ServInfo.svn_revision, (float) CLIENT_VERSION / 100);
937 scr_printf("and located in %s.\n", ipc->ServInfo.site_location);
938 scr_printf("Connected users %d / Active users %d / Highest message #%ld\n", mrtg_users, mrtg_active_users, mrtg_himessage);
939 scr_printf("Server uptime: %s\n", mrtg_server_uptime);
940 scr_printf("Your system administrator is %s.\n", ipc->ServInfo.sysadm);
945 * forget all rooms on current floor
947 void forget_this_floor(CtdlIPC * ipc) {
948 if (curr_floor == 0) {
949 scr_printf("Can't forget this floor.\n");
952 if (floorlist[0][0] == 0) {
955 scr_printf("Are you sure you want to forget all rooms on %s? ", &floorlist[(int) curr_floor][0]);
960 gf_toroom(ipc, "_BASEROOM_", GF_ZAP);
965 * set floor mode depending on client, server, and user settings
967 void set_floor_mode(CtdlIPC * ipc) {
968 if (ipc->ServInfo.ok_floors == 0) {
969 floor_mode = 0; /* Don't use floors if the server */
971 /* doesn't support them! */
973 if (rc_floor_mode == RC_NO) { /* never use floors */
976 if (rc_floor_mode == RC_YES) { /* always use floors */
979 if (rc_floor_mode == RC_DEFAULT) { /* user choice */
980 floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
986 * Set or change the user's password
988 int set_password(CtdlIPC * ipc) {
993 if (!IsEmptyStr(rc_password)) {
994 strcpy(pass1, rc_password);
995 strcpy(pass2, rc_password);
998 IFNEXPERT formout(ipc, "changepw");
999 newprompt("Enter a new password: ", pass1, -19);
1000 newprompt("Enter it again to confirm: ", pass2, -19);
1004 if (!strcasecmp(pass1, pass2)) {
1005 CtdlIPCChangePassword(ipc, pass1, buf);
1006 scr_printf("%s\n", buf);
1007 offer_to_remember_password(ipc, hostbuf, portbuf, fullname, pass1);
1011 scr_printf("*** They don't match... try again.\n");
1018 * get info about the server we've connected to
1020 void get_serv_info(CtdlIPC * ipc, char *supplied_hostname) {
1023 CtdlIPCServerInfo(ipc, buf);
1024 moreprompt = ipc->ServInfo.moreprompt;
1026 /* be nice and identify ourself to the server */
1027 CtdlIPCIdentifySoftware(ipc, CLIENT_TYPE, 0, CLIENT_VERSION,
1028 (ipc->isLocal ? "local" : "Citadel text mode client"), (supplied_hostname) ? supplied_hostname :
1029 /* Look up the , in the bible if you're confused */
1030 (locate_host(ipc, buf), buf), buf);
1032 // Indicate to the server that we prefer to decode Base64 and
1033 // quoted-printable on the client side.
1034 if ((CtdlIPCSpecifyPreferredFormats(ipc, buf, "dont_decode") / 100) != 2) {
1035 scr_printf("Error %s:%d", __FILE__, __LINE__);
1040 * Tell the server what our preferred content formats are.
1042 * Originally we preferred HTML over plain text because we can format
1043 * it to the reader's screen width, but since our HTML-to-text parser
1044 * isn't really all that great, it's probably better to just go with
1045 * the plain text when we have it available.
1047 if ((CtdlIPCSpecifyPreferredFormats(ipc, buf, "text/plain|text/html|text/x-markdown") / 100) != 2) {
1048 scr_printf("Error %s:%d", __FILE__, __LINE__);
1055 * Session username compare function for SortOnlineUsers()
1057 int rwho_username_cmp(const void *rec1, const void *rec2) {
1060 u1 = strchr(rec1, '|');
1061 u2 = strchr(rec2, '|');
1063 return strcasecmp((u1 ? ++u1 : ""), (u2 ? ++u2 : ""));
1068 * Idle time compare function for SortOnlineUsers()
1070 int idlecmp(const void *rec1, const void *rec2) {
1073 i1 = extract_long(rec1, 5);
1074 i2 = extract_long(rec2, 5);
1085 * Sort the list of online users by idle time.
1086 * This function frees the supplied buffer, and returns a new one
1087 * to the caller. The caller is responsible for freeing the returned buffer.
1089 * If 'condense' is nonzero, multiple sessions for the same user will be
1090 * combined into one for brevity.
1092 char *SortOnlineUsers(char *listing, int condense) {
1099 rows = num_tokens(listing, '\n');
1100 sortbuf = malloc(rows * SIZ);
1101 if (sortbuf == NULL)
1103 retbuf = malloc(rows * SIZ);
1104 if (retbuf == NULL) {
1109 /* Copy the list into a fixed-record-size array for sorting */
1110 for (i = 0; i < rows; ++i) {
1111 memset(buf, 0, SIZ);
1112 extract_token(buf, listing, i, '\n', sizeof buf);
1113 memcpy(&sortbuf[i * SIZ], buf, (size_t) SIZ);
1116 /* Sort by idle time */
1117 qsort(sortbuf, rows, SIZ, idlecmp);
1119 /* Combine multiple sessions for the same user */
1121 qsort(sortbuf, rows, SIZ, rwho_username_cmp);
1123 for (i = 1; i < rows; ++i)
1125 char u1[USERNAME_SIZE];
1126 char u2[USERNAME_SIZE];
1127 extract_token(u1, &sortbuf[(i - 1) * SIZ], 1, '|', sizeof u1);
1128 extract_token(u2, &sortbuf[i * SIZ], 1, '|', sizeof u2);
1129 if (!strcasecmp(u1, u2)) {
1130 memcpy(&sortbuf[i * SIZ], &sortbuf[(i + 1) * SIZ], (rows - i - 1) * SIZ);
1136 qsort(sortbuf, rows, SIZ, idlecmp); /* idle sort again */
1139 /* Copy back to a \n delimited list */
1141 for (i = 0; i < rows; ++i) {
1142 if (!IsEmptyStr(&sortbuf[i * SIZ])) {
1143 strcat(retbuf, &sortbuf[i * SIZ]);
1145 strcat(retbuf, "\n");
1155 * Display list of users currently logged on to the server
1157 void who_is_online(CtdlIPC * ipc, int longlist) {
1158 char buf[SIZ], username[SIZ], roomname[SIZ], fromhost[SIZ];
1160 char actual_user[SIZ], actual_room[SIZ], actual_host[SIZ];
1161 char clientsoft[SIZ];
1163 time_t idletime, idlehours, idlemins, idlesecs;
1164 int last_session = (-1);
1166 char *listing = NULL;
1167 int r; /* IPC response code */
1169 if (longlist == 2) {
1175 color(BRIGHT_WHITE);
1176 scr_printf(" User Name Room ");
1177 if (screenwidth >= 80)
1178 scr_printf(" Idle From host");
1181 scr_printf(" ------------------------- --------------------");
1182 if (screenwidth >= 80)
1183 scr_printf(" ---- ------------------------");
1186 r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
1187 listing = SortOnlineUsers(listing, (!longlist));
1189 while (!IsEmptyStr(listing)) {
1192 /* Get another line */
1193 extract_token(buf, listing, 0, '\n', sizeof buf);
1194 remove_token(listing, 0, '\n');
1196 extract_token(username, buf, 1, '|', sizeof username);
1197 extract_token(roomname, buf, 2, '|', sizeof roomname);
1198 extract_token(fromhost, buf, 3, '|', sizeof fromhost);
1199 extract_token(clientsoft, buf, 4, '|', sizeof clientsoft);
1200 extract_token(flags, buf, 7, '|', sizeof flags);
1202 idletime = timenow - extract_long(buf, 5);
1203 idlehours = idletime / 3600;
1204 idlemins = (idletime - (idlehours * 3600)) / 60;
1205 idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
1207 if (idletime > rc_idle_threshold) {
1214 extract_token(actual_user, buf, 8, '|', sizeof actual_user);
1215 extract_token(actual_room, buf, 9, '|', sizeof actual_room);
1216 extract_token(actual_host, buf, 10, '|', sizeof actual_host);
1218 scr_printf(" Flags: %s\n", flags);
1219 scr_printf("Session: %d\n", extract_int(buf, 0));
1220 scr_printf(" Name: %s\n", username);
1221 scr_printf("In room: %s\n", roomname);
1222 scr_printf(" Host: %s\n", fromhost);
1223 scr_printf(" Client: %s\n", clientsoft);
1224 scr_printf(" Idle: %ld:%02ld:%02ld\n", (long) idlehours, (long) idlemins, (long) idlesecs);
1226 if ((!IsEmptyStr(actual_user) && !IsEmptyStr(actual_room) && !IsEmptyStr(actual_host))) {
1227 scr_printf("(really ");
1228 if (!IsEmptyStr(actual_user))
1229 scr_printf("<%s> ", actual_user);
1230 if (!IsEmptyStr(actual_room))
1231 scr_printf("in <%s> ", actual_room);
1232 if (!IsEmptyStr(actual_host))
1233 scr_printf("from <%s> ", actual_host);
1241 if (extract_int(buf, 0) == last_session) {
1245 color(BRIGHT_MAGENTA);
1246 scr_printf("%-3s", flags);
1248 last_session = extract_int(buf, 0);
1250 scr_printf("%-25s ", username);
1251 color(BRIGHT_MAGENTA);
1253 scr_printf("%-20s", roomname);
1255 if (screenwidth >= 80) {
1257 if (idletime > rc_idle_threshold) {
1258 /* over 1000d, must be gone fishing */
1259 if (idlehours > 23999) {
1263 else if (idlehours > 239) {
1264 scr_printf("%3ldd", idlehours / 24);
1267 else if (idlehours > 9) {
1268 scr_printf("%1ldd%02ld", idlehours / 24, idlehours % 24);
1269 /* less than 10 hours */
1272 scr_printf("%1ld:%02ld", idlehours, idlemins);
1280 fromhost[24] = '\0';
1281 scr_printf("%-24s", fromhost);
1293 void enternew(CtdlIPC * ipc, char *desc, char *buf, int maxlen) {
1295 snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
1296 newprompt(bbb, buf, maxlen);
1300 int shift(int argc, char **argv, int start, int count) {
1303 for (i = start; i < (argc - count); ++i) {
1304 argv[i] = argv[i + count];
1306 argc = argc - count;
1311 static void statusHook(char *s) {
1319 int main(int argc, char **argv) {
1321 char aaa[100], bbb[100]; /* general purpose variables */
1322 char argbuf[64]; /* command line buf */
1323 char *telnet_client_host = NULL;
1324 char *sptr, *sptr2; /* USed to extract the nonce */
1326 struct ctdlipcmisc chek;
1327 struct ctdluser *myself = NULL;
1328 CtdlIPC *ipc; /* Our server connection */
1329 int r; /* IPC result code */
1330 int rv = 0; /* fetch but ignore syscall return value to suppress warnings */
1334 char relhome[PATH_MAX] = "";
1335 char ctdldir[PATH_MAX] = CTDLDIR;
1337 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
1339 #ifdef HAVE_BACKTRACE
1340 bzero(¶ms, sizeof(params));
1341 params.debugLevel = ECRASH_DEBUG_VERBOSE;
1342 params.dumpAllThreads = TRUE;
1343 params.useBacktraceSymbols = 1;
1344 params.signals[0] = SIGSEGV;
1345 params.signals[1] = SIGILL;
1346 params.signals[2] = SIGBUS;
1347 params.signals[3] = SIGABRT;
1349 setIPCErrorPrintf(scr_printf);
1350 setCryptoStatusHook(statusHook);
1352 stty_ctdl(SB_SAVE); /* Store the old terminal parameters */
1353 load_command_set(); /* parse the citadel.rc file */
1354 stty_ctdl(SB_NO_INTR); /* Install the new ones */
1355 signal(SIGPIPE, dropcarr); /* Cleanup gracefully if local conn. dropped */
1356 signal(SIGTERM, dropcarr); /* Cleanup gracefully if terminated */
1357 signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
1359 signal(SIGWINCH, scr_winch); /* Window resize signal */
1363 arg_encrypt = RC_DEFAULT;
1366 // Handle command line options as if we were called like /bin/login (i.e. from in.telnetd)
1367 for (a = 0; a < argc; ++a) {
1368 if ((argc > a + 1) && (!strcmp(argv[a], "-h"))) {
1369 telnet_client_host = argv[a + 1];
1370 argc = shift(argc, argv, a, 2);
1372 if (!strcmp(argv[a], "-x")) {
1374 arg_encrypt = RC_NO;
1376 argc = shift(argc, argv, a, 1);
1378 if (!strcmp(argv[a], "-X")) {
1380 arg_encrypt = RC_YES;
1381 argc = shift(argc, argv, a, 1);
1383 fprintf(stderr, "Not compiled with encryption support");
1387 if (!strcmp(argv[a], "-p")) {
1388 // ignore this argument when called from telnetd
1389 argc = shift(argc, argv, a, 1);
1394 /* Get screen dimensions. First we go to a default of 80x24.
1395 * Then attempt to read the actual screen size from the terminal.
1397 check_screen_dims();
1399 scr_printf("Attaching to server...\n");
1400 ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
1402 error_printf("Can't connect: %s\n", strerror(errno));
1406 CtdlIPC_SetNetworkStatusCallback(ipc, scr_wait_indicator);
1408 if (!(ipc->isLocal)) {
1409 scr_printf("Connected to %s [%s].\n", ipc->ip_hostname, ipc->ip_address);
1412 ipc_for_signal_handlers = ipc; /* KLUDGE cover your eyes */
1414 CtdlIPC_chat_recv(ipc, aaa);
1415 if (aaa[0] != '2') {
1416 scr_printf("%s\n", &aaa[4]);
1417 logoff(ipc, atoi(aaa));
1420 #ifdef HAVE_OPENSSLLLLLL
1421 /* Evaluate encryption preferences */
1422 if (arg_encrypt != RC_NO && rc_encrypt != RC_NO) {
1423 if (!ipc->isLocal || arg_encrypt == RC_YES || rc_encrypt == RC_YES) {
1424 secure = (CtdlIPCStartEncryption(ipc, aaa) / 100 == 2) ? 1 : 0;
1426 error_printf("Can't encrypt: %s\n", aaa);
1431 get_serv_info(ipc, telnet_client_host);
1432 scr_printf("%-24s\n%s\n%s\n", ipc->ServInfo.software, ipc->ServInfo.humannode, ipc->ServInfo.site_location);
1434 scr_printf(" pause next stop\n");
1435 scr_printf(" ctrl-s ctrl-o ctrl-c\n\n");
1436 formout(ipc, "hello"); /* print the opening greeting */
1439 GSTA: /* See if we have a username and password on disk */
1440 if (rc_remember_passwords) {
1441 get_stored_password(hostbuf, portbuf, fullname, password);
1442 if (!IsEmptyStr(fullname)) {
1443 r = CtdlIPCTryLogin(ipc, fullname, aaa);
1445 r = CtdlIPCTryPassword(ipc, password, aaa);
1448 load_user_info(aaa);
1452 set_stored_password(hostbuf, portbuf, "", "");
1460 if (!IsEmptyStr(rc_username)) {
1461 strcpy(fullname, rc_username);
1464 newprompt("Enter your name: ", fullname, 29);
1467 if (!strcasecmp(fullname, "new")) { /* just in case */
1468 scr_printf("Please enter the name you wish to log in with.\n");
1470 } while ((!strcasecmp(fullname, "bbs"))
1471 || (!strcasecmp(fullname, "new"))
1472 || (IsEmptyStr(fullname)));
1474 if (!strcasecmp(fullname, "off")) {
1479 /* FIXME this is a stupid way to do guest mode but it's a reasonable test harness FIXME */
1480 if ((ipc->ServInfo.guest_logins) && (!strcasecmp(fullname, "guest"))) {
1484 /* sign on to the server */
1485 r = CtdlIPCTryLogin(ipc, fullname, aaa);
1489 /* password authentication */
1490 if (!IsEmptyStr(rc_password)) {
1491 strcpy(password, rc_password);
1494 newprompt("\rPlease enter your password: ", password, -(SIZ - 1));
1497 r = CtdlIPCTryPassword(ipc, password, aaa);
1500 r = CtdlIPCTryPassword(ipc, password, aaa);
1504 load_user_info(aaa);
1505 offer_to_remember_password(ipc, hostbuf, portbuf, fullname, password);
1508 scr_printf("<< wrong password >>\n");
1509 if (!IsEmptyStr(rc_password))
1513 NEWUSR:if (IsEmptyStr(rc_password)) {
1514 scr_printf("'%s' not found.\n", fullname);
1515 scr_printf("Type 'off' if you would like to exit.\n");
1516 if (ipc->ServInfo.newuser_disabled == 1) {
1519 scr_printf("Do you want to create a new user account called '%s'? ", fullname);
1525 r = CtdlIPCCreateUser(ipc, fullname, 1, aaa);
1527 scr_printf("%s\n", aaa);
1530 load_user_info(aaa);
1532 while (set_password(ipc) != 0);
1535 enter_config(ipc, 1);
1538 /* Switch color support on or off if we're in user mode */
1539 if (rc_ansi_color == 3) {
1540 if (userflags & US_COLOR)
1546 color(BRIGHT_WHITE);
1547 if (lastcall > 0L) {
1548 scr_printf("Last login: %s", asctime(localtime(&lastcall)));
1552 r = CtdlIPCMiscCheck(ipc, &chek, aaa);
1558 scr_printf("*** You have a new private message in Mail>\n");
1560 scr_printf("*** You have %d new private messages in Mail>\n", b);
1562 if (!IsEmptyStr(rc_gotmail_cmd)) {
1563 rv = system(rc_gotmail_cmd);
1565 scr_printf("*** failed to check for mail calling %s Reason %d.\n", rc_gotmail_cmd, rv);
1569 if ((axlevel >= AxAideU) && (chek.needvalid > 0)) {
1570 scr_printf("*** Users need validation\n");
1572 if (chek.needregis > 0) {
1573 scr_printf("*** Please register.\n");
1574 formout(ipc, "register");
1578 /* Make up some temporary filenames for use in various parts of the
1579 * program. Don't mess with these once they've been set, because we
1580 * will be unlinking them later on in the program and we don't
1581 * want to delete something that we didn't create. */
1582 CtdlMakeTempFileName(temp, sizeof temp);
1583 CtdlMakeTempFileName(temp2, sizeof temp2);
1584 CtdlMakeTempFileName(tempdir, sizeof tempdir);
1586 r = CtdlIPCGetConfig(ipc, &myself, aaa);
1587 set_floor_mode(ipc);
1589 /* Enter the lobby */
1590 dotgoto(ipc, "_BASEROOM_", 1, 0);
1592 /* Main loop for the system... user is logged in. */
1597 readmsgs(ipc, LastMessages, ReadForward, 5);
1599 readmsgs(ipc, NewMessages, ReadForward, 0);
1601 /* MAIN COMMAND LOOP */
1603 mcmd = getcmd(ipc, argbuf); /* Get keyboard command */
1606 check_screen_dims(); /* get screen size */
1612 display_help(ipc, "help");
1615 entmsg(ipc, 0, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1618 entmsg(ipc, 0, 1, 0);
1621 entmsg(ipc, 0, 2, 0);
1624 entmsg(ipc, 0, ((userflags & US_EXTEDIT) ? 2 : 0), 1);
1626 case 5: /* <G>oto */
1630 case 47: /* <A>bandon */
1633 case 90: /* <.A>bandon */
1634 dotgoto(ipc, argbuf, 0, 0);
1636 case 58: /* <M>ail */
1638 dotgoto(ipc, "_MAIL_", 1, 0);
1641 if (!IsEmptyStr(argbuf)) {
1643 dotgoto(ipc, argbuf, 0, 0);
1647 if (!IsEmptyStr(argbuf)) {
1648 dotgoto(ipc, argbuf, 0, 0);
1651 case 95: /* what exactly is the numbering scheme supposed to be anyway? --Ford, there isn't one. -IO */
1652 dotungoto(ipc, argbuf);
1655 readmsgs(ipc, AllMessages, ReadForward, 0);
1658 readmsgs(ipc, LastMessages, ReadForward, 5);
1661 readmsgs(ipc, NewMessages, ReadForward, 0);
1664 readmsgs(ipc, AllMessages, ReadReverse, 0);
1667 readmsgs(ipc, OldMessages, ReadReverse, 0);
1670 readmsgs(ipc, LastMessages, ReadForward, atoi(argbuf));
1686 userlist(ipc, argbuf);
1754 cli_image_upload(ipc, "_userpic_");
1757 cli_image_upload(ipc, "_roompic_");
1764 if (argbuf[0] == 0) {
1765 strcpy(argbuf, "?");
1767 display_help(ipc, argbuf);
1771 formout(ipc, "register");
1776 scr_printf("Are you sure (y/n)? ");
1785 scr_printf("All users will be disconnected! " "Really terminate the server? ");
1788 r = CtdlIPCTerminateServerNow(ipc, aaa);
1789 scr_printf("%s\n", aaa);
1798 scr_printf("Do you really want to schedule a " "server shutdown? ");
1800 r = CtdlIPCTerminateServerScheduled(ipc, 1, aaa);
1804 ("The Citadel server will terminate when all users are logged off.\n");
1807 scr_printf("The Citadel server will not terminate.\n");
1814 network_config_management(ipc, "listrecp", "Message-by-message mailing list recipients");
1818 network_config_management(ipc, "digestrecp", "Digest mailing list recipients");
1830 who_is_online(ipc, 0);
1834 who_is_online(ipc, 1);
1838 who_is_online(ipc, 2);
1842 do_system_configuration(ipc);
1846 do_internet_configuration(ipc);
1858 enter_config(ipc, 2);
1862 enter_config(ipc, 0);
1863 set_floor_mode(ipc);
1867 enter_config(ipc, 3);
1868 set_floor_mode(ipc);
1872 gotofloor(ipc, argbuf, GF_GOTO);
1876 gotofloor(ipc, argbuf, GF_SKIP);
1880 forget_this_floor(ipc);
1912 knrooms(ipc, floor_mode);
1922 misc_server_cmd(ipc, argbuf);
1926 edit_system_message(ipc, argbuf);
1946 case 110: /* <+> Next room */
1947 gotoroomstep(ipc, 1, 0);
1950 case 111: /* <-> Previous room */
1951 gotoroomstep(ipc, 0, 0);
1954 case 112: /* <>> Next floor */
1955 gotofloorstep(ipc, 1, GF_GOTO);
1958 case 113: /* <<> Previous floor */
1959 gotofloorstep(ipc, 0, GF_GOTO);
1962 case 116: /* <.> skip to <+> Next room */
1963 gotoroomstep(ipc, 1, 1);
1966 case 117: /* <.> skip to <-> Previous room */
1967 gotoroomstep(ipc, 0, 1);
1970 case 118: /* <.> skip to <>> Next floor */
1971 gotofloorstep(ipc, 1, GF_SKIP);
1974 case 119: /* <.> skip to <<> Previous floor */
1975 gotofloorstep(ipc, 0, GF_SKIP);
1986 case 120: /* .KAnonymous */
1987 dotknown(ipc, 0, NULL);
1990 case 121: /* .KDirectory */
1991 dotknown(ipc, 1, NULL);
1994 case 122: /* .KMatch */
1995 dotknown(ipc, 2, argbuf);
1998 case 123: /* .KpreferredOnly */
1999 dotknown(ipc, 3, NULL);
2002 case 124: /* .KPrivate */
2003 dotknown(ipc, 4, NULL);
2006 case 125: /* .KRead only */
2007 dotknown(ipc, 5, NULL);
2010 case 127: /* Configure POP3 aggregation */
2011 do_pop3client_configuration(ipc);
2014 case 128: /* Configure XML/RSS feed retrieval */
2015 do_rssclient_configuration(ipc);
2021 } while (termn8 == 0);
2023 TERMN8:scr_printf("%s logged out.", fullname);
2025 color(ORIGINAL_PAIR);
2027 while (marchptr != NULL) {
2028 remove_march(marchptr->march_name, 0);
2031 scr_printf("\n\nType 'off' to disconnect, or next user...\n");
2034 if ((mcmd == 29) || (mcmd == 15)) {
2035 stty_ctdl(SB_RESTORE);
2036 formout(ipc, "goodbye");
2039 /* Free the ungoto list */
2040 for (lp = 0; lp < uglistsize; lp++) {