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