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),
182 void userlist(char *patn)
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 if (pattern(fl, patn) >= 0) {
204 printf("%-25s ", fl);
205 printf("%5ld %d ", extract_long(buf, 2),
206 extract_int(buf, 1));
207 lc = extract_long(buf, 3);
208 tmbuf = (struct tm *) localtime(&lc);
209 printf("%02d/%02d/%04d ",
212 (tmbuf->tm_year + 1900));
213 printf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
216 linecount = checkpagin(linecount,
217 ((userflags & US_PAGINATOR) ? 1 : 0),
229 * grab assorted info about the user...
231 void load_user_info(char *params)
233 extract(fullname, params, 0);
234 axlevel = extract_int(params, 1);
235 timescalled = extract_int(params, 2);
236 posted = extract_int(params, 3);
237 userflags = extract_int(params, 4);
238 usernum = extract_long(params, 5);
243 * Remove a room from the march list. 'floornum' is ignored unless
244 * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
245 * floor will be removed from the march list.
247 void remove_march(char *roomname, int floornum)
249 struct march *mptr, *mptr2;
254 if ((!strucmp(march->march_name, roomname))
255 || ((!strucmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
262 for (mptr = march; mptr != NULL; mptr = mptr->next) {
264 if ((!strucmp(mptr->march_name, roomname))
265 || ((!strucmp(roomname, "_FLOOR_"))
266 && (mptr->march_floor == floornum))) {
268 mptr2->next = mptr->next;
279 * Locate the room on the march list which we most want to go to. Each room
280 * is measured given a "weight" of preference based on various factors.
282 char *pop_march(int desired_floor)
284 static char TheRoom[ROOMNAMELEN];
286 int TheOrder = 32767;
289 struct march *mptr = NULL;
291 strcpy(TheRoom, "_BASEROOM_");
295 for (mptr = march; mptr != NULL; mptr = mptr->next) {
297 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
298 weight = weight + 10000;
299 if (mptr->march_floor == desired_floor)
300 weight = weight + 5000;
302 weight = weight + ((128 - (mptr->march_floor)) * 128);
303 weight = weight + (128 - (mptr->march_order));
305 if (weight > TheWeight) {
307 strcpy(TheRoom, mptr->march_name);
308 TheFloor = mptr->march_floor;
309 TheOrder = mptr->march_order;
317 * jump directly to a room
319 void dotgoto(char *towhere, int display_name)
321 char aaa[256], bbb[256], psearch[256];
324 static int oldmailcount = (-1);
325 int partial_match, best_match;
328 /* store ungoto information */
329 strcpy(ugname, room_name);
332 /* first try an exact match */
333 snprintf(aaa, sizeof aaa, "GOTO %s", towhere);
338 if (!strncmp(aaa, "54", 2)) {
339 newprompt("Enter room password: ", bbb, 9);
340 snprintf(aaa, sizeof aaa, "GOTO %s|%s", towhere, bbb);
346 if (!strncmp(aaa, "54", 2)) {
347 printf("Wrong password.\n");
351 * If a match is not found, try a partial match.
352 * Partial matches anywhere in the string carry a weight of 1,
353 * left-aligned matches carry a weight of 2. Pick the room that
354 * has the highest-weighted match.
362 while (serv_gets(aaa), strcmp(aaa, "000")) {
363 extract(psearch, aaa, 0);
365 if (pattern(psearch, towhere) >= 0) {
368 if (!struncmp(towhere, psearch, strlen(towhere))) {
371 if (partial_match > best_match) {
372 strcpy(bbb, psearch);
373 best_match = partial_match;
376 if (strlen(bbb) == 0) {
377 printf("No room '%s'.\n", towhere);
380 snprintf(aaa, sizeof aaa, "GOTO %s", bbb);
390 extract(room_name, &aaa[4], 0);
391 room_flags = extract_int(&aaa[4], 4);
392 from_floor = curr_floor;
393 curr_floor = extract_int(&aaa[4], 10);
395 remove_march(room_name, 0);
396 if (!strucmp(towhere, "_BASEROOM_"))
397 remove_march(towhere, 0);
398 if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
399 if (floorlist[(int) curr_floor][0] == 0)
401 printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
403 if (display_name == 1) {
405 printf("%s ", room_name);
409 if (display_name != 2) {
410 color(BRIGHT_YELLOW);
411 printf("%d ", extract_int(&aaa[4], 1));
414 color(BRIGHT_YELLOW);
415 printf("%d ", extract_int(&aaa[4], 2));
417 printf("messages.\n");
419 highest_msg_read = extract_int(&aaa[4], 6);
420 maxmsgnum = extract_int(&aaa[4], 5);
421 is_mail = (char) extract_int(&aaa[4], 7);
422 is_room_aide = (char) extract_int(&aaa[4], 8);
423 ls = extract_long(&aaa[4], 6);
425 /* read info file if necessary */
426 if (extract_int(&aaa[4], 3) > 0)
429 /* check for newly arrived mail if we can */
430 if (num_parms(&aaa[4]) >= 10) {
431 newmailcount = extract_int(&aaa[4], 9);
432 if ((oldmailcount >= 0) && (newmailcount > oldmailcount)) {
434 printf("*** You have new mail\n");
437 oldmailcount = newmailcount;
441 /* Goto next room having unread messages.
442 * We want to skip over rooms that the user has already been to, and take the
443 * user back to the lobby when done. The room we end up in is placed in
444 * newroom - which is set to 0 (the lobby) initially.
449 struct march *mptr, *mptr2;
452 /* Check to see if the march-mode list is already allocated.
453 * If it is, pop the first room off the list and go there.
459 while (serv_gets(buf), strcmp(buf, "000")) {
460 mptr = (struct march *) malloc(sizeof(struct march));
462 extract(mptr->march_name, buf, 0);
463 mptr->march_floor = (char) (extract_int(buf, 2) & 0x7F);
464 mptr->march_order = (char) (extract_int(buf, 3) & 0x7F);
469 while (mptr2->next != NULL)
474 /* add _BASEROOM_ to the end of the march list, so the user will end up
475 * in the system base room (usually the Lobby>) at the end of the loop
477 mptr = (struct march *) malloc(sizeof(struct march));
479 strcpy(mptr->march_name, "_BASEROOM_");
484 while (mptr2->next != NULL)
489 * ...and remove the room we're currently in, so a <G>oto doesn't make us
490 * walk around in circles
492 remove_march(room_name, 0);
495 strcpy(next_room, pop_march(curr_floor));
497 strcpy(next_room, "_BASEROOM_");
499 remove_march(next_room, 0);
500 dotgoto(next_room, 1);
504 * forget all rooms on a given floor
506 void forget_all_rooms_on(int ffloor)
509 struct march *flist, *fptr;
511 printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
513 snprintf(buf, sizeof buf, "LKRA %d", ffloor);
517 printf("%-72s\n", &buf[4]);
521 while (serv_gets(buf), strcmp(buf, "000")) {
522 fptr = (struct march *) malloc(sizeof(struct march));
525 extract(fptr->march_name, buf, 0);
527 while (flist != NULL) {
528 snprintf(buf, sizeof buf, "GOTO %s", flist->march_name);
539 printf("%-72s\r", "");
544 * routine called by gotofloor() to move to a new room on a new floor
546 void gf_toroom(char *towhere, int mode)
548 int floor_being_left;
550 floor_being_left = curr_floor;
552 if (mode == GF_GOTO) { /* <;G>oto mode */
556 if (mode == GF_SKIP) { /* <;S>kip mode */
558 remove_march("_FLOOR_", floor_being_left);
560 if (mode == GF_ZAP) { /* <;Z>ap mode */
562 remove_march("_FLOOR_", floor_being_left);
563 forget_all_rooms_on(floor_being_left);
571 void gotofloor(char *towhere, int mode)
575 char buf[256], targ[256];
577 if (floorlist[0][0] == 0)
580 for (a = 0; a < 128; ++a)
581 if (!strucmp(&floorlist[a][0], towhere))
585 for (a = 0; a < 128; ++a) {
586 if (!struncmp(&floorlist[a][0], towhere, strlen(towhere))) {
592 for (a = 0; a < 128; ++a)
593 if (pattern(towhere, &floorlist[a][0]) > 0)
597 printf("No floor '%s'.\n", towhere);
600 for (mptr = march; mptr != NULL; mptr = mptr->next) {
601 if ((mptr->march_floor) == tofloor)
602 gf_toroom(mptr->march_name, mode);
607 snprintf(buf, sizeof buf, "LKRA %d", tofloor);
611 while (serv_gets(buf), strcmp(buf, "000")) {
612 if ((extract_int(buf, 2) == tofloor) && (strlen(targ) == 0))
613 extract(targ, buf, 0);
615 if (strlen(targ) > 0) {
616 gf_toroom(targ, mode);
618 printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
624 * forget all rooms on current floor
626 void forget_this_floor(void)
629 if (curr_floor == 0) {
630 printf("Can't forget this floor.\n");
633 if (floorlist[0][0] == 0)
635 printf("Are you sure you want to forget all rooms on %s? ",
636 &floorlist[(int) curr_floor][0]);
640 gf_toroom("_BASEROOM_", GF_ZAP);
645 * Figure out the physical screen dimensions, if we can
647 void check_screen_dims(void)
651 unsigned short height; /* rows */
652 unsigned short width; /* columns */
653 unsigned short xpixels;
654 unsigned short ypixels; /* pixels */
657 if (have_xterm) { /* dynamically size screen if on an xterm */
658 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
660 screenheight = (int) xwinsz.height;
662 screenwidth = (int) xwinsz.width;
670 * set floor mode depending on client, server, and user settings
672 void set_floor_mode(void)
674 if (serv_info.serv_ok_floors == 0) {
675 floor_mode = 0; /* Don't use floors if the server */
677 /* doesn't support them! */
679 if (rc_floor_mode == RC_NO) { /* never use floors */
682 if (rc_floor_mode == RC_YES) { /* always use floors */
685 if (rc_floor_mode == RC_DEFAULT) { /* user choice */
686 floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
692 * Set or change the user's password
694 int set_password(void)
700 if (strlen(rc_password) > 0) {
701 strcpy(pass1, rc_password);
702 strcpy(pass2, rc_password);
704 IFNEXPERT formout("changepw");
705 newprompt("Enter a new password: ", pass1, -19);
706 newprompt("Enter it again to confirm: ", pass2, -19);
710 if (!strucmp(pass1, pass2)) {
711 snprintf(buf, sizeof buf, "SETP %s", pass1);
714 printf("%s\n", &buf[4]);
717 printf("*** They don't match... try again.\n");
725 * get info about the server we've connected to
727 void get_serv_info(void)
731 CtdlInternalGetServInfo(&serv_info);
733 /* be nice and identify ourself to the server */
734 snprintf(buf, sizeof buf, "IDEN %d|%d|%d|%s|",
735 SERVER_TYPE, 0, REV_LEVEL,
736 (server_is_local ? "local" : CITADEL));
737 locate_host(&buf[strlen(buf)]); /* append to the end */
739 serv_gets(buf); /* we don't care about the result code */
747 * Display list of users currently logged on to the server
749 void who_is_online(int longlist)
751 char buf[128], username[128], roomname[128], fromhost[128],
753 char tbuf[128], clientsoft[128];
755 time_t idletime, idlehours, idlemins, idlesecs;
756 int last_session = (-1);
762 if (tbuf[0] == '2') {
763 timenow = extract_long(&tbuf[4], 0);
769 printf("FLG ### User Name Room From host\n");
771 printf("--- --- ------------------------- -------------------- ------------------------\n");
776 while (serv_gets(buf), strcmp(buf, "000")) {
777 extract(username, buf, 1);
778 extract(roomname, buf, 2);
779 extract(fromhost, buf, 3);
780 extract(clientsoft, buf, 4);
781 extract(flags, buf, 7);
784 idletime = timenow - extract_long(buf, 5);
785 idlehours = idletime / 3600;
786 idlemins = (idletime - (idlehours * 3600)) / 60;
787 idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
788 printf("\nFlags: %-3s Sess# %-3d Name: %-25s Room: %s\n",
789 flags, extract_int(buf, 0), username, roomname);
790 printf("from <%s> using <%s>, idle %ld:%02ld:%02ld\n",
791 fromhost, clientsoft,
792 (long) idlehours, (long) idlemins, (long) idlesecs);
796 if (extract_int(buf, 0) == last_session) {
799 color(BRIGHT_MAGENTA);
800 printf("%-3s ", flags);
802 printf("%-3d ", extract_int(buf, 0));
804 last_session = extract_int(buf, 0);
806 printf("%-25s ", username);
807 color(BRIGHT_MAGENTA);
809 printf("%-20s ", roomname);
811 printf("%-24s\n", fromhost);
815 linecount = checkpagin(linecount,
816 ((userflags & US_PAGINATOR) ? 1 : 0),
822 void enternew(char *desc, char *buf, int maxlen)
825 snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
826 newprompt(bbb, buf, maxlen);
832 int main(int argc, char **argv)
835 char aaa[100], bbb[100], eee[100]; /* general purpose variables */
836 char argbuf[32]; /* command line buf */
837 volatile int termn8 = 0;
840 sttybbs(SB_SAVE); /* Store the old terminal parameters */
841 load_command_set(); /* parse the citadel.rc file */
842 sttybbs(SB_NO_INTR); /* Install the new ones */
843 signal(SIGINT, SIG_IGN);
844 signal(SIGQUIT, SIG_IGN);
845 signal(SIGHUP, dropcarr); /* Cleanup gracefully if carrier is dropped */
846 signal(SIGTERM, dropcarr); /* Cleanup gracefully if terminated */
847 signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
849 printf("Attaching to server...\r");
851 attach_to_server(argc, argv);
857 printf("%s\n", &aaa[4]);
866 printf("%-22s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
867 serv_info.serv_bbs_city);
868 screenwidth = 80; /* default screen dimensions */
871 printf(" pause next stop\n");
872 printf(" ctrl-s ctrl-o ctrl-c\n\n");
873 formout("hello"); /* print the opening greeting */
879 if (strlen(rc_username) > 0) {
880 strcpy(fullname, rc_username);
882 newprompt("Enter your name: ", fullname, 29);
885 if (!strucmp(fullname, "new")) { /* just in case */
886 printf("Please enter the name you wish to log in with.\n");
889 (!strucmp(fullname, "bbs"))
890 || (!strucmp(fullname, "new"))
891 || (strlen(fullname) == 0));
893 if (!strucmp(fullname, "off")) {
897 /* sign on to the server */
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 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 signal(SIGINT, SIG_IGN);
1016 signal(SIGQUIT, SIG_IGN);
1017 mcmd = getcmd(argbuf);
1020 check_screen_dims();
1038 newprompt("What do you want your username to be? ", aaa, 32);
1039 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1042 if (strncmp("200", aaa, 3))
1043 printf("\n%s\n", aaa);
1057 dotgoto("_MAIL_", 1);
1074 readmsgs(0, (-1), 0);
1077 readmsgs(2, (-1), 0);
1080 readmsgs(3, 1, atoi(argbuf));
1167 cli_image_upload("_userpic_");
1170 cli_image_upload("_roompic_");
1174 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1175 cli_image_upload(aaa);
1179 enternew("roomname", aaa, 20);
1180 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1183 if (strncmp("200", aaa, 3))
1184 printf("\n%s\n", aaa);
1187 enternew("hostname", aaa, 25);
1188 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1191 if (strncmp("200", aaa, 3))
1192 printf("\n%s\n", aaa);
1195 enternew("username", aaa, 32);
1196 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1199 if (strncmp("200", aaa, 3))
1200 printf("\n%s\n", aaa);
1210 display_help(argbuf);
1214 formout("register");
1219 printf("Are you sure (y/n)? ");
1236 if (server_is_local) {
1237 sttybbs(SB_RESTORE);
1238 snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1239 "exec ./subsystem %ld %d %d", fullname,
1240 usernum, screenwidth, axlevel);
1242 sttybbs(SB_NO_INTR);
1244 printf("*** Can't run doors when server is not local.\n");
1257 do_system_configuration();
1261 do_internet_configuration();
1265 check_message_base();
1287 gotofloor(argbuf, GF_GOTO);
1291 gotofloor(argbuf, GF_SKIP);
1295 forget_this_floor();
1323 knrooms(floor_mode);
1333 misc_server_cmd(argbuf);
1337 edit_system_message(argbuf);
1366 } while (termn8 == 0);
1368 TERMN8:printf("%s logged out.\n", fullname);
1369 while (march != NULL)
1370 remove_march(march->march_name, 0);
1372 printf("\n\nType 'off' to hang up, or next user...\n");
1373 snprintf(aaa, sizeof aaa, "LOUT");
1376 if ((mcmd == 29) || (mcmd == 15)) {