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