4 * Main source module for the client program.
15 #if TIME_WITH_SYS_TIME
16 # include <sys/time.h>
20 # include <sys/time.h>
27 #include <sys/types.h>
30 #include <sys/ioctl.h>
37 #include "citadel_ipc.h"
40 #include "routines2.h"
44 #include "client_chat.h"
45 #include "client_passwords.h"
46 #include "citadel_decls.h"
56 #define IFEXPERT if (userflags&US_EXPERT)
57 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
58 #define IFAIDE if (axlevel>=6)
59 #define IFNAIDE if (axlevel<6)
61 struct march *march = NULL;
63 /* globals associated with the client program */
64 char temp[PATH_MAX]; /* Name of general-purpose temp file */
65 char temp2[PATH_MAX]; /* Name of general-purpose temp file */
66 char tempdir[PATH_MAX]; /* Name of general-purpose temp directory */
67 char editor_paths[MAX_EDITORS][SIZ]; /* paths to external editors */
68 char printcmd[SIZ]; /* print command */
69 int editor_pid = (-1);
70 char fullname[USERNAME_SIZE];
74 char room_name[ROOMNAMELEN];
75 char *uglist[UGLISTLEN]; /* size of the ungoto list */
76 long uglistlsn[UGLISTLEN]; /* current read position for all the ungoto's. Not going to make any friends with this one. */
78 char is_mail = 0; /* nonzero when we're in a mail room */
79 char axlevel = 0; /* access level */
80 char is_room_aide = 0; /* boolean flag, 1 if room aide */
84 long usernum = 0L; /* user number */
85 time_t lastcall = 0L; /* Date/time of previous login */
87 long highest_msg_read; /* used for <A>bandon room cmd */
88 long maxmsgnum; /* used for <G>oto */
90 char have_xterm = 0; /* are we running on an xterm? */
91 char rc_username[USERNAME_SIZE];
97 char curr_floor = 0; /* number of current floor */
98 char floorlist[128][SIZ]; /* names of floors */
99 int termn8 = 0; /* Set to nonzero to cause a logoff */
100 int secure; /* Set to nonzero when wire is encrypted */
101 int can_do_msg4 = 0; /* Set to nonzero if the server can handle MSG4 commands */
103 extern char instant_msgs; /* instant messages waiting! */
104 extern int rc_ansi_color; /* ansi color value from citadel.rc */
105 extern int next_lazy_cmd;
107 CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
110 * here is our 'clean up gracefully and exit' routine
112 void ctdl_logoff(char *file, int line, CtdlIPC *ipc, int code)
116 if (editor_pid > 0) { /* kill the editor if it's running */
117 kill(editor_pid, SIGHUP);
120 /* Free the ungoto list */
121 for (lp = 0; lp < uglistsize; lp++) {
125 /* Shut down the server connection ... but not if the logoff code is 3,
126 * because that means we're exiting because we already lost the server.
133 * now clean up various things
141 /* Violently kill off any child processes if Citadel is
144 if (getppid() == 1) {
145 kill(0 - getpgrp(), SIGTERM);
147 kill(0 - getpgrp(), SIGKILL);
149 color(ORIGINAL_PAIR); /* Restore the old color settings */
150 sttybbs(SB_RESTORE); /* return the old terminal settings */
152 * uncomment the following if you need to know why Citadel exited
153 printf("*** Exit code %d at %s:%d\n", code, file, line);
156 exit(code); /* exit with the proper exit code */
162 * signal catching function for hangups...
164 void dropcarr(int signum)
166 logoff(NULL, 3); /* No IPC when server's already gone! */
172 * catch SIGCONT to reset terminal modes when were are put back into the
175 void catch_sigcont(int signum)
178 signal(SIGCONT, catch_sigcont);
182 /* general purpose routines */
185 void formout(CtdlIPC *ipc, char *name)
187 int r; /* IPC return code */
191 r = CtdlIPCSystemMessage(ipc, name, &text, buf);
193 scr_printf("%s\n", buf);
197 fmout(screenwidth, NULL, text, NULL,
198 ((userflags & US_PAGINATOR) ? 1 : 0),
205 void userlist(CtdlIPC *ipc, char *patn)
211 int r; /* IPC response code */
212 char *listing = NULL;
214 r = CtdlIPCUserListing(ipc, &listing, buf);
216 pprintf("%s\n", buf);
220 pprintf(" User Name Num L LastCall Calls Posts\n");
221 pprintf("------------------------- ----- - ---------- ----- -----\n");
222 while (strlen(listing) > 0) {
223 extract_token(buf, listing, 0, '\n');
224 remove_token(listing, 0, '\n');
226 if (sigcaught == 0) {
228 if (pattern(fl, patn) >= 0) {
229 pprintf("%-25s ", fl);
230 pprintf("%5ld %d ", extract_long(buf, 2),
231 extract_int(buf, 1));
232 lc = extract_long(buf, 3);
233 tmbuf = (struct tm *) localtime(&lc);
234 pprintf("%02d/%02d/%04d ",
237 (tmbuf->tm_year + 1900));
238 pprintf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
249 * grab assorted info about the user...
251 void load_user_info(char *params)
253 extract(fullname, params, 0);
254 axlevel = extract_int(params, 1);
255 timescalled = extract_int(params, 2);
256 posted = extract_int(params, 3);
257 userflags = extract_int(params, 4);
258 usernum = extract_long(params, 5);
259 lastcall = extract_long(params, 6);
264 * Remove a room from the march list. 'floornum' is ignored unless
265 * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
266 * floor will be removed from the march list.
268 void remove_march(char *roomname, int floornum)
270 struct march *mptr, *mptr2;
275 if ((!strcasecmp(march->march_name, roomname))
276 || ((!strcasecmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
283 for (mptr = march; mptr != NULL; mptr = mptr->next) {
285 if ((!strcasecmp(mptr->march_name, roomname))
286 || ((!strcasecmp(roomname, "_FLOOR_"))
287 && (mptr->march_floor == floornum))) {
289 mptr2->next = mptr->next;
300 * Locate the room on the march list which we most want to go to. Each room
301 * is measured given a "weight" of preference based on various factors.
303 char *pop_march(int desired_floor)
305 static char TheRoom[ROOMNAMELEN];
307 int TheOrder = 32767;
310 struct march *mptr = NULL;
312 strcpy(TheRoom, "_BASEROOM_");
316 for (mptr = march; mptr != NULL; mptr = mptr->next) {
318 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
319 weight = weight + 10000;
320 if (mptr->march_floor == desired_floor)
321 weight = weight + 5000;
323 weight = weight + ((128 - (mptr->march_floor)) * 128);
324 weight = weight + (128 - (mptr->march_order));
326 if (weight > TheWeight) {
328 strcpy(TheRoom, mptr->march_name);
329 TheFloor = mptr->march_floor;
330 TheOrder = mptr->march_order;
338 * jump directly to a room
340 void dotgoto(CtdlIPC *ipc, char *towhere, int display_name, int fromungoto)
342 char aaa[SIZ], bbb[SIZ];
344 int newmailcount = 0;
345 int partial_match, best_match;
347 int ugpos = uglistsize;
348 int r; /* IPC result code */
349 struct ctdlipcroom *room = NULL;
351 /* store ungoto information */
352 if (fromungoto == 0) {
353 /* sloppy slide them all down, hey it's the client, who cares. :-) */
354 if (uglistsize >= (UGLISTLEN-1)) {
357 for (lp = 0; lp < (UGLISTLEN-1); lp++) {
358 uglist[lp] = uglist[lp+1];
359 uglistlsn[lp] = uglistlsn[lp+1];
366 uglist[ugpos] = malloc(strlen(room_name)+1);
367 strcpy(uglist[ugpos], room_name);
368 uglistlsn[ugpos] = ls;
371 /* first try an exact match */
372 r = CtdlIPCGotoRoom(ipc, towhere, "", &room, aaa);
374 newprompt("Enter room password: ", bbb, 9);
375 r = CtdlIPCGotoRoom(ipc, towhere, bbb, &room, aaa);
377 scr_printf("Wrong password.\n");
383 * If a match is not found, try a partial match.
384 * Partial matches anywhere in the string carry a weight of 1,
385 * left-aligned matches carry a weight of 2. Pick the room that
386 * has the highest-weighted match. Do not match on forgotten
390 struct march *march = NULL;
395 r = CtdlIPCKnownRooms(ipc, SubscribedRooms, AllFloors, &march, aaa);
397 /* Run the roomlist; free the data as we go */
398 struct march *mp = march; /* Current */
402 if (pattern(mp->march_name, towhere) >= 0) {
405 if (!strncasecmp(mp->march_name, towhere, strlen(towhere))) {
408 if (partial_match > best_match) {
409 strcpy(bbb, mp->march_name);
410 best_match = partial_match;
412 /* Both pointers are NULL at end of list */
419 if (strlen(bbb) == 0) {
420 scr_printf("No room '%s'.\n", towhere);
424 r = CtdlIPCGotoRoom(ipc, bbb, "", &room, aaa);
426 if (r / 100 != 1 && r / 100 != 2) {
427 scr_printf("%s\n", aaa);
430 safestrncpy(room_name, room->RRname, ROOMNAMELEN);
431 room_flags = room->RRflags;
432 from_floor = curr_floor;
433 curr_floor = room->RRfloor;
435 remove_march(room_name, 0);
436 if (!strcasecmp(towhere, "_BASEROOM_"))
437 remove_march(towhere, 0);
439 next_lazy_cmd = 5; /* Don't read new if no new msgs */
440 if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
441 if (floorlist[(int) curr_floor][0] == 0)
443 scr_printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
445 if (display_name == 1) {
447 scr_printf("%s ", room_name);
451 if (display_name != 2) {
452 color(BRIGHT_YELLOW);
453 scr_printf("%d ", room->RRunread);
455 scr_printf("new of ");
456 color(BRIGHT_YELLOW);
457 scr_printf("%d ", room->RRtotal);
459 scr_printf("messages.\n");
461 highest_msg_read = room->RRlastread;
462 maxmsgnum = room->RRhighest;
463 is_mail = room->RRismailbox;
464 is_room_aide = room->RRaide;
465 ls = room->RRlastread;
467 /* read info file if necessary */
468 if (room->RRinfoupdated > 0)
471 /* check for newly arrived mail if we can */
472 newmailcount = room->RRnewmail;
473 if (newmailcount > 0) {
475 if (newmailcount == 1) {
476 scr_printf("*** A new mail message has arrived.\n");
479 scr_printf("*** %d new mail messages have arrived.\n",
483 if (strlen(rc_gotmail_cmd) > 0) {
484 system(rc_gotmail_cmd);
487 status_line(ipc->ServInfo.humannode, ipc->ServInfo.bbs_city,
488 room_name, secure, newmailcount);
491 /* Goto next room having unread messages.
492 * We want to skip over rooms that the user has already been to, and take the
493 * user back to the lobby when done. The room we end up in is placed in
494 * newroom - which is set to 0 (the lobby) initially.
496 void gotonext(CtdlIPC *ipc)
499 struct march *mptr, *mptr2;
500 char next_room[ROOMNAMELEN];
501 int r; /* IPC response code */
503 /* Check to see if the march-mode list is already allocated.
504 * If it is, pop the first room off the list and go there.
507 r = CtdlIPCKnownRooms(ipc, SubscribedRoomsWithNewMessages,
508 AllFloors, &march, buf);
510 /* add _BASEROOM_ to the end of the march list, so the user will end up
511 * in the system base room (usually the Lobby>) at the end of the loop
513 mptr = (struct march *) malloc(sizeof(struct march));
515 strcpy(mptr->march_name, "_BASEROOM_");
520 while (mptr2->next != NULL)
525 * ...and remove the room we're currently in, so a <G>oto doesn't make us
526 * walk around in circles
528 remove_march(room_name, 0);
531 strcpy(next_room, pop_march(curr_floor));
533 strcpy(next_room, "_BASEROOM_");
535 remove_march(next_room, 0);
536 dotgoto(ipc, next_room, 1, 0);
540 * forget all rooms on a given floor
542 void forget_all_rooms_on(CtdlIPC *ipc, int ffloor)
545 struct march *flist, *fptr;
546 struct ctdlipcroom *room; /* Ignored */
547 int r; /* IPC response code */
549 scr_printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
551 remove_march("_FLOOR_", ffloor);
552 r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, ffloor, &flist, buf);
554 scr_printf("%-72s\n", buf);
558 r = CtdlIPCGotoRoom(ipc, flist->march_name, "", &room, buf);
560 r = CtdlIPCForgetRoom(ipc, buf);
566 scr_printf("%-72s\r", "");
571 * routine called by gotofloor() to move to a new room on a new floor
573 void gf_toroom(CtdlIPC *ipc, char *towhere, int mode)
575 int floor_being_left;
577 floor_being_left = curr_floor;
579 if (mode == GF_GOTO) { /* <;G>oto mode */
581 dotgoto(ipc, towhere, 1, 0);
583 if (mode == GF_SKIP) { /* <;S>kip mode */
584 dotgoto(ipc, towhere, 1, 0);
585 remove_march("_FLOOR_", floor_being_left);
587 if (mode == GF_ZAP) { /* <;Z>ap mode */
588 dotgoto(ipc, towhere, 1, 0);
589 remove_march("_FLOOR_", floor_being_left);
590 forget_all_rooms_on(ipc, floor_being_left);
598 void gotofloor(CtdlIPC *ipc, char *towhere, int mode)
601 int r; /* IPC response code */
603 char buf[SIZ], targ[SIZ];
605 if (floorlist[0][0] == 0)
608 for (a = 0; a < 128; ++a)
609 if (!strcasecmp(&floorlist[a][0], towhere))
613 for (a = 0; a < 128; ++a) {
614 if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
620 for (a = 0; a < 128; ++a)
621 if (pattern(towhere, &floorlist[a][0]) > 0)
625 scr_printf("No floor '%s'.\n", towhere);
628 for (mptr = march; mptr != NULL; mptr = mptr->next) {
629 if ((mptr->march_floor) == tofloor)
630 gf_toroom(ipc, mptr->march_name, mode);
634 /* Find first known room on the floor */
638 r = CtdlIPCKnownRooms(ipc, SubscribedRooms, tofloor, &mptr, buf);
640 struct march *tmp = mptr;
642 /* TODO: room order is being ignored? */
644 strncpy(targ, mptr->march_name, ROOMNAMELEN);
651 if (strlen(targ) > 0) {
652 gf_toroom(ipc, targ, mode);
656 /* No known rooms on the floor; unzap the first one then */
660 r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, tofloor, &mptr, buf);
662 struct march *tmp = mptr;
664 /* TODO: room order is being ignored? */
666 strncpy(targ, mptr->march_name, ROOMNAMELEN);
673 if (strlen(targ) > 0) {
674 gf_toroom(ipc, targ, mode);
676 scr_printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
682 * forget all rooms on current floor
684 void forget_this_floor(CtdlIPC *ipc)
687 if (curr_floor == 0) {
688 scr_printf("Can't forget this floor.\n");
691 if (floorlist[0][0] == 0)
693 scr_printf("Are you sure you want to forget all rooms on %s? ",
694 &floorlist[(int) curr_floor][0]);
698 gf_toroom(ipc, "_BASEROOM_", GF_ZAP);
703 * Figure out the physical screen dimensions, if we can
704 * WARNING: this is now called from a signal handler!
706 void check_screen_dims(void)
710 unsigned short height; /* rows */
711 unsigned short width; /* columns */
712 unsigned short xpixels;
713 unsigned short ypixels; /* pixels */
716 if (have_xterm) { /* dynamically size screen if on an xterm */
717 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
719 screenheight = is_curses_enabled() ? (int)xwinsz.height - 1 : (int) xwinsz.height;
721 screenwidth = (int) xwinsz.width;
729 * set floor mode depending on client, server, and user settings
731 void set_floor_mode(CtdlIPC* ipc)
733 if (ipc->ServInfo.ok_floors == 0) {
734 floor_mode = 0; /* Don't use floors if the server */
736 /* doesn't support them! */
738 if (rc_floor_mode == RC_NO) { /* never use floors */
741 if (rc_floor_mode == RC_YES) { /* always use floors */
744 if (rc_floor_mode == RC_DEFAULT) { /* user choice */
745 floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
751 * Set or change the user's password
753 int set_password(CtdlIPC *ipc)
759 if (strlen(rc_password) > 0) {
760 strcpy(pass1, rc_password);
761 strcpy(pass2, rc_password);
763 IFNEXPERT formout(ipc, "changepw");
764 newprompt("Enter a new password: ", pass1, -19);
765 newprompt("Enter it again to confirm: ", pass2, -19);
769 if (!strcasecmp(pass1, pass2)) {
770 CtdlIPCChangePassword(ipc, pass1, buf);
771 scr_printf("%s\n", buf);
772 offer_to_remember_password(ipc, hostbuf, portbuf, fullname, pass1);
775 scr_printf("*** They don't match... try again.\n");
783 * get info about the server we've connected to
785 void get_serv_info(CtdlIPC *ipc, char *supplied_hostname)
789 CtdlIPCServerInfo(ipc, buf);
791 /* be nice and identify ourself to the server */
792 CtdlIPCIdentifySoftware(ipc, SERVER_TYPE, 0, REV_LEVEL,
793 (ipc->isLocal ? "local" : CITADEL),
794 (supplied_hostname) ? supplied_hostname :
795 /* Look up the , in the bible if you're confused */
796 (locate_host(ipc, buf), buf), buf);
798 /* Tell the server what our preferred content formats are */
799 if ((CtdlIPCSpecifyPreferredFormats(ipc, buf, "text/html|text/plain") / 100 )== 2) {
807 * Record compare function for SortOnlineUsers()
809 int idlecmp(const void *rec1, const void *rec2) {
812 i1 = extract_long(rec1, 5);
813 i2 = extract_long(rec2, 5);
815 if (i1 < i2) return(1);
816 if (i1 > i2) return(-1);
822 * Sort the list of online users by idle time.
823 * This function frees the supplied buffer, and returns a new one
824 * to the caller. The caller is responsible for freeing the returned buffer.
826 char *SortOnlineUsers(char *listing) {
833 rows = num_tokens(listing, '\n');
834 sortbuf = malloc(rows * SIZ);
835 if (sortbuf == NULL) return(listing);
836 retbuf = malloc(rows * SIZ);
837 if (retbuf == NULL) {
842 /* Copy the list into a fixed-record-size array for sorting */
843 for (i=0; i<rows; ++i) {
845 extract_token(buf, listing, i, '\n');
846 memcpy(&sortbuf[i*SIZ], buf, SIZ);
850 qsort(sortbuf, rows, SIZ, idlecmp);
852 /* Copy back to a \n delimited list */
854 for (i=0; i<rows; ++i) {
855 strcat(retbuf, &sortbuf[i*SIZ]);
856 if (i<(rows-1)) strcat(retbuf, "\n");
864 * Display list of users currently logged on to the server
866 void who_is_online(CtdlIPC *ipc, int longlist)
868 char buf[SIZ], username[SIZ], roomname[SIZ], fromhost[SIZ];
870 char actual_user[SIZ], actual_room[SIZ], actual_host[SIZ];
871 char clientsoft[SIZ];
873 time_t idletime, idlehours, idlemins, idlesecs;
874 int last_session = (-1);
876 char *listing = NULL;
877 int r; /* IPC response code */
886 pprintf(" User Name Room Idle From host\n");
888 pprintf(" ------------------------- -------------------- ---- ------------------------\n");
890 r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
891 listing = SortOnlineUsers(listing);
893 while (strlen(listing) > 0) {
896 /* Get another line */
897 extract_token(buf, listing, 0, '\n');
898 remove_token(listing, 0, '\n');
900 extract(username, buf, 1);
901 extract(roomname, buf, 2);
902 extract(fromhost, buf, 3);
903 extract(clientsoft, buf, 4);
904 extract(flags, buf, 7);
906 idletime = timenow - extract_long(buf, 5);
907 idlehours = idletime / 3600;
908 idlemins = (idletime - (idlehours * 3600)) / 60;
909 idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
911 if (idletime > rc_idle_threshold) {
913 while (strlen(roomname) < 20) {
914 strcat(roomname, " ");
916 strcpy(&roomname[14], "[idle]");
923 extract(actual_user, buf, 8);
924 extract(actual_room, buf, 9);
925 extract(actual_host, buf, 10);
927 pprintf(" Flags: %s\n", flags);
928 pprintf("Session: %d\n", extract_int(buf, 0));
929 pprintf(" Name: %s\n", username);
930 pprintf("In room: %s\n", roomname);
931 pprintf(" Host: %s\n", fromhost);
932 pprintf(" Client: %s\n", clientsoft);
933 pprintf(" Idle: %ld:%02ld:%02ld\n",
938 if ( (strlen(actual_user)+strlen(actual_room)+strlen(actual_host)) > 0) {
940 if (strlen(actual_user)>0) pprintf("<%s> ", actual_user);
941 if (strlen(actual_room)>0) pprintf("in <%s> ", actual_room);
942 if (strlen(actual_host)>0) pprintf("from <%s> ", actual_host);
949 if (extract_int(buf, 0) == last_session) {
952 color(BRIGHT_MAGENTA);
953 pprintf("%-3s", flags);
955 last_session = extract_int(buf, 0);
957 pprintf("%-25s ", username);
958 color(BRIGHT_MAGENTA);
960 pprintf("%-20s ", roomname);
961 if (idletime > rc_idle_threshold) {
962 /* over 1000d, must be gone fishing */
963 if (idlehours > 23999) {
966 } else if (idlehours > 239) {
970 } else if (idlehours > 9) {
971 pprintf("%1ldd%02ld",
974 /* less than 10 hours */
976 pprintf("%1ld:%02ld",
977 idlehours, idlemins);
986 pprintf("%-24s\n", fromhost);
995 void enternew(CtdlIPC *ipc, char *desc, char *buf, int maxlen)
998 snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
999 newprompt(bbb, buf, maxlen);
1004 int shift(int argc, char **argv, int start, int count) {
1007 for (i=start; i<(argc-count); ++i) {
1008 argv[i] = argv[i+count];
1010 argc = argc - count;
1014 static void statusHook(char *s) {
1022 int main(int argc, char **argv)
1025 char aaa[100], bbb[100];/* general purpose variables */
1026 char argbuf[32]; /* command line buf */
1027 char nonce[NONCE_SIZE];
1028 char *telnet_client_host = NULL;
1029 char *sptr, *sptr2; /* USed to extract the nonce */
1030 char hexstring[MD5_HEXSTRING_SIZE];
1031 int stored_password = 0;
1033 struct ctdlipcmisc chek;
1034 struct ctdluser *myself = NULL;
1035 CtdlIPC* ipc; /* Our server connection */
1036 int r; /* IPC result code */
1038 setIPCDeathHook(screen_delete);
1039 setIPCErrorPrintf(err_printf);
1040 setCryptoStatusHook(statusHook);
1042 /* Permissions sanity check - don't run citadel setuid/setgid */
1043 if (getuid() != geteuid()) {
1044 err_printf("Please do not run citadel setuid!\n");
1046 } else if (getgid() != getegid()) {
1047 err_printf("Please do not run citadel setgid!\n");
1051 sttybbs(SB_SAVE); /* Store the old terminal parameters */
1052 load_command_set(); /* parse the citadel.rc file */
1053 sttybbs(SB_NO_INTR); /* Install the new ones */
1054 /* signal(SIGHUP, dropcarr);FIXME */ /* Cleanup gracefully if carrier is dropped */
1055 signal(SIGPIPE, dropcarr); /* Cleanup gracefully if local conn. dropped */
1056 signal(SIGTERM, dropcarr); /* Cleanup gracefully if terminated */
1057 signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
1059 signal(SIGWINCH, scr_winch); /* Window resize signal */
1063 arg_encrypt = RC_DEFAULT;
1065 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1066 arg_screen = RC_DEFAULT;
1070 * Handle command line options as if we were called like /bin/login
1071 * (i.e. from in.telnetd)
1073 for (a=0; a<argc; ++a) {
1074 if ((argc > a+1) && (!strcmp(argv[a], "-h")) ) {
1075 telnet_client_host = argv[a+1];
1076 argc = shift(argc, argv, a, 2);
1078 if (!strcmp(argv[a], "-x")) {
1080 arg_encrypt = RC_NO;
1082 argc = shift(argc, argv, a, 1);
1084 if (!strcmp(argv[a], "-X")) {
1086 arg_encrypt = RC_YES;
1087 argc = shift(argc, argv, a, 1);
1089 fprintf(stderr, "Not compiled with encryption support");
1093 if (!strcmp(argv[a], "-s")) {
1094 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1097 argc = shift(argc, argv, a, 1);
1099 if (!strcmp(argv[a], "-S")) {
1100 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1101 arg_screen = RC_YES;
1103 argc = shift(argc, argv, a, 1);
1105 if (!strcmp(argv[a], "-p")) {
1108 if (chdir(BBSDIR) < 0) {
1109 perror("can't change to " BBSDIR);
1114 * Drop privileges if necessary. We stat
1115 * citadel.config to get the uid/gid since it's
1116 * guaranteed to have the uid/gid we want.
1118 if (!getuid() || !getgid()) {
1119 if (stat(BBSDIR "/citadel.config", &st) < 0) {
1120 perror("couldn't stat citadel.config");
1123 if (!getgid() && (setgid(st.st_gid) < 0)) {
1124 perror("couldn't change gid");
1127 if (!getuid() && (setuid(st.st_uid) < 0)) {
1128 perror("couldn't change uid");
1132 scr_printf("Privileges changed to uid %d gid %d\n",
1133 getuid(), getgid());
1136 argc = shift(argc, argv, a, 1);
1143 newprompt("Connect to (return for local server): ", hostbuf, 64);
1146 sln_printf("Attaching to server... \r");
1148 ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
1151 error_printf("Can't connect: %s\n", strerror(errno));
1154 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1155 CtdlIPC_SetNetworkStatusCallback(ipc, wait_indicator);
1157 ipc_for_signal_handlers = ipc; /* KLUDGE cover your eyes */
1159 CtdlIPC_chat_recv(ipc, aaa);
1160 if (aaa[0] != '2') {
1161 scr_printf("%s\n", &aaa[4]);
1162 logoff(ipc, atoi(aaa));
1165 /* If there is a [nonce] at the end, put the nonce in <nonce>, else nonce
1169 if ((sptr = strchr(aaa, '<')) == NULL)
1175 if ((sptr2 = strchr(sptr, '>')) == NULL)
1183 strncpy(nonce, sptr, NONCE_SIZE);
1188 /* Evaluate encryption preferences */
1189 if (arg_encrypt != RC_NO && rc_encrypt != RC_NO) {
1190 if (!ipc->isLocal || arg_encrypt == RC_YES || rc_encrypt == RC_YES) {
1191 secure = (CtdlIPCStartEncryption(ipc, aaa) / 100 == 2) ? 1 : 0;
1193 error_printf("Can't encrypt: %s\n", aaa);
1198 get_serv_info(ipc, telnet_client_host);
1199 scr_printf("%-24s\n%s\n%s\n", ipc->ServInfo.software, ipc->ServInfo.humannode,
1200 ipc->ServInfo.bbs_city);
1203 status_line(ipc->ServInfo.humannode, ipc->ServInfo.bbs_city, NULL,
1206 screenwidth = 80; /* default screen dimensions */
1209 scr_printf(" pause next stop\n");
1210 scr_printf(" ctrl-s ctrl-o ctrl-c\n\n");
1211 formout(ipc, "hello"); /* print the opening greeting */
1214 GSTA: /* See if we have a username and password on disk */
1215 if (rc_remember_passwords) {
1216 get_stored_password(hostbuf, portbuf, fullname, password);
1217 if (strlen(fullname) > 0) {
1218 r = CtdlIPCTryLogin(ipc, fullname, aaa);
1221 r = CtdlIPCTryApopPassword(ipc, make_apop_string(password, nonce, hexstring, sizeof hexstring), aaa);
1223 r = CtdlIPCTryPassword(ipc, password, aaa);
1228 load_user_info(aaa);
1229 stored_password = 1;
1232 set_stored_password(hostbuf, portbuf, "", "");
1240 if (strlen(rc_username) > 0) {
1241 strcpy(fullname, rc_username);
1243 newprompt("Enter your name: ", fullname, 29);
1246 if (!strcasecmp(fullname, "new")) { /* just in case */
1247 scr_printf("Please enter the name you wish to log in with.\n");
1250 (!strcasecmp(fullname, "bbs"))
1251 || (!strcasecmp(fullname, "new"))
1252 || (strlen(fullname) == 0));
1254 if (!strcasecmp(fullname, "off")) {
1258 /* sign on to the server */
1259 r = CtdlIPCTryLogin(ipc, fullname, aaa);
1263 /* password authentication */
1264 if (strlen(rc_password) > 0) {
1265 strcpy(password, rc_password);
1267 newprompt("\rPlease enter your password: ", password, -19);
1272 r = CtdlIPCTryApopPassword(ipc, make_apop_string(password, nonce, hexstring, sizeof hexstring), aaa);
1274 r = CtdlIPCTryPassword(ipc, password, aaa);
1278 load_user_info(aaa);
1279 offer_to_remember_password(ipc, hostbuf, portbuf,
1280 fullname, password);
1283 scr_printf("<< wrong password >>\n");
1284 if (strlen(rc_password) > 0)
1288 NEWUSR: if (strlen(rc_password) == 0) {
1289 scr_printf("'%s' not found.\n"
1290 "Do you want to create a new account with this name? ",
1296 r = CtdlIPCCreateUser(ipc, fullname, 1, aaa);
1298 scr_printf("%s\n", aaa);
1301 load_user_info(aaa);
1303 while (set_password(ipc) != 0);
1306 enter_config(ipc, 1);
1309 /* Switch color support on or off if we're in user mode */
1310 if (rc_ansi_color == 3) {
1311 if (userflags & US_COLOR)
1317 color(BRIGHT_WHITE);
1318 scr_printf("\n%s\nAccess level: %d (%s)\n"
1319 "User #%ld / Login #%d",
1320 fullname, axlevel, axdefs[(int) axlevel],
1321 usernum, timescalled);
1322 if (lastcall > 0L) {
1323 scr_printf(" / Last login: %s",
1324 asctime(localtime(&lastcall)));
1328 r = CtdlIPCMiscCheck(ipc, &chek, aaa);
1334 scr_printf("*** You have a new private message in Mail>\n");
1336 scr_printf("*** You have %d new private messages in Mail>\n", b);
1338 if (strlen(rc_gotmail_cmd) > 0) {
1339 system(rc_gotmail_cmd);
1342 if ((axlevel >= 6) && (chek.needvalid > 0)) {
1343 scr_printf("*** Users need validation\n");
1345 if (chek.needregis > 0) {
1346 scr_printf("*** Please register.\n");
1347 formout(ipc, "register");
1351 /* Make up some temporary filenames for use in various parts of the
1352 * program. Don't mess with these once they've been set, because we
1353 * will be unlinking them later on in the program and we don't
1354 * want to delete something that we didn't create. */
1355 snprintf(temp, sizeof temp, tmpnam(NULL));
1356 snprintf(temp2, sizeof temp2, tmpnam(NULL));
1357 snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
1359 /* Get screen dimensions. First we go to a default of 80x24. Then
1360 * we try to get the user's actual screen dimensions off the server.
1361 * However, if we're running on an xterm, all this stuff is
1362 * irrelevant because we're going to dynamically size the screen
1363 * during the session.
1367 r = CtdlIPCGetConfig(ipc, &myself, aaa);
1369 screenwidth = myself->USscreenwidth;
1370 screenheight = myself->USscreenheight;
1372 if (getenv("TERM") != NULL)
1373 if (!strcmp(getenv("TERM"), "xterm")) {
1377 check_screen_dims();
1380 set_floor_mode(ipc);
1382 /* Enter the lobby */
1383 dotgoto(ipc, "_BASEROOM_", 1, 0);
1385 /* Main loop for the system... user is logged in. */
1389 readmsgs(ipc, LastMessages, ReadForward, 5);
1391 readmsgs(ipc, NewMessages, ReadForward, 0);
1393 /* MAIN COMMAND LOOP */
1395 mcmd = getcmd(ipc, argbuf); /* Get keyboard command */
1398 check_screen_dims(); /* if xterm, get screen size */
1404 formout(ipc, "help");
1417 /* Only m.author is used */
1418 struct ctdlipcmessage m;
1419 newprompt("What do you want your username to be? ", m.author, USERNAME_SIZE - 1);
1420 r = CtdlIPCPostMessage(ipc, 2, &m, aaa);
1422 scr_printf("%s\n", aaa);
1427 case 5: /* <G>oto */
1431 case 47: /* <A>bandon */
1432 if (!rc_alt_semantics) {
1437 case 90: /* <.A>bandon */
1438 if (!rc_alt_semantics)
1440 dotgoto(ipc, argbuf, 0, 0);
1442 case 58: /* <M>ail */
1444 dotgoto(ipc, "_MAIL_", 1, 0);
1447 if (strlen(argbuf) > 0) {
1449 dotgoto(ipc, argbuf, 0, 0);
1453 if (strlen(argbuf) > 0) {
1454 if (rc_alt_semantics) {
1457 dotgoto(ipc, argbuf, 0, 0);
1460 case 95: /* what exactly is the numbering scheme supposed to be anyway? --Ford, there isn't one. -IO */
1461 dotungoto(ipc, argbuf);
1464 readmsgs(ipc, AllMessages, ReadForward, 0);
1467 readmsgs(ipc, LastMessages, ReadForward, 5);
1470 readmsgs(ipc, NewMessages, ReadForward, 0);
1473 readmsgs(ipc, AllMessages, ReadReverse, 0);
1476 readmsgs(ipc, OldMessages, ReadReverse, 0);
1479 readmsgs(ipc, LastMessages, ReadForward,
1496 userlist(ipc, argbuf);
1554 if (!rc_alt_semantics) {
1566 cli_image_upload(ipc, "_userpic_");
1569 cli_image_upload(ipc, "_roompic_");
1573 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1574 cli_image_upload(ipc, aaa);
1578 enternew(ipc, "roomname", aaa, 20);
1579 r = CtdlIPCChangeRoomname(ipc, aaa, bbb);
1581 scr_printf("\n%s\n", bbb);
1584 enternew(ipc, "hostname", aaa, 25);
1585 r = CtdlIPCChangeHostname(ipc, aaa, bbb);
1587 scr_printf("\n%s\n", bbb);
1590 enternew(ipc, "username", aaa, 32);
1591 r = CtdlIPCChangeUsername(ipc, aaa, bbb);
1593 scr_printf("\n%s\n", bbb);
1603 display_help(ipc, argbuf);
1607 formout(ipc, "register");
1612 scr_printf("Are you sure (y/n)? ");
1614 if (!rc_alt_semantics)
1622 scr_printf("All users will be disconnected! "
1623 "Really terminate the server? ");
1625 if (!rc_alt_semantics)
1627 r = CtdlIPCTerminateServerNow(ipc, aaa);
1628 scr_printf("%s\n", aaa);
1637 scr_printf("Do you really want to schedule a "
1638 "server shutdown? ");
1640 r = CtdlIPCTerminateServerScheduled(ipc, 1, aaa);
1644 "The Citadel server will terminate when all users are logged off.\n"
1648 "The Citadel server will not terminate.\n"
1656 network_config_management(ipc, "listrecp",
1657 "Message-by-message mailing list recipients");
1661 network_config_management(ipc, "digestrecp",
1662 "Digest mailing list recipients");
1666 network_config_management(ipc, "ignet_push_share",
1667 "Nodes with which we share this room");
1671 do_ignet_configuration(ipc);
1675 do_filterlist_configuration(ipc);
1679 if (rc_alt_semantics) {
1692 sttybbs(SB_RESTORE);
1693 snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1694 "exec ./subsystem %ld %d %d", fullname,
1695 usernum, screenwidth, axlevel);
1697 sttybbs(SB_NO_INTR);
1700 scr_printf("*** Can't run doors when server is not local.\n");
1705 who_is_online(ipc, 0);
1709 who_is_online(ipc, 1);
1713 who_is_online(ipc, 2);
1717 do_system_configuration(ipc);
1721 do_internet_configuration(ipc);
1725 check_message_base(ipc);
1737 enter_config(ipc, 2);
1741 enter_config(ipc, 0);
1742 set_floor_mode(ipc);
1746 enter_config(ipc, 3);
1747 set_floor_mode(ipc);
1751 gotofloor(ipc, argbuf, GF_GOTO);
1755 gotofloor(ipc, argbuf, GF_SKIP);
1759 forget_this_floor(ipc);
1787 knrooms(ipc, floor_mode);
1797 misc_server_cmd(ipc, argbuf);
1801 edit_system_message(ipc, argbuf);
1825 default: /* allow some math on the command */
1826 /* commands 100... to 100+MAX_EDITORS-1 will
1827 call the appropriate editor... in other
1828 words, command numbers 100+ will match
1829 the citadel.rc keys editor0, editor1, etc.*/
1830 if (mcmd >= 100 && mcmd < (100+MAX_EDITORS))
1832 /* entmsg mode >=2 select editor */
1833 entmsg(ipc, 0, mcmd - 100 + 2);
1837 } while (termn8 == 0);
1839 TERMN8: scr_printf("%s logged out.", fullname);
1841 color(ORIGINAL_PAIR);
1843 while (march != NULL) {
1844 remove_march(march->march_name, 0);
1847 sln_printf("\n\nType 'off' to disconnect, or next user...\n");
1850 if ((mcmd == 29) || (mcmd == 15)) {
1852 sttybbs(SB_RESTORE);
1853 formout(ipc, "goodbye");