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