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