4 * citadel.c - Main source file.
17 #include <sys/types.h>
20 #include <sys/ioctl.h>
28 #include "serv_info.h"
30 #include "routines2.h"
35 #include "client_chat.h"
36 #include "client_passwords.h"
37 #include "citadel_decls.h"
45 char march_name[ROOMNAMELEN];
50 #define IFEXPERT if (userflags&US_EXPERT)
51 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
52 #define IFAIDE if (axlevel>=6)
53 #define IFNAIDE if (axlevel<6)
55 struct march *march = NULL;
57 /* globals associated with the client program */
58 char temp[PATH_MAX]; /* Name of general temp file */
59 char temp2[PATH_MAX]; /* Name of general temp file */
60 char tempdir[PATH_MAX]; /* Name of general temp dir */
61 char editor_path[256]; /* path to external editor */
62 char printcmd[256]; /* print command */
63 int editor_pid = (-1);
66 struct CtdlServInfo serv_info; /* Info on the server connected */
70 char room_name[ROOMNAMELEN];
71 char ugname[ROOMNAMELEN];
72 long uglsn; /* holds <u>ngoto info */
73 char is_mail = 0; /* nonzero when we're in a mail room */
74 char axlevel = 0; /* access level */
75 char is_room_aide = 0; /* boolean flag, 1 if room aide */
79 long usernum = 0L; /* user number */
81 long highest_msg_read; /* used for <A>bandon room cmd */
82 long maxmsgnum; /* used for <G>oto */
84 char have_xterm = 0; /* are we running on an xterm? */
91 char curr_floor = 0; /* number of current floor */
92 char floorlist[128][256]; /* names of floors */
93 char express_msgs = 0; /* express messages waiting! */
96 * here is our 'clean up gracefully and exit' routine
100 if (editor_pid > 0) { /* kill the editor if it's running */
101 kill(editor_pid, SIGHUP);
103 /* shut down the server... but not if the logoff code is 3, because
104 * that means we're exiting because we already lost the server
110 * now clean up various things
117 /* Violently kill off any child processes if Citadel is
120 if (getppid() == 1) {
121 kill(0 - getpgrp(), SIGTERM);
123 kill(0 - getpgrp(), SIGKILL);
125 sttybbs(SB_RESTORE); /* return the old terminal settings */
126 exit(code); /* exit with the proper exit code */
132 * We handle "next" and "stop" much differently than in earlier versions.
133 * The signal catching routine simply sets a flag and returns.
135 void sighandler(int which_sig)
137 signal(SIGINT, SIG_IGN);
138 signal(SIGQUIT, SIG_IGN);
139 sigcaught = which_sig;
145 * signal catching function for hangups...
147 void dropcarr(int signum)
155 * catch SIGCONT to reset terminal modes when were are put back into the
158 void catch_sigcont(int signum)
161 signal(SIGCONT, catch_sigcont);
166 /* general purpose routines */
168 void formout(char *name)
169 { /* display a file */
171 snprintf(cmd, sizeof cmd, "MESG %s", name);
175 printf("%s\n", &cmd[4]);
178 fmout(screenwidth, NULL,
179 ((userflags & US_PAGINATOR) ? 1 : 0),
184 void userlist(char *patn)
194 pprintf("%s\n", &buf[4]);
198 sttybbs(SB_YES_INTR);
199 pprintf(" User Name Num L LastCall Calls Posts\n");
200 pprintf("------------------------- ----- - ---------- ----- -----\n");
201 while (serv_gets(buf), strcmp(buf, "000")) {
202 if (sigcaught == 0) {
204 if (pattern(fl, patn) >= 0) {
205 pprintf("%-25s ", fl);
206 pprintf("%5ld %d ", extract_long(buf, 2),
207 extract_int(buf, 1));
208 lc = extract_long(buf, 3);
209 tmbuf = (struct tm *) localtime(&lc);
210 pprintf("%02d/%02d/%04d ",
213 (tmbuf->tm_year + 1900));
214 pprintf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
225 * grab assorted info about the user...
227 void load_user_info(char *params)
229 extract(fullname, params, 0);
230 axlevel = extract_int(params, 1);
231 timescalled = extract_int(params, 2);
232 posted = extract_int(params, 3);
233 userflags = extract_int(params, 4);
234 usernum = extract_long(params, 5);
239 * Remove a room from the march list. 'floornum' is ignored unless
240 * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
241 * floor will be removed from the march list.
243 void remove_march(char *roomname, int floornum)
245 struct march *mptr, *mptr2;
250 if ((!strcasecmp(march->march_name, roomname))
251 || ((!strcasecmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
258 for (mptr = march; mptr != NULL; mptr = mptr->next) {
260 if ((!strcasecmp(mptr->march_name, roomname))
261 || ((!strcasecmp(roomname, "_FLOOR_"))
262 && (mptr->march_floor == floornum))) {
264 mptr2->next = mptr->next;
275 * Locate the room on the march list which we most want to go to. Each room
276 * is measured given a "weight" of preference based on various factors.
278 char *pop_march(int desired_floor)
280 static char TheRoom[ROOMNAMELEN];
282 int TheOrder = 32767;
285 struct march *mptr = NULL;
287 strcpy(TheRoom, "_BASEROOM_");
291 for (mptr = march; mptr != NULL; mptr = mptr->next) {
293 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
294 weight = weight + 10000;
295 if (mptr->march_floor == desired_floor)
296 weight = weight + 5000;
298 weight = weight + ((128 - (mptr->march_floor)) * 128);
299 weight = weight + (128 - (mptr->march_order));
301 if (weight > TheWeight) {
303 strcpy(TheRoom, mptr->march_name);
304 TheFloor = mptr->march_floor;
305 TheOrder = mptr->march_order;
313 * jump directly to a room
315 void dotgoto(char *towhere, int display_name)
317 char aaa[256], bbb[256], psearch[256];
320 static int oldmailcount = (-1);
321 int partial_match, best_match;
324 /* store ungoto information */
325 strcpy(ugname, room_name);
328 /* first try an exact match */
329 snprintf(aaa, sizeof aaa, "GOTO %s", towhere);
334 if (!strncmp(aaa, "54", 2)) {
335 newprompt("Enter room password: ", bbb, 9);
336 snprintf(aaa, sizeof aaa, "GOTO %s|%s", towhere, bbb);
342 if (!strncmp(aaa, "54", 2)) {
343 printf("Wrong password.\n");
347 * If a match is not found, try a partial match.
348 * Partial matches anywhere in the string carry a weight of 1,
349 * left-aligned matches carry a weight of 2. Pick the room that
350 * has the highest-weighted match.
358 while (serv_gets(aaa), strcmp(aaa, "000")) {
359 extract(psearch, aaa, 0);
361 if (pattern(psearch, towhere) >= 0) {
364 if (!strncasecmp(towhere, psearch, strlen(towhere))) {
367 if (partial_match > best_match) {
368 strcpy(bbb, psearch);
369 best_match = partial_match;
372 if (strlen(bbb) == 0) {
373 printf("No room '%s'.\n", towhere);
376 snprintf(aaa, sizeof aaa, "GOTO %s", bbb);
386 extract(room_name, &aaa[4], 0);
387 room_flags = extract_int(&aaa[4], 4);
388 from_floor = curr_floor;
389 curr_floor = extract_int(&aaa[4], 10);
391 remove_march(room_name, 0);
392 if (!strcasecmp(towhere, "_BASEROOM_"))
393 remove_march(towhere, 0);
394 if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
395 if (floorlist[(int) curr_floor][0] == 0)
397 printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
399 if (display_name == 1) {
401 printf("%s ", room_name);
405 if (display_name != 2) {
406 color(BRIGHT_YELLOW);
407 printf("%d ", extract_int(&aaa[4], 1));
410 color(BRIGHT_YELLOW);
411 printf("%d ", extract_int(&aaa[4], 2));
413 printf("messages.\n");
415 highest_msg_read = extract_int(&aaa[4], 6);
416 maxmsgnum = extract_int(&aaa[4], 5);
417 is_mail = (char) extract_int(&aaa[4], 7);
418 is_room_aide = (char) extract_int(&aaa[4], 8);
419 ls = extract_long(&aaa[4], 6);
421 /* read info file if necessary */
422 if (extract_int(&aaa[4], 3) > 0)
425 /* check for newly arrived mail if we can */
426 if (num_parms(&aaa[4]) >= 10) {
427 newmailcount = extract_int(&aaa[4], 9);
428 if ((oldmailcount >= 0) && (newmailcount > oldmailcount)) {
430 printf("*** You have new mail\n");
433 oldmailcount = newmailcount;
437 /* Goto next room having unread messages.
438 * We want to skip over rooms that the user has already been to, and take the
439 * user back to the lobby when done. The room we end up in is placed in
440 * newroom - which is set to 0 (the lobby) initially.
445 struct march *mptr, *mptr2;
446 char next_room[ROOMNAMELEN];
448 /* Check to see if the march-mode list is already allocated.
449 * If it is, pop the first room off the list and go there.
455 while (serv_gets(buf), strcmp(buf, "000")) {
456 mptr = (struct march *) malloc(sizeof(struct march));
458 extract(mptr->march_name, buf, 0);
459 mptr->march_floor = (char) (extract_int(buf, 2) & 0x7F);
460 mptr->march_order = (char) (extract_int(buf, 3) & 0x7F);
465 while (mptr2->next != NULL)
470 /* add _BASEROOM_ to the end of the march list, so the user will end up
471 * in the system base room (usually the Lobby>) at the end of the loop
473 mptr = (struct march *) malloc(sizeof(struct march));
475 strcpy(mptr->march_name, "_BASEROOM_");
480 while (mptr2->next != NULL)
485 * ...and remove the room we're currently in, so a <G>oto doesn't make us
486 * walk around in circles
488 remove_march(room_name, 0);
491 strcpy(next_room, pop_march(curr_floor));
493 strcpy(next_room, "_BASEROOM_");
495 remove_march(next_room, 0);
496 dotgoto(next_room, 1);
500 * forget all rooms on a given floor
502 void forget_all_rooms_on(int ffloor)
505 struct march *flist, *fptr;
507 printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
509 snprintf(buf, sizeof buf, "LKRA %d", ffloor);
513 printf("%-72s\n", &buf[4]);
517 while (serv_gets(buf), strcmp(buf, "000")) {
518 fptr = (struct march *) malloc(sizeof(struct march));
521 extract(fptr->march_name, buf, 0);
523 while (flist != NULL) {
524 snprintf(buf, sizeof buf, "GOTO %s", flist->march_name);
535 printf("%-72s\r", "");
540 * routine called by gotofloor() to move to a new room on a new floor
542 void gf_toroom(char *towhere, int mode)
544 int floor_being_left;
546 floor_being_left = curr_floor;
548 if (mode == GF_GOTO) { /* <;G>oto mode */
552 if (mode == GF_SKIP) { /* <;S>kip mode */
554 remove_march("_FLOOR_", floor_being_left);
556 if (mode == GF_ZAP) { /* <;Z>ap mode */
558 remove_march("_FLOOR_", floor_being_left);
559 forget_all_rooms_on(floor_being_left);
567 void gotofloor(char *towhere, int mode)
571 char buf[256], targ[256];
573 if (floorlist[0][0] == 0)
576 for (a = 0; a < 128; ++a)
577 if (!strcasecmp(&floorlist[a][0], towhere))
581 for (a = 0; a < 128; ++a) {
582 if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
588 for (a = 0; a < 128; ++a)
589 if (pattern(towhere, &floorlist[a][0]) > 0)
593 printf("No floor '%s'.\n", towhere);
596 for (mptr = march; mptr != NULL; mptr = mptr->next) {
597 if ((mptr->march_floor) == tofloor)
598 gf_toroom(mptr->march_name, mode);
603 snprintf(buf, sizeof buf, "LKRA %d", tofloor);
607 while (serv_gets(buf), strcmp(buf, "000")) {
608 if ((extract_int(buf, 2) == tofloor) && (strlen(targ) == 0))
609 extract(targ, buf, 0);
611 if (strlen(targ) > 0) {
612 gf_toroom(targ, mode);
614 printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
620 * forget all rooms on current floor
622 void forget_this_floor(void)
625 if (curr_floor == 0) {
626 printf("Can't forget this floor.\n");
629 if (floorlist[0][0] == 0)
631 printf("Are you sure you want to forget all rooms on %s? ",
632 &floorlist[(int) curr_floor][0]);
636 gf_toroom("_BASEROOM_", GF_ZAP);
641 * Figure out the physical screen dimensions, if we can
643 void check_screen_dims(void)
647 unsigned short height; /* rows */
648 unsigned short width; /* columns */
649 unsigned short xpixels;
650 unsigned short ypixels; /* pixels */
653 if (have_xterm) { /* dynamically size screen if on an xterm */
654 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
656 screenheight = (int) xwinsz.height;
658 screenwidth = (int) xwinsz.width;
666 * set floor mode depending on client, server, and user settings
668 void set_floor_mode(void)
670 if (serv_info.serv_ok_floors == 0) {
671 floor_mode = 0; /* Don't use floors if the server */
673 /* doesn't support them! */
675 if (rc_floor_mode == RC_NO) { /* never use floors */
678 if (rc_floor_mode == RC_YES) { /* always use floors */
681 if (rc_floor_mode == RC_DEFAULT) { /* user choice */
682 floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
688 * Set or change the user's password
690 int set_password(void)
696 if (strlen(rc_password) > 0) {
697 strcpy(pass1, rc_password);
698 strcpy(pass2, rc_password);
700 IFNEXPERT formout("changepw");
701 newprompt("Enter a new password: ", pass1, -19);
702 newprompt("Enter it again to confirm: ", pass2, -19);
706 if (!strcasecmp(pass1, pass2)) {
707 snprintf(buf, sizeof buf, "SETP %s", pass1);
710 printf("%s\n", &buf[4]);
711 offer_to_remember_password(hostbuf, portbuf, fullname, pass1);
714 printf("*** They don't match... try again.\n");
722 * get info about the server we've connected to
724 void get_serv_info(void)
728 CtdlInternalGetServInfo(&serv_info);
730 /* be nice and identify ourself to the server */
731 snprintf(buf, sizeof buf, "IDEN %d|%d|%d|%s|",
732 SERVER_TYPE, 0, REV_LEVEL,
733 (server_is_local ? "local" : CITADEL));
734 locate_host(&buf[strlen(buf)]); /* append to the end */
736 serv_gets(buf); /* we don't care about the result code */
744 * Display list of users currently logged on to the server
746 void who_is_online(int longlist)
748 char buf[128], username[128], roomname[128], fromhost[128],
750 char tbuf[128], clientsoft[128];
752 time_t idletime, idlehours, idlemins, idlesecs;
753 int last_session = (-1);
758 if (tbuf[0] == '2') {
759 timenow = extract_long(&tbuf[4], 0);
765 pprintf("FLG ### User Name Room From host\n");
767 pprintf("--- --- ------------------------- -------------------- ------------------------\n");
772 while (serv_gets(buf), strcmp(buf, "000")) {
773 extract(username, buf, 1);
774 extract(roomname, buf, 2);
775 extract(fromhost, buf, 3);
776 extract(clientsoft, buf, 4);
777 extract(flags, buf, 7);
780 idletime = timenow - extract_long(buf, 5);
781 idlehours = idletime / 3600;
782 idlemins = (idletime - (idlehours * 3600)) / 60;
783 idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
784 pprintf("\nFlags: %-3s Sess# %-3d Name: %-25s Room: %s\n",
785 flags, extract_int(buf, 0), username, roomname);
786 pprintf("from <%s> using <%s>, idle %ld:%02ld:%02ld\n",
787 fromhost, clientsoft,
788 (long) idlehours, (long) idlemins, (long) idlesecs);
791 if (extract_int(buf, 0) == last_session) {
794 color(BRIGHT_MAGENTA);
795 pprintf("%-3s ", flags);
797 pprintf("%-3d ", extract_int(buf, 0));
799 last_session = extract_int(buf, 0);
801 pprintf("%-25s ", username);
802 color(BRIGHT_MAGENTA);
804 pprintf("%-20s ", roomname);
806 pprintf("%-24s\n", fromhost);
813 void enternew(char *desc, char *buf, int maxlen)
816 snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
817 newprompt(bbb, buf, maxlen);
823 int main(int argc, char **argv)
826 char aaa[100], bbb[100];/* general purpose variables */
827 char argbuf[32]; /* command line buf */
828 volatile int termn8 = 0;
829 int stored_password = 0;
832 sttybbs(SB_SAVE); /* Store the old terminal parameters */
833 load_command_set(); /* parse the citadel.rc file */
834 sttybbs(SB_NO_INTR); /* Install the new ones */
835 signal(SIGINT, SIG_IGN);
836 signal(SIGQUIT, SIG_IGN);
837 signal(SIGHUP, dropcarr); /* Cleanup gracefully if carrier is dropped */
838 signal(SIGTERM, dropcarr); /* Cleanup gracefully if terminated */
839 signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
841 printf("Attaching to server... \r");
843 attach_to_server(argc, argv, hostbuf, portbuf);
849 printf("%s\n", &aaa[4]);
858 printf("%-23s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
859 serv_info.serv_bbs_city);
860 screenwidth = 80; /* default screen dimensions */
863 printf(" pause next stop\n");
864 printf(" ctrl-s ctrl-o ctrl-c\n\n");
865 formout("hello"); /* print the opening greeting */
868 GSTA: /* See if we have a username and password on disk */
869 if (rc_remember_passwords) {
870 get_stored_password(hostbuf, portbuf, fullname, password);
871 if (strlen(fullname) > 0) {
872 sprintf(aaa, "USER %s", fullname);
875 sprintf(aaa, "PASS %s", password);
879 load_user_info(&aaa[4]);
884 set_stored_password(hostbuf, portbuf, "", "");
892 if (strlen(rc_username) > 0) {
893 strcpy(fullname, rc_username);
895 newprompt("Enter your name: ", fullname, 29);
898 if (!strcasecmp(fullname, "new")) { /* just in case */
899 printf("Please enter the name you wish to log in with.\n");
902 (!strcasecmp(fullname, "bbs"))
903 || (!strcasecmp(fullname, "new"))
904 || (strlen(fullname) == 0));
906 if (!strcasecmp(fullname, "off")) {
910 /* sign on to the server */
911 snprintf(aaa, sizeof aaa, "USER %s", fullname);
917 /* password authentication */
918 if (strlen(rc_password) > 0) {
919 strcpy(password, rc_password);
921 newprompt("\rPlease enter your password: ", password, -19);
924 snprintf(aaa, sizeof aaa, "PASS %s", password);
928 load_user_info(&aaa[4]);
929 offer_to_remember_password(hostbuf, portbuf,
933 printf("<< wrong password >>\n");
934 if (strlen(rc_password) > 0)
938 NEWUSR: if (strlen(rc_password) == 0) {
939 printf("No record. Enter as new user? ");
943 snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
950 load_user_info(&aaa[4]);
952 while (set_password() != 0);;
958 PWOK: printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
959 fullname, axlevel, axdefs[(int) axlevel],
960 usernum, timescalled);
965 b = extract_int(&aaa[4], 0);
969 printf("*** You have a new private message in Mail>\n");
971 printf("*** You have %d new private messages in Mail>\n", b);
974 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
975 printf("*** Users need validation\n");
977 if (extract_int(&aaa[4], 1) > 0) {
978 printf("*** Please register.\n");
983 /* Make up some temporary filenames for use in various parts of the
984 * program. Don't mess with these once they've been set, because we
985 * will be unlinking them later on in the program and we don't
986 * want to delete something that we didn't create. */
987 snprintf(temp, sizeof temp, tmpnam(NULL));
988 snprintf(temp2, sizeof temp2, tmpnam(NULL));
989 snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
991 /* Get screen dimensions. First we go to a default of 80x24. Then
992 * we try to get the user's actual screen dimensions off the server.
993 * However, if we're running on an xterm, all this stuff is
994 * irrelevant because we're going to dynamically size the screen
995 * during the session.
1001 if (aaa[0] == '2') {
1002 screenwidth = extract_int(&aaa[4], 0);
1003 screenheight = extract_int(&aaa[4], 1);
1005 if (getenv("TERM") != NULL)
1006 if (!strcmp(getenv("TERM"), "xterm")) {
1010 check_screen_dims();
1016 /* Enter the lobby */
1017 dotgoto("_BASEROOM_", 1);
1019 /* Main loop for the system... user is logged in. */
1028 do { /* MAIN LOOP OF PROGRAM */
1030 signal(SIGINT, SIG_IGN);
1031 signal(SIGQUIT, SIG_IGN);
1032 mcmd = getcmd(argbuf);
1035 check_screen_dims();
1053 newprompt("What do you want your username to be? ", aaa, 32);
1054 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1057 if (strncmp("200", aaa, 3))
1058 printf("\n%s\n", aaa);
1072 dotgoto("_MAIL_", 1);
1089 readmsgs(0, (-1), 0);
1092 readmsgs(2, (-1), 0);
1095 readmsgs(3, 1, atoi(argbuf));
1182 cli_image_upload("_userpic_");
1185 cli_image_upload("_roompic_");
1189 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1190 cli_image_upload(aaa);
1194 enternew("roomname", aaa, 20);
1195 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1198 if (strncmp("200", aaa, 3))
1199 printf("\n%s\n", aaa);
1202 enternew("hostname", aaa, 25);
1203 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1206 if (strncmp("200", aaa, 3))
1207 printf("\n%s\n", aaa);
1210 enternew("username", aaa, 32);
1211 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1214 if (strncmp("200", aaa, 3))
1215 printf("\n%s\n", aaa);
1225 display_help(argbuf);
1229 formout("register");
1234 printf("Are you sure (y/n)? ");
1251 if (server_is_local) {
1252 sttybbs(SB_RESTORE);
1253 snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1254 "exec ./subsystem %ld %d %d", fullname,
1255 usernum, screenwidth, axlevel);
1257 sttybbs(SB_NO_INTR);
1259 printf("*** Can't run doors when server is not local.\n");
1272 do_system_configuration();
1276 do_internet_configuration();
1280 check_message_base();
1302 gotofloor(argbuf, GF_GOTO);
1306 gotofloor(argbuf, GF_SKIP);
1310 forget_this_floor();
1338 knrooms(floor_mode);
1348 misc_server_cmd(argbuf);
1352 edit_system_message(argbuf);
1377 } while (termn8 == 0);
1379 TERMN8:printf("%s logged out.\n", fullname);
1380 while (march != NULL)
1381 remove_march(march->march_name, 0);
1383 printf("\n\nType 'off' to hang up, or next user...\n");
1384 snprintf(aaa, sizeof aaa, "LOUT");
1387 if ((mcmd == 29) || (mcmd == 15)) {