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