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);
715 if (!strucmp(pass1, pass2)) {
716 snprintf(buf, sizeof buf, "SETP %s", pass1);
719 printf("%s\n", &buf[4]);
720 strcpy(re_password, pass1);
723 printf("*** They don't match... try again.\n");
731 * get info about the server we've connected to
733 void get_serv_info(void)
737 CtdlInternalGetServInfo(&serv_info);
739 /* be nice and identify ourself to the server */
740 snprintf(buf, sizeof buf, "IDEN %d|%d|%d|%s|",
741 SERVER_TYPE, 0, REV_LEVEL,
742 (server_is_local ? "local" : CITADEL));
743 locate_host(&buf[strlen(buf)]); /* append to the end */
745 serv_gets(buf); /* we don't care about the result code */
753 * Display list of users currently logged on to the server
755 void who_is_online(int longlist)
757 char buf[128], username[128], roomname[128], fromhost[128],
759 char tbuf[128], clientsoft[128];
761 time_t idletime, idlehours, idlemins, idlesecs;
762 int last_session = (-1);
767 if (tbuf[0] == '2') {
768 timenow = extract_long(&tbuf[4], 0);
774 printf("FLG ### User Name Room From host\n");
776 printf("--- --- ------------------------- -------------------- ------------------------\n");
781 while (serv_gets(buf), strcmp(buf, "000")) {
782 extract(username, buf, 1);
783 extract(roomname, buf, 2);
784 extract(fromhost, buf, 3);
785 extract(clientsoft, buf, 4);
786 extract(flags, buf, 7);
789 idletime = timenow - extract_long(buf, 5);
790 idlehours = idletime / 3600;
791 idlemins = (idletime - (idlehours * 3600)) / 60;
792 idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
793 printf("\nFlags: %-3s Sess# %-3d Name: %-25s Room: %s\n",
794 flags, extract_int(buf, 0), username, roomname);
795 printf("from <%s> using <%s>, idle %ld:%02ld:%02ld\n",
796 fromhost, clientsoft,
797 (long) idlehours, (long) idlemins, (long) idlesecs);
800 if (extract_int(buf, 0) == last_session) {
803 color(BRIGHT_MAGENTA);
804 printf("%-3s ", flags);
806 printf("%-3d ", extract_int(buf, 0));
808 last_session = extract_int(buf, 0);
810 printf("%-25s ", username);
811 color(BRIGHT_MAGENTA);
812 printf("%-20s ", roomname);
814 printf("%-24s\n", fromhost);
821 void enternew(char *desc, char *buf, int maxlen)
824 snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
825 newprompt(bbb, buf, maxlen);
831 int main(int argc, char **argv)
834 char aaa[100], bbb[100], eee[100]; /* general purpose variables */
835 char argbuf[32]; /* command line buf */
836 volatile int termn8 = 0;
839 sttybbs(SB_SAVE); /* Store the old terminal parameters */
840 load_command_set(); /* parse the citadel.rc file */
841 sttybbs(SB_NO_INTR); /* Install the new ones */
842 signal(SIGINT, SIG_IGN);
843 signal(SIGQUIT, SIG_IGN);
844 signal(SIGHUP, dropcarr); /* Cleanup gracefully if carrier is dropped */
845 signal(SIGTERM, dropcarr); /* Cleanup gracefully if terminated */
846 signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
848 printf("Attaching to server...\r");
850 attach_to_server(argc, argv);
856 printf("%s\n", &aaa[4]);
865 printf("%-22s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
866 serv_info.serv_bbs_city);
867 screenwidth = 80; /* default screen dimensions */
870 printf(" pause next stop\n");
871 printf(" ctrl-s ctrl-o ctrl-c\n\n");
872 formout("hello"); /* print the opening greeting */
878 if (strlen(rc_username) > 0) {
879 strcpy(fullname, rc_username);
881 newprompt("Enter your name: ", fullname, 29);
884 if (!strucmp(fullname, "new")) { /* just in case */
885 printf("Please enter the name you wish to log in with.\n");
888 (!strucmp(fullname, "bbs"))
889 || (!strucmp(fullname, "new"))
890 || (strlen(fullname) == 0));
892 if (!strucmp(fullname, "off")) {
896 /* sign on to the server */
897 strcpy(re_username, fullname);
898 snprintf(aaa, sizeof aaa, "USER %s", fullname);
904 /* password authentication */
905 if (strlen(rc_password) > 0) {
906 strcpy(eee, rc_password);
908 newprompt("\rPlease enter your password: ", eee, -19);
911 snprintf(aaa, sizeof aaa, "PASS %s", eee);
915 strcpy(re_password, eee);
916 load_user_info(&aaa[4]);
919 printf("<< wrong password >>\n");
920 if (strlen(rc_password) > 0)
924 NEWUSR:if (strlen(rc_password) == 0) {
925 printf("No record. Enter as new user? ");
929 snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
936 load_user_info(&aaa[4]);
938 while (set_password() != 0);;
944 PWOK:printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
945 fullname, axlevel, axdefs[(int) axlevel],
946 usernum, timescalled);
951 b = extract_int(&aaa[4], 0);
955 printf("*** You have a new private message in Mail>\n");
957 printf("*** You have %d new private messages in Mail>\n", b);
960 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
961 printf("*** Users need validation\n");
963 if (extract_int(&aaa[4], 1) > 0) {
964 printf("*** Please register.\n");
969 /* Make up some temporary filenames for use in various parts of the
970 * program. Don't mess with these once they've been set, because we
971 * will be unlinking them later on in the program and we don't
972 * want to delete something that we didn't create. */
973 snprintf(temp, sizeof temp, tmpnam(NULL));
974 snprintf(temp2, sizeof temp2, tmpnam(NULL));
975 snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
977 /* Get screen dimensions. First we go to a default of 80x24. Then
978 * we try to get the user's actual screen dimensions off the server.
979 * However, if we're running on an xterm, all this stuff is
980 * irrelevant because we're going to dynamically size the screen
981 * during the session.
988 screenwidth = extract_int(&aaa[4], 0);
989 screenheight = extract_int(&aaa[4], 1);
991 if (getenv("TERM") != NULL)
992 if (!strcmp(getenv("TERM"), "xterm")) {
1002 /* Enter the lobby */
1003 dotgoto("_BASEROOM_", 1);
1005 /* Main loop for the system... user is logged in. */
1014 do { /* MAIN LOOP OF PROGRAM */
1016 if (!is_connected()) {
1017 for (a=15; a>=0; --a) {
1018 printf("\rServer connection broken; "
1019 "will reconnect in %d seconds\r", a);
1026 attach_to_server(argc, argv);
1030 if (aaa[0] != '2') {
1031 printf("%s\n", &aaa[4]);
1035 sprintf(aaa, "USER %s", re_username);
1038 if (aaa[0] != '3') {
1039 printf("%s\n", &aaa[4]);
1042 sprintf(aaa, "PASS %s", re_password);
1045 if (aaa[0] != '2') {
1046 printf("%s\n", &aaa[4]);
1049 load_user_info(&aaa[4]);
1050 sprintf(aaa, "GOTO %s", room_name);
1055 signal(SIGINT, SIG_IGN);
1056 signal(SIGQUIT, SIG_IGN);
1057 signal(SIGPIPE, SIG_IGN);
1058 mcmd = getcmd(argbuf);
1061 check_screen_dims();
1079 newprompt("What do you want your username to be? ", aaa, 32);
1080 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1083 if (strncmp("200", aaa, 3))
1084 printf("\n%s\n", aaa);
1098 dotgoto("_MAIL_", 1);
1115 readmsgs(0, (-1), 0);
1118 readmsgs(2, (-1), 0);
1121 readmsgs(3, 1, atoi(argbuf));
1208 cli_image_upload("_userpic_");
1211 cli_image_upload("_roompic_");
1215 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1216 cli_image_upload(aaa);
1220 enternew("roomname", aaa, 20);
1221 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1224 if (strncmp("200", aaa, 3))
1225 printf("\n%s\n", aaa);
1228 enternew("hostname", aaa, 25);
1229 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1232 if (strncmp("200", aaa, 3))
1233 printf("\n%s\n", aaa);
1236 enternew("username", aaa, 32);
1237 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1240 if (strncmp("200", aaa, 3))
1241 printf("\n%s\n", aaa);
1251 display_help(argbuf);
1255 formout("register");
1260 printf("Are you sure (y/n)? ");
1277 if (server_is_local) {
1278 sttybbs(SB_RESTORE);
1279 snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1280 "exec ./subsystem %ld %d %d", fullname,
1281 usernum, screenwidth, axlevel);
1283 sttybbs(SB_NO_INTR);
1285 printf("*** Can't run doors when server is not local.\n");
1298 do_system_configuration();
1316 gotofloor(argbuf, GF_GOTO);
1320 gotofloor(argbuf, GF_SKIP);
1324 forget_this_floor();
1352 knrooms(floor_mode);
1362 misc_server_cmd(argbuf);
1366 edit_system_message(argbuf);
1395 } while (termn8 == 0);
1397 TERMN8:printf("%s logged out.\n", fullname);
1398 while (march != NULL)
1399 remove_march(march->march_name, 0);
1401 printf("\n\nType 'off' to hang up, or next user...\n");
1402 snprintf(aaa, sizeof aaa, "LOUT");
1405 if ((mcmd == 29) || (mcmd == 15)) {