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_icq.h"
37 #include "citadel_decls.h"
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? */
89 char curr_floor = 0; /* number of current floor */
90 char floorlist[128][256]; /* names of floors */
91 char express_msgs = 0; /* express messages waiting! */
93 jmp_buf jmp_reconnect; /* for server reconnects */
97 void sigpipehandler(int nothing)
99 longjmp(jmp_reconnect, nothing);
103 * here is our 'clean up gracefully and exit' routine
105 void logoff(int code)
107 if (editor_pid > 0) { /* kill the editor if it's running */
108 kill(editor_pid, SIGHUP);
110 /* shut down the server... but not if the logoff code is 3, because
111 * that means we're exiting because we already lost the server
117 * now clean up various things
124 /* Violently kill off any child processes if Citadel is
127 if (getppid() == 1) {
128 kill(0 - getpgrp(), SIGTERM);
130 kill(0 - getpgrp(), SIGKILL);
132 sttybbs(SB_RESTORE); /* return the old terminal settings */
133 exit(code); /* exit with the proper exit code */
139 * We handle "next" and "stop" much differently than in earlier versions.
140 * The signal catching routine simply sets a flag and returns.
142 void sighandler(int which_sig)
144 signal(SIGINT, SIG_IGN);
145 signal(SIGQUIT, SIG_IGN);
146 sigcaught = which_sig;
152 * signal catching function for hangups...
154 void dropcarr(int signum)
162 * catch SIGCONT to reset terminal modes when were are put back into the
165 void catch_sigcont(int signum)
168 signal(SIGCONT, catch_sigcont);
173 /* general purpose routines */
175 void formout(char *name)
176 { /* display a file */
178 snprintf(cmd, sizeof cmd, "MESG %s", name);
182 printf("%s\n", &cmd[4]);
185 fmout(screenwidth, NULL,
186 ((userflags & US_PAGINATOR) ? 1 : 0),
202 printf("%s\n", &buf[4]);
206 sttybbs(SB_YES_INTR);
207 printf(" User Name Num L LastCall Calls Posts\n");
208 printf("------------------------- ----- - ---------- ----- -----\n");
209 while (serv_gets(buf), strcmp(buf, "000")) {
210 if (sigcaught == 0) {
212 printf("%-25s ", fl);
213 printf("%5ld %d ", extract_long(buf, 2),
214 extract_int(buf, 1));
215 lc = extract_long(buf, 3);
216 tmbuf = (struct tm *) localtime(&lc);
217 printf("%02d/%02d/%04d ",
220 (tmbuf->tm_year + 1900));
221 printf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
224 linecount = checkpagin(linecount,
225 ((userflags & US_PAGINATOR) ? 1 : 0),
236 * grab assorted info about the user...
238 void load_user_info(char *params)
240 extract(fullname, params, 0);
241 axlevel = extract_int(params, 1);
242 timescalled = extract_int(params, 2);
243 posted = extract_int(params, 3);
244 userflags = extract_int(params, 4);
245 usernum = extract_long(params, 5);
250 * Remove a room from the march list. 'floornum' is ignored unless
251 * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
252 * floor will be removed from the march list.
254 void remove_march(char *roomname, int floornum)
256 struct march *mptr, *mptr2;
261 if ((!strucmp(march->march_name, roomname))
262 || ((!strucmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
269 for (mptr = march; mptr != NULL; mptr = mptr->next) {
271 if ((!strucmp(mptr->march_name, roomname))
272 || ((!strucmp(roomname, "_FLOOR_"))
273 && (mptr->march_floor == floornum))) {
275 mptr2->next = mptr->next;
286 * Locate the room on the march list which we most want to go to. Each room
287 * is measured given a "weight" of preference based on various factors.
289 char *pop_march(int desired_floor)
291 static char TheRoom[ROOMNAMELEN];
293 int TheOrder = 32767;
296 struct march *mptr = NULL;
298 strcpy(TheRoom, "_BASEROOM_");
302 for (mptr = march; mptr != NULL; mptr = mptr->next) {
304 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
305 weight = weight + 10000;
306 if (mptr->march_floor == desired_floor)
307 weight = weight + 5000;
309 weight = weight + ((128 - (mptr->march_floor)) * 128);
310 weight = weight + (128 - (mptr->march_order));
312 if (weight > TheWeight) {
314 strcpy(TheRoom, mptr->march_name);
315 TheFloor = mptr->march_floor;
316 TheOrder = mptr->march_order;
324 * jump directly to a room
326 void dotgoto(char *towhere, int display_name)
328 char aaa[256], bbb[256], psearch[256];
331 static int oldmailcount = (-1);
332 int partial_match, best_match;
335 /* store ungoto information */
336 strcpy(ugname, room_name);
339 /* first try an exact match */
340 snprintf(aaa, sizeof aaa, "GOTO %s", towhere);
345 if (!strncmp(aaa, "54", 2)) {
346 newprompt("Enter room password: ", bbb, 9);
347 snprintf(aaa, sizeof aaa, "GOTO %s|%s", towhere, bbb);
353 if (!strncmp(aaa, "54", 2)) {
354 printf("Wrong password.\n");
358 * If a match is not found, try a partial match.
359 * Partial matches anywhere in the string carry a weight of 1,
360 * left-aligned matches carry a weight of 2. Pick the room that
361 * has the highest-weighted match.
369 while (serv_gets(aaa), strcmp(aaa, "000")) {
370 extract(psearch, aaa, 0);
372 if (pattern(psearch, towhere) >= 0) {
375 if (!struncmp(towhere, psearch, strlen(towhere))) {
378 if (partial_match > best_match) {
379 strcpy(bbb, psearch);
380 best_match = partial_match;
383 if (strlen(bbb) == 0) {
384 printf("No room '%s'.\n", towhere);
387 snprintf(aaa, sizeof aaa, "GOTO %s", bbb);
397 extract(room_name, &aaa[4], 0);
398 room_flags = extract_int(&aaa[4], 4);
399 from_floor = curr_floor;
400 curr_floor = extract_int(&aaa[4], 10);
402 remove_march(room_name, 0);
403 if (!strucmp(towhere, "_BASEROOM_"))
404 remove_march(towhere, 0);
405 if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
406 if (floorlist[(int) curr_floor][0] == 0)
408 printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
410 if (display_name == 1) {
412 printf("%s ", room_name);
416 if (display_name != 2) {
417 color(BRIGHT_YELLOW);
418 printf("%d ", extract_int(&aaa[4], 1));
421 color(BRIGHT_YELLOW);
422 printf("%d ", extract_int(&aaa[4], 2));
424 printf("messages.\n");
426 highest_msg_read = extract_int(&aaa[4], 6);
427 maxmsgnum = extract_int(&aaa[4], 5);
428 is_mail = (char) extract_int(&aaa[4], 7);
429 is_room_aide = (char) extract_int(&aaa[4], 8);
430 ls = extract_long(&aaa[4], 6);
432 /* read info file if necessary */
433 if (extract_int(&aaa[4], 3) > 0)
436 /* check for newly arrived mail if we can */
437 if (num_parms(&aaa[4]) >= 10) {
438 newmailcount = extract_int(&aaa[4], 9);
439 if ((oldmailcount >= 0) && (newmailcount > oldmailcount)) {
441 printf("*** You have new mail\n");
444 oldmailcount = newmailcount;
448 /* Goto next room having unread messages.
449 * We want to skip over rooms that the user has already been to, and take the
450 * user back to the lobby when done. The room we end up in is placed in
451 * newroom - which is set to 0 (the lobby) initially.
456 struct march *mptr, *mptr2;
459 /* Check to see if the march-mode list is already allocated.
460 * If it is, pop the first room off the list and go there.
466 while (serv_gets(buf), strcmp(buf, "000")) {
467 mptr = (struct march *) malloc(sizeof(struct march));
469 extract(mptr->march_name, buf, 0);
470 mptr->march_floor = (char) (extract_int(buf, 2) & 0x7F);
471 mptr->march_order = (char) (extract_int(buf, 3) & 0x7F);
476 while (mptr2->next != NULL)
481 /* add _BASEROOM_ to the end of the march list, so the user will end up
482 * in the system base room (usually the Lobby>) at the end of the loop
484 mptr = (struct march *) malloc(sizeof(struct march));
486 strcpy(mptr->march_name, "_BASEROOM_");
491 while (mptr2->next != NULL)
496 * ...and remove the room we're currently in, so a <G>oto doesn't make us
497 * walk around in circles
499 remove_march(room_name, 0);
502 strcpy(next_room, pop_march(curr_floor));
504 strcpy(next_room, "_BASEROOM_");
506 remove_march(next_room, 0);
507 dotgoto(next_room, 1);
511 * forget all rooms on a given floor
513 void forget_all_rooms_on(int ffloor)
516 struct march *flist, *fptr;
518 printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
520 snprintf(buf, sizeof buf, "LKRA %d", ffloor);
524 printf("%-72s\n", &buf[4]);
528 while (serv_gets(buf), strcmp(buf, "000")) {
529 fptr = (struct march *) malloc(sizeof(struct march));
532 extract(fptr->march_name, buf, 0);
534 while (flist != NULL) {
535 snprintf(buf, sizeof buf, "GOTO %s", flist->march_name);
546 printf("%-72s\r", "");
551 * routine called by gotofloor() to move to a new room on a new floor
553 void gf_toroom(char *towhere, int mode)
555 int floor_being_left;
557 floor_being_left = curr_floor;
559 if (mode == GF_GOTO) { /* <;G>oto mode */
563 if (mode == GF_SKIP) { /* <;S>kip mode */
565 remove_march("_FLOOR_", floor_being_left);
567 if (mode == GF_ZAP) { /* <;Z>ap mode */
569 remove_march("_FLOOR_", floor_being_left);
570 forget_all_rooms_on(floor_being_left);
578 void gotofloor(char *towhere, int mode)
582 char buf[256], targ[256];
584 if (floorlist[0][0] == 0)
587 for (a = 0; a < 128; ++a)
588 if (!strucmp(&floorlist[a][0], towhere))
592 for (a = 0; a < 128; ++a) {
593 if (!struncmp(&floorlist[a][0], towhere, strlen(towhere))) {
599 for (a = 0; a < 128; ++a)
600 if (pattern(towhere, &floorlist[a][0]) > 0)
604 printf("No floor '%s'.\n", towhere);
607 for (mptr = march; mptr != NULL; mptr = mptr->next) {
608 if ((mptr->march_floor) == tofloor)
609 gf_toroom(mptr->march_name, mode);
614 snprintf(buf, sizeof buf, "LKRA %d", tofloor);
618 while (serv_gets(buf), strcmp(buf, "000")) {
619 if ((extract_int(buf, 2) == tofloor) && (strlen(targ) == 0))
620 extract(targ, buf, 0);
622 if (strlen(targ) > 0) {
623 gf_toroom(targ, mode);
625 printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
631 * forget all rooms on current floor
633 void forget_this_floor(void)
636 if (curr_floor == 0) {
637 printf("Can't forget this floor.\n");
640 if (floorlist[0][0] == 0)
642 printf("Are you sure you want to forget all rooms on %s? ",
643 &floorlist[(int) curr_floor][0]);
647 gf_toroom("_BASEROOM_", GF_ZAP);
652 * Figure out the physical screen dimensions, if we can
654 void check_screen_dims(void)
658 unsigned short height; /* rows */
659 unsigned short width; /* columns */
660 unsigned short xpixels;
661 unsigned short ypixels; /* pixels */
664 if (have_xterm) { /* dynamically size screen if on an xterm */
665 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
667 screenheight = (int) xwinsz.height;
669 screenwidth = (int) xwinsz.width;
677 * set floor mode depending on client, server, and user settings
679 void set_floor_mode(void)
681 if (serv_info.serv_ok_floors == 0) {
682 floor_mode = 0; /* Don't use floors if the server */
684 /* doesn't support them! */
686 if (rc_floor_mode == RC_NO) { /* never use floors */
689 if (rc_floor_mode == RC_YES) { /* always use floors */
692 if (rc_floor_mode == RC_DEFAULT) { /* user choice */
693 floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
699 * Set or change the user's password
701 int set_password(void)
707 if (strlen(rc_password) > 0) {
708 strcpy(pass1, rc_password);
709 strcpy(pass2, rc_password);
711 IFNEXPERT formout("changepw");
712 newprompt("Enter a new password: ", pass1, -19);
713 newprompt("Enter it again to confirm: ", pass2, -19);
717 if (!strucmp(pass1, pass2)) {
718 snprintf(buf, sizeof buf, "SETP %s", pass1);
721 printf("%s\n", &buf[4]);
722 strcpy(re_password, pass1);
725 printf("*** They don't match... try again.\n");
733 * get info about the server we've connected to
735 void get_serv_info(void)
739 CtdlInternalGetServInfo(&serv_info);
741 /* be nice and identify ourself to the server */
742 snprintf(buf, sizeof buf, "IDEN %d|%d|%d|%s|",
743 SERVER_TYPE, 0, REV_LEVEL,
744 (server_is_local ? "local" : CITADEL));
745 locate_host(&buf[strlen(buf)]); /* append to the end */
747 serv_gets(buf); /* we don't care about the result code */
755 * Display list of users currently logged on to the server
757 void who_is_online(int longlist)
759 char buf[128], username[128], roomname[128], fromhost[128],
761 char tbuf[128], clientsoft[128];
763 time_t idletime, idlehours, idlemins, idlesecs;
764 int last_session = (-1);
769 if (tbuf[0] == '2') {
770 timenow = extract_long(&tbuf[4], 0);
776 printf("FLG ### User Name Room From host\n");
778 printf("--- --- ------------------------- -------------------- ------------------------\n");
783 while (serv_gets(buf), strcmp(buf, "000")) {
784 extract(username, buf, 1);
785 extract(roomname, buf, 2);
786 extract(fromhost, buf, 3);
787 extract(clientsoft, buf, 4);
788 extract(flags, buf, 7);
791 idletime = timenow - extract_long(buf, 5);
792 idlehours = idletime / 3600;
793 idlemins = (idletime - (idlehours * 3600)) / 60;
794 idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
795 printf("\nFlags: %-3s Sess# %-3d Name: %-25s Room: %s\n",
796 flags, extract_int(buf, 0), username, roomname);
797 printf("from <%s> using <%s>, idle %ld:%02ld:%02ld\n",
798 fromhost, clientsoft,
799 (long) idlehours, (long) idlemins, (long) idlesecs);
802 if (extract_int(buf, 0) == last_session) {
805 color(BRIGHT_MAGENTA);
806 printf("%-3s ", flags);
808 printf("%-3d ", extract_int(buf, 0));
810 last_session = extract_int(buf, 0);
812 printf("%-25s ", username);
813 color(BRIGHT_MAGENTA);
814 printf("%-20s ", roomname);
816 printf("%-24s\n", fromhost);
823 void enternew(char *desc, char *buf, int maxlen)
826 snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
827 newprompt(bbb, buf, maxlen);
833 int main(int argc, char **argv)
836 char aaa[100], bbb[100], eee[100]; /* general purpose variables */
837 char argbuf[32]; /* command line buf */
838 volatile int termn8 = 0;
841 sttybbs(SB_SAVE); /* Store the old terminal parameters */
842 load_command_set(); /* parse the citadel.rc file */
843 sttybbs(SB_NO_INTR); /* Install the new ones */
844 signal(SIGINT, SIG_IGN);
845 signal(SIGQUIT, SIG_IGN);
846 signal(SIGHUP, dropcarr); /* Cleanup gracefully if carrier is dropped */
847 signal(SIGTERM, dropcarr); /* Cleanup gracefully if terminated */
848 signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
850 printf("Attaching to server...\r");
852 attach_to_server(argc, argv);
858 printf("%s\n", &aaa[4]);
867 printf("%-22s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
868 serv_info.serv_bbs_city);
869 screenwidth = 80; /* default screen dimensions */
872 printf(" pause next stop\n");
873 printf(" ctrl-s ctrl-o ctrl-c\n\n");
874 formout("hello"); /* print the opening greeting */
880 if (strlen(rc_username) > 0) {
881 strcpy(fullname, rc_username);
883 newprompt("Enter your name: ", fullname, 29);
886 if (!strucmp(fullname, "new")) { /* just in case */
887 printf("Please enter the name you wish to log in with.\n");
890 (!strucmp(fullname, "bbs"))
891 || (!strucmp(fullname, "new"))
892 || (strlen(fullname) == 0));
894 if (!strucmp(fullname, "off")) {
898 /* sign on to the server */
899 strcpy(re_username, fullname);
900 snprintf(aaa, sizeof aaa, "USER %s", fullname);
906 /* password authentication */
907 if (strlen(rc_password) > 0) {
908 strcpy(eee, rc_password);
910 newprompt("\rPlease enter your password: ", eee, -19);
913 snprintf(aaa, sizeof aaa, "PASS %s", eee);
917 strcpy(re_password, eee);
918 load_user_info(&aaa[4]);
921 printf("<< wrong password >>\n");
922 if (strlen(rc_password) > 0)
926 NEWUSR:if (strlen(rc_password) == 0) {
927 printf("No record. Enter as new user? ");
931 snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
938 load_user_info(&aaa[4]);
940 while (set_password() != 0);;
946 PWOK:printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
947 fullname, axlevel, axdefs[(int) axlevel],
948 usernum, timescalled);
953 b = extract_int(&aaa[4], 0);
957 printf("*** You have a new private message in Mail>\n");
959 printf("*** You have %d new private messages in Mail>\n", b);
962 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
963 printf("*** Users need validation\n");
965 if (extract_int(&aaa[4], 1) > 0) {
966 printf("*** Please register.\n");
971 /* Make up some temporary filenames for use in various parts of the
972 * program. Don't mess with these once they've been set, because we
973 * will be unlinking them later on in the program and we don't
974 * want to delete something that we didn't create. */
975 snprintf(temp, sizeof temp, tmpnam(NULL));
976 snprintf(temp2, sizeof temp2, tmpnam(NULL));
977 snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
979 /* Get screen dimensions. First we go to a default of 80x24. Then
980 * we try to get the user's actual screen dimensions off the server.
981 * However, if we're running on an xterm, all this stuff is
982 * irrelevant because we're going to dynamically size the screen
983 * during the session.
990 screenwidth = extract_int(&aaa[4], 0);
991 screenheight = extract_int(&aaa[4], 1);
993 if (getenv("TERM") != NULL)
994 if (!strcmp(getenv("TERM"), "xterm")) {
1004 /* Enter the lobby */
1005 dotgoto("_BASEROOM_", 1);
1007 /* Main loop for the system... user is logged in. */
1016 do { /* MAIN LOOP OF PROGRAM */
1018 if (!is_connected()) {
1019 for (a=15; a>=0; --a) {
1020 printf("\rServer connection broken; "
1021 "will reconnect in %d seconds\r", a);
1028 attach_to_server(argc, argv);
1032 if (aaa[0] != '2') {
1033 printf("%s\n", &aaa[4]);
1037 sprintf(aaa, "USER %s", re_username);
1040 if (aaa[0] != '3') {
1041 printf("%s\n", &aaa[4]);
1044 sprintf(aaa, "PASS %s", re_password);
1047 if (aaa[0] != '2') {
1048 printf("%s\n", &aaa[4]);
1051 load_user_info(&aaa[4]);
1052 sprintf(aaa, "GOTO %s", room_name);
1057 signal(SIGINT, SIG_IGN);
1058 signal(SIGQUIT, SIG_IGN);
1059 signal(SIGPIPE, SIG_IGN);
1060 mcmd = getcmd(argbuf);
1063 check_screen_dims();
1081 newprompt("What do you want your username to be? ", aaa, 32);
1082 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1085 if (strncmp("200", aaa, 3))
1086 printf("\n%s\n", aaa);
1100 dotgoto("_MAIL_", 1);
1117 readmsgs(0, (-1), 0);
1120 readmsgs(2, (-1), 0);
1123 readmsgs(3, 1, atoi(argbuf));
1210 cli_image_upload("_userpic_");
1213 cli_image_upload("_roompic_");
1217 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1218 cli_image_upload(aaa);
1222 enternew("roomname", aaa, 20);
1223 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1226 if (strncmp("200", aaa, 3))
1227 printf("\n%s\n", aaa);
1230 enternew("hostname", aaa, 25);
1231 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1234 if (strncmp("200", aaa, 3))
1235 printf("\n%s\n", aaa);
1238 enternew("username", aaa, 32);
1239 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1242 if (strncmp("200", aaa, 3))
1243 printf("\n%s\n", aaa);
1253 display_help(argbuf);
1257 formout("register");
1262 printf("Are you sure (y/n)? ");
1279 if (server_is_local) {
1280 sttybbs(SB_RESTORE);
1281 snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1282 "exec ./subsystem %ld %d %d", fullname,
1283 usernum, screenwidth, axlevel);
1285 sttybbs(SB_NO_INTR);
1287 printf("*** Can't run doors when server is not local.\n");
1300 do_system_configuration();
1318 gotofloor(argbuf, GF_GOTO);
1322 gotofloor(argbuf, GF_SKIP);
1326 forget_this_floor();
1354 knrooms(floor_mode);
1364 misc_server_cmd(argbuf);
1368 edit_system_message(argbuf);
1397 } while (termn8 == 0);
1399 TERMN8:printf("%s logged out.\n", fullname);
1400 while (march != NULL)
1401 remove_march(march->march_name, 0);
1403 printf("\n\nType 'off' to hang up, or next user...\n");
1404 snprintf(aaa, sizeof aaa, "LOUT");
1407 if ((mcmd == 29) || (mcmd == 15)) {