]> code.citadel.org Git - citadel.git/blob - citadel/citadel.c
* citadel.c: when ansi_color=user, enable color at login, so Lobby> posts
[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
97 extern int rc_ansi_color;       /* ansi color value from citadel.rc */
98
99 /*
100  * here is our 'clean up gracefully and exit' routine
101  */
102 void logoff(int code)
103 {
104         if (editor_pid > 0) {   /* kill the editor if it's running */
105                 kill(editor_pid, SIGHUP);
106         }
107 /* shut down the server... but not if the logoff code is 3, because
108  * that means we're exiting because we already lost the server
109  */
110         if (code != 3)
111                 serv_puts("QUIT");
112
113 /*
114  * now clean up various things
115  */
116
117         unlink(temp);
118         unlink(temp2);
119         nukedir(tempdir);
120
121         /* Violently kill off any child processes if Citadel is
122          * the login shell. 
123          */
124         if (getppid() == 1) {
125                 kill(0 - getpgrp(), SIGTERM);
126                 sleep(1);
127                 kill(0 - getpgrp(), SIGKILL);
128         }
129         sttybbs(SB_RESTORE);    /* return the old terminal settings */
130         exit(code);             /* exit with the proper exit code */
131 }
132
133
134
135 /*
136  * signal catching function for hangups...
137  */
138 void dropcarr(int signum)
139 {
140         logoff(SIGHUP);
141 }
142
143
144
145 /*
146  * catch SIGCONT to reset terminal modes when were are put back into the
147  * foreground.
148  */
149 void catch_sigcont(int signum)
150 {
151         sttybbs(SB_LAST);
152         signal(SIGCONT, catch_sigcont);
153 }
154
155
156
157 /* general purpose routines */
158
159 void formout(char *name)
160 {                               /* display a file */
161         char cmd[256];
162         snprintf(cmd, sizeof cmd, "MESG %s", name);
163         serv_puts(cmd);
164         serv_gets(cmd);
165         if (cmd[0] != '1') {
166                 printf("%s\n", &cmd[4]);
167                 return;
168         }
169         fmout(screenwidth, NULL,
170               ((userflags & US_PAGINATOR) ? 1 : 0),
171               screenheight, 1, 1);
172 }
173
174
175 void userlist(char *patn)
176 {
177         char buf[256];
178         char fl[256];
179         struct tm *tmbuf;
180         time_t lc;
181
182         serv_puts("LIST");
183         serv_gets(buf);
184         if (buf[0] != '1') {
185                 pprintf("%s\n", &buf[4]);
186                 return;
187         }
188         pprintf("       User Name           Num  L  LastCall  Calls Posts\n");
189         pprintf("------------------------- ----- - ---------- ----- -----\n");
190         while (serv_gets(buf), strcmp(buf, "000")) {
191                 if (sigcaught == 0) {
192                     extract(fl, buf, 0);
193                     if (pattern(fl, patn) >= 0) {
194                         pprintf("%-25s ", fl);
195                         pprintf("%5ld %d ", extract_long(buf, 2),
196                                extract_int(buf, 1));
197                         lc = extract_long(buf, 3);
198                         tmbuf = (struct tm *) localtime(&lc);
199                         pprintf("%02d/%02d/%04d ",
200                                (tmbuf->tm_mon + 1),
201                                tmbuf->tm_mday,
202                                (tmbuf->tm_year + 1900));
203                         pprintf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
204                     }
205
206                 }
207         }
208         pprintf("\n");
209 }
210
211
212 /*
213  * grab assorted info about the user...
214  */
215 void load_user_info(char *params)
216 {
217         extract(fullname, params, 0);
218         axlevel = extract_int(params, 1);
219         timescalled = extract_int(params, 2);
220         posted = extract_int(params, 3);
221         userflags = extract_int(params, 4);
222         usernum = extract_long(params, 5);
223 }
224
225
226 /*
227  * Remove a room from the march list.  'floornum' is ignored unless
228  * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
229  * floor will be removed from the march list.
230  */
231 void remove_march(char *roomname, int floornum)
232 {
233         struct march *mptr, *mptr2;
234
235         if (march == NULL)
236                 return;
237
238         if ((!strcasecmp(march->march_name, roomname))
239             || ((!strcasecmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
240                 mptr = march->next;
241                 free(march);
242                 march = mptr;
243                 return;
244         }
245         mptr2 = march;
246         for (mptr = march; mptr != NULL; mptr = mptr->next) {
247
248                 if ((!strcasecmp(mptr->march_name, roomname))
249                     || ((!strcasecmp(roomname, "_FLOOR_"))
250                         && (mptr->march_floor == floornum))) {
251
252                         mptr2->next = mptr->next;
253                         free(mptr);
254                         mptr = mptr2;
255                 } else {
256                         mptr2 = mptr;
257                 }
258         }
259 }
260
261
262 /*
263  * Locate the room on the march list which we most want to go to.  Each room
264  * is measured given a "weight" of preference based on various factors.
265  */
266 char *pop_march(int desired_floor)
267 {
268         static char TheRoom[ROOMNAMELEN];
269         int TheFloor = 0;
270         int TheOrder = 32767;
271         int TheWeight = 0;
272         int weight;
273         struct march *mptr = NULL;
274
275         strcpy(TheRoom, "_BASEROOM_");
276         if (march == NULL)
277                 return (TheRoom);
278
279         for (mptr = march; mptr != NULL; mptr = mptr->next) {
280                 weight = 0;
281                 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
282                         weight = weight + 10000;
283                 if (mptr->march_floor == desired_floor)
284                         weight = weight + 5000;
285
286                 weight = weight + ((128 - (mptr->march_floor)) * 128);
287                 weight = weight + (128 - (mptr->march_order));
288
289                 if (weight > TheWeight) {
290                         TheWeight = weight;
291                         strcpy(TheRoom, mptr->march_name);
292                         TheFloor = mptr->march_floor;
293                         TheOrder = mptr->march_order;
294                 }
295         }
296         return (TheRoom);
297 }
298
299
300 /*
301  * jump directly to a room
302  */
303 void dotgoto(char *towhere, int display_name)
304 {
305         char aaa[256], bbb[256], psearch[256];
306         static long ls = 0L;
307         int newmailcount;
308         static int oldmailcount = (-1);
309         int partial_match, best_match;
310         char from_floor;
311
312         /* store ungoto information */
313         strcpy(ugname, room_name);
314         uglsn = ls;
315
316         /* first try an exact match */
317         snprintf(aaa, sizeof aaa, "GOTO %s", towhere);
318         serv_puts(aaa);
319         serv_gets(aaa);
320         if (aaa[3] == '*')
321                 express_msgs = 1;
322         if (!strncmp(aaa, "54", 2)) {
323                 newprompt("Enter room password: ", bbb, 9);
324                 snprintf(aaa, sizeof aaa, "GOTO %s|%s", towhere, bbb);
325                 serv_puts(aaa);
326                 serv_gets(aaa);
327                 if (aaa[3] == '*')
328                         express_msgs = 1;
329         }
330         if (!strncmp(aaa, "54", 2)) {
331                 printf("Wrong password.\n");
332                 return;
333         }
334         /*
335          * If a match is not found, try a partial match.
336          * Partial matches anywhere in the string carry a weight of 1,
337          * left-aligned matches carry a weight of 2.  Pick the room that
338          * has the highest-weighted match.
339          */
340         if (aaa[0] != '2') {
341                 best_match = 0;
342                 strcpy(bbb, "");
343                 serv_puts("LKRA");
344                 serv_gets(aaa);
345                 if (aaa[0] == '1')
346                         while (serv_gets(aaa), strcmp(aaa, "000")) {
347                                 extract(psearch, aaa, 0);
348                                 partial_match = 0;
349                                 if (pattern(psearch, towhere) >= 0) {
350                                         partial_match = 1;
351                                 }
352                                 if (!strncasecmp(towhere, psearch, strlen(towhere))) {
353                                         partial_match = 2;
354                                 }
355                                 if (partial_match > best_match) {
356                                         strcpy(bbb, psearch);
357                                         best_match = partial_match;
358                                 }
359                         }
360                 if (strlen(bbb) == 0) {
361                         printf("No room '%s'.\n", towhere);
362                         return;
363                 }
364                 snprintf(aaa, sizeof aaa, "GOTO %s", bbb);
365                 serv_puts(aaa);
366                 serv_gets(aaa);
367                 if (aaa[3] == '*')
368                         express_msgs = 1;
369         }
370         if (aaa[0] != '2') {
371                 printf("%s\n", aaa);
372                 return;
373         }
374         extract(room_name, &aaa[4], 0);
375         room_flags = extract_int(&aaa[4], 4);
376         from_floor = curr_floor;
377         curr_floor = extract_int(&aaa[4], 10);
378
379         remove_march(room_name, 0);
380         if (!strcasecmp(towhere, "_BASEROOM_"))
381                 remove_march(towhere, 0);
382         if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
383                 if (floorlist[(int) curr_floor][0] == 0)
384                         load_floorlist();
385                 printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
386         }
387         if (display_name == 1) {
388                 color(BRIGHT_WHITE);
389                 printf("%s ", room_name);
390                 color(DIM_WHITE);
391                 printf("- ");
392         }
393         if (display_name != 2) {
394                 color(BRIGHT_YELLOW);
395                 printf("%d ", extract_int(&aaa[4], 1));
396                 color(DIM_WHITE);
397                 printf("new of ");
398                 color(BRIGHT_YELLOW);
399                 printf("%d ", extract_int(&aaa[4], 2));
400                 color(DIM_WHITE);
401                 printf("messages.\n");
402         }
403         highest_msg_read = extract_int(&aaa[4], 6);
404         maxmsgnum = extract_int(&aaa[4], 5);
405         is_mail = (char) extract_int(&aaa[4], 7);
406         is_room_aide = (char) extract_int(&aaa[4], 8);
407         ls = extract_long(&aaa[4], 6);
408
409         /* read info file if necessary */
410         if (extract_int(&aaa[4], 3) > 0)
411                 readinfo();
412
413         /* check for newly arrived mail if we can */
414         if (num_parms(&aaa[4]) >= 10) {
415                 newmailcount = extract_int(&aaa[4], 9);
416                 if ((oldmailcount >= 0) && (newmailcount > oldmailcount)) {
417                         color(BRIGHT_RED);
418                         printf("*** You have new mail\n");
419                         color(DIM_WHITE);
420                 }
421                 oldmailcount = newmailcount;
422         }
423 }
424
425 /* Goto next room having unread messages.
426  * We want to skip over rooms that the user has already been to, and take the
427  * user back to the lobby when done.  The room we end up in is placed in
428  * newroom - which is set to 0 (the lobby) initially.
429  */
430 void gotonext(void)
431 {
432         char buf[256];
433         struct march *mptr, *mptr2;
434         char next_room[ROOMNAMELEN];
435
436         /* Check to see if the march-mode list is already allocated.
437          * If it is, pop the first room off the list and go there.
438          */
439         if (march == NULL) {
440                 serv_puts("LKRN");
441                 serv_gets(buf);
442                 if (buf[0] == '1')
443                         while (serv_gets(buf), strcmp(buf, "000")) {
444                                 mptr = (struct march *) malloc(sizeof(struct march));
445                                 mptr->next = NULL;
446                                 extract(mptr->march_name, buf, 0);
447                                 mptr->march_floor = (char) (extract_int(buf, 2) & 0x7F);
448                                 mptr->march_order = (char) (extract_int(buf, 3) & 0x7F);
449                                 if (march == NULL) {
450                                         march = mptr;
451                                 } else {
452                                         mptr2 = march;
453                                         while (mptr2->next != NULL)
454                                                 mptr2 = mptr2->next;
455                                         mptr2->next = mptr;
456                                 }
457                         }
458 /* add _BASEROOM_ to the end of the march list, so the user will end up
459  * in the system base room (usually the Lobby>) at the end of the loop
460  */
461                 mptr = (struct march *) malloc(sizeof(struct march));
462                 mptr->next = NULL;
463                 strcpy(mptr->march_name, "_BASEROOM_");
464                 if (march == NULL) {
465                         march = mptr;
466                 } else {
467                         mptr2 = march;
468                         while (mptr2->next != NULL)
469                                 mptr2 = mptr2->next;
470                         mptr2->next = mptr;
471                 }
472 /*
473  * ...and remove the room we're currently in, so a <G>oto doesn't make us
474  * walk around in circles
475  */
476                 remove_march(room_name, 0);
477         }
478         if (march != NULL) {
479                 strcpy(next_room, pop_march(curr_floor));
480         } else {
481                 strcpy(next_room, "_BASEROOM_");
482         }
483         remove_march(next_room, 0);
484         dotgoto(next_room, 1);
485 }
486
487 /*
488  * forget all rooms on a given floor
489  */
490 void forget_all_rooms_on(int ffloor)
491 {
492         char buf[256];
493         struct march *flist, *fptr;
494
495         printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
496         fflush(stdout);
497         snprintf(buf, sizeof buf, "LKRA %d", ffloor);
498         serv_puts(buf);
499         serv_gets(buf);
500         if (buf[0] != '1') {
501                 printf("%-72s\n", &buf[4]);
502                 return;
503         }
504         flist = NULL;
505         while (serv_gets(buf), strcmp(buf, "000")) {
506                 fptr = (struct march *) malloc(sizeof(struct march));
507                 fptr->next = flist;
508                 flist = fptr;
509                 extract(fptr->march_name, buf, 0);
510         }
511         while (flist != NULL) {
512                 snprintf(buf, sizeof buf, "GOTO %s", flist->march_name);
513                 serv_puts(buf);
514                 serv_gets(buf);
515                 if (buf[0] == '2') {
516                         serv_puts("FORG");
517                         serv_gets(buf);
518                 }
519                 fptr = flist;
520                 flist = flist->next;
521                 free(fptr);
522         }
523         printf("%-72s\r", "");
524 }
525
526
527 /*
528  * routine called by gotofloor() to move to a new room on a new floor
529  */
530 void gf_toroom(char *towhere, int mode)
531 {
532         int floor_being_left;
533
534         floor_being_left = curr_floor;
535
536         if (mode == GF_GOTO) {  /* <;G>oto mode */
537                 updatels();
538                 dotgoto(towhere, 1);
539         }
540         if (mode == GF_SKIP) {  /* <;S>kip mode */
541                 dotgoto(towhere, 1);
542                 remove_march("_FLOOR_", floor_being_left);
543         }
544         if (mode == GF_ZAP) {   /* <;Z>ap mode */
545                 dotgoto(towhere, 1);
546                 remove_march("_FLOOR_", floor_being_left);
547                 forget_all_rooms_on(floor_being_left);
548         }
549 }
550
551
552 /*
553  * go to a new floor
554  */
555 void gotofloor(char *towhere, int mode)
556 {
557         int a, tofloor;
558         struct march *mptr;
559         char buf[256], targ[256];
560
561         if (floorlist[0][0] == 0)
562                 load_floorlist();
563         tofloor = (-1);
564         for (a = 0; a < 128; ++a)
565                 if (!strcasecmp(&floorlist[a][0], towhere))
566                         tofloor = a;
567
568         if (tofloor < 0) {
569                 for (a = 0; a < 128; ++a) {
570                         if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
571                                 tofloor = a;
572                         }
573                 }
574         }
575         if (tofloor < 0) {
576                 for (a = 0; a < 128; ++a)
577                         if (pattern(towhere, &floorlist[a][0]) > 0)
578                                 tofloor = a;
579         }
580         if (tofloor < 0) {
581                 printf("No floor '%s'.\n", towhere);
582                 return;
583         }
584         for (mptr = march; mptr != NULL; mptr = mptr->next) {
585                 if ((mptr->march_floor) == tofloor)
586                         gf_toroom(mptr->march_name, mode);
587                 return;
588         }
589
590         strcpy(targ, "");
591         snprintf(buf, sizeof buf, "LKRA %d", tofloor);
592         serv_puts(buf);
593         serv_gets(buf);
594         if (buf[0] == '1')
595                 while (serv_gets(buf), strcmp(buf, "000")) {
596                         if ((extract_int(buf, 2) == tofloor) && (strlen(targ) == 0))
597                                 extract(targ, buf, 0);
598                 }
599         if (strlen(targ) > 0) {
600                 gf_toroom(targ, mode);
601         } else {
602                 printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
603         }
604 }
605
606
607 /*
608  * forget all rooms on current floor
609  */
610 void forget_this_floor(void)
611 {
612
613         if (curr_floor == 0) {
614                 printf("Can't forget this floor.\n");
615                 return;
616         }
617         if (floorlist[0][0] == 0)
618                 load_floorlist();
619         printf("Are you sure you want to forget all rooms on %s? ",
620                &floorlist[(int) curr_floor][0]);
621         if (yesno() == 0)
622                 return;
623
624         gf_toroom("_BASEROOM_", GF_ZAP);
625 }
626
627
628 /* 
629  * Figure out the physical screen dimensions, if we can
630  */
631 void check_screen_dims(void)
632 {
633 #ifdef TIOCGWINSZ
634         struct {
635                 unsigned short height;  /* rows */
636                 unsigned short width;   /* columns */
637                 unsigned short xpixels;
638                 unsigned short ypixels;         /* pixels */
639         } xwinsz;
640
641         if (have_xterm) {       /* dynamically size screen if on an xterm */
642                 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
643                         if (xwinsz.height)
644                                 screenheight = (int) xwinsz.height;
645                         if (xwinsz.width)
646                                 screenwidth = (int) xwinsz.width;
647                 }
648         }
649 #endif
650 }
651
652
653 /*
654  * set floor mode depending on client, server, and user settings
655  */
656 void set_floor_mode(void)
657 {
658         if (serv_info.serv_ok_floors == 0) {
659                 floor_mode = 0; /* Don't use floors if the server */
660         }
661         /* doesn't support them!          */
662         else {
663                 if (rc_floor_mode == RC_NO) {   /* never use floors */
664                         floor_mode = 0;
665                 }
666                 if (rc_floor_mode == RC_YES) {  /* always use floors */
667                         floor_mode = 1;
668                 }
669                 if (rc_floor_mode == RC_DEFAULT) {      /* user choice */
670                         floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
671                 }
672         }
673 }
674
675 /*
676  * Set or change the user's password
677  */
678 int set_password(void)
679 {
680         char pass1[20];
681         char pass2[20];
682         char buf[256];
683
684         if (strlen(rc_password) > 0) {
685                 strcpy(pass1, rc_password);
686                 strcpy(pass2, rc_password);
687         } else {
688                 IFNEXPERT formout("changepw");
689                 newprompt("Enter a new password: ", pass1, -19);
690                 newprompt("Enter it again to confirm: ", pass2, -19);
691         }
692         strproc(pass1);
693         strproc(pass2);
694         if (!strcasecmp(pass1, pass2)) {
695                 snprintf(buf, sizeof buf, "SETP %s", pass1);
696                 serv_puts(buf);
697                 serv_gets(buf);
698                 printf("%s\n", &buf[4]);
699                 offer_to_remember_password(hostbuf, portbuf, fullname, pass1);
700                 return (0);
701         } else {
702                 printf("*** They don't match... try again.\n");
703                 return (1);
704         }
705 }
706
707
708
709 /*
710  * get info about the server we've connected to
711  */
712 void get_serv_info(char *supplied_hostname)
713 {
714         char buf[512];
715
716         CtdlInternalGetServInfo(&serv_info);
717
718         /* be nice and identify ourself to the server */
719         snprintf(buf, sizeof buf, "IDEN %d|%d|%d|%s|",
720                  SERVER_TYPE, 0, REV_LEVEL,
721                  (server_is_local ? "local" : CITADEL));
722
723         /* Append a hostname */
724         if (supplied_hostname != NULL) {
725                 strcat(buf, supplied_hostname);
726         }
727         else {
728                 locate_host(&buf[strlen(buf)]); /* append to the end */
729         }
730
731         serv_puts(buf);
732         serv_gets(buf);         /* we don't care about the result code */
733 }
734
735
736
737
738
739 /*
740  * Display list of users currently logged on to the server
741  */
742 void who_is_online(int longlist)
743 {
744         char buf[256], username[256], roomname[256], fromhost[256];
745         char flags[256];
746         char actual_user[256], actual_room[256], actual_host[256];
747         char tbuf[256], clientsoft[256];
748         time_t timenow = 0;
749         time_t idletime, idlehours, idlemins, idlesecs;
750         int last_session = (-1);
751
752         serv_puts("TIME");
753         serv_gets(tbuf);
754         if (tbuf[0] == '2') {
755                 timenow = extract_long(&tbuf[4], 0);
756         }
757         else {
758                 time(&timenow);
759         }
760
761         if (!longlist) {
762                 color(BRIGHT_WHITE);
763                 pprintf("FLG ###        User Name                 Room                 From host\n");
764                 color(DIM_WHITE);
765                 pprintf("--- --- ------------------------- -------------------- ------------------------\n");
766         }
767         serv_puts("RWHO");
768         serv_gets(buf);
769         if (buf[0] == '1') {
770                 while (serv_gets(buf), strcmp(buf, "000")) {
771                         extract(username, buf, 1);
772                         extract(roomname, buf, 2);
773                         extract(fromhost, buf, 3);
774                         extract(clientsoft, buf, 4);
775                         extract(flags, buf, 7);
776
777                         idletime = timenow - extract_long(buf, 5);
778                         idlehours = idletime / 3600;
779                         idlemins = (idletime - (idlehours * 3600)) / 60;
780                         idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
781
782                         if (idletime > 900) {
783                                 while (strlen(roomname) < 20) {
784                                         strcat(roomname, " ");
785                                 }
786                                 strcpy(&roomname[14], "[idle]");
787                         }
788
789                         if (longlist) {
790
791                                 extract(actual_user, buf, 8);
792                                 extract(actual_room, buf, 9);
793                                 extract(actual_host, buf, 10);
794
795                                 pprintf("\nFlags: %-3s  Sess# %-3d  Name: %-25s  Room: %s\n",
796                                        flags, extract_int(buf, 0), username, roomname);
797                                 pprintf("from <%s> using <%s>, idle %ld:%02ld:%02ld\n",
798                                        fromhost, clientsoft,
799                                        (long) idlehours, (long) idlemins, (long) idlesecs);
800
801                                 if ( (strlen(actual_user)+strlen(actual_room)+strlen(actual_host)) > 0) {
802                                         pprintf("(really ");
803                                         if (strlen(actual_user)>0) pprintf("<%s> ", actual_user);
804                                         if (strlen(actual_room)>0) pprintf("in <%s> ", actual_room);
805                                         if (strlen(actual_host)>0) pprintf("from <%s> ", actual_host);
806                                         pprintf(")\n");
807                                 }
808
809                         } else {
810                                 if (extract_int(buf, 0) == last_session) {
811                                         pprintf("        ");
812                                 } else {
813                                         color(BRIGHT_MAGENTA);
814                                         pprintf("%-3s ", flags);
815                                         color(DIM_WHITE);
816                                         pprintf("%-3d ", extract_int(buf, 0));
817                                 }
818                                 last_session = extract_int(buf, 0);
819                                 color(BRIGHT_CYAN);
820                                 pprintf("%-25s ", username);
821                                 color(BRIGHT_MAGENTA);
822                                 roomname[20] = 0;
823                                 pprintf("%-20s ", roomname);
824                                 color(BRIGHT_CYAN);
825                                 pprintf("%-24s\n", fromhost);
826                                 color(DIM_WHITE);
827                         }
828                 }
829         }
830 }
831
832 void enternew(char *desc, char *buf, int maxlen)
833 {
834         char bbb[128];
835         snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
836         newprompt(bbb, buf, maxlen);
837 }
838
839
840
841 int shift(int argc, char **argv, int start, int count) {
842         int i;
843
844         for (i=start; i<(argc-count); ++i) {
845                 argv[i] = argv[i+count];
846         }
847         argc = argc - count;
848         return argc;
849 }
850
851 /*
852  * main
853  */
854 int main(int argc, char **argv)
855 {
856         int a, b, mcmd;
857         char aaa[100], bbb[100];/* general purpose variables */
858         char argbuf[32];        /* command line buf */
859         char nonce[NONCE_SIZE];
860         char *telnet_client_host = NULL;
861         char *sptr, *sptr2;     /* USed to extract the nonce */
862         char hexstring[MD5_HEXSTRING_SIZE];
863         volatile int termn8 = 0;
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         if (mcmd == 30)
1510                 printf("\n\nType 'off' to hang up, or next user...\n");
1511         snprintf(aaa, sizeof aaa, "LOUT");
1512         serv_puts(aaa);
1513         serv_gets(aaa);
1514         if ((mcmd == 29) || (mcmd == 15)) {
1515                 formout("goodbye");
1516                 logoff(0);
1517         }
1518         goto GSTA;
1519
1520 }                               /* end main() */