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