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"
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? */
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! */
94 * here is our 'clean up gracefully and exit' routine
98 if (editor_pid > 0) { /* kill the editor if it's running */
99 kill(editor_pid, SIGHUP);
101 /* shut down the server... but not if the logoff code is 3, because
102 * that means we're exiting because we already lost the server
108 * now clean up various things
115 /* Violently kill off any child processes if Citadel is
118 if (getppid() == 1) {
119 kill(0 - getpgrp(), SIGTERM);
121 kill(0 - getpgrp(), SIGKILL);
123 sttybbs(SB_RESTORE); /* return the old terminal settings */
124 exit(code); /* exit with the proper exit code */
130 * We handle "next" and "stop" much differently than in earlier versions.
131 * The signal catching routine simply sets a flag and returns.
133 void sighandler(int which_sig)
135 signal(SIGINT, SIG_IGN);
136 signal(SIGQUIT, SIG_IGN);
137 sigcaught = which_sig;
143 * signal catching function for hangups...
145 void dropcarr(int signum)
153 * catch SIGCONT to reset terminal modes when were are put back into the
156 void catch_sigcont(int signum)
159 signal(SIGCONT, catch_sigcont);
164 /* general purpose routines */
166 void formout(char *name)
167 { /* display a file */
169 snprintf(cmd, sizeof cmd, "MESG %s", name);
173 printf("%s\n", &cmd[4]);
176 fmout(screenwidth, NULL,
177 ((userflags & US_PAGINATOR) ? 1 : 0),
182 void userlist(char *patn)
192 pprintf("%s\n", &buf[4]);
196 sttybbs(SB_YES_INTR);
197 pprintf(" User Name Num L LastCall Calls Posts\n");
198 pprintf("------------------------- ----- - ---------- ----- -----\n");
199 while (serv_gets(buf), strcmp(buf, "000")) {
200 if (sigcaught == 0) {
202 if (pattern(fl, patn) >= 0) {
203 pprintf("%-25s ", fl);
204 pprintf("%5ld %d ", extract_long(buf, 2),
205 extract_int(buf, 1));
206 lc = extract_long(buf, 3);
207 tmbuf = (struct tm *) localtime(&lc);
208 pprintf("%02d/%02d/%04d ",
211 (tmbuf->tm_year + 1900));
212 pprintf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
223 * grab assorted info about the user...
225 void load_user_info(char *params)
227 extract(fullname, params, 0);
228 axlevel = extract_int(params, 1);
229 timescalled = extract_int(params, 2);
230 posted = extract_int(params, 3);
231 userflags = extract_int(params, 4);
232 usernum = extract_long(params, 5);
237 * Remove a room from the march list. 'floornum' is ignored unless
238 * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
239 * floor will be removed from the march list.
241 void remove_march(char *roomname, int floornum)
243 struct march *mptr, *mptr2;
248 if ((!strucmp(march->march_name, roomname))
249 || ((!strucmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
256 for (mptr = march; mptr != NULL; mptr = mptr->next) {
258 if ((!strucmp(mptr->march_name, roomname))
259 || ((!strucmp(roomname, "_FLOOR_"))
260 && (mptr->march_floor == floornum))) {
262 mptr2->next = mptr->next;
273 * Locate the room on the march list which we most want to go to. Each room
274 * is measured given a "weight" of preference based on various factors.
276 char *pop_march(int desired_floor)
278 static char TheRoom[ROOMNAMELEN];
280 int TheOrder = 32767;
283 struct march *mptr = NULL;
285 strcpy(TheRoom, "_BASEROOM_");
289 for (mptr = march; mptr != NULL; mptr = mptr->next) {
291 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
292 weight = weight + 10000;
293 if (mptr->march_floor == desired_floor)
294 weight = weight + 5000;
296 weight = weight + ((128 - (mptr->march_floor)) * 128);
297 weight = weight + (128 - (mptr->march_order));
299 if (weight > TheWeight) {
301 strcpy(TheRoom, mptr->march_name);
302 TheFloor = mptr->march_floor;
303 TheOrder = mptr->march_order;
311 * jump directly to a room
313 void dotgoto(char *towhere, int display_name)
315 char aaa[256], bbb[256], psearch[256];
318 static int oldmailcount = (-1);
319 int partial_match, best_match;
322 /* store ungoto information */
323 strcpy(ugname, room_name);
326 /* first try an exact match */
327 snprintf(aaa, sizeof aaa, "GOTO %s", towhere);
332 if (!strncmp(aaa, "54", 2)) {
333 newprompt("Enter room password: ", bbb, 9);
334 snprintf(aaa, sizeof aaa, "GOTO %s|%s", towhere, bbb);
340 if (!strncmp(aaa, "54", 2)) {
341 printf("Wrong password.\n");
345 * If a match is not found, try a partial match.
346 * Partial matches anywhere in the string carry a weight of 1,
347 * left-aligned matches carry a weight of 2. Pick the room that
348 * has the highest-weighted match.
356 while (serv_gets(aaa), strcmp(aaa, "000")) {
357 extract(psearch, aaa, 0);
359 if (pattern(psearch, towhere) >= 0) {
362 if (!struncmp(towhere, psearch, strlen(towhere))) {
365 if (partial_match > best_match) {
366 strcpy(bbb, psearch);
367 best_match = partial_match;
370 if (strlen(bbb) == 0) {
371 printf("No room '%s'.\n", towhere);
374 snprintf(aaa, sizeof aaa, "GOTO %s", bbb);
384 extract(room_name, &aaa[4], 0);
385 room_flags = extract_int(&aaa[4], 4);
386 from_floor = curr_floor;
387 curr_floor = extract_int(&aaa[4], 10);
389 remove_march(room_name, 0);
390 if (!strucmp(towhere, "_BASEROOM_"))
391 remove_march(towhere, 0);
392 if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
393 if (floorlist[(int) curr_floor][0] == 0)
395 printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
397 if (display_name == 1) {
399 printf("%s ", room_name);
403 if (display_name != 2) {
404 color(BRIGHT_YELLOW);
405 printf("%d ", extract_int(&aaa[4], 1));
408 color(BRIGHT_YELLOW);
409 printf("%d ", extract_int(&aaa[4], 2));
411 printf("messages.\n");
413 highest_msg_read = extract_int(&aaa[4], 6);
414 maxmsgnum = extract_int(&aaa[4], 5);
415 is_mail = (char) extract_int(&aaa[4], 7);
416 is_room_aide = (char) extract_int(&aaa[4], 8);
417 ls = extract_long(&aaa[4], 6);
419 /* read info file if necessary */
420 if (extract_int(&aaa[4], 3) > 0)
423 /* check for newly arrived mail if we can */
424 if (num_parms(&aaa[4]) >= 10) {
425 newmailcount = extract_int(&aaa[4], 9);
426 if ((oldmailcount >= 0) && (newmailcount > oldmailcount)) {
428 printf("*** You have new mail\n");
431 oldmailcount = newmailcount;
435 /* Goto next room having unread messages.
436 * We want to skip over rooms that the user has already been to, and take the
437 * user back to the lobby when done. The room we end up in is placed in
438 * newroom - which is set to 0 (the lobby) initially.
443 struct march *mptr, *mptr2;
444 char next_room[ROOMNAMELEN];
446 /* Check to see if the march-mode list is already allocated.
447 * If it is, pop the first room off the list and go there.
453 while (serv_gets(buf), strcmp(buf, "000")) {
454 mptr = (struct march *) malloc(sizeof(struct march));
456 extract(mptr->march_name, buf, 0);
457 mptr->march_floor = (char) (extract_int(buf, 2) & 0x7F);
458 mptr->march_order = (char) (extract_int(buf, 3) & 0x7F);
463 while (mptr2->next != NULL)
468 /* add _BASEROOM_ to the end of the march list, so the user will end up
469 * in the system base room (usually the Lobby>) at the end of the loop
471 mptr = (struct march *) malloc(sizeof(struct march));
473 strcpy(mptr->march_name, "_BASEROOM_");
478 while (mptr2->next != NULL)
483 * ...and remove the room we're currently in, so a <G>oto doesn't make us
484 * walk around in circles
486 remove_march(room_name, 0);
489 strcpy(next_room, pop_march(curr_floor));
491 strcpy(next_room, "_BASEROOM_");
493 remove_march(next_room, 0);
494 dotgoto(next_room, 1);
498 * forget all rooms on a given floor
500 void forget_all_rooms_on(int ffloor)
503 struct march *flist, *fptr;
505 printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
507 snprintf(buf, sizeof buf, "LKRA %d", ffloor);
511 printf("%-72s\n", &buf[4]);
515 while (serv_gets(buf), strcmp(buf, "000")) {
516 fptr = (struct march *) malloc(sizeof(struct march));
519 extract(fptr->march_name, buf, 0);
521 while (flist != NULL) {
522 snprintf(buf, sizeof buf, "GOTO %s", flist->march_name);
533 printf("%-72s\r", "");
538 * routine called by gotofloor() to move to a new room on a new floor
540 void gf_toroom(char *towhere, int mode)
542 int floor_being_left;
544 floor_being_left = curr_floor;
546 if (mode == GF_GOTO) { /* <;G>oto mode */
550 if (mode == GF_SKIP) { /* <;S>kip mode */
552 remove_march("_FLOOR_", floor_being_left);
554 if (mode == GF_ZAP) { /* <;Z>ap mode */
556 remove_march("_FLOOR_", floor_being_left);
557 forget_all_rooms_on(floor_being_left);
565 void gotofloor(char *towhere, int mode)
569 char buf[256], targ[256];
571 if (floorlist[0][0] == 0)
574 for (a = 0; a < 128; ++a)
575 if (!strucmp(&floorlist[a][0], towhere))
579 for (a = 0; a < 128; ++a) {
580 if (!struncmp(&floorlist[a][0], towhere, strlen(towhere))) {
586 for (a = 0; a < 128; ++a)
587 if (pattern(towhere, &floorlist[a][0]) > 0)
591 printf("No floor '%s'.\n", towhere);
594 for (mptr = march; mptr != NULL; mptr = mptr->next) {
595 if ((mptr->march_floor) == tofloor)
596 gf_toroom(mptr->march_name, mode);
601 snprintf(buf, sizeof buf, "LKRA %d", tofloor);
605 while (serv_gets(buf), strcmp(buf, "000")) {
606 if ((extract_int(buf, 2) == tofloor) && (strlen(targ) == 0))
607 extract(targ, buf, 0);
609 if (strlen(targ) > 0) {
610 gf_toroom(targ, mode);
612 printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
618 * forget all rooms on current floor
620 void forget_this_floor(void)
623 if (curr_floor == 0) {
624 printf("Can't forget this floor.\n");
627 if (floorlist[0][0] == 0)
629 printf("Are you sure you want to forget all rooms on %s? ",
630 &floorlist[(int) curr_floor][0]);
634 gf_toroom("_BASEROOM_", GF_ZAP);
639 * Figure out the physical screen dimensions, if we can
641 void check_screen_dims(void)
645 unsigned short height; /* rows */
646 unsigned short width; /* columns */
647 unsigned short xpixels;
648 unsigned short ypixels; /* pixels */
651 if (have_xterm) { /* dynamically size screen if on an xterm */
652 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
654 screenheight = (int) xwinsz.height;
656 screenwidth = (int) xwinsz.width;
664 * set floor mode depending on client, server, and user settings
666 void set_floor_mode(void)
668 if (serv_info.serv_ok_floors == 0) {
669 floor_mode = 0; /* Don't use floors if the server */
671 /* doesn't support them! */
673 if (rc_floor_mode == RC_NO) { /* never use floors */
676 if (rc_floor_mode == RC_YES) { /* always use floors */
679 if (rc_floor_mode == RC_DEFAULT) { /* user choice */
680 floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
686 * Set or change the user's password
688 int set_password(void)
694 if (strlen(rc_password) > 0) {
695 strcpy(pass1, rc_password);
696 strcpy(pass2, rc_password);
698 IFNEXPERT formout("changepw");
699 newprompt("Enter a new password: ", pass1, -19);
700 newprompt("Enter it again to confirm: ", pass2, -19);
704 if (!strucmp(pass1, pass2)) {
705 snprintf(buf, sizeof buf, "SETP %s", pass1);
708 printf("%s\n", &buf[4]);
711 printf("*** They don't match... try again.\n");
719 * get info about the server we've connected to
721 void get_serv_info(void)
725 CtdlInternalGetServInfo(&serv_info);
727 /* be nice and identify ourself to the server */
728 snprintf(buf, sizeof buf, "IDEN %d|%d|%d|%s|",
729 SERVER_TYPE, 0, REV_LEVEL,
730 (server_is_local ? "local" : CITADEL));
731 locate_host(&buf[strlen(buf)]); /* append to the end */
733 serv_gets(buf); /* we don't care about the result code */
741 * Display list of users currently logged on to the server
743 void who_is_online(int longlist)
745 char buf[128], username[128], roomname[128], fromhost[128],
747 char tbuf[128], clientsoft[128];
749 time_t idletime, idlehours, idlemins, idlesecs;
750 int last_session = (-1);
755 if (tbuf[0] == '2') {
756 timenow = extract_long(&tbuf[4], 0);
762 pprintf("FLG ### User Name Room From host\n");
764 pprintf("--- --- ------------------------- -------------------- ------------------------\n");
769 while (serv_gets(buf), strcmp(buf, "000")) {
770 extract(username, buf, 1);
771 extract(roomname, buf, 2);
772 extract(fromhost, buf, 3);
773 extract(clientsoft, buf, 4);
774 extract(flags, buf, 7);
777 idletime = timenow - extract_long(buf, 5);
778 idlehours = idletime / 3600;
779 idlemins = (idletime - (idlehours * 3600)) / 60;
780 idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
781 pprintf("\nFlags: %-3s Sess# %-3d Name: %-25s Room: %s\n",
782 flags, extract_int(buf, 0), username, roomname);
783 pprintf("from <%s> using <%s>, idle %ld:%02ld:%02ld\n",
784 fromhost, clientsoft,
785 (long) idlehours, (long) idlemins, (long) idlesecs);
788 if (extract_int(buf, 0) == last_session) {
791 color(BRIGHT_MAGENTA);
792 pprintf("%-3s ", flags);
794 pprintf("%-3d ", extract_int(buf, 0));
796 last_session = extract_int(buf, 0);
798 pprintf("%-25s ", username);
799 color(BRIGHT_MAGENTA);
801 pprintf("%-20s ", roomname);
803 pprintf("%-24s\n", fromhost);
810 void enternew(char *desc, char *buf, int maxlen)
813 snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
814 newprompt(bbb, buf, maxlen);
820 int main(int argc, char **argv)
823 char aaa[100], bbb[100], eee[100]; /* general purpose variables */
824 char argbuf[32]; /* command line buf */
825 volatile int termn8 = 0;
828 sttybbs(SB_SAVE); /* Store the old terminal parameters */
829 load_command_set(); /* parse the citadel.rc file */
830 sttybbs(SB_NO_INTR); /* Install the new ones */
831 signal(SIGINT, SIG_IGN);
832 signal(SIGQUIT, SIG_IGN);
833 signal(SIGHUP, dropcarr); /* Cleanup gracefully if carrier is dropped */
834 signal(SIGTERM, dropcarr); /* Cleanup gracefully if terminated */
835 signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
837 printf("Attaching to server...\r");
839 attach_to_server(argc, argv);
845 printf("%s\n", &aaa[4]);
854 printf("%-22s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
855 serv_info.serv_bbs_city);
856 screenwidth = 80; /* default screen dimensions */
859 printf(" pause next stop\n");
860 printf(" ctrl-s ctrl-o ctrl-c\n\n");
861 formout("hello"); /* print the opening greeting */
867 if (strlen(rc_username) > 0) {
868 strcpy(fullname, rc_username);
870 newprompt("Enter your name: ", fullname, 29);
873 if (!strucmp(fullname, "new")) { /* just in case */
874 printf("Please enter the name you wish to log in with.\n");
877 (!strucmp(fullname, "bbs"))
878 || (!strucmp(fullname, "new"))
879 || (strlen(fullname) == 0));
881 if (!strucmp(fullname, "off")) {
885 /* sign on to the server */
886 snprintf(aaa, sizeof aaa, "USER %s", fullname);
892 /* password authentication */
893 if (strlen(rc_password) > 0) {
894 strcpy(eee, rc_password);
896 newprompt("\rPlease enter your password: ", eee, -19);
899 snprintf(aaa, sizeof aaa, "PASS %s", eee);
903 load_user_info(&aaa[4]);
906 printf("<< wrong password >>\n");
907 if (strlen(rc_password) > 0)
911 NEWUSR:if (strlen(rc_password) == 0) {
912 printf("No record. Enter as new user? ");
916 snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
923 load_user_info(&aaa[4]);
925 while (set_password() != 0);;
931 PWOK:printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
932 fullname, axlevel, axdefs[(int) axlevel],
933 usernum, timescalled);
938 b = extract_int(&aaa[4], 0);
942 printf("*** You have a new private message in Mail>\n");
944 printf("*** You have %d new private messages in Mail>\n", b);
947 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
948 printf("*** Users need validation\n");
950 if (extract_int(&aaa[4], 1) > 0) {
951 printf("*** Please register.\n");
956 /* Make up some temporary filenames for use in various parts of the
957 * program. Don't mess with these once they've been set, because we
958 * will be unlinking them later on in the program and we don't
959 * want to delete something that we didn't create. */
960 snprintf(temp, sizeof temp, tmpnam(NULL));
961 snprintf(temp2, sizeof temp2, tmpnam(NULL));
962 snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
964 /* Get screen dimensions. First we go to a default of 80x24. Then
965 * we try to get the user's actual screen dimensions off the server.
966 * However, if we're running on an xterm, all this stuff is
967 * irrelevant because we're going to dynamically size the screen
968 * during the session.
975 screenwidth = extract_int(&aaa[4], 0);
976 screenheight = extract_int(&aaa[4], 1);
978 if (getenv("TERM") != NULL)
979 if (!strcmp(getenv("TERM"), "xterm")) {
989 /* Enter the lobby */
990 dotgoto("_BASEROOM_", 1);
992 /* Main loop for the system... user is logged in. */
1001 do { /* MAIN LOOP OF PROGRAM */
1003 signal(SIGINT, SIG_IGN);
1004 signal(SIGQUIT, SIG_IGN);
1005 mcmd = getcmd(argbuf);
1008 check_screen_dims();
1026 newprompt("What do you want your username to be? ", aaa, 32);
1027 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1030 if (strncmp("200", aaa, 3))
1031 printf("\n%s\n", aaa);
1045 dotgoto("_MAIL_", 1);
1062 readmsgs(0, (-1), 0);
1065 readmsgs(2, (-1), 0);
1068 readmsgs(3, 1, atoi(argbuf));
1155 cli_image_upload("_userpic_");
1158 cli_image_upload("_roompic_");
1162 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1163 cli_image_upload(aaa);
1167 enternew("roomname", aaa, 20);
1168 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1171 if (strncmp("200", aaa, 3))
1172 printf("\n%s\n", aaa);
1175 enternew("hostname", aaa, 25);
1176 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1179 if (strncmp("200", aaa, 3))
1180 printf("\n%s\n", aaa);
1183 enternew("username", aaa, 32);
1184 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1187 if (strncmp("200", aaa, 3))
1188 printf("\n%s\n", aaa);
1198 display_help(argbuf);
1202 formout("register");
1207 printf("Are you sure (y/n)? ");
1224 if (server_is_local) {
1225 sttybbs(SB_RESTORE);
1226 snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1227 "exec ./subsystem %ld %d %d", fullname,
1228 usernum, screenwidth, axlevel);
1230 sttybbs(SB_NO_INTR);
1232 printf("*** Can't run doors when server is not local.\n");
1245 do_system_configuration();
1249 do_internet_configuration();
1253 check_message_base();
1275 gotofloor(argbuf, GF_GOTO);
1279 gotofloor(argbuf, GF_SKIP);
1283 forget_this_floor();
1311 knrooms(floor_mode);
1321 misc_server_cmd(argbuf);
1325 edit_system_message(argbuf);
1354 } while (termn8 == 0);
1356 TERMN8:printf("%s logged out.\n", fullname);
1357 while (march != NULL)
1358 remove_march(march->march_name, 0);
1360 printf("\n\nType 'off' to hang up, or next user...\n");
1361 snprintf(aaa, sizeof aaa, "LOUT");
1364 if ((mcmd == 29) || (mcmd == 15)) {