4 * Main source module for the client program.
16 #include <sys/types.h>
19 #include <sys/ioctl.h>
27 #include "serv_info.h"
29 #include "routines2.h"
34 #include "client_chat.h"
35 #include "client_passwords.h"
36 #include "citadel_decls.h"
44 char march_name[ROOMNAMELEN];
49 #define IFEXPERT if (userflags&US_EXPERT)
50 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
51 #define IFAIDE if (axlevel>=6)
52 #define IFNAIDE if (axlevel<6)
54 struct march *march = NULL;
56 /* globals associated with the client program */
57 char temp[PATH_MAX]; /* Name of general temp file */
58 char temp2[PATH_MAX]; /* Name of general temp file */
59 char tempdir[PATH_MAX]; /* Name of general temp dir */
60 char editor_path[256]; /* path to external editor */
61 char printcmd[256]; /* print command */
62 int editor_pid = (-1);
65 struct CtdlServInfo serv_info; /* Info on the server connected */
69 char room_name[ROOMNAMELEN];
70 char ugname[ROOMNAMELEN];
71 long uglsn; /* holds <u>ngoto info */
72 char is_mail = 0; /* nonzero when we're in a mail room */
73 char axlevel = 0; /* access level */
74 char is_room_aide = 0; /* boolean flag, 1 if room aide */
78 long usernum = 0L; /* user number */
80 long highest_msg_read; /* used for <A>bandon room cmd */
81 long maxmsgnum; /* used for <G>oto */
83 char have_xterm = 0; /* are we running on an xterm? */
90 char curr_floor = 0; /* number of current floor */
91 char floorlist[128][256]; /* names of floors */
92 char express_msgs = 0; /* express messages waiting! */
95 * here is our 'clean up gracefully and exit' routine
99 if (editor_pid > 0) { /* kill the editor if it's running */
100 kill(editor_pid, SIGHUP);
102 /* shut down the server... but not if the logoff code is 3, because
103 * that means we're exiting because we already lost the server
109 * now clean up various things
116 /* Violently kill off any child processes if Citadel is
119 if (getppid() == 1) {
120 kill(0 - getpgrp(), SIGTERM);
122 kill(0 - getpgrp(), SIGKILL);
124 sttybbs(SB_RESTORE); /* return the old terminal settings */
125 exit(code); /* exit with the proper exit code */
131 * We handle "next" and "stop" much differently than in earlier versions.
132 * The signal catching routine simply sets a flag and returns.
134 void sighandler(int which_sig)
136 signal(SIGINT, SIG_IGN);
137 signal(SIGQUIT, SIG_IGN);
138 sigcaught = which_sig;
144 * signal catching function for hangups...
146 void dropcarr(int signum)
154 * catch SIGCONT to reset terminal modes when were are put back into the
157 void catch_sigcont(int signum)
160 signal(SIGCONT, catch_sigcont);
165 /* general purpose routines */
167 void formout(char *name)
168 { /* display a file */
170 snprintf(cmd, sizeof cmd, "MESG %s", name);
174 printf("%s\n", &cmd[4]);
177 fmout(screenwidth, NULL,
178 ((userflags & US_PAGINATOR) ? 1 : 0),
183 void userlist(char *patn)
193 pprintf("%s\n", &buf[4]);
197 sttybbs(SB_YES_INTR);
198 pprintf(" User Name Num L LastCall Calls Posts\n");
199 pprintf("------------------------- ----- - ---------- ----- -----\n");
200 while (serv_gets(buf), strcmp(buf, "000")) {
201 if (sigcaught == 0) {
203 if (pattern(fl, patn) >= 0) {
204 pprintf("%-25s ", fl);
205 pprintf("%5ld %d ", extract_long(buf, 2),
206 extract_int(buf, 1));
207 lc = extract_long(buf, 3);
208 tmbuf = (struct tm *) localtime(&lc);
209 pprintf("%02d/%02d/%04d ",
212 (tmbuf->tm_year + 1900));
213 pprintf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
224 * grab assorted info about the user...
226 void load_user_info(char *params)
228 extract(fullname, params, 0);
229 axlevel = extract_int(params, 1);
230 timescalled = extract_int(params, 2);
231 posted = extract_int(params, 3);
232 userflags = extract_int(params, 4);
233 usernum = extract_long(params, 5);
238 * Remove a room from the march list. 'floornum' is ignored unless
239 * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
240 * floor will be removed from the march list.
242 void remove_march(char *roomname, int floornum)
244 struct march *mptr, *mptr2;
249 if ((!strcasecmp(march->march_name, roomname))
250 || ((!strcasecmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
257 for (mptr = march; mptr != NULL; mptr = mptr->next) {
259 if ((!strcasecmp(mptr->march_name, roomname))
260 || ((!strcasecmp(roomname, "_FLOOR_"))
261 && (mptr->march_floor == floornum))) {
263 mptr2->next = mptr->next;
274 * Locate the room on the march list which we most want to go to. Each room
275 * is measured given a "weight" of preference based on various factors.
277 char *pop_march(int desired_floor)
279 static char TheRoom[ROOMNAMELEN];
281 int TheOrder = 32767;
284 struct march *mptr = NULL;
286 strcpy(TheRoom, "_BASEROOM_");
290 for (mptr = march; mptr != NULL; mptr = mptr->next) {
292 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
293 weight = weight + 10000;
294 if (mptr->march_floor == desired_floor)
295 weight = weight + 5000;
297 weight = weight + ((128 - (mptr->march_floor)) * 128);
298 weight = weight + (128 - (mptr->march_order));
300 if (weight > TheWeight) {
302 strcpy(TheRoom, mptr->march_name);
303 TheFloor = mptr->march_floor;
304 TheOrder = mptr->march_order;
312 * jump directly to a room
314 void dotgoto(char *towhere, int display_name)
316 char aaa[256], bbb[256], psearch[256];
319 static int oldmailcount = (-1);
320 int partial_match, best_match;
323 /* store ungoto information */
324 strcpy(ugname, room_name);
327 /* first try an exact match */
328 snprintf(aaa, sizeof aaa, "GOTO %s", towhere);
333 if (!strncmp(aaa, "54", 2)) {
334 newprompt("Enter room password: ", bbb, 9);
335 snprintf(aaa, sizeof aaa, "GOTO %s|%s", towhere, bbb);
341 if (!strncmp(aaa, "54", 2)) {
342 printf("Wrong password.\n");
346 * If a match is not found, try a partial match.
347 * Partial matches anywhere in the string carry a weight of 1,
348 * left-aligned matches carry a weight of 2. Pick the room that
349 * has the highest-weighted match.
357 while (serv_gets(aaa), strcmp(aaa, "000")) {
358 extract(psearch, aaa, 0);
360 if (pattern(psearch, towhere) >= 0) {
363 if (!strncasecmp(towhere, psearch, strlen(towhere))) {
366 if (partial_match > best_match) {
367 strcpy(bbb, psearch);
368 best_match = partial_match;
371 if (strlen(bbb) == 0) {
372 printf("No room '%s'.\n", towhere);
375 snprintf(aaa, sizeof aaa, "GOTO %s", bbb);
385 extract(room_name, &aaa[4], 0);
386 room_flags = extract_int(&aaa[4], 4);
387 from_floor = curr_floor;
388 curr_floor = extract_int(&aaa[4], 10);
390 remove_march(room_name, 0);
391 if (!strcasecmp(towhere, "_BASEROOM_"))
392 remove_march(towhere, 0);
393 if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
394 if (floorlist[(int) curr_floor][0] == 0)
396 printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
398 if (display_name == 1) {
400 printf("%s ", room_name);
404 if (display_name != 2) {
405 color(BRIGHT_YELLOW);
406 printf("%d ", extract_int(&aaa[4], 1));
409 color(BRIGHT_YELLOW);
410 printf("%d ", extract_int(&aaa[4], 2));
412 printf("messages.\n");
414 highest_msg_read = extract_int(&aaa[4], 6);
415 maxmsgnum = extract_int(&aaa[4], 5);
416 is_mail = (char) extract_int(&aaa[4], 7);
417 is_room_aide = (char) extract_int(&aaa[4], 8);
418 ls = extract_long(&aaa[4], 6);
420 /* read info file if necessary */
421 if (extract_int(&aaa[4], 3) > 0)
424 /* check for newly arrived mail if we can */
425 if (num_parms(&aaa[4]) >= 10) {
426 newmailcount = extract_int(&aaa[4], 9);
427 if ((oldmailcount >= 0) && (newmailcount > oldmailcount)) {
429 printf("*** You have new mail\n");
432 oldmailcount = newmailcount;
436 /* Goto next room having unread messages.
437 * We want to skip over rooms that the user has already been to, and take the
438 * user back to the lobby when done. The room we end up in is placed in
439 * newroom - which is set to 0 (the lobby) initially.
444 struct march *mptr, *mptr2;
445 char next_room[ROOMNAMELEN];
447 /* Check to see if the march-mode list is already allocated.
448 * If it is, pop the first room off the list and go there.
454 while (serv_gets(buf), strcmp(buf, "000")) {
455 mptr = (struct march *) malloc(sizeof(struct march));
457 extract(mptr->march_name, buf, 0);
458 mptr->march_floor = (char) (extract_int(buf, 2) & 0x7F);
459 mptr->march_order = (char) (extract_int(buf, 3) & 0x7F);
464 while (mptr2->next != NULL)
469 /* add _BASEROOM_ to the end of the march list, so the user will end up
470 * in the system base room (usually the Lobby>) at the end of the loop
472 mptr = (struct march *) malloc(sizeof(struct march));
474 strcpy(mptr->march_name, "_BASEROOM_");
479 while (mptr2->next != NULL)
484 * ...and remove the room we're currently in, so a <G>oto doesn't make us
485 * walk around in circles
487 remove_march(room_name, 0);
490 strcpy(next_room, pop_march(curr_floor));
492 strcpy(next_room, "_BASEROOM_");
494 remove_march(next_room, 0);
495 dotgoto(next_room, 1);
499 * forget all rooms on a given floor
501 void forget_all_rooms_on(int ffloor)
504 struct march *flist, *fptr;
506 printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
508 snprintf(buf, sizeof buf, "LKRA %d", ffloor);
512 printf("%-72s\n", &buf[4]);
516 while (serv_gets(buf), strcmp(buf, "000")) {
517 fptr = (struct march *) malloc(sizeof(struct march));
520 extract(fptr->march_name, buf, 0);
522 while (flist != NULL) {
523 snprintf(buf, sizeof buf, "GOTO %s", flist->march_name);
534 printf("%-72s\r", "");
539 * routine called by gotofloor() to move to a new room on a new floor
541 void gf_toroom(char *towhere, int mode)
543 int floor_being_left;
545 floor_being_left = curr_floor;
547 if (mode == GF_GOTO) { /* <;G>oto mode */
551 if (mode == GF_SKIP) { /* <;S>kip mode */
553 remove_march("_FLOOR_", floor_being_left);
555 if (mode == GF_ZAP) { /* <;Z>ap mode */
557 remove_march("_FLOOR_", floor_being_left);
558 forget_all_rooms_on(floor_being_left);
566 void gotofloor(char *towhere, int mode)
570 char buf[256], targ[256];
572 if (floorlist[0][0] == 0)
575 for (a = 0; a < 128; ++a)
576 if (!strcasecmp(&floorlist[a][0], towhere))
580 for (a = 0; a < 128; ++a) {
581 if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
587 for (a = 0; a < 128; ++a)
588 if (pattern(towhere, &floorlist[a][0]) > 0)
592 printf("No floor '%s'.\n", towhere);
595 for (mptr = march; mptr != NULL; mptr = mptr->next) {
596 if ((mptr->march_floor) == tofloor)
597 gf_toroom(mptr->march_name, mode);
602 snprintf(buf, sizeof buf, "LKRA %d", tofloor);
606 while (serv_gets(buf), strcmp(buf, "000")) {
607 if ((extract_int(buf, 2) == tofloor) && (strlen(targ) == 0))
608 extract(targ, buf, 0);
610 if (strlen(targ) > 0) {
611 gf_toroom(targ, mode);
613 printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
619 * forget all rooms on current floor
621 void forget_this_floor(void)
624 if (curr_floor == 0) {
625 printf("Can't forget this floor.\n");
628 if (floorlist[0][0] == 0)
630 printf("Are you sure you want to forget all rooms on %s? ",
631 &floorlist[(int) curr_floor][0]);
635 gf_toroom("_BASEROOM_", GF_ZAP);
640 * Figure out the physical screen dimensions, if we can
642 void check_screen_dims(void)
646 unsigned short height; /* rows */
647 unsigned short width; /* columns */
648 unsigned short xpixels;
649 unsigned short ypixels; /* pixels */
652 if (have_xterm) { /* dynamically size screen if on an xterm */
653 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
655 screenheight = (int) xwinsz.height;
657 screenwidth = (int) xwinsz.width;
665 * set floor mode depending on client, server, and user settings
667 void set_floor_mode(void)
669 if (serv_info.serv_ok_floors == 0) {
670 floor_mode = 0; /* Don't use floors if the server */
672 /* doesn't support them! */
674 if (rc_floor_mode == RC_NO) { /* never use floors */
677 if (rc_floor_mode == RC_YES) { /* always use floors */
680 if (rc_floor_mode == RC_DEFAULT) { /* user choice */
681 floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
687 * Set or change the user's password
689 int set_password(void)
695 if (strlen(rc_password) > 0) {
696 strcpy(pass1, rc_password);
697 strcpy(pass2, rc_password);
699 IFNEXPERT formout("changepw");
700 newprompt("Enter a new password: ", pass1, -19);
701 newprompt("Enter it again to confirm: ", pass2, -19);
705 if (!strcasecmp(pass1, pass2)) {
706 snprintf(buf, sizeof buf, "SETP %s", pass1);
709 printf("%s\n", &buf[4]);
710 offer_to_remember_password(hostbuf, portbuf, fullname, pass1);
713 printf("*** They don't match... try again.\n");
721 * get info about the server we've connected to
723 void get_serv_info(void)
727 CtdlInternalGetServInfo(&serv_info);
729 /* be nice and identify ourself to the server */
730 snprintf(buf, sizeof buf, "IDEN %d|%d|%d|%s|",
731 SERVER_TYPE, 0, REV_LEVEL,
732 (server_is_local ? "local" : CITADEL));
733 locate_host(&buf[strlen(buf)]); /* append to the end */
735 serv_gets(buf); /* we don't care about the result code */
743 * Display list of users currently logged on to the server
745 void who_is_online(int longlist)
747 char buf[128], username[128], roomname[128], fromhost[128],
749 char tbuf[128], clientsoft[128];
751 time_t idletime, idlehours, idlemins, idlesecs;
752 int last_session = (-1);
757 if (tbuf[0] == '2') {
758 timenow = extract_long(&tbuf[4], 0);
764 pprintf("FLG ### User Name Room From host\n");
766 pprintf("--- --- ------------------------- -------------------- ------------------------\n");
771 while (serv_gets(buf), strcmp(buf, "000")) {
772 extract(username, buf, 1);
773 extract(roomname, buf, 2);
774 extract(fromhost, buf, 3);
775 extract(clientsoft, buf, 4);
776 extract(flags, buf, 7);
779 idletime = timenow - extract_long(buf, 5);
780 idlehours = idletime / 3600;
781 idlemins = (idletime - (idlehours * 3600)) / 60;
782 idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
783 pprintf("\nFlags: %-3s Sess# %-3d Name: %-25s Room: %s\n",
784 flags, extract_int(buf, 0), username, roomname);
785 pprintf("from <%s> using <%s>, idle %ld:%02ld:%02ld\n",
786 fromhost, clientsoft,
787 (long) idlehours, (long) idlemins, (long) idlesecs);
790 if (extract_int(buf, 0) == last_session) {
793 color(BRIGHT_MAGENTA);
794 pprintf("%-3s ", flags);
796 pprintf("%-3d ", extract_int(buf, 0));
798 last_session = extract_int(buf, 0);
800 pprintf("%-25s ", username);
801 color(BRIGHT_MAGENTA);
803 pprintf("%-20s ", roomname);
805 pprintf("%-24s\n", fromhost);
812 void enternew(char *desc, char *buf, int maxlen)
815 snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
816 newprompt(bbb, buf, maxlen);
822 int main(int argc, char **argv)
825 char aaa[100], bbb[100];/* general purpose variables */
826 char argbuf[32]; /* command line buf */
827 volatile int termn8 = 0;
828 int stored_password = 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, hostbuf, portbuf);
848 printf("%s\n", &aaa[4]);
857 printf("%-23s\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 */
867 GSTA: /* See if we have a username and password on disk */
868 if (rc_remember_passwords) {
869 get_stored_password(hostbuf, portbuf, fullname, password);
870 if (strlen(fullname) > 0) {
871 sprintf(aaa, "USER %s", fullname);
874 sprintf(aaa, "PASS %s", password);
878 load_user_info(&aaa[4]);
883 set_stored_password(hostbuf, portbuf, "", "");
891 if (strlen(rc_username) > 0) {
892 strcpy(fullname, rc_username);
894 newprompt("Enter your name: ", fullname, 29);
897 if (!strcasecmp(fullname, "new")) { /* just in case */
898 printf("Please enter the name you wish to log in with.\n");
901 (!strcasecmp(fullname, "bbs"))
902 || (!strcasecmp(fullname, "new"))
903 || (strlen(fullname) == 0));
905 if (!strcasecmp(fullname, "off")) {
909 /* sign on to the server */
910 snprintf(aaa, sizeof aaa, "USER %s", fullname);
916 /* password authentication */
917 if (strlen(rc_password) > 0) {
918 strcpy(password, rc_password);
920 newprompt("\rPlease enter your password: ", password, -19);
923 snprintf(aaa, sizeof aaa, "PASS %s", password);
927 load_user_info(&aaa[4]);
928 offer_to_remember_password(hostbuf, portbuf,
932 printf("<< wrong password >>\n");
933 if (strlen(rc_password) > 0)
937 NEWUSR: if (strlen(rc_password) == 0) {
938 printf("No record. Enter as new user? ");
942 snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
949 load_user_info(&aaa[4]);
951 while (set_password() != 0);;
957 PWOK: printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
958 fullname, axlevel, axdefs[(int) axlevel],
959 usernum, timescalled);
964 b = extract_int(&aaa[4], 0);
968 printf("*** You have a new private message in Mail>\n");
970 printf("*** You have %d new private messages in Mail>\n", b);
973 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
974 printf("*** Users need validation\n");
976 if (extract_int(&aaa[4], 1) > 0) {
977 printf("*** Please register.\n");
982 /* Make up some temporary filenames for use in various parts of the
983 * program. Don't mess with these once they've been set, because we
984 * will be unlinking them later on in the program and we don't
985 * want to delete something that we didn't create. */
986 snprintf(temp, sizeof temp, tmpnam(NULL));
987 snprintf(temp2, sizeof temp2, tmpnam(NULL));
988 snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
990 /* Get screen dimensions. First we go to a default of 80x24. Then
991 * we try to get the user's actual screen dimensions off the server.
992 * However, if we're running on an xterm, all this stuff is
993 * irrelevant because we're going to dynamically size the screen
994 * during the session.
1000 if (aaa[0] == '2') {
1001 screenwidth = extract_int(&aaa[4], 0);
1002 screenheight = extract_int(&aaa[4], 1);
1004 if (getenv("TERM") != NULL)
1005 if (!strcmp(getenv("TERM"), "xterm")) {
1009 check_screen_dims();
1015 /* Enter the lobby */
1016 dotgoto("_BASEROOM_", 1);
1018 /* Main loop for the system... user is logged in. */
1027 do { /* MAIN LOOP OF PROGRAM */
1029 signal(SIGINT, SIG_IGN);
1030 signal(SIGQUIT, SIG_IGN);
1031 mcmd = getcmd(argbuf);
1034 check_screen_dims();
1052 newprompt("What do you want your username to be? ", aaa, 32);
1053 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1056 if (strncmp("200", aaa, 3))
1057 printf("\n%s\n", aaa);
1071 dotgoto("_MAIL_", 1);
1088 readmsgs(0, (-1), 0);
1091 readmsgs(2, (-1), 0);
1094 readmsgs(3, 1, atoi(argbuf));
1181 cli_image_upload("_userpic_");
1184 cli_image_upload("_roompic_");
1188 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1189 cli_image_upload(aaa);
1193 enternew("roomname", aaa, 20);
1194 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1197 if (strncmp("200", aaa, 3))
1198 printf("\n%s\n", aaa);
1201 enternew("hostname", aaa, 25);
1202 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1205 if (strncmp("200", aaa, 3))
1206 printf("\n%s\n", aaa);
1209 enternew("username", aaa, 32);
1210 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1213 if (strncmp("200", aaa, 3))
1214 printf("\n%s\n", aaa);
1224 display_help(argbuf);
1228 formout("register");
1233 printf("Are you sure (y/n)? ");
1250 if (server_is_local) {
1251 sttybbs(SB_RESTORE);
1252 snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1253 "exec ./subsystem %ld %d %d", fullname,
1254 usernum, screenwidth, axlevel);
1256 sttybbs(SB_NO_INTR);
1258 printf("*** Can't run doors when server is not local.\n");
1271 do_system_configuration();
1275 do_internet_configuration();
1279 check_message_base();
1301 gotofloor(argbuf, GF_GOTO);
1305 gotofloor(argbuf, GF_SKIP);
1309 forget_this_floor();
1337 knrooms(floor_mode);
1347 misc_server_cmd(argbuf);
1351 edit_system_message(argbuf);
1376 } while (termn8 == 0);
1378 TERMN8:printf("%s logged out.\n", fullname);
1379 while (march != NULL)
1380 remove_march(march->march_name, 0);
1382 printf("\n\nType 'off' to hang up, or next user...\n");
1383 snprintf(aaa, sizeof aaa, "LOUT");
1386 if ((mcmd == 29) || (mcmd == 15)) {