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"
53 #include "citadel_dirs.h"
57 #define IFEXPERT if (userflags&US_EXPERT)
58 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
59 #define IFAIDE if (axlevel>=6)
60 #define IFNAIDE if (axlevel<6)
62 struct march *march = NULL;
64 /* globals associated with the client program */
65 char temp[PATH_MAX]; /* Name of general-purpose temp file */
66 char temp2[PATH_MAX]; /* Name of general-purpose temp file */
67 char tempdir[PATH_MAX]; /* Name of general-purpose temp directory */
68 char editor_paths[MAX_EDITORS][SIZ]; /* paths to external editors */
69 char printcmd[SIZ]; /* print command */
70 int editor_pid = (-1);
71 char fullname[USERNAME_SIZE];
75 char room_name[ROOMNAMELEN];
76 char *uglist[UGLISTLEN]; /* size of the ungoto list */
77 long uglistlsn[UGLISTLEN]; /* current read position for all the ungoto's. Not going to make any friends with this one. */
79 char is_mail = 0; /* nonzero when we're in a mail room */
80 char axlevel = 0; /* access level */
81 char is_room_aide = 0; /* boolean flag, 1 if room aide */
85 long usernum = 0L; /* user number */
86 time_t lastcall = 0L; /* Date/time of previous login */
88 long highest_msg_read; /* used for <A>bandon room cmd */
89 long maxmsgnum; /* used for <G>oto */
91 char have_xterm = 0; /* are we running on an xterm? */
92 char rc_username[USERNAME_SIZE];
98 char curr_floor = 0; /* number of current floor */
99 char floorlist[128][SIZ]; /* names of floors */
100 int termn8 = 0; /* Set to nonzero to cause a logoff */
101 int secure; /* Set to nonzero when wire is encrypted */
102 int can_do_msg4 = 0; /* Set to nonzero if the server can handle MSG4 commands */
104 extern char instant_msgs; /* instant messages waiting! */
105 extern int rc_ansi_color; /* ansi color value from citadel.rc */
106 extern int next_lazy_cmd;
108 CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
111 * here is our 'clean up gracefully and exit' routine
113 void ctdl_logoff(char *file, int line, CtdlIPC *ipc, int code)
117 if (editor_pid > 0) { /* kill the editor if it's running */
118 kill(editor_pid, SIGHUP);
121 /* Free the ungoto list */
122 for (lp = 0; lp < uglistsize; lp++) {
126 /* Shut down the server connection ... but not if the logoff code is 3,
127 * because that means we're exiting because we already lost the server.
134 * now clean up various things
142 /* Violently kill off any child processes if Citadel is
145 if (getppid() == 1) {
146 kill(0 - getpgrp(), SIGTERM);
148 kill(0 - getpgrp(), SIGKILL);
150 color(ORIGINAL_PAIR); /* Restore the old color settings */
151 stty_ctdl(SB_RESTORE); /* return the old terminal settings */
153 * uncomment the following if you need to know why Citadel exited
154 printf("*** Exit code %d at %s:%d\n", code, file, line);
157 exit(code); /* exit with the proper exit code */
163 * signal catching function for hangups...
165 void dropcarr(int signum)
167 logoff(NULL, 3); /* No IPC when server's already gone! */
173 * catch SIGCONT to reset terminal modes when were are put back into the
176 void catch_sigcont(int signum)
179 signal(SIGCONT, catch_sigcont);
183 /* general purpose routines */
186 void formout(CtdlIPC *ipc, char *name)
188 int r; /* IPC return code */
192 r = CtdlIPCSystemMessage(ipc, name, &text, buf);
194 scr_printf("%s\n", buf);
198 fmout(screenwidth, NULL, text, NULL,
199 ((userflags & US_PAGINATOR) ? 1 : 0),
206 void userlist(CtdlIPC *ipc, char *patn)
212 int r; /* IPC response code */
213 char *listing = NULL;
215 r = CtdlIPCUserListing(ipc, patn, &listing, buf);
217 pprintf("%s\n", buf);
221 pprintf(" User Name Num L LastCall Calls Posts\n");
222 pprintf("------------------------- ----- - ---------- ----- -----\n");
223 if (listing != NULL) while (strlen(listing) > 0) {
224 extract_token(buf, listing, 0, '\n', sizeof buf);
225 remove_token(listing, 0, '\n');
227 if (sigcaught == 0) {
228 extract_token(fl, buf, 0, '|', sizeof fl);
229 if (pattern(fl, patn) >= 0) {
230 pprintf("%-25s ", fl);
231 pprintf("%5ld %d ", extract_long(buf, 2),
232 extract_int(buf, 1));
233 lc = extract_long(buf, 3);
234 localtime_r(&lc, &tmbuf);
235 pprintf("%02d/%02d/%04d ",
238 (tmbuf.tm_year + 1900));
239 pprintf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
250 * grab assorted info about the user...
252 void load_user_info(char *params)
254 extract_token(fullname, params, 0, '|', sizeof fullname);
255 axlevel = extract_int(params, 1);
256 timescalled = extract_int(params, 2);
257 posted = extract_int(params, 3);
258 userflags = extract_int(params, 4);
259 usernum = extract_long(params, 5);
260 lastcall = extract_long(params, 6);
265 * Remove a room from the march list. 'floornum' is ignored unless
266 * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
267 * floor will be removed from the march list.
269 void remove_march(char *roomname, int floornum)
271 struct march *mptr, *mptr2;
276 if ((!strcasecmp(march->march_name, roomname))
277 || ((!strcasecmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
284 for (mptr = march; mptr != NULL; mptr = mptr->next) {
286 if ((!strcasecmp(mptr->march_name, roomname))
287 || ((!strcasecmp(roomname, "_FLOOR_"))
288 && (mptr->march_floor == floornum))) {
290 mptr2->next = mptr->next;
301 * Locate the room on the march list which we most want to go to. Each room
302 * is measured given a "weight" of preference based on various factors.
304 char *pop_march(int desired_floor)
306 static char TheRoom[ROOMNAMELEN];
308 int TheOrder = 32767;
311 struct march *mptr = NULL;
313 strcpy(TheRoom, "_BASEROOM_");
317 for (mptr = march; mptr != NULL; mptr = mptr->next) {
319 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
320 weight = weight + 10000;
321 if (mptr->march_floor == desired_floor)
322 weight = weight + 5000;
324 weight = weight + ((128 - (mptr->march_floor)) * 128);
325 weight = weight + (128 - (mptr->march_order));
327 if (weight > TheWeight) {
329 strcpy(TheRoom, mptr->march_name);
330 TheFloor = mptr->march_floor;
331 TheOrder = mptr->march_order;
339 * jump directly to a room
341 void dotgoto(CtdlIPC *ipc, char *towhere, int display_name, int fromungoto)
343 char aaa[SIZ], bbb[SIZ];
345 int newmailcount = 0;
346 int partial_match, best_match;
348 int ugpos = uglistsize;
349 int r; /* IPC result code */
350 struct ctdlipcroom *room = NULL;
352 /* store ungoto information */
353 if (fromungoto == 0) {
354 /* sloppy slide them all down, hey it's the client, who cares. :-) */
355 if (uglistsize >= (UGLISTLEN-1)) {
358 for (lp = 0; lp < (UGLISTLEN-1); lp++) {
359 uglist[lp] = uglist[lp+1];
360 uglistlsn[lp] = uglistlsn[lp+1];
367 uglist[ugpos] = malloc(strlen(room_name)+1);
368 strcpy(uglist[ugpos], room_name);
369 uglistlsn[ugpos] = ls;
372 /* first try an exact match */
373 r = CtdlIPCGotoRoom(ipc, towhere, "", &room, aaa);
375 newprompt("Enter room password: ", bbb, 9);
376 r = CtdlIPCGotoRoom(ipc, towhere, bbb, &room, aaa);
378 scr_printf("Wrong password.\n");
384 * If a match is not found, try a partial match.
385 * Partial matches anywhere in the string carry a weight of 1,
386 * left-aligned matches carry a weight of 2. Pick the room that
387 * has the highest-weighted match. Do not match on forgotten
391 struct march *march = NULL;
396 r = CtdlIPCKnownRooms(ipc, SubscribedRooms, AllFloors, &march, aaa);
398 /* Run the roomlist; free the data as we go */
399 struct march *mp = march; /* Current */
403 if (pattern(mp->march_name, towhere) >= 0) {
406 if (!strncasecmp(mp->march_name, towhere, strlen(towhere))) {
409 if (partial_match > best_match) {
410 strcpy(bbb, mp->march_name);
411 best_match = partial_match;
413 /* Both pointers are NULL at end of list */
420 if (strlen(bbb) == 0) {
421 scr_printf("No room '%s'.\n", towhere);
425 r = CtdlIPCGotoRoom(ipc, bbb, "", &room, aaa);
427 if (r / 100 != 1 && r / 100 != 2) {
428 scr_printf("%s\n", aaa);
431 safestrncpy(room_name, room->RRname, ROOMNAMELEN);
432 room_flags = room->RRflags;
433 from_floor = curr_floor;
434 curr_floor = room->RRfloor;
436 remove_march(room_name, 0);
437 if (!strcasecmp(towhere, "_BASEROOM_"))
438 remove_march(towhere, 0);
440 next_lazy_cmd = 5; /* Don't read new if no new msgs */
441 if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
442 if (floorlist[(int) curr_floor][0] == 0)
444 scr_printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
446 if (display_name == 1) {
448 scr_printf("%s ", room_name);
452 if (display_name != 2) {
453 color(BRIGHT_YELLOW);
454 scr_printf("%d ", room->RRunread);
456 scr_printf("new of ");
457 color(BRIGHT_YELLOW);
458 scr_printf("%d ", room->RRtotal);
460 scr_printf("messages.\n");
462 highest_msg_read = room->RRlastread;
463 maxmsgnum = room->RRhighest;
464 is_mail = room->RRismailbox;
465 is_room_aide = room->RRaide;
466 ls = room->RRlastread;
468 /* read info file if necessary */
469 if (room->RRinfoupdated > 0)
472 /* check for newly arrived mail if we can */
473 newmailcount = room->RRnewmail;
474 if (newmailcount > 0) {
476 if (newmailcount == 1) {
477 scr_printf("*** A new mail message has arrived.\n");
480 scr_printf("*** %d new mail messages have arrived.\n",
484 if (strlen(rc_gotmail_cmd) > 0) {
485 system(rc_gotmail_cmd);
488 status_line(ipc->ServInfo.humannode, ipc->ServInfo.site_location,
489 room_name, secure, newmailcount);
492 /* Goto next room having unread messages.
493 * We want to skip over rooms that the user has already been to, and take the
494 * user back to the lobby when done. The room we end up in is placed in
495 * newroom - which is set to 0 (the lobby) initially.
497 void gotonext(CtdlIPC *ipc)
500 struct march *mptr, *mptr2;
501 char next_room[ROOMNAMELEN];
502 int r; /* IPC response code */
504 /* Check to see if the march-mode list is already allocated.
505 * If it is, pop the first room off the list and go there.
508 r = CtdlIPCKnownRooms(ipc, SubscribedRoomsWithNewMessages,
509 AllFloors, &march, buf);
511 /* add _BASEROOM_ to the end of the march list, so the user will end up
512 * in the system base room (usually the Lobby>) at the end of the loop
514 mptr = (struct march *) malloc(sizeof(struct march));
516 strcpy(mptr->march_name, "_BASEROOM_");
521 while (mptr2->next != NULL)
526 * ...and remove the room we're currently in, so a <G>oto doesn't make us
527 * walk around in circles
529 remove_march(room_name, 0);
532 strcpy(next_room, pop_march(curr_floor));
534 strcpy(next_room, "_BASEROOM_");
536 remove_march(next_room, 0);
537 dotgoto(ipc, next_room, 1, 0);
541 * forget all rooms on a given floor
543 void forget_all_rooms_on(CtdlIPC *ipc, int ffloor)
546 struct march *flist = NULL;
547 struct march *fptr = NULL;
548 struct ctdlipcroom *room = NULL;
549 int r; /* IPC response code */
551 scr_printf("Forgetting all rooms on %s...\n", &floorlist[ffloor][0]);
553 remove_march("_FLOOR_", ffloor);
554 r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, ffloor, &flist, buf);
556 scr_printf("Error %d: %s\n", r, buf);
560 r = CtdlIPCGotoRoom(ipc, flist->march_name, "", &room, buf);
562 r = CtdlIPCForgetRoom(ipc, buf);
564 scr_printf("Error %d: %s\n", r, buf);
576 * routine called by gotofloor() to move to a new room on a new floor
578 void gf_toroom(CtdlIPC *ipc, char *towhere, int mode)
580 int floor_being_left;
582 floor_being_left = curr_floor;
584 if (mode == GF_GOTO) { /* <;G>oto mode */
586 dotgoto(ipc, towhere, 1, 0);
588 else if (mode == GF_SKIP) { /* <;S>kip mode */
589 dotgoto(ipc, towhere, 1, 0);
590 remove_march("_FLOOR_", floor_being_left);
592 else if (mode == GF_ZAP) { /* <;Z>ap mode */
593 dotgoto(ipc, towhere, 1, 0);
594 remove_march("_FLOOR_", floor_being_left);
595 forget_all_rooms_on(ipc, floor_being_left);
603 void gotofloor(CtdlIPC *ipc, char *towhere, int mode)
606 int r; /* IPC response code */
608 char buf[SIZ], targ[SIZ];
610 if (floorlist[0][0] == 0)
613 for (a = 0; a < 128; ++a)
614 if (!strcasecmp(&floorlist[a][0], towhere))
618 for (a = 0; a < 128; ++a) {
619 if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
625 for (a = 0; a < 128; ++a)
626 if (pattern(towhere, &floorlist[a][0]) > 0)
630 scr_printf("No floor '%s'.\n", towhere);
633 for (mptr = march; mptr != NULL; mptr = mptr->next) {
634 if ((mptr->march_floor) == tofloor) {
635 gf_toroom(ipc, mptr->march_name, mode);
640 /* Find first known room on the floor */
644 r = CtdlIPCKnownRooms(ipc, SubscribedRooms, tofloor, &mptr, buf);
646 struct march *tmp = mptr;
648 /* TODO: room order is being ignored? */
650 strncpy(targ, mptr->march_name, ROOMNAMELEN);
657 if (strlen(targ) > 0) {
658 gf_toroom(ipc, targ, mode);
662 /* No known rooms on the floor; unzap the first one then */
666 r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, tofloor, &mptr, buf);
668 struct march *tmp = mptr;
670 /* TODO: room order is being ignored? */
672 strncpy(targ, mptr->march_name, ROOMNAMELEN);
679 if (strlen(targ) > 0) {
680 gf_toroom(ipc, targ, mode);
682 scr_printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
688 * forget all rooms on current floor
690 void forget_this_floor(CtdlIPC *ipc)
692 if (curr_floor == 0) {
693 scr_printf("Can't forget this floor.\n");
696 if (floorlist[0][0] == 0) {
699 scr_printf("Are you sure you want to forget all rooms on %s? ",
700 &floorlist[(int) curr_floor][0]);
705 gf_toroom(ipc, "_BASEROOM_", GF_ZAP);
710 * Figure out the physical screen dimensions, if we can
711 * WARNING: this is now called from a signal handler!
713 void check_screen_dims(void)
717 unsigned short height; /* rows */
718 unsigned short width; /* columns */
719 unsigned short xpixels;
720 unsigned short ypixels; /* pixels */
723 if (have_xterm) { /* dynamically size screen if on an xterm */
724 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
726 screenheight = is_curses_enabled() ? (int)xwinsz.height - 1 : (int) xwinsz.height;
728 screenwidth = (int) xwinsz.width;
736 * set floor mode depending on client, server, and user settings
738 void set_floor_mode(CtdlIPC* ipc)
740 if (ipc->ServInfo.ok_floors == 0) {
741 floor_mode = 0; /* Don't use floors if the server */
743 /* doesn't support them! */
745 if (rc_floor_mode == RC_NO) { /* never use floors */
748 if (rc_floor_mode == RC_YES) { /* always use floors */
751 if (rc_floor_mode == RC_DEFAULT) { /* user choice */
752 floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
758 * Set or change the user's password
760 int set_password(CtdlIPC *ipc)
766 if (strlen(rc_password) > 0) {
767 strcpy(pass1, rc_password);
768 strcpy(pass2, rc_password);
770 IFNEXPERT formout(ipc, "changepw");
771 newprompt("Enter a new password: ", pass1, -19);
772 newprompt("Enter it again to confirm: ", pass2, -19);
776 if (!strcasecmp(pass1, pass2)) {
777 CtdlIPCChangePassword(ipc, pass1, buf);
778 scr_printf("%s\n", buf);
779 offer_to_remember_password(ipc, hostbuf, portbuf, fullname, pass1);
782 scr_printf("*** They don't match... try again.\n");
790 * get info about the server we've connected to
792 void get_serv_info(CtdlIPC *ipc, char *supplied_hostname)
796 CtdlIPCServerInfo(ipc, buf);
798 /* be nice and identify ourself to the server */
799 CtdlIPCIdentifySoftware(ipc, SERVER_TYPE, 0, REV_LEVEL,
800 (ipc->isLocal ? "local" : CITADEL),
801 (supplied_hostname) ? supplied_hostname :
802 /* Look up the , in the bible if you're confused */
803 (locate_host(ipc, buf), buf), buf);
805 /* Tell the server what our preferred content formats are */
806 if ((CtdlIPCSpecifyPreferredFormats(ipc, buf, "text/html|text/plain") / 100 )== 2) {
814 * Record compare function for SortOnlineUsers()
816 int idlecmp(const void *rec1, const void *rec2) {
819 i1 = extract_long(rec1, 5);
820 i2 = extract_long(rec2, 5);
822 if (i1 < i2) return(1);
823 if (i1 > i2) return(-1);
829 * Sort the list of online users by idle time.
830 * This function frees the supplied buffer, and returns a new one
831 * to the caller. The caller is responsible for freeing the returned buffer.
833 char *SortOnlineUsers(char *listing) {
840 rows = num_tokens(listing, '\n');
841 sortbuf = malloc(rows * SIZ);
842 if (sortbuf == NULL) return(listing);
843 retbuf = malloc(rows * SIZ);
844 if (retbuf == NULL) {
849 /* Copy the list into a fixed-record-size array for sorting */
850 for (i=0; i<rows; ++i) {
852 extract_token(buf, listing, i, '\n', sizeof buf);
853 memcpy(&sortbuf[i*SIZ], buf, (size_t)SIZ);
857 qsort(sortbuf, rows, SIZ, idlecmp);
859 /* Copy back to a \n delimited list */
861 for (i=0; i<rows; ++i) {
862 strcat(retbuf, &sortbuf[i*SIZ]);
863 if (i<(rows-1)) strcat(retbuf, "\n");
871 * Display list of users currently logged on to the server
873 void who_is_online(CtdlIPC *ipc, int longlist)
875 char buf[SIZ], username[SIZ], roomname[SIZ], fromhost[SIZ];
877 char actual_user[SIZ], actual_room[SIZ], actual_host[SIZ];
878 char clientsoft[SIZ];
880 time_t idletime, idlehours, idlemins, idlesecs;
881 int last_session = (-1);
883 char *listing = NULL;
884 int r; /* IPC response code */
893 pprintf(" User Name Room Idle From host\n");
895 pprintf(" ------------------------- -------------------- ---- ------------------------\n");
897 r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
898 listing = SortOnlineUsers(listing);
900 while (strlen(listing) > 0) {
903 /* Get another line */
904 extract_token(buf, listing, 0, '\n', sizeof buf);
905 remove_token(listing, 0, '\n');
907 extract_token(username, buf, 1, '|', sizeof username);
908 extract_token(roomname, buf, 2, '|', sizeof roomname);
909 extract_token(fromhost, buf, 3, '|', sizeof fromhost);
910 extract_token(clientsoft, buf, 4, '|', sizeof clientsoft);
911 extract_token(flags, buf, 7, '|', sizeof flags);
913 idletime = timenow - extract_long(buf, 5);
914 idlehours = idletime / 3600;
915 idlemins = (idletime - (idlehours * 3600)) / 60;
916 idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
918 if (idletime > rc_idle_threshold) {
920 while (strlen(roomname) < 20) {
921 strcat(roomname, " ");
923 strcpy(&roomname[14], "[idle]");
930 extract_token(actual_user, buf, 8, '|', sizeof actual_user);
931 extract_token(actual_room, buf, 9, '|', sizeof actual_room);
932 extract_token(actual_host, buf, 10, '|', sizeof actual_host);
934 pprintf(" Flags: %s\n", flags);
935 pprintf("Session: %d\n", extract_int(buf, 0));
936 pprintf(" Name: %s\n", username);
937 pprintf("In room: %s\n", roomname);
938 pprintf(" Host: %s\n", fromhost);
939 pprintf(" Client: %s\n", clientsoft);
940 pprintf(" Idle: %ld:%02ld:%02ld\n",
945 if ( (strlen(actual_user)+strlen(actual_room)+strlen(actual_host)) > 0) {
947 if (strlen(actual_user)>0) pprintf("<%s> ", actual_user);
948 if (strlen(actual_room)>0) pprintf("in <%s> ", actual_room);
949 if (strlen(actual_host)>0) pprintf("from <%s> ", actual_host);
956 if (extract_int(buf, 0) == last_session) {
959 color(BRIGHT_MAGENTA);
960 pprintf("%-3s", flags);
962 last_session = extract_int(buf, 0);
964 pprintf("%-25s ", username);
965 color(BRIGHT_MAGENTA);
967 pprintf("%-20s ", roomname);
968 if (idletime > rc_idle_threshold) {
969 /* over 1000d, must be gone fishing */
970 if (idlehours > 23999) {
973 } else if (idlehours > 239) {
977 } else if (idlehours > 9) {
978 pprintf("%1ldd%02ld",
981 /* less than 10 hours */
983 pprintf("%1ld:%02ld",
984 idlehours, idlemins);
993 pprintf("%-24s\n", fromhost);
1002 void enternew(CtdlIPC *ipc, char *desc, char *buf, int maxlen)
1005 snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
1006 newprompt(bbb, buf, maxlen);
1011 int shift(int argc, char **argv, int start, int count) {
1014 for (i=start; i<(argc-count); ++i) {
1015 argv[i] = argv[i+count];
1017 argc = argc - count;
1021 static void statusHook(char *s) {
1029 int main(int argc, char **argv)
1032 char aaa[100], bbb[100];/* general purpose variables */
1033 char argbuf[32]; /* command line buf */
1034 char nonce[NONCE_SIZE];
1035 char *telnet_client_host = NULL;
1036 char *sptr, *sptr2; /* USed to extract the nonce */
1037 char hexstring[MD5_HEXSTRING_SIZE];
1038 int stored_password = 0;
1040 struct ctdlipcmisc chek;
1041 struct ctdluser *myself = NULL;
1042 CtdlIPC* ipc; /* Our server connection */
1043 int r; /* IPC result code */
1047 setIPCDeathHook(screen_delete);
1048 setIPCErrorPrintf(err_printf);
1049 setCryptoStatusHook(statusHook);
1051 /* Permissions sanity check - don't run citadel setuid/setgid */
1052 if (getuid() != geteuid()) {
1053 err_printf("Please do not run citadel setuid!\n");
1055 } else if (getgid() != getegid()) {
1056 err_printf("Please do not run citadel setgid!\n");
1060 stty_ctdl(SB_SAVE); /* Store the old terminal parameters */
1061 load_command_set(); /* parse the citadel.rc file */
1062 stty_ctdl(SB_NO_INTR); /* Install the new ones */
1063 /* signal(SIGHUP, dropcarr);FIXME */ /* Cleanup gracefully if carrier is dropped */
1064 signal(SIGPIPE, dropcarr); /* Cleanup gracefully if local conn. dropped */
1065 signal(SIGTERM, dropcarr); /* Cleanup gracefully if terminated */
1066 signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
1068 signal(SIGWINCH, scr_winch); /* Window resize signal */
1072 arg_encrypt = RC_DEFAULT;
1074 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1075 arg_screen = RC_DEFAULT;
1079 * Handle command line options as if we were called like /bin/login
1080 * (i.e. from in.telnetd)
1082 for (a=0; a<argc; ++a) {
1083 if ((argc > a+1) && (!strcmp(argv[a], "-h")) ) {
1084 telnet_client_host = argv[a+1];
1085 argc = shift(argc, argv, a, 2);
1087 if (!strcmp(argv[a], "-x")) {
1089 arg_encrypt = RC_NO;
1091 argc = shift(argc, argv, a, 1);
1093 if (!strcmp(argv[a], "-X")) {
1095 arg_encrypt = RC_YES;
1096 argc = shift(argc, argv, a, 1);
1098 fprintf(stderr, "Not compiled with encryption support");
1102 if (!strcmp(argv[a], "-s")) {
1103 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1106 argc = shift(argc, argv, a, 1);
1108 if (!strcmp(argv[a], "-S")) {
1109 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1110 arg_screen = RC_YES;
1112 argc = shift(argc, argv, a, 1);
1114 if (!strcmp(argv[a], "-p")) {
1117 if (chdir(CTDLDIR) < 0) {
1118 perror("can't change to " CTDLDIR);
1122 calc_dirs_n_files(relh, home, relhome, ctdldir);
1125 * Drop privileges if necessary. We stat
1126 * citadel.config to get the uid/gid since it's
1127 * guaranteed to have the uid/gid we want.
1129 if (!getuid() || !getgid()) {
1130 if (stat(file_citadel_config, &st) < 0) {
1131 perror("couldn't stat citadel.config");
1134 if (!getgid() && (setgid(st.st_gid) < 0)) {
1135 perror("couldn't change gid");
1138 if (!getuid() && (setuid(st.st_uid) < 0)) {
1139 perror("couldn't change uid");
1143 scr_printf("Privileges changed to uid %d gid %d\n",
1144 getuid(), getgid());
1147 argc = shift(argc, argv, a, 1);
1154 newprompt("Connect to (return for local server): ", hostbuf, 64);
1157 sln_printf("Attaching to server... \r");
1159 ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
1162 error_printf("Can't connect: %s\n", strerror(errno));
1165 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1166 CtdlIPC_SetNetworkStatusCallback(ipc, wait_indicator);
1168 ipc_for_signal_handlers = ipc; /* KLUDGE cover your eyes */
1170 CtdlIPC_chat_recv(ipc, aaa);
1171 if (aaa[0] != '2') {
1172 scr_printf("%s\n", &aaa[4]);
1173 logoff(ipc, atoi(aaa));
1176 /* If there is a [nonce] at the end, put the nonce in <nonce>, else nonce
1180 if ((sptr = strchr(aaa, '<')) == NULL)
1186 if ((sptr2 = strchr(sptr, '>')) == NULL)
1194 strncpy(nonce, sptr, (size_t)NONCE_SIZE);
1199 /* Evaluate encryption preferences */
1200 if (arg_encrypt != RC_NO && rc_encrypt != RC_NO) {
1201 if (!ipc->isLocal || arg_encrypt == RC_YES || rc_encrypt == RC_YES) {
1202 secure = (CtdlIPCStartEncryption(ipc, aaa) / 100 == 2) ? 1 : 0;
1204 error_printf("Can't encrypt: %s\n", aaa);
1209 get_serv_info(ipc, telnet_client_host);
1210 scr_printf("%-24s\n%s\n%s\n", ipc->ServInfo.software, ipc->ServInfo.humannode,
1211 ipc->ServInfo.site_location);
1214 status_line(ipc->ServInfo.humannode, ipc->ServInfo.site_location, NULL,
1217 screenwidth = 80; /* default screen dimensions */
1220 scr_printf(" pause next stop\n");
1221 scr_printf(" ctrl-s ctrl-o ctrl-c\n\n");
1222 formout(ipc, "hello"); /* print the opening greeting */
1225 GSTA: /* See if we have a username and password on disk */
1226 if (rc_remember_passwords) {
1227 get_stored_password(hostbuf, portbuf, fullname, password);
1228 if (strlen(fullname) > 0) {
1229 r = CtdlIPCTryLogin(ipc, fullname, aaa);
1232 r = CtdlIPCTryApopPassword(ipc, make_apop_string(password, nonce, hexstring, sizeof hexstring), aaa);
1234 r = CtdlIPCTryPassword(ipc, password, aaa);
1239 load_user_info(aaa);
1240 stored_password = 1;
1243 set_stored_password(hostbuf, portbuf, "", "");
1251 if (strlen(rc_username) > 0) {
1252 strcpy(fullname, rc_username);
1254 newprompt("Enter your name: ", fullname, 29);
1257 if (!strcasecmp(fullname, "new")) { /* just in case */
1258 scr_printf("Please enter the name you wish to log in with.\n");
1261 (!strcasecmp(fullname, "bbs"))
1262 || (!strcasecmp(fullname, "new"))
1263 || (strlen(fullname) == 0));
1265 if (!strcasecmp(fullname, "off")) {
1269 /* sign on to the server */
1270 r = CtdlIPCTryLogin(ipc, fullname, aaa);
1274 /* password authentication */
1275 if (strlen(rc_password) > 0) {
1276 strcpy(password, rc_password);
1278 newprompt("\rPlease enter your password: ", password, -19);
1283 r = CtdlIPCTryApopPassword(ipc, make_apop_string(password, nonce, hexstring, sizeof hexstring), aaa);
1285 r = CtdlIPCTryPassword(ipc, password, aaa);
1289 load_user_info(aaa);
1290 offer_to_remember_password(ipc, hostbuf, portbuf,
1291 fullname, password);
1294 scr_printf("<< wrong password >>\n");
1295 if (strlen(rc_password) > 0)
1299 NEWUSR: if (strlen(rc_password) == 0) {
1300 scr_printf("'%s' not found.\n"
1301 "Do you want to create a new account with this name? ",
1307 r = CtdlIPCCreateUser(ipc, fullname, 1, aaa);
1309 scr_printf("%s\n", aaa);
1312 load_user_info(aaa);
1314 while (set_password(ipc) != 0);
1317 enter_config(ipc, 1);
1320 /* Switch color support on or off if we're in user mode */
1321 if (rc_ansi_color == 3) {
1322 if (userflags & US_COLOR)
1328 color(BRIGHT_WHITE);
1329 scr_printf("\n%s\nAccess level: %d (%s)\n"
1330 "User #%ld / Login #%d",
1331 fullname, axlevel, axdefs[(int) axlevel],
1332 usernum, timescalled);
1333 if (lastcall > 0L) {
1334 scr_printf(" / Last login: %s",
1335 asctime(localtime(&lastcall)));
1339 r = CtdlIPCMiscCheck(ipc, &chek, aaa);
1345 scr_printf("*** You have a new private message in Mail>\n");
1347 scr_printf("*** You have %d new private messages in Mail>\n", b);
1349 if (strlen(rc_gotmail_cmd) > 0) {
1350 system(rc_gotmail_cmd);
1353 if ((axlevel >= 6) && (chek.needvalid > 0)) {
1354 scr_printf("*** Users need validation\n");
1356 if (chek.needregis > 0) {
1357 scr_printf("*** Please register.\n");
1358 formout(ipc, "register");
1362 /* Make up some temporary filenames for use in various parts of the
1363 * program. Don't mess with these once they've been set, because we
1364 * will be unlinking them later on in the program and we don't
1365 * want to delete something that we didn't create. */
1366 CtdlMakeTempFileName(temp, sizeof temp);
1367 CtdlMakeTempFileName(temp2, sizeof temp2);
1368 CtdlMakeTempFileName(tempdir, sizeof tempdir);
1370 /* Get screen dimensions. First we go to a default of 80x24. Then
1371 * we try to get the user's actual screen dimensions off the server.
1372 * However, if we're running on an xterm, all this stuff is
1373 * irrelevant because we're going to dynamically size the screen
1374 * during the session.
1378 r = CtdlIPCGetConfig(ipc, &myself, aaa);
1380 screenwidth = myself->USscreenwidth;
1381 screenheight = myself->USscreenheight;
1383 if (getenv("TERM") != NULL)
1384 if (!strcmp(getenv("TERM"), "xterm")) {
1388 check_screen_dims();
1391 set_floor_mode(ipc);
1393 /* Enter the lobby */
1394 dotgoto(ipc, "_BASEROOM_", 1, 0);
1396 /* Main loop for the system... user is logged in. */
1400 readmsgs(ipc, LastMessages, ReadForward, 5);
1402 readmsgs(ipc, NewMessages, ReadForward, 0);
1404 /* MAIN COMMAND LOOP */
1406 mcmd = getcmd(ipc, argbuf); /* Get keyboard command */
1409 check_screen_dims(); /* if xterm, get screen size */
1415 formout(ipc, "help");
1418 entmsg(ipc, 0, ((userflags & US_EXTEDIT) ? 2 : 0));
1428 /* Only m.author is used */
1429 struct ctdlipcmessage m;
1430 newprompt("What do you want your username to be? ",
1431 m.author, USERNAME_SIZE - 1);
1433 r = CtdlIPCPostMessage(ipc, 2, &m, aaa);
1435 scr_printf("%s\n", aaa);
1440 case 5: /* <G>oto */
1444 case 47: /* <A>bandon */
1445 if (!rc_alt_semantics) {
1450 case 90: /* <.A>bandon */
1451 if (!rc_alt_semantics)
1453 dotgoto(ipc, argbuf, 0, 0);
1455 case 58: /* <M>ail */
1457 dotgoto(ipc, "_MAIL_", 1, 0);
1460 if (strlen(argbuf) > 0) {
1462 dotgoto(ipc, argbuf, 0, 0);
1466 if (strlen(argbuf) > 0) {
1467 if (rc_alt_semantics) {
1470 dotgoto(ipc, argbuf, 0, 0);
1473 case 95: /* what exactly is the numbering scheme supposed to be anyway? --Ford, there isn't one. -IO */
1474 dotungoto(ipc, argbuf);
1477 readmsgs(ipc, AllMessages, ReadForward, 0);
1480 readmsgs(ipc, LastMessages, ReadForward, 5);
1483 readmsgs(ipc, NewMessages, ReadForward, 0);
1486 readmsgs(ipc, AllMessages, ReadReverse, 0);
1489 readmsgs(ipc, OldMessages, ReadReverse, 0);
1492 readmsgs(ipc, LastMessages, ReadForward,
1509 userlist(ipc, argbuf);
1567 if (!rc_alt_semantics) {
1579 cli_image_upload(ipc, "_userpic_");
1582 cli_image_upload(ipc, "_roompic_");
1586 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1587 cli_image_upload(ipc, aaa);
1591 enternew(ipc, "roomname", aaa, 20);
1592 r = CtdlIPCChangeRoomname(ipc, aaa, bbb);
1594 scr_printf("\n%s\n", bbb);
1597 enternew(ipc, "hostname", aaa, 25);
1598 r = CtdlIPCChangeHostname(ipc, aaa, bbb);
1600 scr_printf("\n%s\n", bbb);
1603 enternew(ipc, "username", aaa, 32);
1604 r = CtdlIPCChangeUsername(ipc, aaa, bbb);
1606 scr_printf("\n%s\n", bbb);
1616 display_help(ipc, argbuf);
1620 formout(ipc, "register");
1625 scr_printf("Are you sure (y/n)? ");
1627 if (!rc_alt_semantics)
1635 scr_printf("All users will be disconnected! "
1636 "Really terminate the server? ");
1638 if (!rc_alt_semantics)
1640 r = CtdlIPCTerminateServerNow(ipc, aaa);
1641 scr_printf("%s\n", aaa);
1650 scr_printf("Do you really want to schedule a "
1651 "server shutdown? ");
1653 r = CtdlIPCTerminateServerScheduled(ipc, 1, aaa);
1657 "The Citadel server will terminate when all users are logged off.\n"
1661 "The Citadel server will not terminate.\n"
1669 network_config_management(ipc, "listrecp",
1670 "Message-by-message mailing list recipients");
1674 network_config_management(ipc, "digestrecp",
1675 "Digest mailing list recipients");
1679 network_config_management(ipc, "ignet_push_share",
1680 "Nodes with which we share this room");
1684 do_ignet_configuration(ipc);
1688 do_filterlist_configuration(ipc);
1692 if (rc_alt_semantics) {
1705 stty_ctdl(SB_RESTORE);
1706 snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1707 "exec ./subsystem %ld %d %d", fullname,
1708 usernum, screenwidth, axlevel);
1710 stty_ctdl(SB_NO_INTR);
1713 scr_printf("*** Can't run doors when server is not local.\n");
1718 who_is_online(ipc, 0);
1722 who_is_online(ipc, 1);
1726 who_is_online(ipc, 2);
1730 do_system_configuration(ipc);
1734 do_internet_configuration(ipc);
1738 check_message_base(ipc);
1750 enter_config(ipc, 2);
1754 enter_config(ipc, 0);
1755 set_floor_mode(ipc);
1759 enter_config(ipc, 3);
1760 set_floor_mode(ipc);
1764 gotofloor(ipc, argbuf, GF_GOTO);
1768 gotofloor(ipc, argbuf, GF_SKIP);
1772 forget_this_floor(ipc);
1804 knrooms(ipc, floor_mode);
1814 misc_server_cmd(ipc, argbuf);
1818 edit_system_message(ipc, argbuf);
1842 default: /* allow some math on the command */
1843 /* commands 100... to 100+MAX_EDITORS-1 will
1844 call the appropriate editor... in other
1845 words, command numbers 100+ will match
1846 the citadel.rc keys editor0, editor1, etc.*/
1847 if (mcmd >= 100 && mcmd < (100+MAX_EDITORS))
1849 /* entmsg mode >=2 select editor */
1850 entmsg(ipc, 0, mcmd - 100 + 2);
1854 } while (termn8 == 0);
1856 TERMN8: scr_printf("%s logged out.", fullname);
1858 color(ORIGINAL_PAIR);
1860 while (march != NULL) {
1861 remove_march(march->march_name, 0);
1864 sln_printf("\n\nType 'off' to disconnect, or next user...\n");
1867 if ((mcmd == 29) || (mcmd == 15)) {
1869 stty_ctdl(SB_RESTORE);
1870 formout(ipc, "goodbye");