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 "citadel_decls.h"
49 #define IFEXPERT if (userflags&US_EXPERT)
50 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
51 #define IFAIDE if (axlevel>=6)
52 #define IFNAIDE if (axlevel<6)
54 struct march *march = NULL;
56 /* globals associated with the client program */
57 char temp[PATH_MAX]; /* Name of general temp file */
58 char temp2[PATH_MAX]; /* Name of general temp file */
59 char tempdir[PATH_MAX]; /* Name of general temp dir */
60 char editor_path[256]; /* path to external editor */
61 char printcmd[256]; /* print command */
62 int editor_pid = (-1);
65 struct CtdlServInfo serv_info; /* Info on the server connected */
69 char room_name[ROOMNAMELEN];
70 char ugname[ROOMNAMELEN];
71 long uglsn; /* holds <u>ngoto info */
72 char is_mail = 0; /* nonzero when we're in a mail room */
73 char axlevel = 0; /* access level */
74 char is_room_aide = 0; /* boolean flag, 1 if room aide */
78 long usernum = 0L; /* user number */
80 long highest_msg_read; /* used for <A>bandon room cmd */
81 long maxmsgnum; /* used for <G>oto */
83 char have_xterm = 0; /* are we running on an xterm? */
88 char curr_floor = 0; /* number of current floor */
89 char floorlist[128][256]; /* names of floors */
90 char express_msgs = 0; /* express messages waiting! */
92 jmp_buf jmp_reconnect; /* for server reconnects */
96 void sigpipehandler(int nothing)
98 longjmp(jmp_reconnect, nothing);
102 * here is our 'clean up gracefully and exit' routine
104 void logoff(int code)
106 if (editor_pid > 0) { /* kill the editor if it's running */
107 kill(editor_pid, SIGHUP);
109 /* shut down the server... but not if the logoff code is 3, because
110 * that means we're exiting because we already lost the server
116 * now clean up various things
123 /* Violently kill off any child processes if Citadel is
126 if (getppid() == 1) {
127 kill(0 - getpgrp(), SIGTERM);
129 kill(0 - getpgrp(), SIGKILL);
131 sttybbs(SB_RESTORE); /* return the old terminal settings */
132 exit(code); /* exit with the proper exit code */
138 * We handle "next" and "stop" much differently than in earlier versions.
139 * The signal catching routine simply sets a flag and returns.
141 void sighandler(int which_sig)
143 signal(SIGINT, SIG_IGN);
144 signal(SIGQUIT, SIG_IGN);
145 sigcaught = which_sig;
151 * signal catching function for hangups...
153 void dropcarr(int signum)
161 * catch SIGCONT to reset terminal modes when were are put back into the
164 void catch_sigcont(int signum)
167 signal(SIGCONT, catch_sigcont);
172 /* general purpose routines */
174 void formout(char *name)
175 { /* display a file */
177 snprintf(cmd, sizeof cmd, "MESG %s", name);
181 printf("%s\n", &cmd[4]);
184 fmout(screenwidth, NULL,
185 ((userflags & US_PAGINATOR) ? 1 : 0),
201 printf("%s\n", &buf[4]);
205 sttybbs(SB_YES_INTR);
206 printf(" User Name Num L LastCall Calls Posts\n");
207 printf("------------------------- ----- - ---------- ----- -----\n");
208 while (serv_gets(buf), strcmp(buf, "000")) {
209 if (sigcaught == 0) {
211 printf("%-25s ", fl);
212 printf("%5ld %d ", extract_long(buf, 2),
213 extract_int(buf, 1));
214 lc = extract_long(buf, 3);
215 tmbuf = (struct tm *) localtime(&lc);
216 printf("%02d/%02d/%04d ",
219 (tmbuf->tm_year + 1900));
220 printf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
223 linecount = checkpagin(linecount,
224 ((userflags & US_PAGINATOR) ? 1 : 0),
235 * grab assorted info about the user...
237 void load_user_info(char *params)
239 extract(fullname, params, 0);
240 axlevel = extract_int(params, 1);
241 timescalled = extract_int(params, 2);
242 posted = extract_int(params, 3);
243 userflags = extract_int(params, 4);
244 usernum = extract_long(params, 5);
249 * Remove a room from the march list. 'floornum' is ignored unless
250 * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
251 * floor will be removed from the march list.
253 void remove_march(char *roomname, int floornum)
255 struct march *mptr, *mptr2;
260 if ((!strucmp(march->march_name, roomname))
261 || ((!strucmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
268 for (mptr = march; mptr != NULL; mptr = mptr->next) {
270 if ((!strucmp(mptr->march_name, roomname))
271 || ((!strucmp(roomname, "_FLOOR_"))
272 && (mptr->march_floor == floornum))) {
274 mptr2->next = mptr->next;
285 * Locate the room on the march list which we most want to go to. Each room
286 * is measured given a "weight" of preference based on various factors.
288 char *pop_march(int desired_floor)
290 static char TheRoom[ROOMNAMELEN];
292 int TheOrder = 32767;
295 struct march *mptr = NULL;
297 strcpy(TheRoom, "_BASEROOM_");
301 for (mptr = march; mptr != NULL; mptr = mptr->next) {
303 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
304 weight = weight + 10000;
305 if (mptr->march_floor == desired_floor)
306 weight = weight + 5000;
308 weight = weight + ((128 - (mptr->march_floor)) * 128);
309 weight = weight + (128 - (mptr->march_order));
311 if (weight > TheWeight) {
313 strcpy(TheRoom, mptr->march_name);
314 TheFloor = mptr->march_floor;
315 TheOrder = mptr->march_order;
323 * jump directly to a room
325 void dotgoto(char *towhere, int display_name)
327 char aaa[256], bbb[256], psearch[256];
330 static int oldmailcount = (-1);
331 int partial_match, best_match;
334 /* store ungoto information */
335 strcpy(ugname, room_name);
338 /* first try an exact match */
339 snprintf(aaa, sizeof aaa, "GOTO %s", towhere);
344 if (!strncmp(aaa, "54", 2)) {
345 newprompt("Enter room password: ", bbb, 9);
346 snprintf(aaa, sizeof aaa, "GOTO %s|%s", towhere, bbb);
352 if (!strncmp(aaa, "54", 2)) {
353 printf("Wrong password.\n");
357 * If a match is not found, try a partial match.
358 * Partial matches anywhere in the string carry a weight of 1,
359 * left-aligned matches carry a weight of 2. Pick the room that
360 * has the highest-weighted match.
368 while (serv_gets(aaa), strcmp(aaa, "000")) {
369 extract(psearch, aaa, 0);
371 if (pattern(psearch, towhere) >= 0) {
374 if (!struncmp(towhere, psearch, strlen(towhere))) {
377 if (partial_match > best_match) {
378 strcpy(bbb, psearch);
379 best_match = partial_match;
382 if (strlen(bbb) == 0) {
383 printf("No room '%s'.\n", towhere);
386 snprintf(aaa, sizeof aaa, "GOTO %s", bbb);
396 extract(room_name, &aaa[4], 0);
397 room_flags = extract_int(&aaa[4], 4);
398 from_floor = curr_floor;
399 curr_floor = extract_int(&aaa[4], 10);
401 remove_march(room_name, 0);
402 if (!strucmp(towhere, "_BASEROOM_"))
403 remove_march(towhere, 0);
404 if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
405 if (floorlist[(int) curr_floor][0] == 0)
407 printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
409 if (display_name == 1) {
411 printf("%s ", room_name);
415 if (display_name != 2) {
416 color(BRIGHT_YELLOW);
417 printf("%d ", extract_int(&aaa[4], 1));
420 color(BRIGHT_YELLOW);
421 printf("%d ", extract_int(&aaa[4], 2));
423 printf("messages.\n");
425 highest_msg_read = extract_int(&aaa[4], 6);
426 maxmsgnum = extract_int(&aaa[4], 5);
427 is_mail = (char) extract_int(&aaa[4], 7);
428 is_room_aide = (char) extract_int(&aaa[4], 8);
429 ls = extract_long(&aaa[4], 6);
431 /* read info file if necessary */
432 if (extract_int(&aaa[4], 3) > 0)
435 /* check for newly arrived mail if we can */
436 if (num_parms(&aaa[4]) >= 10) {
437 newmailcount = extract_int(&aaa[4], 9);
438 if ((oldmailcount >= 0) && (newmailcount > oldmailcount)) {
440 printf("*** You have new mail\n");
443 oldmailcount = newmailcount;
447 /* Goto next room having unread messages.
448 * We want to skip over rooms that the user has already been to, and take the
449 * user back to the lobby when done. The room we end up in is placed in
450 * newroom - which is set to 0 (the lobby) initially.
455 struct march *mptr, *mptr2;
458 /* Check to see if the march-mode list is already allocated.
459 * If it is, pop the first room off the list and go there.
465 while (serv_gets(buf), strcmp(buf, "000")) {
466 mptr = (struct march *) malloc(sizeof(struct march));
468 extract(mptr->march_name, buf, 0);
469 mptr->march_floor = (char) (extract_int(buf, 2) & 0x7F);
470 mptr->march_order = (char) (extract_int(buf, 3) & 0x7F);
475 while (mptr2->next != NULL)
480 /* add _BASEROOM_ to the end of the march list, so the user will end up
481 * in the system base room (usually the Lobby>) at the end of the loop
483 mptr = (struct march *) malloc(sizeof(struct march));
485 strcpy(mptr->march_name, "_BASEROOM_");
490 while (mptr2->next != NULL)
495 * ...and remove the room we're currently in, so a <G>oto doesn't make us
496 * walk around in circles
498 remove_march(room_name, 0);
501 strcpy(next_room, pop_march(curr_floor));
503 strcpy(next_room, "_BASEROOM_");
505 remove_march(next_room, 0);
506 dotgoto(next_room, 1);
510 * forget all rooms on a given floor
512 void forget_all_rooms_on(int ffloor)
515 struct march *flist, *fptr;
517 printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
519 snprintf(buf, sizeof buf, "LKRA %d", ffloor);
523 printf("%-72s\n", &buf[4]);
527 while (serv_gets(buf), strcmp(buf, "000")) {
528 fptr = (struct march *) malloc(sizeof(struct march));
531 extract(fptr->march_name, buf, 0);
533 while (flist != NULL) {
534 snprintf(buf, sizeof buf, "GOTO %s", flist->march_name);
545 printf("%-72s\r", "");
550 * routine called by gotofloor() to move to a new room on a new floor
552 void gf_toroom(char *towhere, int mode)
554 int floor_being_left;
556 floor_being_left = curr_floor;
558 if (mode == GF_GOTO) { /* <;G>oto mode */
562 if (mode == GF_SKIP) { /* <;S>kip mode */
564 remove_march("_FLOOR_", floor_being_left);
566 if (mode == GF_ZAP) { /* <;Z>ap mode */
568 remove_march("_FLOOR_", floor_being_left);
569 forget_all_rooms_on(floor_being_left);
577 void gotofloor(char *towhere, int mode)
581 char buf[256], targ[256];
583 if (floorlist[0][0] == 0)
586 for (a = 0; a < 128; ++a)
587 if (!strucmp(&floorlist[a][0], towhere))
591 for (a = 0; a < 128; ++a) {
592 if (!struncmp(&floorlist[a][0], towhere, strlen(towhere))) {
598 for (a = 0; a < 128; ++a)
599 if (pattern(towhere, &floorlist[a][0]) > 0)
603 printf("No floor '%s'.\n", towhere);
606 for (mptr = march; mptr != NULL; mptr = mptr->next) {
607 if ((mptr->march_floor) == tofloor)
608 gf_toroom(mptr->march_name, mode);
613 snprintf(buf, sizeof buf, "LKRA %d", tofloor);
617 while (serv_gets(buf), strcmp(buf, "000")) {
618 if ((extract_int(buf, 2) == tofloor) && (strlen(targ) == 0))
619 extract(targ, buf, 0);
621 if (strlen(targ) > 0) {
622 gf_toroom(targ, mode);
624 printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
630 * forget all rooms on current floor
632 void forget_this_floor(void)
635 if (curr_floor == 0) {
636 printf("Can't forget this floor.\n");
639 if (floorlist[0][0] == 0)
641 printf("Are you sure you want to forget all rooms on %s? ",
642 &floorlist[(int) curr_floor][0]);
646 gf_toroom("_BASEROOM_", GF_ZAP);
651 * Figure out the physical screen dimensions, if we can
653 void check_screen_dims(void)
657 unsigned short height; /* rows */
658 unsigned short width; /* columns */
659 unsigned short xpixels;
660 unsigned short ypixels; /* pixels */
663 if (have_xterm) { /* dynamically size screen if on an xterm */
664 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
666 screenheight = (int) xwinsz.height;
668 screenwidth = (int) xwinsz.width;
676 * set floor mode depending on client, server, and user settings
678 void set_floor_mode(void)
680 if (serv_info.serv_ok_floors == 0) {
681 floor_mode = 0; /* Don't use floors if the server */
683 /* doesn't support them! */
685 if (rc_floor_mode == RC_NO) { /* never use floors */
688 if (rc_floor_mode == RC_YES) { /* always use floors */
691 if (rc_floor_mode == RC_DEFAULT) { /* user choice */
692 floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
698 * Set or change the user's password
700 int set_password(void)
706 if (strlen(rc_password) > 0) {
707 strcpy(pass1, rc_password);
708 strcpy(pass2, rc_password);
710 IFNEXPERT formout("changepw");
711 newprompt("Enter a new password: ", pass1, -19);
712 newprompt("Enter it again to confirm: ", pass2, -19);
714 if (!strucmp(pass1, pass2)) {
715 snprintf(buf, sizeof buf, "SETP %s", pass1);
718 printf("%s\n", &buf[4]);
719 strcpy(re_password, pass1);
722 printf("*** They don't match... try again.\n");
730 * get info about the server we've connected to
732 void get_serv_info(void)
736 CtdlInternalGetServInfo(&serv_info);
738 /* be nice and identify ourself to the server */
739 snprintf(buf, sizeof buf, "IDEN %d|%d|%d|%s|",
740 SERVER_TYPE, 0, REV_LEVEL,
741 (server_is_local ? "local" : CITADEL));
742 locate_host(&buf[strlen(buf)]); /* append to the end */
744 serv_gets(buf); /* we don't care about the result code */
752 * Display list of users currently logged on to the server
754 void who_is_online(int longlist)
756 char buf[128], username[128], roomname[128], fromhost[128],
758 char tbuf[128], clientsoft[128];
760 time_t idletime, idlehours, idlemins, idlesecs;
761 int last_session = (-1);
766 if (tbuf[0] == '2') {
767 timenow = extract_long(&tbuf[4], 0);
773 printf("FLG ### User Name Room From host\n");
775 printf("--- --- ------------------------- -------------------- ------------------------\n");
780 while (serv_gets(buf), strcmp(buf, "000")) {
781 extract(username, buf, 1);
782 extract(roomname, buf, 2);
783 extract(fromhost, buf, 3);
784 extract(clientsoft, buf, 4);
785 extract(flags, buf, 7);
788 idletime = timenow - extract_long(buf, 5);
789 idlehours = idletime / 3600;
790 idlemins = (idletime - (idlehours * 3600)) / 60;
791 idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
792 printf("\nFlags: %-3s Sess# %-3d Name: %-25s Room: %s\n",
793 flags, extract_int(buf, 0), username, roomname);
794 printf("from <%s> using <%s>, idle %ld:%02ld:%02ld\n",
795 fromhost, clientsoft,
796 (long) idlehours, (long) idlemins, (long) idlesecs);
799 if (extract_int(buf, 0) == last_session) {
802 color(BRIGHT_MAGENTA);
803 printf("%-3s ", flags);
805 printf("%-3d ", extract_int(buf, 0));
807 last_session = extract_int(buf, 0);
809 printf("%-25s ", username);
810 color(BRIGHT_MAGENTA);
811 printf("%-20s ", roomname);
813 printf("%-24s\n", fromhost);
820 void enternew(char *desc, char *buf, int maxlen)
823 snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
824 newprompt(bbb, buf, maxlen);
830 int main(int argc, char **argv)
833 char aaa[100], bbb[100], eee[100]; /* general purpose variables */
834 char argbuf[32]; /* command line buf */
835 volatile int termn8 = 0;
838 sttybbs(SB_SAVE); /* Store the old terminal parameters */
839 load_command_set(); /* parse the citadel.rc file */
840 sttybbs(SB_NO_INTR); /* Install the new ones */
841 signal(SIGINT, SIG_IGN);
842 signal(SIGQUIT, SIG_IGN);
843 signal(SIGHUP, dropcarr); /* Cleanup gracefully if carrier is dropped */
844 signal(SIGTERM, dropcarr); /* Cleanup gracefully if terminated */
845 signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
847 printf("Attaching to server...\r");
849 attach_to_server(argc, argv);
855 printf("%s\n", &aaa[4]);
864 printf("%-22s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
865 serv_info.serv_bbs_city);
866 screenwidth = 80; /* default screen dimensions */
869 printf(" pause next stop\n");
870 printf(" ctrl-s ctrl-o ctrl-c\n\n");
871 formout("hello"); /* print the opening greeting */
877 if (strlen(rc_username) > 0) {
878 strcpy(fullname, rc_username);
880 newprompt("Enter your name: ", fullname, 29);
883 if (!strucmp(fullname, "new")) { /* just in case */
884 printf("Please enter the name you wish to log in with.\n");
887 (!strucmp(fullname, "bbs"))
888 || (!strucmp(fullname, "new"))
889 || (strlen(fullname) == 0));
891 if (!strucmp(fullname, "off")) {
895 /* sign on to the server */
896 strcpy(re_username, fullname);
897 snprintf(aaa, sizeof aaa, "USER %s", fullname);
903 /* password authentication */
904 if (strlen(rc_password) > 0) {
905 strcpy(eee, rc_password);
907 newprompt("\rPlease enter your password: ", eee, -19);
910 snprintf(aaa, sizeof aaa, "PASS %s", eee);
914 strcpy(re_password, eee);
915 load_user_info(&aaa[4]);
918 printf("<< wrong password >>\n");
919 if (strlen(rc_password) > 0)
923 NEWUSR:if (strlen(rc_password) == 0) {
924 printf("No record. Enter as new user? ");
928 snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
935 load_user_info(&aaa[4]);
937 while (set_password() != 0);;
943 PWOK:printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
944 fullname, axlevel, axdefs[(int) axlevel],
945 usernum, timescalled);
950 b = extract_int(&aaa[4], 0);
954 printf("*** You have a new private message in Mail>\n");
956 printf("*** You have %d new private messages in Mail>\n", b);
959 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
960 printf("*** Users need validation\n");
962 if (extract_int(&aaa[4], 1) > 0) {
963 printf("*** Please register.\n");
968 /* Make up some temporary filenames for use in various parts of the
969 * program. Don't mess with these once they've been set, because we
970 * will be unlinking them later on in the program and we don't
971 * want to delete something that we didn't create. */
972 snprintf(temp, sizeof temp, tmpnam(NULL));
973 snprintf(temp2, sizeof temp2, tmpnam(NULL));
974 snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
976 /* Get screen dimensions. First we go to a default of 80x24. Then
977 * we try to get the user's actual screen dimensions off the server.
978 * However, if we're running on an xterm, all this stuff is
979 * irrelevant because we're going to dynamically size the screen
980 * during the session.
987 screenwidth = extract_int(&aaa[4], 0);
988 screenheight = extract_int(&aaa[4], 1);
990 if (getenv("TERM") != NULL)
991 if (!strcmp(getenv("TERM"), "xterm")) {
1001 /* Enter the lobby */
1002 dotgoto("_BASEROOM_", 1);
1004 /* Main loop for the system... user is logged in. */
1013 do { /* MAIN LOOP OF PROGRAM */
1015 if (!is_connected()) {
1016 for (a=15; a>=0; --a) {
1017 printf("\rServer connection broken; "
1018 "will reconnect in %d seconds\r", a);
1025 attach_to_server(argc, argv);
1029 if (aaa[0] != '2') {
1030 printf("%s\n", &aaa[4]);
1034 sprintf(aaa, "USER %s", re_username);
1037 if (aaa[0] != '3') {
1038 printf("%s\n", &aaa[4]);
1041 sprintf(aaa, "PASS %s", re_password);
1044 if (aaa[0] != '2') {
1045 printf("%s\n", &aaa[4]);
1048 load_user_info(&aaa[4]);
1049 sprintf(aaa, "GOTO %s", room_name);
1054 signal(SIGINT, SIG_IGN);
1055 signal(SIGQUIT, SIG_IGN);
1056 signal(SIGPIPE, SIG_IGN);
1057 mcmd = getcmd(argbuf);
1060 check_screen_dims();
1078 newprompt("What do you want your username to be? ", aaa, 32);
1079 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1082 if (strncmp("200", aaa, 3))
1083 printf("\n%s\n", aaa);
1097 dotgoto("_MAIL_", 1);
1114 readmsgs(0, (-1), 0);
1117 readmsgs(2, (-1), 0);
1120 readmsgs(3, 1, atoi(argbuf));
1207 cli_image_upload("_userpic_");
1210 cli_image_upload("_roompic_");
1214 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1215 cli_image_upload(aaa);
1219 enternew("roomname", aaa, 20);
1220 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1223 if (strncmp("200", aaa, 3))
1224 printf("\n%s\n", aaa);
1227 enternew("hostname", aaa, 25);
1228 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1231 if (strncmp("200", aaa, 3))
1232 printf("\n%s\n", aaa);
1235 enternew("username", aaa, 32);
1236 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1239 if (strncmp("200", aaa, 3))
1240 printf("\n%s\n", aaa);
1250 display_help(argbuf);
1254 formout("register");
1259 printf("Are you sure (y/n)? ");
1276 if (server_is_local) {
1277 sttybbs(SB_RESTORE);
1278 snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1279 "exec ./subsystem %ld %d %d", fullname,
1280 usernum, screenwidth, axlevel);
1282 sttybbs(SB_NO_INTR);
1284 printf("*** Can't run doors when server is not local.\n");
1297 do_system_configuration();
1315 gotofloor(argbuf, GF_GOTO);
1319 gotofloor(argbuf, GF_SKIP);
1323 forget_this_floor();
1351 knrooms(floor_mode);
1361 misc_server_cmd(argbuf);
1365 edit_system_message(argbuf);
1390 } while (termn8 == 0);
1392 TERMN8:printf("%s logged out.\n", fullname);
1393 while (march != NULL)
1394 remove_march(march->march_name, 0);
1396 printf("\n\nType 'off' to hang up, or next user...\n");
1397 snprintf(aaa, sizeof aaa, "LOUT");
1400 if ((mcmd == 29) || (mcmd == 15)) {