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! */
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),
193 printf("%s\n", &buf[4]);
197 sttybbs(SB_YES_INTR);
198 printf(" User Name Num L LastCall Calls Posts\n");
199 printf("------------------------- ----- - ---------- ----- -----\n");
200 while (serv_gets(buf), strcmp(buf, "000")) {
201 if (sigcaught == 0) {
203 printf("%-25s ", fl);
204 printf("%5ld %d ", extract_long(buf, 2),
205 extract_int(buf, 1));
206 lc = extract_long(buf, 3);
207 tmbuf = (struct tm *) localtime(&lc);
208 printf("%02d/%02d/%04d ",
211 (tmbuf->tm_year + 1900));
212 printf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
215 linecount = checkpagin(linecount,
216 ((userflags & US_PAGINATOR) ? 1 : 0),
227 * grab assorted info about the user...
229 void load_user_info(char *params)
231 extract(fullname, params, 0);
232 axlevel = extract_int(params, 1);
233 timescalled = extract_int(params, 2);
234 posted = extract_int(params, 3);
235 userflags = extract_int(params, 4);
236 usernum = extract_long(params, 5);
241 * Remove a room from the march list. 'floornum' is ignored unless
242 * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
243 * floor will be removed from the march list.
245 void remove_march(char *roomname, int floornum)
247 struct march *mptr, *mptr2;
252 if ((!strucmp(march->march_name, roomname))
253 || ((!strucmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
260 for (mptr = march; mptr != NULL; mptr = mptr->next) {
262 if ((!strucmp(mptr->march_name, roomname))
263 || ((!strucmp(roomname, "_FLOOR_"))
264 && (mptr->march_floor == floornum))) {
266 mptr2->next = mptr->next;
277 * Locate the room on the march list which we most want to go to. Each room
278 * is measured given a "weight" of preference based on various factors.
280 char *pop_march(int desired_floor)
282 static char TheRoom[ROOMNAMELEN];
284 int TheOrder = 32767;
287 struct march *mptr = NULL;
289 strcpy(TheRoom, "_BASEROOM_");
293 for (mptr = march; mptr != NULL; mptr = mptr->next) {
295 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
296 weight = weight + 10000;
297 if (mptr->march_floor == desired_floor)
298 weight = weight + 5000;
300 weight = weight + ((128 - (mptr->march_floor)) * 128);
301 weight = weight + (128 - (mptr->march_order));
303 if (weight > TheWeight) {
305 strcpy(TheRoom, mptr->march_name);
306 TheFloor = mptr->march_floor;
307 TheOrder = mptr->march_order;
315 * jump directly to a room
317 void dotgoto(char *towhere, int display_name)
319 char aaa[256], bbb[256], psearch[256];
322 static int oldmailcount = (-1);
323 int partial_match, best_match;
326 /* store ungoto information */
327 strcpy(ugname, room_name);
330 /* first try an exact match */
331 snprintf(aaa, sizeof aaa, "GOTO %s", towhere);
336 if (!strncmp(aaa, "54", 2)) {
337 newprompt("Enter room password: ", bbb, 9);
338 snprintf(aaa, sizeof aaa, "GOTO %s|%s", towhere, bbb);
344 if (!strncmp(aaa, "54", 2)) {
345 printf("Wrong password.\n");
349 * If a match is not found, try a partial match.
350 * Partial matches anywhere in the string carry a weight of 1,
351 * left-aligned matches carry a weight of 2. Pick the room that
352 * has the highest-weighted match.
360 while (serv_gets(aaa), strcmp(aaa, "000")) {
361 extract(psearch, aaa, 0);
363 if (pattern(psearch, towhere) >= 0) {
366 if (!struncmp(towhere, psearch, strlen(towhere))) {
369 if (partial_match > best_match) {
370 strcpy(bbb, psearch);
371 best_match = partial_match;
374 if (strlen(bbb) == 0) {
375 printf("No room '%s'.\n", towhere);
378 snprintf(aaa, sizeof aaa, "GOTO %s", bbb);
388 extract(room_name, &aaa[4], 0);
389 room_flags = extract_int(&aaa[4], 4);
390 from_floor = curr_floor;
391 curr_floor = extract_int(&aaa[4], 10);
393 remove_march(room_name, 0);
394 if (!strucmp(towhere, "_BASEROOM_"))
395 remove_march(towhere, 0);
396 if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
397 if (floorlist[(int) curr_floor][0] == 0)
399 printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
401 if (display_name == 1) {
403 printf("%s ", room_name);
407 if (display_name != 2) {
408 color(BRIGHT_YELLOW);
409 printf("%d ", extract_int(&aaa[4], 1));
412 color(BRIGHT_YELLOW);
413 printf("%d ", extract_int(&aaa[4], 2));
415 printf("messages.\n");
417 highest_msg_read = extract_int(&aaa[4], 6);
418 maxmsgnum = extract_int(&aaa[4], 5);
419 is_mail = (char) extract_int(&aaa[4], 7);
420 is_room_aide = (char) extract_int(&aaa[4], 8);
421 ls = extract_long(&aaa[4], 6);
423 /* read info file if necessary */
424 if (extract_int(&aaa[4], 3) > 0)
427 /* check for newly arrived mail if we can */
428 if (num_parms(&aaa[4]) >= 10) {
429 newmailcount = extract_int(&aaa[4], 9);
430 if ((oldmailcount >= 0) && (newmailcount > oldmailcount)) {
432 printf("*** You have new mail\n");
435 oldmailcount = newmailcount;
439 /* Goto next room having unread messages.
440 * We want to skip over rooms that the user has already been to, and take the
441 * user back to the lobby when done. The room we end up in is placed in
442 * newroom - which is set to 0 (the lobby) initially.
447 struct march *mptr, *mptr2;
450 /* Check to see if the march-mode list is already allocated.
451 * If it is, pop the first room off the list and go there.
457 while (serv_gets(buf), strcmp(buf, "000")) {
458 mptr = (struct march *) malloc(sizeof(struct march));
460 extract(mptr->march_name, buf, 0);
461 mptr->march_floor = (char) (extract_int(buf, 2) & 0x7F);
462 mptr->march_order = (char) (extract_int(buf, 3) & 0x7F);
467 while (mptr2->next != NULL)
472 /* add _BASEROOM_ to the end of the march list, so the user will end up
473 * in the system base room (usually the Lobby>) at the end of the loop
475 mptr = (struct march *) malloc(sizeof(struct march));
477 strcpy(mptr->march_name, "_BASEROOM_");
482 while (mptr2->next != NULL)
487 * ...and remove the room we're currently in, so a <G>oto doesn't make us
488 * walk around in circles
490 remove_march(room_name, 0);
493 strcpy(next_room, pop_march(curr_floor));
495 strcpy(next_room, "_BASEROOM_");
497 remove_march(next_room, 0);
498 dotgoto(next_room, 1);
502 * forget all rooms on a given floor
504 void forget_all_rooms_on(int ffloor)
507 struct march *flist, *fptr;
509 printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
511 snprintf(buf, sizeof buf, "LKRA %d", ffloor);
515 printf("%-72s\n", &buf[4]);
519 while (serv_gets(buf), strcmp(buf, "000")) {
520 fptr = (struct march *) malloc(sizeof(struct march));
523 extract(fptr->march_name, buf, 0);
525 while (flist != NULL) {
526 snprintf(buf, sizeof buf, "GOTO %s", flist->march_name);
537 printf("%-72s\r", "");
542 * routine called by gotofloor() to move to a new room on a new floor
544 void gf_toroom(char *towhere, int mode)
546 int floor_being_left;
548 floor_being_left = curr_floor;
550 if (mode == GF_GOTO) { /* <;G>oto mode */
554 if (mode == GF_SKIP) { /* <;S>kip mode */
556 remove_march("_FLOOR_", floor_being_left);
558 if (mode == GF_ZAP) { /* <;Z>ap mode */
560 remove_march("_FLOOR_", floor_being_left);
561 forget_all_rooms_on(floor_being_left);
569 void gotofloor(char *towhere, int mode)
573 char buf[256], targ[256];
575 if (floorlist[0][0] == 0)
578 for (a = 0; a < 128; ++a)
579 if (!strucmp(&floorlist[a][0], towhere))
583 for (a = 0; a < 128; ++a) {
584 if (!struncmp(&floorlist[a][0], towhere, strlen(towhere))) {
590 for (a = 0; a < 128; ++a)
591 if (pattern(towhere, &floorlist[a][0]) > 0)
595 printf("No floor '%s'.\n", towhere);
598 for (mptr = march; mptr != NULL; mptr = mptr->next) {
599 if ((mptr->march_floor) == tofloor)
600 gf_toroom(mptr->march_name, mode);
605 snprintf(buf, sizeof buf, "LKRA %d", tofloor);
609 while (serv_gets(buf), strcmp(buf, "000")) {
610 if ((extract_int(buf, 2) == tofloor) && (strlen(targ) == 0))
611 extract(targ, buf, 0);
613 if (strlen(targ) > 0) {
614 gf_toroom(targ, mode);
616 printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
622 * forget all rooms on current floor
624 void forget_this_floor(void)
627 if (curr_floor == 0) {
628 printf("Can't forget this floor.\n");
631 if (floorlist[0][0] == 0)
633 printf("Are you sure you want to forget all rooms on %s? ",
634 &floorlist[(int) curr_floor][0]);
638 gf_toroom("_BASEROOM_", GF_ZAP);
643 * Figure out the physical screen dimensions, if we can
645 void check_screen_dims(void)
649 unsigned short height; /* rows */
650 unsigned short width; /* columns */
651 unsigned short xpixels;
652 unsigned short ypixels; /* pixels */
655 if (have_xterm) { /* dynamically size screen if on an xterm */
656 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
658 screenheight = (int) xwinsz.height;
660 screenwidth = (int) xwinsz.width;
668 * set floor mode depending on client, server, and user settings
670 void set_floor_mode(void)
672 if (serv_info.serv_ok_floors == 0) {
673 floor_mode = 0; /* Don't use floors if the server */
675 /* doesn't support them! */
677 if (rc_floor_mode == RC_NO) { /* never use floors */
680 if (rc_floor_mode == RC_YES) { /* always use floors */
683 if (rc_floor_mode == RC_DEFAULT) { /* user choice */
684 floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
690 * Set or change the user's password
692 int set_password(void)
698 if (strlen(rc_password) > 0) {
699 strcpy(pass1, rc_password);
700 strcpy(pass2, rc_password);
702 IFNEXPERT formout("changepw");
703 newprompt("Enter a new password: ", pass1, -19);
704 newprompt("Enter it again to confirm: ", pass2, -19);
708 if (!strucmp(pass1, pass2)) {
709 snprintf(buf, sizeof buf, "SETP %s", pass1);
712 printf("%s\n", &buf[4]);
715 printf("*** They don't match... try again.\n");
723 * get info about the server we've connected to
725 void get_serv_info(void)
729 CtdlInternalGetServInfo(&serv_info);
731 /* be nice and identify ourself to the server */
732 snprintf(buf, sizeof buf, "IDEN %d|%d|%d|%s|",
733 SERVER_TYPE, 0, REV_LEVEL,
734 (server_is_local ? "local" : CITADEL));
735 locate_host(&buf[strlen(buf)]); /* append to the end */
737 serv_gets(buf); /* we don't care about the result code */
745 * Display list of users currently logged on to the server
747 void who_is_online(int longlist)
749 char buf[128], username[128], roomname[128], fromhost[128],
751 char tbuf[128], clientsoft[128];
753 time_t idletime, idlehours, idlemins, idlesecs;
754 int last_session = (-1);
759 if (tbuf[0] == '2') {
760 timenow = extract_long(&tbuf[4], 0);
766 printf("FLG ### User Name Room From host\n");
768 printf("--- --- ------------------------- -------------------- ------------------------\n");
773 while (serv_gets(buf), strcmp(buf, "000")) {
774 extract(username, buf, 1);
775 extract(roomname, buf, 2);
776 extract(fromhost, buf, 3);
777 extract(clientsoft, buf, 4);
778 extract(flags, buf, 7);
781 idletime = timenow - extract_long(buf, 5);
782 idlehours = idletime / 3600;
783 idlemins = (idletime - (idlehours * 3600)) / 60;
784 idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
785 printf("\nFlags: %-3s Sess# %-3d Name: %-25s Room: %s\n",
786 flags, extract_int(buf, 0), username, roomname);
787 printf("from <%s> using <%s>, idle %ld:%02ld:%02ld\n",
788 fromhost, clientsoft,
789 (long) idlehours, (long) idlemins, (long) idlesecs);
792 if (extract_int(buf, 0) == last_session) {
795 color(BRIGHT_MAGENTA);
796 printf("%-3s ", flags);
798 printf("%-3d ", extract_int(buf, 0));
800 last_session = extract_int(buf, 0);
802 printf("%-25s ", username);
803 color(BRIGHT_MAGENTA);
804 printf("%-20s ", roomname);
806 printf("%-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], eee[100]; /* general purpose variables */
827 char argbuf[32]; /* command line buf */
828 volatile int termn8 = 0;
831 sttybbs(SB_SAVE); /* Store the old terminal parameters */
832 load_command_set(); /* parse the citadel.rc file */
833 sttybbs(SB_NO_INTR); /* Install the new ones */
834 signal(SIGINT, SIG_IGN);
835 signal(SIGQUIT, SIG_IGN);
836 signal(SIGHUP, dropcarr); /* Cleanup gracefully if carrier is dropped */
837 signal(SIGTERM, dropcarr); /* Cleanup gracefully if terminated */
838 signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
840 printf("Attaching to server...\r");
842 attach_to_server(argc, argv);
848 printf("%s\n", &aaa[4]);
857 printf("%-22s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
858 serv_info.serv_bbs_city);
859 screenwidth = 80; /* default screen dimensions */
862 printf(" pause next stop\n");
863 printf(" ctrl-s ctrl-o ctrl-c\n\n");
864 formout("hello"); /* print the opening greeting */
870 if (strlen(rc_username) > 0) {
871 strcpy(fullname, rc_username);
873 newprompt("Enter your name: ", fullname, 29);
876 if (!strucmp(fullname, "new")) { /* just in case */
877 printf("Please enter the name you wish to log in with.\n");
880 (!strucmp(fullname, "bbs"))
881 || (!strucmp(fullname, "new"))
882 || (strlen(fullname) == 0));
884 if (!strucmp(fullname, "off")) {
888 /* sign on to the server */
889 snprintf(aaa, sizeof aaa, "USER %s", fullname);
895 /* password authentication */
896 if (strlen(rc_password) > 0) {
897 strcpy(eee, rc_password);
899 newprompt("\rPlease enter your password: ", eee, -19);
902 snprintf(aaa, sizeof aaa, "PASS %s", eee);
906 load_user_info(&aaa[4]);
909 printf("<< wrong password >>\n");
910 if (strlen(rc_password) > 0)
914 NEWUSR:if (strlen(rc_password) == 0) {
915 printf("No record. Enter as new user? ");
919 snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
926 load_user_info(&aaa[4]);
928 while (set_password() != 0);;
934 PWOK:printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
935 fullname, axlevel, axdefs[(int) axlevel],
936 usernum, timescalled);
941 b = extract_int(&aaa[4], 0);
945 printf("*** You have a new private message in Mail>\n");
947 printf("*** You have %d new private messages in Mail>\n", b);
950 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
951 printf("*** Users need validation\n");
953 if (extract_int(&aaa[4], 1) > 0) {
954 printf("*** Please register.\n");
959 /* Make up some temporary filenames for use in various parts of the
960 * program. Don't mess with these once they've been set, because we
961 * will be unlinking them later on in the program and we don't
962 * want to delete something that we didn't create. */
963 snprintf(temp, sizeof temp, tmpnam(NULL));
964 snprintf(temp2, sizeof temp2, tmpnam(NULL));
965 snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
967 /* Get screen dimensions. First we go to a default of 80x24. Then
968 * we try to get the user's actual screen dimensions off the server.
969 * However, if we're running on an xterm, all this stuff is
970 * irrelevant because we're going to dynamically size the screen
971 * during the session.
978 screenwidth = extract_int(&aaa[4], 0);
979 screenheight = extract_int(&aaa[4], 1);
981 if (getenv("TERM") != NULL)
982 if (!strcmp(getenv("TERM"), "xterm")) {
992 /* Enter the lobby */
993 dotgoto("_BASEROOM_", 1);
995 /* Main loop for the system... user is logged in. */
1004 do { /* MAIN LOOP OF PROGRAM */
1006 signal(SIGINT, SIG_IGN);
1007 signal(SIGQUIT, SIG_IGN);
1008 mcmd = getcmd(argbuf);
1011 check_screen_dims();
1029 newprompt("What do you want your username to be? ", aaa, 32);
1030 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1033 if (strncmp("200", aaa, 3))
1034 printf("\n%s\n", aaa);
1048 dotgoto("_MAIL_", 1);
1065 readmsgs(0, (-1), 0);
1068 readmsgs(2, (-1), 0);
1071 readmsgs(3, 1, atoi(argbuf));
1158 cli_image_upload("_userpic_");
1161 cli_image_upload("_roompic_");
1165 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1166 cli_image_upload(aaa);
1170 enternew("roomname", aaa, 20);
1171 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1174 if (strncmp("200", aaa, 3))
1175 printf("\n%s\n", aaa);
1178 enternew("hostname", aaa, 25);
1179 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1182 if (strncmp("200", aaa, 3))
1183 printf("\n%s\n", aaa);
1186 enternew("username", aaa, 32);
1187 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1190 if (strncmp("200", aaa, 3))
1191 printf("\n%s\n", aaa);
1201 display_help(argbuf);
1205 formout("register");
1210 printf("Are you sure (y/n)? ");
1227 if (server_is_local) {
1228 sttybbs(SB_RESTORE);
1229 snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1230 "exec ./subsystem %ld %d %d", fullname,
1231 usernum, screenwidth, axlevel);
1233 sttybbs(SB_NO_INTR);
1235 printf("*** Can't run doors when server is not local.\n");
1248 do_system_configuration();
1266 gotofloor(argbuf, GF_GOTO);
1270 gotofloor(argbuf, GF_SKIP);
1274 forget_this_floor();
1302 knrooms(floor_mode);
1312 misc_server_cmd(argbuf);
1316 edit_system_message(argbuf);
1345 } while (termn8 == 0);
1347 TERMN8:printf("%s logged out.\n", fullname);
1348 while (march != NULL)
1349 remove_march(march->march_name, 0);
1351 printf("\n\nType 'off' to hang up, or next user...\n");
1352 snprintf(aaa, sizeof aaa, "LOUT");
1355 if ((mcmd == 29) || (mcmd == 15)) {