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_passwords.h"
37 #include "client_icq.h"
38 #include "citadel_decls.h"
46 char march_name[ROOMNAMELEN];
51 #define IFEXPERT if (userflags&US_EXPERT)
52 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
53 #define IFAIDE if (axlevel>=6)
54 #define IFNAIDE if (axlevel<6)
56 struct march *march = NULL;
58 /* globals associated with the client program */
59 char temp[PATH_MAX]; /* Name of general temp file */
60 char temp2[PATH_MAX]; /* Name of general temp file */
61 char tempdir[PATH_MAX]; /* Name of general temp dir */
62 char editor_path[256]; /* path to external editor */
63 char printcmd[256]; /* print command */
64 int editor_pid = (-1);
67 struct CtdlServInfo serv_info; /* Info on the server connected */
71 char room_name[ROOMNAMELEN];
72 char ugname[ROOMNAMELEN];
73 long uglsn; /* holds <u>ngoto info */
74 char is_mail = 0; /* nonzero when we're in a mail room */
75 char axlevel = 0; /* access level */
76 char is_room_aide = 0; /* boolean flag, 1 if room aide */
80 long usernum = 0L; /* user number */
82 long highest_msg_read; /* used for <A>bandon room cmd */
83 long maxmsgnum; /* used for <G>oto */
85 char have_xterm = 0; /* are we running on an xterm? */
92 char curr_floor = 0; /* number of current floor */
93 char floorlist[128][256]; /* names of floors */
94 char express_msgs = 0; /* express messages waiting! */
97 * here is our 'clean up gracefully and exit' routine
101 if (editor_pid > 0) { /* kill the editor if it's running */
102 kill(editor_pid, SIGHUP);
104 /* shut down the server... but not if the logoff code is 3, because
105 * that means we're exiting because we already lost the server
111 * now clean up various things
118 /* Violently kill off any child processes if Citadel is
121 if (getppid() == 1) {
122 kill(0 - getpgrp(), SIGTERM);
124 kill(0 - getpgrp(), SIGKILL);
126 sttybbs(SB_RESTORE); /* return the old terminal settings */
127 exit(code); /* exit with the proper exit code */
133 * We handle "next" and "stop" much differently than in earlier versions.
134 * The signal catching routine simply sets a flag and returns.
136 void sighandler(int which_sig)
138 signal(SIGINT, SIG_IGN);
139 signal(SIGQUIT, SIG_IGN);
140 sigcaught = which_sig;
146 * signal catching function for hangups...
148 void dropcarr(int signum)
156 * catch SIGCONT to reset terminal modes when were are put back into the
159 void catch_sigcont(int signum)
162 signal(SIGCONT, catch_sigcont);
167 /* general purpose routines */
169 void formout(char *name)
170 { /* display a file */
172 snprintf(cmd, sizeof cmd, "MESG %s", name);
176 printf("%s\n", &cmd[4]);
179 fmout(screenwidth, NULL,
180 ((userflags & US_PAGINATOR) ? 1 : 0),
185 void userlist(char *patn)
195 pprintf("%s\n", &buf[4]);
199 sttybbs(SB_YES_INTR);
200 pprintf(" User Name Num L LastCall Calls Posts\n");
201 pprintf("------------------------- ----- - ---------- ----- -----\n");
202 while (serv_gets(buf), strcmp(buf, "000")) {
203 if (sigcaught == 0) {
205 if (pattern(fl, patn) >= 0) {
206 pprintf("%-25s ", fl);
207 pprintf("%5ld %d ", extract_long(buf, 2),
208 extract_int(buf, 1));
209 lc = extract_long(buf, 3);
210 tmbuf = (struct tm *) localtime(&lc);
211 pprintf("%02d/%02d/%04d ",
214 (tmbuf->tm_year + 1900));
215 pprintf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
226 * grab assorted info about the user...
228 void load_user_info(char *params)
230 extract(fullname, params, 0);
231 axlevel = extract_int(params, 1);
232 timescalled = extract_int(params, 2);
233 posted = extract_int(params, 3);
234 userflags = extract_int(params, 4);
235 usernum = extract_long(params, 5);
240 * Remove a room from the march list. 'floornum' is ignored unless
241 * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
242 * floor will be removed from the march list.
244 void remove_march(char *roomname, int floornum)
246 struct march *mptr, *mptr2;
251 if ((!strcasecmp(march->march_name, roomname))
252 || ((!strcasecmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
259 for (mptr = march; mptr != NULL; mptr = mptr->next) {
261 if ((!strcasecmp(mptr->march_name, roomname))
262 || ((!strcasecmp(roomname, "_FLOOR_"))
263 && (mptr->march_floor == floornum))) {
265 mptr2->next = mptr->next;
276 * Locate the room on the march list which we most want to go to. Each room
277 * is measured given a "weight" of preference based on various factors.
279 char *pop_march(int desired_floor)
281 static char TheRoom[ROOMNAMELEN];
283 int TheOrder = 32767;
286 struct march *mptr = NULL;
288 strcpy(TheRoom, "_BASEROOM_");
292 for (mptr = march; mptr != NULL; mptr = mptr->next) {
294 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
295 weight = weight + 10000;
296 if (mptr->march_floor == desired_floor)
297 weight = weight + 5000;
299 weight = weight + ((128 - (mptr->march_floor)) * 128);
300 weight = weight + (128 - (mptr->march_order));
302 if (weight > TheWeight) {
304 strcpy(TheRoom, mptr->march_name);
305 TheFloor = mptr->march_floor;
306 TheOrder = mptr->march_order;
314 * jump directly to a room
316 void dotgoto(char *towhere, int display_name)
318 char aaa[256], bbb[256], psearch[256];
321 static int oldmailcount = (-1);
322 int partial_match, best_match;
325 /* store ungoto information */
326 strcpy(ugname, room_name);
329 /* first try an exact match */
330 snprintf(aaa, sizeof aaa, "GOTO %s", towhere);
335 if (!strncmp(aaa, "54", 2)) {
336 newprompt("Enter room password: ", bbb, 9);
337 snprintf(aaa, sizeof aaa, "GOTO %s|%s", towhere, bbb);
343 if (!strncmp(aaa, "54", 2)) {
344 printf("Wrong password.\n");
348 * If a match is not found, try a partial match.
349 * Partial matches anywhere in the string carry a weight of 1,
350 * left-aligned matches carry a weight of 2. Pick the room that
351 * has the highest-weighted match.
359 while (serv_gets(aaa), strcmp(aaa, "000")) {
360 extract(psearch, aaa, 0);
362 if (pattern(psearch, towhere) >= 0) {
365 if (!strncasecmp(towhere, psearch, strlen(towhere))) {
368 if (partial_match > best_match) {
369 strcpy(bbb, psearch);
370 best_match = partial_match;
373 if (strlen(bbb) == 0) {
374 printf("No room '%s'.\n", towhere);
377 snprintf(aaa, sizeof aaa, "GOTO %s", bbb);
387 extract(room_name, &aaa[4], 0);
388 room_flags = extract_int(&aaa[4], 4);
389 from_floor = curr_floor;
390 curr_floor = extract_int(&aaa[4], 10);
392 remove_march(room_name, 0);
393 if (!strcasecmp(towhere, "_BASEROOM_"))
394 remove_march(towhere, 0);
395 if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
396 if (floorlist[(int) curr_floor][0] == 0)
398 printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
400 if (display_name == 1) {
402 printf("%s ", room_name);
406 if (display_name != 2) {
407 color(BRIGHT_YELLOW);
408 printf("%d ", extract_int(&aaa[4], 1));
411 color(BRIGHT_YELLOW);
412 printf("%d ", extract_int(&aaa[4], 2));
414 printf("messages.\n");
416 highest_msg_read = extract_int(&aaa[4], 6);
417 maxmsgnum = extract_int(&aaa[4], 5);
418 is_mail = (char) extract_int(&aaa[4], 7);
419 is_room_aide = (char) extract_int(&aaa[4], 8);
420 ls = extract_long(&aaa[4], 6);
422 /* read info file if necessary */
423 if (extract_int(&aaa[4], 3) > 0)
426 /* check for newly arrived mail if we can */
427 if (num_parms(&aaa[4]) >= 10) {
428 newmailcount = extract_int(&aaa[4], 9);
429 if ((oldmailcount >= 0) && (newmailcount > oldmailcount)) {
431 printf("*** You have new mail\n");
434 oldmailcount = newmailcount;
438 /* Goto next room having unread messages.
439 * We want to skip over rooms that the user has already been to, and take the
440 * user back to the lobby when done. The room we end up in is placed in
441 * newroom - which is set to 0 (the lobby) initially.
446 struct march *mptr, *mptr2;
447 char next_room[ROOMNAMELEN];
449 /* Check to see if the march-mode list is already allocated.
450 * If it is, pop the first room off the list and go there.
456 while (serv_gets(buf), strcmp(buf, "000")) {
457 mptr = (struct march *) malloc(sizeof(struct march));
459 extract(mptr->march_name, buf, 0);
460 mptr->march_floor = (char) (extract_int(buf, 2) & 0x7F);
461 mptr->march_order = (char) (extract_int(buf, 3) & 0x7F);
466 while (mptr2->next != NULL)
471 /* add _BASEROOM_ to the end of the march list, so the user will end up
472 * in the system base room (usually the Lobby>) at the end of the loop
474 mptr = (struct march *) malloc(sizeof(struct march));
476 strcpy(mptr->march_name, "_BASEROOM_");
481 while (mptr2->next != NULL)
486 * ...and remove the room we're currently in, so a <G>oto doesn't make us
487 * walk around in circles
489 remove_march(room_name, 0);
492 strcpy(next_room, pop_march(curr_floor));
494 strcpy(next_room, "_BASEROOM_");
496 remove_march(next_room, 0);
497 dotgoto(next_room, 1);
501 * forget all rooms on a given floor
503 void forget_all_rooms_on(int ffloor)
506 struct march *flist, *fptr;
508 printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
510 snprintf(buf, sizeof buf, "LKRA %d", ffloor);
514 printf("%-72s\n", &buf[4]);
518 while (serv_gets(buf), strcmp(buf, "000")) {
519 fptr = (struct march *) malloc(sizeof(struct march));
522 extract(fptr->march_name, buf, 0);
524 while (flist != NULL) {
525 snprintf(buf, sizeof buf, "GOTO %s", flist->march_name);
536 printf("%-72s\r", "");
541 * routine called by gotofloor() to move to a new room on a new floor
543 void gf_toroom(char *towhere, int mode)
545 int floor_being_left;
547 floor_being_left = curr_floor;
549 if (mode == GF_GOTO) { /* <;G>oto mode */
553 if (mode == GF_SKIP) { /* <;S>kip mode */
555 remove_march("_FLOOR_", floor_being_left);
557 if (mode == GF_ZAP) { /* <;Z>ap mode */
559 remove_march("_FLOOR_", floor_being_left);
560 forget_all_rooms_on(floor_being_left);
568 void gotofloor(char *towhere, int mode)
572 char buf[256], targ[256];
574 if (floorlist[0][0] == 0)
577 for (a = 0; a < 128; ++a)
578 if (!strcasecmp(&floorlist[a][0], towhere))
582 for (a = 0; a < 128; ++a) {
583 if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
589 for (a = 0; a < 128; ++a)
590 if (pattern(towhere, &floorlist[a][0]) > 0)
594 printf("No floor '%s'.\n", towhere);
597 for (mptr = march; mptr != NULL; mptr = mptr->next) {
598 if ((mptr->march_floor) == tofloor)
599 gf_toroom(mptr->march_name, mode);
604 snprintf(buf, sizeof buf, "LKRA %d", tofloor);
608 while (serv_gets(buf), strcmp(buf, "000")) {
609 if ((extract_int(buf, 2) == tofloor) && (strlen(targ) == 0))
610 extract(targ, buf, 0);
612 if (strlen(targ) > 0) {
613 gf_toroom(targ, mode);
615 printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
621 * forget all rooms on current floor
623 void forget_this_floor(void)
626 if (curr_floor == 0) {
627 printf("Can't forget this floor.\n");
630 if (floorlist[0][0] == 0)
632 printf("Are you sure you want to forget all rooms on %s? ",
633 &floorlist[(int) curr_floor][0]);
637 gf_toroom("_BASEROOM_", GF_ZAP);
642 * Figure out the physical screen dimensions, if we can
644 void check_screen_dims(void)
648 unsigned short height; /* rows */
649 unsigned short width; /* columns */
650 unsigned short xpixels;
651 unsigned short ypixels; /* pixels */
654 if (have_xterm) { /* dynamically size screen if on an xterm */
655 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
657 screenheight = (int) xwinsz.height;
659 screenwidth = (int) xwinsz.width;
667 * set floor mode depending on client, server, and user settings
669 void set_floor_mode(void)
671 if (serv_info.serv_ok_floors == 0) {
672 floor_mode = 0; /* Don't use floors if the server */
674 /* doesn't support them! */
676 if (rc_floor_mode == RC_NO) { /* never use floors */
679 if (rc_floor_mode == RC_YES) { /* always use floors */
682 if (rc_floor_mode == RC_DEFAULT) { /* user choice */
683 floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
689 * Set or change the user's password
691 int set_password(void)
697 if (strlen(rc_password) > 0) {
698 strcpy(pass1, rc_password);
699 strcpy(pass2, rc_password);
701 IFNEXPERT formout("changepw");
702 newprompt("Enter a new password: ", pass1, -19);
703 newprompt("Enter it again to confirm: ", pass2, -19);
707 if (!strcasecmp(pass1, pass2)) {
708 snprintf(buf, sizeof buf, "SETP %s", pass1);
711 printf("%s\n", &buf[4]);
712 offer_to_remember_password(hostbuf, portbuf, fullname, pass1);
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 pprintf("FLG ### User Name Room From host\n");
768 pprintf("--- --- ------------------------- -------------------- ------------------------\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 pprintf("\nFlags: %-3s Sess# %-3d Name: %-25s Room: %s\n",
786 flags, extract_int(buf, 0), username, roomname);
787 pprintf("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 pprintf("%-3s ", flags);
798 pprintf("%-3d ", extract_int(buf, 0));
800 last_session = extract_int(buf, 0);
802 pprintf("%-25s ", username);
803 color(BRIGHT_MAGENTA);
805 pprintf("%-20s ", roomname);
807 pprintf("%-24s\n", fromhost);
814 void enternew(char *desc, char *buf, int maxlen)
817 snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
818 newprompt(bbb, buf, maxlen);
824 int main(int argc, char **argv)
827 char aaa[100], bbb[100];/* general purpose variables */
828 char argbuf[32]; /* command line buf */
829 volatile int termn8 = 0;
830 int stored_password = 0;
833 sttybbs(SB_SAVE); /* Store the old terminal parameters */
834 load_command_set(); /* parse the citadel.rc file */
835 sttybbs(SB_NO_INTR); /* Install the new ones */
836 signal(SIGINT, SIG_IGN);
837 signal(SIGQUIT, SIG_IGN);
838 signal(SIGHUP, dropcarr); /* Cleanup gracefully if carrier is dropped */
839 signal(SIGTERM, dropcarr); /* Cleanup gracefully if terminated */
840 signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
842 printf("Attaching to server... \r");
844 attach_to_server(argc, argv, hostbuf, portbuf);
850 printf("%s\n", &aaa[4]);
859 printf("%-23s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
860 serv_info.serv_bbs_city);
861 screenwidth = 80; /* default screen dimensions */
864 printf(" pause next stop\n");
865 printf(" ctrl-s ctrl-o ctrl-c\n\n");
866 formout("hello"); /* print the opening greeting */
869 GSTA: /* See if we have a username and password on disk */
870 if (rc_remember_passwords) {
871 get_stored_password(hostbuf, portbuf, fullname, password);
872 if (strlen(fullname) > 0) {
873 sprintf(aaa, "USER %s", fullname);
876 sprintf(aaa, "PASS %s", password);
880 load_user_info(&aaa[4]);
885 set_stored_password(hostbuf, portbuf, "", "");
893 if (strlen(rc_username) > 0) {
894 strcpy(fullname, rc_username);
896 newprompt("Enter your name: ", fullname, 29);
899 if (!strcasecmp(fullname, "new")) { /* just in case */
900 printf("Please enter the name you wish to log in with.\n");
903 (!strcasecmp(fullname, "bbs"))
904 || (!strcasecmp(fullname, "new"))
905 || (strlen(fullname) == 0));
907 if (!strcasecmp(fullname, "off")) {
911 /* sign on to the server */
912 snprintf(aaa, sizeof aaa, "USER %s", fullname);
918 /* password authentication */
919 if (strlen(rc_password) > 0) {
920 strcpy(password, rc_password);
922 newprompt("\rPlease enter your password: ", password, -19);
925 snprintf(aaa, sizeof aaa, "PASS %s", password);
929 load_user_info(&aaa[4]);
930 offer_to_remember_password(hostbuf, portbuf,
934 printf("<< wrong password >>\n");
935 if (strlen(rc_password) > 0)
939 NEWUSR: if (strlen(rc_password) == 0) {
940 printf("No record. Enter as new user? ");
944 snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
951 load_user_info(&aaa[4]);
953 while (set_password() != 0);;
959 PWOK: printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
960 fullname, axlevel, axdefs[(int) axlevel],
961 usernum, timescalled);
966 b = extract_int(&aaa[4], 0);
970 printf("*** You have a new private message in Mail>\n");
972 printf("*** You have %d new private messages in Mail>\n", b);
975 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
976 printf("*** Users need validation\n");
978 if (extract_int(&aaa[4], 1) > 0) {
979 printf("*** Please register.\n");
984 /* Make up some temporary filenames for use in various parts of the
985 * program. Don't mess with these once they've been set, because we
986 * will be unlinking them later on in the program and we don't
987 * want to delete something that we didn't create. */
988 snprintf(temp, sizeof temp, tmpnam(NULL));
989 snprintf(temp2, sizeof temp2, tmpnam(NULL));
990 snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
992 /* Get screen dimensions. First we go to a default of 80x24. Then
993 * we try to get the user's actual screen dimensions off the server.
994 * However, if we're running on an xterm, all this stuff is
995 * irrelevant because we're going to dynamically size the screen
996 * during the session.
1002 if (aaa[0] == '2') {
1003 screenwidth = extract_int(&aaa[4], 0);
1004 screenheight = extract_int(&aaa[4], 1);
1006 if (getenv("TERM") != NULL)
1007 if (!strcmp(getenv("TERM"), "xterm")) {
1011 check_screen_dims();
1017 /* Enter the lobby */
1018 dotgoto("_BASEROOM_", 1);
1020 /* Main loop for the system... user is logged in. */
1029 do { /* MAIN LOOP OF PROGRAM */
1031 signal(SIGINT, SIG_IGN);
1032 signal(SIGQUIT, SIG_IGN);
1033 mcmd = getcmd(argbuf);
1036 check_screen_dims();
1054 newprompt("What do you want your username to be? ", aaa, 32);
1055 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1058 if (strncmp("200", aaa, 3))
1059 printf("\n%s\n", aaa);
1073 dotgoto("_MAIL_", 1);
1090 readmsgs(0, (-1), 0);
1093 readmsgs(2, (-1), 0);
1096 readmsgs(3, 1, atoi(argbuf));
1183 cli_image_upload("_userpic_");
1186 cli_image_upload("_roompic_");
1190 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1191 cli_image_upload(aaa);
1195 enternew("roomname", aaa, 20);
1196 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1199 if (strncmp("200", aaa, 3))
1200 printf("\n%s\n", aaa);
1203 enternew("hostname", aaa, 25);
1204 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1207 if (strncmp("200", aaa, 3))
1208 printf("\n%s\n", aaa);
1211 enternew("username", aaa, 32);
1212 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1215 if (strncmp("200", aaa, 3))
1216 printf("\n%s\n", aaa);
1226 display_help(argbuf);
1230 formout("register");
1235 printf("Are you sure (y/n)? ");
1252 if (server_is_local) {
1253 sttybbs(SB_RESTORE);
1254 snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1255 "exec ./subsystem %ld %d %d", fullname,
1256 usernum, screenwidth, axlevel);
1258 sttybbs(SB_NO_INTR);
1260 printf("*** Can't run doors when server is not local.\n");
1273 do_system_configuration();
1277 do_internet_configuration();
1281 check_message_base();
1303 gotofloor(argbuf, GF_GOTO);
1307 gotofloor(argbuf, GF_SKIP);
1311 forget_this_floor();
1339 knrooms(floor_mode);
1349 misc_server_cmd(argbuf);
1353 edit_system_message(argbuf);
1382 } while (termn8 == 0);
1384 TERMN8:printf("%s logged out.\n", fullname);
1385 while (march != NULL)
1386 remove_march(march->march_name, 0);
1388 printf("\n\nType 'off' to hang up, or next user...\n");
1389 snprintf(aaa, sizeof aaa, "LOUT");
1392 if ((mcmd == 29) || (mcmd == 15)) {