]> code.citadel.org Git - citadel.git/blob - citadel/citadel.c
* Minor changes to avoid host lookup for local clients
[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         printf("Establishing session...\r");
841         fflush(stdout);
842
843         send_ansi_detect();
844
845         serv_gets(aaa);
846         if (aaa[0] != '2') {
847                 printf("%s\n", &aaa[4]);
848                 logoff(atoi(aaa));
849         }
850         get_serv_info();
851
852         look_for_ansi();
853         cls(0);
854         color(7);
855
856         printf("%-23s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
857                serv_info.serv_bbs_city);
858         screenwidth = 80;       /* default screen dimensions */
859         screenheight = 24;
860
861         printf(" pause    next    stop\n");
862         printf(" ctrl-s  ctrl-o  ctrl-c\n\n");
863         formout("hello");       /* print the opening greeting */
864         printf("\n");
865
866 GSTA:   termn8 = 0;
867         newnow = 0;
868         do {
869                 if (strlen(rc_username) > 0) {
870                         strcpy(fullname, rc_username);
871                 } else {
872                         newprompt("Enter your name: ", fullname, 29);
873                 }
874                 strproc(fullname);
875                 if (!strucmp(fullname, "new")) {        /* just in case */
876                         printf("Please enter the name you wish to log in with.\n");
877                 }
878         } while (
879                         (!strucmp(fullname, "bbs"))
880                         || (!strucmp(fullname, "new"))
881                         || (strlen(fullname) == 0));
882
883         if (!strucmp(fullname, "off")) {
884                 mcmd = 29;
885                 goto TERMN8;
886         }
887         /* sign on to the server */
888         snprintf(aaa, sizeof aaa, "USER %s", fullname);
889         serv_puts(aaa);
890         serv_gets(aaa);
891         if (aaa[0] != '3')
892                 goto NEWUSR;
893
894         /* password authentication */
895         if (strlen(rc_password) > 0) {
896                 strcpy(eee, rc_password);
897         } else {
898                 newprompt("\rPlease enter your password: ", eee, -19);
899         }
900         strproc(eee);
901         snprintf(aaa, sizeof aaa, "PASS %s", eee);
902         serv_puts(aaa);
903         serv_gets(aaa);
904         if (aaa[0] == '2') {
905                 load_user_info(&aaa[4]);
906                 goto PWOK;
907         }
908         printf("<< wrong password >>\n");
909         if (strlen(rc_password) > 0)
910                 logoff(0);
911         goto GSTA;
912
913       NEWUSR:if (strlen(rc_password) == 0) {
914                 printf("No record. Enter as new user? ");
915                 if (yesno() == 0)
916                         goto GSTA;
917         }
918         snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
919         serv_puts(aaa);
920         serv_gets(aaa);
921         if (aaa[0] != '2') {
922                 printf("%s\n", aaa);
923                 goto GSTA;
924         }
925         load_user_info(&aaa[4]);
926
927         while (set_password() != 0);;
928         newnow = 1;
929
930         enter_config(1);
931
932
933       PWOK:printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
934                fullname, axlevel, axdefs[(int) axlevel],
935                usernum, timescalled);
936
937         serv_puts("CHEK");
938         serv_gets(aaa);
939         if (aaa[0] == '2') {
940                 b = extract_int(&aaa[4], 0);
941                 if (b > 0) {
942                         color(BRIGHT_RED);
943                         if (b == 1)
944                                 printf("*** You have a new private message in Mail>\n");
945                         if (b > 1)
946                                 printf("*** You have %d new private messages in Mail>\n", b);
947                         color(DIM_WHITE);
948                 }
949                 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
950                         printf("*** Users need validation\n");
951                 }
952                 if (extract_int(&aaa[4], 1) > 0) {
953                         printf("*** Please register.\n");
954                         formout("register");
955                         entregis();
956                 }
957         }
958         /* Make up some temporary filenames for use in various parts of the
959          * program.  Don't mess with these once they've been set, because we
960          * will be unlinking them later on in the program and we don't
961          * want to delete something that we didn't create. */
962         snprintf(temp, sizeof temp, tmpnam(NULL));
963         snprintf(temp2, sizeof temp2, tmpnam(NULL));
964         snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
965
966         /* Get screen dimensions.  First we go to a default of 80x24.  Then
967          * we try to get the user's actual screen dimensions off the server.
968          * However, if we're running on an xterm, all this stuff is
969          * irrelevant because we're going to dynamically size the screen
970          * during the session.
971          */
972         screenwidth = 80;
973         screenheight = 24;
974         serv_puts("GETU");
975         serv_gets(aaa);
976         if (aaa[0] == '2') {
977                 screenwidth = extract_int(&aaa[4], 0);
978                 screenheight = extract_int(&aaa[4], 1);
979         }
980         if (getenv("TERM") != NULL)
981                 if (!strcmp(getenv("TERM"), "xterm")) {
982                         have_xterm = 1;
983                 }
984 #ifdef TIOCGWINSZ
985         check_screen_dims();
986 #endif
987
988         set_floor_mode();
989
990
991         /* Enter the lobby */
992         dotgoto("_BASEROOM_", 1);
993
994 /* Main loop for the system... user is logged in. */
995         strcpy(ugname, "");
996         uglsn = 0L;
997
998         if (newnow == 1)
999                 readmsgs(3, 1, 5);
1000         else
1001                 readmsgs(1, 1, 0);
1002
1003         do {                    /* MAIN LOOP OF PROGRAM */
1004
1005                 signal(SIGINT, SIG_IGN);
1006                 signal(SIGQUIT, SIG_IGN);
1007                 mcmd = getcmd(argbuf);
1008
1009 #ifdef TIOCGWINSZ
1010                 check_screen_dims();
1011 #endif
1012
1013                 if (termn8 == 0)
1014                         switch (mcmd) {
1015                         case 1:
1016                                 formout("help");
1017                                 break;
1018                         case 4:
1019                                 entmsg(0, 0);
1020                                 break;
1021                         case 36:
1022                                 entmsg(0, 1);
1023                                 break;
1024                         case 46:
1025                                 entmsg(0, 2);
1026                                 break;
1027                         case 78:
1028                                 newprompt("What do you want your username to be? ", aaa, 32);
1029                                 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1030                                 serv_puts(bbb);
1031                                 serv_gets(aaa);
1032                                 if (strncmp("200", aaa, 3))
1033                                         printf("\n%s\n", aaa);
1034                                 else
1035                                         entmsg(0, 0);
1036                                 break;
1037                         case 5:
1038                                 updatels();
1039                                 gotonext();
1040                                 break;
1041                         case 47:
1042                                 updatelsa();
1043                                 gotonext();
1044                                 break;
1045                         case 58:
1046                                 updatelsa();
1047                                 dotgoto("_MAIL_", 1);
1048                                 break;
1049                         case 20:
1050                                 updatels();
1051                         case 52:
1052                                 dotgoto(argbuf, 0);
1053                                 break;
1054                         case 10:
1055                                 readmsgs(0, 1, 0);
1056                                 break;
1057                         case 9:
1058                                 readmsgs(3, 1, 5);
1059                                 break;
1060                         case 13:
1061                                 readmsgs(1, 1, 0);
1062                                 break;
1063                         case 11:
1064                                 readmsgs(0, (-1), 0);
1065                                 break;
1066                         case 12:
1067                                 readmsgs(2, (-1), 0);
1068                                 break;
1069                         case 71:
1070                                 readmsgs(3, 1, atoi(argbuf));
1071                                 break;
1072                         case 7:
1073                                 forget();
1074                                 break;
1075                         case 18:
1076                                 subshell();
1077                                 break;
1078                         case 38:
1079                                 updatels();
1080                                 entroom();
1081                                 break;
1082                         case 22:
1083                                 killroom();
1084                                 break;
1085                         case 32:
1086                                 userlist(argbuf);
1087                                 break;
1088                         case 27:
1089                                 invite();
1090                                 break;
1091                         case 28:
1092                                 kickout();
1093                                 break;
1094                         case 23:
1095                                 editthisroom();
1096                                 break;
1097                         case 14:
1098                                 roomdir();
1099                                 break;
1100                         case 33:
1101                                 download(0);
1102                                 break;
1103                         case 34:
1104                                 download(1);
1105                                 break;
1106                         case 31:
1107                                 download(2);
1108                                 break;
1109                         case 43:
1110                                 download(3);
1111                                 break;
1112                         case 45:
1113                                 download(4);
1114                                 break;
1115                         case 55:
1116                                 download(5);
1117                                 break;
1118                         case 39:
1119                                 upload(0);
1120                                 break;
1121                         case 40:
1122                                 upload(1);
1123                                 break;
1124                         case 42:
1125                                 upload(2);
1126                                 break;
1127                         case 44:
1128                                 upload(3);
1129                                 break;
1130                         case 57:
1131                                 cli_upload();
1132                                 break;
1133                         case 16:
1134                                 ungoto();
1135                                 break;
1136                         case 24:
1137                                 whoknows();
1138                                 break;
1139                         case 26:
1140                                 validate();
1141                                 break;
1142                         case 29:
1143                                 updatels();
1144                                 termn8 = 1;
1145                                 break;
1146                         case 30:
1147                                 updatels();
1148                                 termn8 = 1;
1149                                 break;
1150                         case 48:
1151                                 enterinfo();
1152                                 break;
1153                         case 49:
1154                                 readinfo();
1155                                 break;
1156                         case 72:
1157                                 cli_image_upload("_userpic_");
1158                                 break;
1159                         case 73:
1160                                 cli_image_upload("_roompic_");
1161                                 break;
1162
1163                         case 74:
1164                                 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1165                                 cli_image_upload(aaa);
1166                                 break;
1167
1168                         case 75:
1169                                 enternew("roomname", aaa, 20);
1170                                 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1171                                 serv_puts(bbb);
1172                                 serv_gets(aaa);
1173                                 if (strncmp("200", aaa, 3))
1174                                         printf("\n%s\n", aaa);
1175                                 break;
1176                         case 76:
1177                                 enternew("hostname", aaa, 25);
1178                                 snprintf(bbb, sizeof bbb, "HCHG %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 77:
1185                                 enternew("username", aaa, 32);
1186                                 snprintf(bbb, sizeof bbb, "UCHG %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
1193                         case 35:
1194                                 set_password();
1195                                 break;
1196
1197                         case 21:
1198                                 if (argbuf[0] == 0)
1199                                         strcpy(aaa, "?");
1200                                 display_help(argbuf);
1201                                 break;
1202
1203                         case 41:
1204                                 formout("register");
1205                                 entregis();
1206                                 break;
1207
1208                         case 15:
1209                                 printf("Are you sure (y/n)? ");
1210                                 if (yesno() == 1) {
1211                                         updatels();
1212                                         a = 0;
1213                                         termn8 = 1;
1214                                 }
1215                                 break;
1216
1217                         case 6:
1218                                 gotonext();
1219                                 break;
1220
1221                         case 3:
1222                                 chatmode();
1223                                 break;
1224
1225                         case 2:
1226                                 if (server_is_local) {
1227                                         sttybbs(SB_RESTORE);
1228                                         snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1229                                                  "exec ./subsystem %ld %d %d", fullname,
1230                                           usernum, screenwidth, axlevel);
1231                                         ka_system(aaa);
1232                                         sttybbs(SB_NO_INTR);
1233                                 } else {
1234                                         printf("*** Can't run doors when server is not local.\n");
1235                                 }
1236                                 break;
1237
1238                         case 17:
1239                                 who_is_online(0);
1240                                 break;
1241
1242                         case 79:
1243                                 who_is_online(1);
1244                                 break;
1245
1246                         case 80:
1247                                 do_system_configuration();
1248                                 break;
1249
1250                         case 82:
1251                                 do_internet_configuration();
1252                                 break;
1253
1254                         case 83:
1255                                 check_message_base();
1256                                 break;
1257
1258                         case 84:
1259                                 quiet_mode();
1260                                 break;
1261
1262                         case 50:
1263                                 enter_config(2);
1264                                 break;
1265
1266                         case 37:
1267                                 enter_config(0);
1268                                 set_floor_mode();
1269                                 break;
1270
1271                         case 59:
1272                                 enter_config(3);
1273                                 set_floor_mode();
1274                                 break;
1275
1276                         case 60:
1277                                 gotofloor(argbuf, GF_GOTO);
1278                                 break;
1279
1280                         case 61:
1281                                 gotofloor(argbuf, GF_SKIP);
1282                                 break;
1283
1284                         case 62:
1285                                 forget_this_floor();
1286                                 break;
1287
1288                         case 63:
1289                                 create_floor();
1290                                 break;
1291
1292                         case 64:
1293                                 edit_floor();
1294                                 break;
1295
1296                         case 65:
1297                                 kill_floor();
1298                                 break;
1299
1300                         case 66:
1301                                 enter_bio();
1302                                 break;
1303
1304                         case 67:
1305                                 read_bio();
1306                                 break;
1307
1308                         case 25:
1309                                 edituser();
1310                                 break;
1311
1312                         case 8:
1313                                 knrooms(floor_mode);
1314                                 printf("\n");
1315                                 break;
1316
1317                         case 68:
1318                                 knrooms(2);
1319                                 printf("\n");
1320                                 break;
1321
1322                         case 69:
1323                                 misc_server_cmd(argbuf);
1324                                 break;
1325
1326                         case 70:
1327                                 edit_system_message(argbuf);
1328                                 break;
1329
1330                         case 19:
1331                                 listzrooms();
1332                                 printf("\n");
1333                                 break;
1334
1335                         case 51:
1336                                 deletefile();
1337                                 break;
1338
1339                         case 53:
1340                                 netsendfile();
1341                                 break;
1342
1343                         case 54:
1344                                 movefile();
1345                                 break;
1346
1347                         case 56:
1348                                 page_user();
1349                                 break;
1350
1351                         case 81:
1352                                 setup_icq();
1353                                 break;
1354
1355                         }       /* end switch */
1356         } while (termn8 == 0);
1357
1358       TERMN8:printf("%s logged out.\n", fullname);
1359         while (march != NULL)
1360                 remove_march(march->march_name, 0);
1361         if (mcmd == 30)
1362                 printf("\n\nType 'off' to hang up, or next user...\n");
1363         snprintf(aaa, sizeof aaa, "LOUT");
1364         serv_puts(aaa);
1365         serv_gets(aaa);
1366         if ((mcmd == 29) || (mcmd == 15)) {
1367                 formout("goodbye");
1368                 logoff(0);
1369         }
1370         goto GSTA;
1371
1372 }                               /* end main() */