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