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