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