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