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