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