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