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