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