* Changed the comments at the beginning of each file to a consistent format
[citadel.git] / citadel / citadel.c
1 /*
2  * $Id$
3  *
4  * Main source module for the client program.
5  */
6
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <time.h>
15 #include <limits.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
20 #include <signal.h>
21 #include <pwd.h>
22 #include <setjmp.h>
23 #include <stdarg.h>
24
25 #include "citadel.h"
26 #include "axdefs.h"
27 #include "serv_info.h"
28 #include "routines.h"
29 #include "routines2.h"
30 #include "rooms.h"
31 #include "messages.h"
32 #include "commands.h"
33 #include "ipc.h"
34 #include "client_chat.h"
35 #include "client_passwords.h"
36 #include "citadel_decls.h"
37 #include "tools.h"
38 #ifndef HAVE_SNPRINTF
39 #include "snprintf.h"
40 #endif
41
42 struct march {
43         struct march *next;
44         char march_name[ROOMNAMELEN];
45         char march_floor;
46         char march_order;
47 };
48
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)
53
54 struct march *march = NULL;
55
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);
63 char fullname[32];
64 jmp_buf nextbuf;
65 struct CtdlServInfo serv_info;  /* Info on the server connected */
66 int screenwidth;
67 int screenheight;
68 unsigned room_flags;
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 */
75 int timescalled;
76 int posted;
77 unsigned userflags;
78 long usernum = 0L;              /* user number */
79 char newnow;
80 long highest_msg_read;          /* used for <A>bandon room cmd */
81 long maxmsgnum;                 /* used for <G>oto */
82 char sigcaught = 0;
83 char have_xterm = 0;            /* are we running on an xterm? */
84 char rc_username[32];
85 char rc_password[32];
86 char hostbuf[256];
87 char portbuf[256];
88 char rc_floor_mode;
89 char floor_mode;
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! */
93
94 /*
95  * here is our 'clean up gracefully and exit' routine
96  */
97 void logoff(int code)
98 {
99         if (editor_pid > 0) {   /* kill the editor if it's running */
100                 kill(editor_pid, SIGHUP);
101         }
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
104  */
105         if (code != 3)
106                 serv_puts("QUIT");
107
108 /*
109  * now clean up various things
110  */
111
112         unlink(temp);
113         unlink(temp2);
114         nukedir(tempdir);
115
116         /* Violently kill off any child processes if Citadel is
117          * the login shell. 
118          */
119         if (getppid() == 1) {
120                 kill(0 - getpgrp(), SIGTERM);
121                 sleep(1);
122                 kill(0 - getpgrp(), SIGKILL);
123         }
124         sttybbs(SB_RESTORE);    /* return the old terminal settings */
125         exit(code);             /* exit with the proper exit code */
126 }
127
128
129
130 /*
131  * We handle "next" and "stop" much differently than in earlier versions.
132  * The signal catching routine simply sets a flag and returns.
133  */
134 void sighandler(int which_sig)
135 {
136         signal(SIGINT, SIG_IGN);
137         signal(SIGQUIT, SIG_IGN);
138         sigcaught = which_sig;
139         return;
140 }
141
142
143 /*
144  * signal catching function for hangups...
145  */
146 void dropcarr(int signum)
147 {
148         logoff(SIGHUP);
149 }
150
151
152
153 /*
154  * catch SIGCONT to reset terminal modes when were are put back into the
155  * foreground.
156  */
157 void catch_sigcont(int signum)
158 {
159         sttybbs(SB_LAST);
160         signal(SIGCONT, catch_sigcont);
161 }
162
163
164
165 /* general purpose routines */
166
167 void formout(char *name)
168 {                               /* display a file */
169         char cmd[256];
170         snprintf(cmd, sizeof cmd, "MESG %s", name);
171         serv_puts(cmd);
172         serv_gets(cmd);
173         if (cmd[0] != '1') {
174                 printf("%s\n", &cmd[4]);
175                 return;
176         }
177         fmout(screenwidth, NULL,
178               ((userflags & US_PAGINATOR) ? 1 : 0),
179               screenheight, 1, 1);
180 }
181
182
183 void userlist(char *patn)
184 {
185         char buf[256];
186         char fl[256];
187         struct tm *tmbuf;
188         time_t lc;
189
190         serv_puts("LIST");
191         serv_gets(buf);
192         if (buf[0] != '1') {
193                 pprintf("%s\n", &buf[4]);
194                 return;
195         }
196         sigcaught = 0;
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) {
202                     extract(fl, buf, 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 ",
210                                (tmbuf->tm_mon + 1),
211                                tmbuf->tm_mday,
212                                (tmbuf->tm_year + 1900));
213                         pprintf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
214                     }
215
216                 }
217         }
218         sttybbs(SB_NO_INTR);
219         pprintf("\n");
220 }
221
222
223 /*
224  * grab assorted info about the user...
225  */
226 void load_user_info(char *params)
227 {
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);
234 }
235
236
237 /*
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.
241  */
242 void remove_march(char *roomname, int floornum)
243 {
244         struct march *mptr, *mptr2;
245
246         if (march == NULL)
247                 return;
248
249         if ((!strcasecmp(march->march_name, roomname))
250             || ((!strcasecmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
251                 mptr = march->next;
252                 free(march);
253                 march = mptr;
254                 return;
255         }
256         mptr2 = march;
257         for (mptr = march; mptr != NULL; mptr = mptr->next) {
258
259                 if ((!strcasecmp(mptr->march_name, roomname))
260                     || ((!strcasecmp(roomname, "_FLOOR_"))
261                         && (mptr->march_floor == floornum))) {
262
263                         mptr2->next = mptr->next;
264                         free(mptr);
265                         mptr = mptr2;
266                 } else {
267                         mptr2 = mptr;
268                 }
269         }
270 }
271
272
273 /*
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.
276  */
277 char *pop_march(int desired_floor)
278 {
279         static char TheRoom[ROOMNAMELEN];
280         int TheFloor = 0;
281         int TheOrder = 32767;
282         int TheWeight = 0;
283         int weight;
284         struct march *mptr = NULL;
285
286         strcpy(TheRoom, "_BASEROOM_");
287         if (march == NULL)
288                 return (TheRoom);
289
290         for (mptr = march; mptr != NULL; mptr = mptr->next) {
291                 weight = 0;
292                 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
293                         weight = weight + 10000;
294                 if (mptr->march_floor == desired_floor)
295                         weight = weight + 5000;
296
297                 weight = weight + ((128 - (mptr->march_floor)) * 128);
298                 weight = weight + (128 - (mptr->march_order));
299
300                 if (weight > TheWeight) {
301                         TheWeight = weight;
302                         strcpy(TheRoom, mptr->march_name);
303                         TheFloor = mptr->march_floor;
304                         TheOrder = mptr->march_order;
305                 }
306         }
307         return (TheRoom);
308 }
309
310
311 /*
312  * jump directly to a room
313  */
314 void dotgoto(char *towhere, int display_name)
315 {
316         char aaa[256], bbb[256], psearch[256];
317         static long ls = 0L;
318         int newmailcount;
319         static int oldmailcount = (-1);
320         int partial_match, best_match;
321         char from_floor;
322
323         /* store ungoto information */
324         strcpy(ugname, room_name);
325         uglsn = ls;
326
327         /* first try an exact match */
328         snprintf(aaa, sizeof aaa, "GOTO %s", towhere);
329         serv_puts(aaa);
330         serv_gets(aaa);
331         if (aaa[3] == '*')
332                 express_msgs = 1;
333         if (!strncmp(aaa, "54", 2)) {
334                 newprompt("Enter room password: ", bbb, 9);
335                 snprintf(aaa, sizeof aaa, "GOTO %s|%s", towhere, bbb);
336                 serv_puts(aaa);
337                 serv_gets(aaa);
338                 if (aaa[3] == '*')
339                         express_msgs = 1;
340         }
341         if (!strncmp(aaa, "54", 2)) {
342                 printf("Wrong password.\n");
343                 return;
344         }
345         /*
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.
350          */
351         if (aaa[0] != '2') {
352                 best_match = 0;
353                 strcpy(bbb, "");
354                 serv_puts("LKRA");
355                 serv_gets(aaa);
356                 if (aaa[0] == '1')
357                         while (serv_gets(aaa), strcmp(aaa, "000")) {
358                                 extract(psearch, aaa, 0);
359                                 partial_match = 0;
360                                 if (pattern(psearch, towhere) >= 0) {
361                                         partial_match = 1;
362                                 }
363                                 if (!strncasecmp(towhere, psearch, strlen(towhere))) {
364                                         partial_match = 2;
365                                 }
366                                 if (partial_match > best_match) {
367                                         strcpy(bbb, psearch);
368                                         best_match = partial_match;
369                                 }
370                         }
371                 if (strlen(bbb) == 0) {
372                         printf("No room '%s'.\n", towhere);
373                         return;
374                 }
375                 snprintf(aaa, sizeof aaa, "GOTO %s", bbb);
376                 serv_puts(aaa);
377                 serv_gets(aaa);
378                 if (aaa[3] == '*')
379                         express_msgs = 1;
380         }
381         if (aaa[0] != '2') {
382                 printf("%s\n", aaa);
383                 return;
384         }
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);
389
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)
395                         load_floorlist();
396                 printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
397         }
398         if (display_name == 1) {
399                 color(BRIGHT_WHITE);
400                 printf("%s ", room_name);
401                 color(DIM_WHITE);
402                 printf("- ");
403         }
404         if (display_name != 2) {
405                 color(BRIGHT_YELLOW);
406                 printf("%d ", extract_int(&aaa[4], 1));
407                 color(DIM_WHITE);
408                 printf("new of ");
409                 color(BRIGHT_YELLOW);
410                 printf("%d ", extract_int(&aaa[4], 2));
411                 color(DIM_WHITE);
412                 printf("messages.\n");
413         }
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);
419
420         /* read info file if necessary */
421         if (extract_int(&aaa[4], 3) > 0)
422                 readinfo();
423
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)) {
428                         color(BRIGHT_RED);
429                         printf("*** You have new mail\n");
430                         color(DIM_WHITE);
431                 }
432                 oldmailcount = newmailcount;
433         }
434 }
435
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.
440  */
441 void gotonext(void)
442 {
443         char buf[256];
444         struct march *mptr, *mptr2;
445         char next_room[ROOMNAMELEN];
446
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.
449          */
450         if (march == NULL) {
451                 serv_puts("LKRN");
452                 serv_gets(buf);
453                 if (buf[0] == '1')
454                         while (serv_gets(buf), strcmp(buf, "000")) {
455                                 mptr = (struct march *) malloc(sizeof(struct march));
456                                 mptr->next = NULL;
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);
460                                 if (march == NULL) {
461                                         march = mptr;
462                                 } else {
463                                         mptr2 = march;
464                                         while (mptr2->next != NULL)
465                                                 mptr2 = mptr2->next;
466                                         mptr2->next = mptr;
467                                 }
468                         }
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
471  */
472                 mptr = (struct march *) malloc(sizeof(struct march));
473                 mptr->next = NULL;
474                 strcpy(mptr->march_name, "_BASEROOM_");
475                 if (march == NULL) {
476                         march = mptr;
477                 } else {
478                         mptr2 = march;
479                         while (mptr2->next != NULL)
480                                 mptr2 = mptr2->next;
481                         mptr2->next = mptr;
482                 }
483 /*
484  * ...and remove the room we're currently in, so a <G>oto doesn't make us
485  * walk around in circles
486  */
487                 remove_march(room_name, 0);
488         }
489         if (march != NULL) {
490                 strcpy(next_room, pop_march(curr_floor));
491         } else {
492                 strcpy(next_room, "_BASEROOM_");
493         }
494         remove_march(next_room, 0);
495         dotgoto(next_room, 1);
496 }
497
498 /*
499  * forget all rooms on a given floor
500  */
501 void forget_all_rooms_on(int ffloor)
502 {
503         char buf[256];
504         struct march *flist, *fptr;
505
506         printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
507         fflush(stdout);
508         snprintf(buf, sizeof buf, "LKRA %d", ffloor);
509         serv_puts(buf);
510         serv_gets(buf);
511         if (buf[0] != '1') {
512                 printf("%-72s\n", &buf[4]);
513                 return;
514         }
515         flist = NULL;
516         while (serv_gets(buf), strcmp(buf, "000")) {
517                 fptr = (struct march *) malloc(sizeof(struct march));
518                 fptr->next = flist;
519                 flist = fptr;
520                 extract(fptr->march_name, buf, 0);
521         }
522         while (flist != NULL) {
523                 snprintf(buf, sizeof buf, "GOTO %s", flist->march_name);
524                 serv_puts(buf);
525                 serv_gets(buf);
526                 if (buf[0] == '2') {
527                         serv_puts("FORG");
528                         serv_gets(buf);
529                 }
530                 fptr = flist;
531                 flist = flist->next;
532                 free(fptr);
533         }
534         printf("%-72s\r", "");
535 }
536
537
538 /*
539  * routine called by gotofloor() to move to a new room on a new floor
540  */
541 void gf_toroom(char *towhere, int mode)
542 {
543         int floor_being_left;
544
545         floor_being_left = curr_floor;
546
547         if (mode == GF_GOTO) {  /* <;G>oto mode */
548                 updatels();
549                 dotgoto(towhere, 1);
550         }
551         if (mode == GF_SKIP) {  /* <;S>kip mode */
552                 dotgoto(towhere, 1);
553                 remove_march("_FLOOR_", floor_being_left);
554         }
555         if (mode == GF_ZAP) {   /* <;Z>ap mode */
556                 dotgoto(towhere, 1);
557                 remove_march("_FLOOR_", floor_being_left);
558                 forget_all_rooms_on(floor_being_left);
559         }
560 }
561
562
563 /*
564  * go to a new floor
565  */
566 void gotofloor(char *towhere, int mode)
567 {
568         int a, tofloor;
569         struct march *mptr;
570         char buf[256], targ[256];
571
572         if (floorlist[0][0] == 0)
573                 load_floorlist();
574         tofloor = (-1);
575         for (a = 0; a < 128; ++a)
576                 if (!strcasecmp(&floorlist[a][0], towhere))
577                         tofloor = a;
578
579         if (tofloor < 0) {
580                 for (a = 0; a < 128; ++a) {
581                         if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
582                                 tofloor = a;
583                         }
584                 }
585         }
586         if (tofloor < 0) {
587                 for (a = 0; a < 128; ++a)
588                         if (pattern(towhere, &floorlist[a][0]) > 0)
589                                 tofloor = a;
590         }
591         if (tofloor < 0) {
592                 printf("No floor '%s'.\n", towhere);
593                 return;
594         }
595         for (mptr = march; mptr != NULL; mptr = mptr->next) {
596                 if ((mptr->march_floor) == tofloor)
597                         gf_toroom(mptr->march_name, mode);
598                 return;
599         }
600
601         strcpy(targ, "");
602         snprintf(buf, sizeof buf, "LKRA %d", tofloor);
603         serv_puts(buf);
604         serv_gets(buf);
605         if (buf[0] == '1')
606                 while (serv_gets(buf), strcmp(buf, "000")) {
607                         if ((extract_int(buf, 2) == tofloor) && (strlen(targ) == 0))
608                                 extract(targ, buf, 0);
609                 }
610         if (strlen(targ) > 0) {
611                 gf_toroom(targ, mode);
612         } else {
613                 printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
614         }
615 }
616
617
618 /*
619  * forget all rooms on current floor
620  */
621 void forget_this_floor(void)
622 {
623
624         if (curr_floor == 0) {
625                 printf("Can't forget this floor.\n");
626                 return;
627         }
628         if (floorlist[0][0] == 0)
629                 load_floorlist();
630         printf("Are you sure you want to forget all rooms on %s? ",
631                &floorlist[(int) curr_floor][0]);
632         if (yesno() == 0)
633                 return;
634
635         gf_toroom("_BASEROOM_", GF_ZAP);
636 }
637
638
639 /* 
640  * Figure out the physical screen dimensions, if we can
641  */
642 void check_screen_dims(void)
643 {
644 #ifdef TIOCGWINSZ
645         struct {
646                 unsigned short height;  /* rows */
647                 unsigned short width;   /* columns */
648                 unsigned short xpixels;
649                 unsigned short ypixels;         /* pixels */
650         } xwinsz;
651
652         if (have_xterm) {       /* dynamically size screen if on an xterm */
653                 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
654                         if (xwinsz.height)
655                                 screenheight = (int) xwinsz.height;
656                         if (xwinsz.width)
657                                 screenwidth = (int) xwinsz.width;
658                 }
659         }
660 #endif
661 }
662
663
664 /*
665  * set floor mode depending on client, server, and user settings
666  */
667 void set_floor_mode(void)
668 {
669         if (serv_info.serv_ok_floors == 0) {
670                 floor_mode = 0; /* Don't use floors if the server */
671         }
672         /* doesn't support them!          */
673         else {
674                 if (rc_floor_mode == RC_NO) {   /* never use floors */
675                         floor_mode = 0;
676                 }
677                 if (rc_floor_mode == RC_YES) {  /* always use floors */
678                         floor_mode = 1;
679                 }
680                 if (rc_floor_mode == RC_DEFAULT) {      /* user choice */
681                         floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
682                 }
683         }
684 }
685
686 /*
687  * Set or change the user's password
688  */
689 int set_password(void)
690 {
691         char pass1[20];
692         char pass2[20];
693         char buf[256];
694
695         if (strlen(rc_password) > 0) {
696                 strcpy(pass1, rc_password);
697                 strcpy(pass2, rc_password);
698         } else {
699                 IFNEXPERT formout("changepw");
700                 newprompt("Enter a new password: ", pass1, -19);
701                 newprompt("Enter it again to confirm: ", pass2, -19);
702         }
703         strproc(pass1);
704         strproc(pass2);
705         if (!strcasecmp(pass1, pass2)) {
706                 snprintf(buf, sizeof buf, "SETP %s", pass1);
707                 serv_puts(buf);
708                 serv_gets(buf);
709                 printf("%s\n", &buf[4]);
710                 offer_to_remember_password(hostbuf, portbuf, fullname, pass1);
711                 return (0);
712         } else {
713                 printf("*** They don't match... try again.\n");
714                 return (1);
715         }
716 }
717
718
719
720 /*
721  * get info about the server we've connected to
722  */
723 void get_serv_info(void)
724 {
725         char buf[512];
726
727         CtdlInternalGetServInfo(&serv_info);
728
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 */
734         serv_puts(buf);
735         serv_gets(buf);         /* we don't care about the result code */
736 }
737
738
739
740
741
742 /*
743  * Display list of users currently logged on to the server
744  */
745 void who_is_online(int longlist)
746 {
747         char buf[128], username[128], roomname[128], fromhost[128],
748          flags[128];
749         char tbuf[128], clientsoft[128];
750         time_t timenow = 0;
751         time_t idletime, idlehours, idlemins, idlesecs;
752         int last_session = (-1);
753
754         if (longlist) {
755                 serv_puts("TIME");
756                 serv_gets(tbuf);
757                 if (tbuf[0] == '2') {
758                         timenow = extract_long(&tbuf[4], 0);
759                 } else {
760                         time(&timenow);
761                 }
762         } else {
763                 color(BRIGHT_WHITE);
764                 pprintf("FLG ###        User Name                 Room                 From host\n");
765                 color(DIM_WHITE);
766                 pprintf("--- --- ------------------------- -------------------- ------------------------\n");
767         }
768         serv_puts("RWHO");
769         serv_gets(buf);
770         if (buf[0] == '1') {
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);
777
778                         if (longlist) {
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);
788
789                         } else {
790                                 if (extract_int(buf, 0) == last_session) {
791                                         pprintf("        ");
792                                 } else {
793                                         color(BRIGHT_MAGENTA);
794                                         pprintf("%-3s ", flags);
795                                         color(DIM_WHITE);
796                                         pprintf("%-3d ", extract_int(buf, 0));
797                                 }
798                                 last_session = extract_int(buf, 0);
799                                 color(BRIGHT_CYAN);
800                                 pprintf("%-25s ", username);
801                                 color(BRIGHT_MAGENTA);
802                                 roomname[20] = 0;
803                                 pprintf("%-20s ", roomname);
804                                 color(BRIGHT_CYAN);
805                                 pprintf("%-24s\n", fromhost);
806                                 color(DIM_WHITE);
807                         }
808                 }
809         }
810 }
811
812 void enternew(char *desc, char *buf, int maxlen)
813 {
814         char bbb[128];
815         snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
816         newprompt(bbb, buf, maxlen);
817 }
818
819 /*
820  * main
821  */
822 int main(int argc, char **argv)
823 {
824         int a, b, mcmd;
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;
829         char password[256];
830
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 */
839
840         printf("Attaching to server... \r");
841         fflush(stdout);
842         attach_to_server(argc, argv, hostbuf, portbuf);
843
844         send_ansi_detect();
845
846         serv_gets(aaa);
847         if (aaa[0] != '2') {
848                 printf("%s\n", &aaa[4]);
849                 logoff(atoi(aaa));
850         }
851         get_serv_info();
852
853         look_for_ansi();
854         cls(0);
855         color(7);
856
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 */
860         screenheight = 24;
861
862         printf(" pause    next    stop\n");
863         printf(" ctrl-s  ctrl-o  ctrl-c\n\n");
864         formout("hello");       /* print the opening greeting */
865         printf("\n");
866
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);
872                         serv_puts(aaa);
873                         serv_gets(aaa);
874                         sprintf(aaa, "PASS %s", password);
875                         serv_puts(aaa);
876                         serv_gets(aaa);
877                         if (aaa[0] == '2') {
878                                 load_user_info(&aaa[4]);
879                                 stored_password = 1;
880                                 goto PWOK;
881                         }
882                         else {
883                                 set_stored_password(hostbuf, portbuf, "", "");
884                         }
885                 }
886         }
887
888         termn8 = 0;
889         newnow = 0;
890         do {
891                 if (strlen(rc_username) > 0) {
892                         strcpy(fullname, rc_username);
893                 } else {
894                         newprompt("Enter your name: ", fullname, 29);
895                 }
896                 strproc(fullname);
897                 if (!strcasecmp(fullname, "new")) {     /* just in case */
898                         printf("Please enter the name you wish to log in with.\n");
899                 }
900         } while (
901                         (!strcasecmp(fullname, "bbs"))
902                         || (!strcasecmp(fullname, "new"))
903                         || (strlen(fullname) == 0));
904
905         if (!strcasecmp(fullname, "off")) {
906                 mcmd = 29;
907                 goto TERMN8;
908         }
909         /* sign on to the server */
910         snprintf(aaa, sizeof aaa, "USER %s", fullname);
911         serv_puts(aaa);
912         serv_gets(aaa);
913         if (aaa[0] != '3')
914                 goto NEWUSR;
915
916         /* password authentication */
917         if (strlen(rc_password) > 0) {
918                 strcpy(password, rc_password);
919         } else {
920                 newprompt("\rPlease enter your password: ", password, -19);
921         }
922         strproc(password);
923         snprintf(aaa, sizeof aaa, "PASS %s", password);
924         serv_puts(aaa);
925         serv_gets(aaa);
926         if (aaa[0] == '2') {
927                 load_user_info(&aaa[4]);
928                 offer_to_remember_password(hostbuf, portbuf,
929                                         fullname, password);
930                 goto PWOK;
931         }
932         printf("<< wrong password >>\n");
933         if (strlen(rc_password) > 0)
934                 logoff(0);
935         goto GSTA;
936
937 NEWUSR: if (strlen(rc_password) == 0) {
938                 printf("No record. Enter as new user? ");
939                 if (yesno() == 0)
940                         goto GSTA;
941         }
942         snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
943         serv_puts(aaa);
944         serv_gets(aaa);
945         if (aaa[0] != '2') {
946                 printf("%s\n", aaa);
947                 goto GSTA;
948         }
949         load_user_info(&aaa[4]);
950
951         while (set_password() != 0);;
952         newnow = 1;
953
954         enter_config(1);
955
956
957 PWOK:   printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
958                fullname, axlevel, axdefs[(int) axlevel],
959                usernum, timescalled);
960
961         serv_puts("CHEK");
962         serv_gets(aaa);
963         if (aaa[0] == '2') {
964                 b = extract_int(&aaa[4], 0);
965                 if (b > 0) {
966                         color(BRIGHT_RED);
967                         if (b == 1)
968                                 printf("*** You have a new private message in Mail>\n");
969                         if (b > 1)
970                                 printf("*** You have %d new private messages in Mail>\n", b);
971                         color(DIM_WHITE);
972                 }
973                 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
974                         printf("*** Users need validation\n");
975                 }
976                 if (extract_int(&aaa[4], 1) > 0) {
977                         printf("*** Please register.\n");
978                         formout("register");
979                         entregis();
980                 }
981         }
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));
989
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.
995          */
996         screenwidth = 80;
997         screenheight = 24;
998         serv_puts("GETU");
999         serv_gets(aaa);
1000         if (aaa[0] == '2') {
1001                 screenwidth = extract_int(&aaa[4], 0);
1002                 screenheight = extract_int(&aaa[4], 1);
1003         }
1004         if (getenv("TERM") != NULL)
1005                 if (!strcmp(getenv("TERM"), "xterm")) {
1006                         have_xterm = 1;
1007                 }
1008 #ifdef TIOCGWINSZ
1009         check_screen_dims();
1010 #endif
1011
1012         set_floor_mode();
1013
1014
1015         /* Enter the lobby */
1016         dotgoto("_BASEROOM_", 1);
1017
1018 /* Main loop for the system... user is logged in. */
1019         strcpy(ugname, "");
1020         uglsn = 0L;
1021
1022         if (newnow == 1)
1023                 readmsgs(3, 1, 5);
1024         else
1025                 readmsgs(1, 1, 0);
1026
1027         do {                    /* MAIN LOOP OF PROGRAM */
1028
1029                 signal(SIGINT, SIG_IGN);
1030                 signal(SIGQUIT, SIG_IGN);
1031                 mcmd = getcmd(argbuf);
1032
1033 #ifdef TIOCGWINSZ
1034                 check_screen_dims();
1035 #endif
1036
1037                 if (termn8 == 0)
1038                         switch (mcmd) {
1039                         case 1:
1040                                 formout("help");
1041                                 break;
1042                         case 4:
1043                                 entmsg(0, 0);
1044                                 break;
1045                         case 36:
1046                                 entmsg(0, 1);
1047                                 break;
1048                         case 46:
1049                                 entmsg(0, 2);
1050                                 break;
1051                         case 78:
1052                                 newprompt("What do you want your username to be? ", aaa, 32);
1053                                 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1054                                 serv_puts(bbb);
1055                                 serv_gets(aaa);
1056                                 if (strncmp("200", aaa, 3))
1057                                         printf("\n%s\n", aaa);
1058                                 else
1059                                         entmsg(0, 0);
1060                                 break;
1061                         case 5:
1062                                 updatels();
1063                                 gotonext();
1064                                 break;
1065                         case 47:
1066                                 updatelsa();
1067                                 gotonext();
1068                                 break;
1069                         case 58:
1070                                 updatelsa();
1071                                 dotgoto("_MAIL_", 1);
1072                                 break;
1073                         case 20:
1074                                 updatels();
1075                         case 52:
1076                                 dotgoto(argbuf, 0);
1077                                 break;
1078                         case 10:
1079                                 readmsgs(0, 1, 0);
1080                                 break;
1081                         case 9:
1082                                 readmsgs(3, 1, 5);
1083                                 break;
1084                         case 13:
1085                                 readmsgs(1, 1, 0);
1086                                 break;
1087                         case 11:
1088                                 readmsgs(0, (-1), 0);
1089                                 break;
1090                         case 12:
1091                                 readmsgs(2, (-1), 0);
1092                                 break;
1093                         case 71:
1094                                 readmsgs(3, 1, atoi(argbuf));
1095                                 break;
1096                         case 7:
1097                                 forget();
1098                                 break;
1099                         case 18:
1100                                 subshell();
1101                                 break;
1102                         case 38:
1103                                 updatels();
1104                                 entroom();
1105                                 break;
1106                         case 22:
1107                                 killroom();
1108                                 break;
1109                         case 32:
1110                                 userlist(argbuf);
1111                                 break;
1112                         case 27:
1113                                 invite();
1114                                 break;
1115                         case 28:
1116                                 kickout();
1117                                 break;
1118                         case 23:
1119                                 editthisroom();
1120                                 break;
1121                         case 14:
1122                                 roomdir();
1123                                 break;
1124                         case 33:
1125                                 download(0);
1126                                 break;
1127                         case 34:
1128                                 download(1);
1129                                 break;
1130                         case 31:
1131                                 download(2);
1132                                 break;
1133                         case 43:
1134                                 download(3);
1135                                 break;
1136                         case 45:
1137                                 download(4);
1138                                 break;
1139                         case 55:
1140                                 download(5);
1141                                 break;
1142                         case 39:
1143                                 upload(0);
1144                                 break;
1145                         case 40:
1146                                 upload(1);
1147                                 break;
1148                         case 42:
1149                                 upload(2);
1150                                 break;
1151                         case 44:
1152                                 upload(3);
1153                                 break;
1154                         case 57:
1155                                 cli_upload();
1156                                 break;
1157                         case 16:
1158                                 ungoto();
1159                                 break;
1160                         case 24:
1161                                 whoknows();
1162                                 break;
1163                         case 26:
1164                                 validate();
1165                                 break;
1166                         case 29:
1167                                 updatels();
1168                                 termn8 = 1;
1169                                 break;
1170                         case 30:
1171                                 updatels();
1172                                 termn8 = 1;
1173                                 break;
1174                         case 48:
1175                                 enterinfo();
1176                                 break;
1177                         case 49:
1178                                 readinfo();
1179                                 break;
1180                         case 72:
1181                                 cli_image_upload("_userpic_");
1182                                 break;
1183                         case 73:
1184                                 cli_image_upload("_roompic_");
1185                                 break;
1186
1187                         case 74:
1188                                 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1189                                 cli_image_upload(aaa);
1190                                 break;
1191
1192                         case 75:
1193                                 enternew("roomname", aaa, 20);
1194                                 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1195                                 serv_puts(bbb);
1196                                 serv_gets(aaa);
1197                                 if (strncmp("200", aaa, 3))
1198                                         printf("\n%s\n", aaa);
1199                                 break;
1200                         case 76:
1201                                 enternew("hostname", aaa, 25);
1202                                 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1203                                 serv_puts(bbb);
1204                                 serv_gets(aaa);
1205                                 if (strncmp("200", aaa, 3))
1206                                         printf("\n%s\n", aaa);
1207                                 break;
1208                         case 77:
1209                                 enternew("username", aaa, 32);
1210                                 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1211                                 serv_puts(bbb);
1212                                 serv_gets(aaa);
1213                                 if (strncmp("200", aaa, 3))
1214                                         printf("\n%s\n", aaa);
1215                                 break;
1216
1217                         case 35:
1218                                 set_password();
1219                                 break;
1220
1221                         case 21:
1222                                 if (argbuf[0] == 0)
1223                                         strcpy(aaa, "?");
1224                                 display_help(argbuf);
1225                                 break;
1226
1227                         case 41:
1228                                 formout("register");
1229                                 entregis();
1230                                 break;
1231
1232                         case 15:
1233                                 printf("Are you sure (y/n)? ");
1234                                 if (yesno() == 1) {
1235                                         updatels();
1236                                         a = 0;
1237                                         termn8 = 1;
1238                                 }
1239                                 break;
1240
1241                         case 6:
1242                                 gotonext();
1243                                 break;
1244
1245                         case 3:
1246                                 chatmode();
1247                                 break;
1248
1249                         case 2:
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);
1255                                         ka_system(aaa);
1256                                         sttybbs(SB_NO_INTR);
1257                                 } else {
1258                                         printf("*** Can't run doors when server is not local.\n");
1259                                 }
1260                                 break;
1261
1262                         case 17:
1263                                 who_is_online(0);
1264                                 break;
1265
1266                         case 79:
1267                                 who_is_online(1);
1268                                 break;
1269
1270                         case 80:
1271                                 do_system_configuration();
1272                                 break;
1273
1274                         case 82:
1275                                 do_internet_configuration();
1276                                 break;
1277
1278                         case 83:
1279                                 check_message_base();
1280                                 break;
1281
1282                         case 84:
1283                                 quiet_mode();
1284                                 break;
1285
1286                         case 50:
1287                                 enter_config(2);
1288                                 break;
1289
1290                         case 37:
1291                                 enter_config(0);
1292                                 set_floor_mode();
1293                                 break;
1294
1295                         case 59:
1296                                 enter_config(3);
1297                                 set_floor_mode();
1298                                 break;
1299
1300                         case 60:
1301                                 gotofloor(argbuf, GF_GOTO);
1302                                 break;
1303
1304                         case 61:
1305                                 gotofloor(argbuf, GF_SKIP);
1306                                 break;
1307
1308                         case 62:
1309                                 forget_this_floor();
1310                                 break;
1311
1312                         case 63:
1313                                 create_floor();
1314                                 break;
1315
1316                         case 64:
1317                                 edit_floor();
1318                                 break;
1319
1320                         case 65:
1321                                 kill_floor();
1322                                 break;
1323
1324                         case 66:
1325                                 enter_bio();
1326                                 break;
1327
1328                         case 67:
1329                                 read_bio();
1330                                 break;
1331
1332                         case 25:
1333                                 edituser();
1334                                 break;
1335
1336                         case 8:
1337                                 knrooms(floor_mode);
1338                                 printf("\n");
1339                                 break;
1340
1341                         case 68:
1342                                 knrooms(2);
1343                                 printf("\n");
1344                                 break;
1345
1346                         case 69:
1347                                 misc_server_cmd(argbuf);
1348                                 break;
1349
1350                         case 70:
1351                                 edit_system_message(argbuf);
1352                                 break;
1353
1354                         case 19:
1355                                 listzrooms();
1356                                 printf("\n");
1357                                 break;
1358
1359                         case 51:
1360                                 deletefile();
1361                                 break;
1362
1363                         case 53:
1364                                 netsendfile();
1365                                 break;
1366
1367                         case 54:
1368                                 movefile();
1369                                 break;
1370
1371                         case 56:
1372                                 page_user();
1373                                 break;
1374
1375                         }       /* end switch */
1376         } while (termn8 == 0);
1377
1378       TERMN8:printf("%s logged out.\n", fullname);
1379         while (march != NULL)
1380                 remove_march(march->march_name, 0);
1381         if (mcmd == 30)
1382                 printf("\n\nType 'off' to hang up, or next user...\n");
1383         snprintf(aaa, sizeof aaa, "LOUT");
1384         serv_puts(aaa);
1385         serv_gets(aaa);
1386         if ((mcmd == 29) || (mcmd == 15)) {
1387                 formout("goodbye");
1388                 logoff(0);
1389         }
1390         goto GSTA;
1391
1392 }                               /* end main() */