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