* Remove nulls appended to editor files during replace, edit, and print
[citadel.git] / citadel / rooms.c
1 /* Citadel/UX room-oriented routines */
2 /* $Id$ */
3
4 #include "sysdep.h"
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <string.h>
11 #include <signal.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <sys/wait.h>
15 #include <errno.h>
16 #include <stdarg.h>
17 #include "citadel.h"
18 #include "rooms.h"
19 #include "commands.h"
20 #include "tools.h"
21 #include "messages.h"
22 #ifndef HAVE_SNPRINTF
23 #include "snprintf.h"
24 #endif
25
26 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
27
28
29 void sttybbs(int cmd);
30 void hit_any_key(void);
31 int yesno(void);
32 void strprompt(char *prompt, char *str, int len);
33 void newprompt(char *prompt, char *str, int len);
34 int struncmp(char *lstr, char *rstr, int len);
35 void dotgoto(char *towhere, int display_name);
36 void serv_read(char *buf, int bytes);
37 void formout(char *name);
38 int inkey(void);
39 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
40 void progress(long int curr, long int cmax);
41 int pattern(char *search, char *patn);
42 int file_checksum(char *filename);
43 int nukedir(char *dirname);
44
45 extern unsigned room_flags;
46 extern char room_name[];
47 extern char temp[];
48 extern char tempdir[];
49 extern int editor_pid;
50 extern char editor_path[];
51 extern int screenwidth;
52 extern int screenheight;
53 extern char fullname[];
54 extern int userflags;
55 extern char sigcaught;
56 extern char floor_mode;
57 extern char curr_floor;
58
59
60 extern int ugnum;
61 extern long uglsn;
62 extern char ugname[];
63
64 extern char floorlist[128][256];
65
66
67 void load_floorlist(void) {
68         int a;
69         char buf[256];
70
71         for (a=0; a<128; ++a) floorlist[a][0] = 0;
72
73         serv_puts("LFLR");
74         serv_gets(buf);
75         if (buf[0]!='1') {
76                 strcpy(floorlist[0],"Main Floor");
77                 return;
78                 }
79         while (serv_gets(buf), strcmp(buf,"000")) {
80                 extract(floorlist[extract_int(buf,0)],buf,1);
81                 }
82         }
83
84
85 void room_tree_list(struct roomlisting *rp) {
86         static int c = 0;
87         char rmname[ROOMNAMELEN];
88         int f;
89
90         if (rp == NULL) {
91                 c = 1;
92                 return;
93                 }
94
95         if (rp->lnext != NULL) {
96                 room_tree_list(rp->lnext);
97                 }
98
99         if (sigcaught == 0) {
100                 strcpy(rmname, rp->rlname);
101                 f = rp->rlflags;
102                 if ((c + strlen(rmname) + 4) > screenwidth) {
103                         printf("\n");
104                         c = 1;
105                         }
106                 if (f & QR_MAILBOX) {
107                        color(BRIGHT_YELLOW);
108                        }
109                 else if (f & QR_PRIVATE) {
110                         color(BRIGHT_RED);
111                         }
112                 else {
113                         color(DIM_WHITE);
114                         }
115                 printf("%s",rmname);
116                 if ((f & QR_DIRECTORY) && (f & QR_NETWORK)) printf("}  ");
117                 else if (f & QR_DIRECTORY) printf("]  ");
118                 else if (f & QR_NETWORK) printf(")  ");
119                 else printf(">  ");
120                 c = c + strlen(rmname) + 3;
121                 }
122
123         if (rp->rnext != NULL) {
124                 room_tree_list(rp->rnext);
125                 }
126
127         free(rp);
128         }
129
130
131 /* 
132  * Room ordering stuff (compare first by floor, then by order)
133  */
134 int rordercmp(struct roomlisting *r1, struct roomlisting *r2)
135 {
136         if ((r1==NULL)&&(r2==NULL)) return(0);
137         if (r1==NULL) return(-1);
138         if (r2==NULL) return(1);
139         if (r1->rlfloor < r2->rlfloor) return(-1);
140         if (r1->rlfloor > r2->rlfloor) return(1);
141         if (r1->rlorder < r2->rlorder) return(-1);
142         if (r1->rlorder > r2->rlorder) return(1);
143         return(0);
144         }
145
146
147 /*
148  * Common code for all room listings
149  */
150 void listrms(char *variety)
151 {
152         char buf[256];
153
154         struct roomlisting *rl = NULL;
155         struct roomlisting *rp;
156         struct roomlisting *rs;
157
158
159         /* Ask the server for a room list */
160         serv_puts(variety);
161         serv_gets(buf);
162         if (buf[0]!='1') return;
163         while (serv_gets(buf), strcmp(buf, "000")) {
164                 rp = malloc(sizeof(struct roomlisting));
165                 extract(rp->rlname, buf, 0);
166                 rp->rlflags = extract_int(buf, 1);
167                 rp->rlfloor = extract_int(buf, 2);
168                 rp->rlorder = extract_int(buf, 3);
169                 rp->lnext = NULL;
170                 rp->rnext = NULL;
171
172                 rs = rl;
173                 if (rl == NULL) {
174                         rl = rp;
175                         }
176                 else while (rp != NULL) {
177                         if (rordercmp(rp, rs)<0) {
178                                 if (rs->lnext == NULL) {
179                                         rs->lnext = rp;
180                                         rp = NULL;
181                                         }
182                                 else {
183                                         rs = rs->lnext;
184                                         }
185                                 }
186                         else {
187                                 if (rs->rnext == NULL) {
188                                         rs->rnext = rp;
189                                         rp = NULL;
190                                         }
191                                 else {
192                                         rs = rs->rnext;
193                                         }
194                                 }
195                         }
196                 }
197
198         sigcaught = 0;
199         sttybbs(SB_YES_INTR);
200         room_tree_list(NULL);
201         room_tree_list(rl);
202         color(DIM_WHITE);
203         sttybbs(SB_NO_INTR);
204         }
205
206
207 void list_other_floors(void) {
208         int a,c;
209
210         c = 1;
211         for (a=0; a<128; ++a) if ((strlen(floorlist[a])>0)&&(a!=curr_floor)) {
212                 if ((c + strlen(floorlist[a]) + 4) > screenwidth) {
213                         printf("\n");
214                         c = 1;
215                         }
216                 printf("%s:  ",floorlist[a]);
217                 c = c + strlen(floorlist[a]) + 3;
218                 }
219         }
220
221
222 /*
223  * List known rooms.  kn_floor_mode should be set to 0 for a 'flat' listing,
224  * 1 to list rooms on the current floor, or 1 to list rooms on all floors.
225  */
226 void knrooms(int kn_floor_mode)
227 {
228         char buf[256];
229         int a;
230
231         load_floorlist();
232
233         if (kn_floor_mode == 0) {
234                 color(BRIGHT_CYAN);
235                 printf("\n   Rooms with unread messages:\n");
236                 listrms("LKRN");
237                 color(BRIGHT_CYAN);
238                 printf("\n\n   No unseen messages in:\n");
239                 listrms("LKRO");
240                 printf("\n");
241                 }
242
243         if (kn_floor_mode == 1) {
244                 color(BRIGHT_CYAN);
245                 printf("\n   Rooms with unread messages on %s:\n",
246                         floorlist[(int)curr_floor]);
247                 sprintf(buf,"LKRN %d",curr_floor);
248                 listrms(buf);
249                 color(BRIGHT_CYAN);
250                 printf("\n\n   Rooms with no new messages on %s:\n",
251                         floorlist[(int)curr_floor]);
252                 sprintf(buf,"LKRO %d",curr_floor);
253                 listrms(buf);
254                 color(BRIGHT_CYAN);
255                 printf("\n\n   Other floors:\n");
256                 list_other_floors();
257                 printf("\n");
258                 }
259
260         if (kn_floor_mode == 2) {
261                 for (a=0; a<128; ++a) if (floorlist[a][0]!=0) {
262                         color(BRIGHT_CYAN);
263                         printf("\n   Rooms on %s:\n",floorlist[a]);
264                         sprintf(buf,"LKRA %d",a);
265                         listrms(buf);
266                         printf("\n");
267                         }
268                 }
269         
270         color(DIM_WHITE);
271         IFNEXPERT hit_any_key();
272         }
273
274
275 void listzrooms(void) {         /* list public forgotten rooms */
276         color(BRIGHT_CYAN);
277         printf("\n   Forgotten public rooms:\n");
278         listrms("LZRM");
279         printf("\n");
280         color(DIM_WHITE);
281         IFNEXPERT hit_any_key();
282         }
283
284
285 int set_room_attr(int ibuf, char *prompt, unsigned int sbit)
286 {
287         int a;
288
289         a = boolprompt(prompt, (ibuf&sbit));
290         ibuf=(ibuf|sbit);
291         if (!a) ibuf=(ibuf^sbit);
292         return(ibuf);
293         }
294
295
296
297 /*
298  * Select a floor (used in several commands)
299  * The supplied argument is the 'default' floor number.
300  * This function returns the selected floor number.
301  */
302 int select_floor(int rfloor)
303 {
304         int a, newfloor;
305         char floorstr[256];
306
307         if (floor_mode == 1) {
308                 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
309
310                 do {
311                         newfloor = (-1);
312                         safestrncpy(floorstr,floorlist[rfloor],sizeof floorstr);
313                         strprompt("Which floor",floorstr,256);
314                         for (a=0; a<128; ++a) {
315                                 if (!strucmp(floorstr,&floorlist[a][0]))
316                                         newfloor = a;
317                                 if ((newfloor<0)&&(!struncmp(floorstr,
318                                         &floorlist[a][0],strlen(floorstr))))
319                                                 newfloor = a;
320                                 if ((newfloor<0)&&(pattern(&floorlist[a][0],
321                                         floorstr)>=0)) newfloor = a;
322                                 }
323                         if (newfloor<0) {
324                                 printf("\n One of:\n");
325                                 for (a=0; a<128; ++a)
326                                         if (floorlist[a][0]!=0)
327                                                 printf("%s\n",
328                                                         &floorlist[a][0]);
329                                 }
330                         } while(newfloor < 0);
331                 return(newfloor);
332                 }
333         return(rfloor);
334         }
335
336
337
338
339 /*
340  * .<A>ide <E>dit room
341  */
342 void editthisroom(void) {
343         char rname[ROOMNAMELEN];
344         char rpass[10];
345         char rdir[15];
346         unsigned rflags;
347         int rbump;
348         char raide[32];
349         char buf[256];
350         int rfloor;
351         int rorder;
352         int expire_mode = 0;
353         int expire_value = 0;
354
355         /* Fetch the existing room config */
356         serv_puts("GETR");
357         serv_gets(buf);
358         if (buf[0]!='2') {
359                 printf("%s\n",&buf[4]);
360                 return;
361                 }
362
363         extract(rname,&buf[4],0);
364         extract(rpass,&buf[4],1);
365         extract(rdir, &buf[4],2);
366         rflags = extract_int(&buf[4],3);
367         rfloor = extract_int(&buf[4],4);
368         rorder = extract_int(&buf[4],5);
369         rbump = 0;
370
371         /* Fetch the name of the current room aide */
372         serv_puts("GETA");
373         serv_gets(buf);
374         if (buf[0]=='2') safestrncpy(raide,&buf[4],sizeof raide);
375         else strcpy(raide,"");
376         if (strlen(raide)==0) strcpy(raide,"none");
377
378         /* Fetch the expire policy (this will silently fail on old servers,
379          * resulting in "default" policy)
380          */
381         serv_puts("GPEX room");
382         serv_gets(buf);
383         if (buf[0]=='2') {
384                 expire_mode = extract_int(&buf[4], 0);
385                 expire_value = extract_int(&buf[4], 1);
386                 }
387
388         /* Now interact with the user. */
389         strprompt("Room name",rname,ROOMNAMELEN-1);
390
391         rfloor = select_floor(rfloor);
392         rflags = set_room_attr(rflags,"Private room",QR_PRIVATE);
393         if (rflags & QR_PRIVATE)
394                 rflags = set_room_attr(rflags,
395                         "Accessible by guessing room name",QR_GUESSNAME);
396
397         /* if it's public, clear the privacy classes */
398         if ((rflags & QR_PRIVATE)==0) {
399                 if (rflags & QR_GUESSNAME)  rflags = rflags - QR_GUESSNAME;
400                 if (rflags & QR_PASSWORDED) rflags = rflags - QR_PASSWORDED;
401                 }
402
403         /* if it's private, choose the privacy classes */
404         if ( (rflags & QR_PRIVATE)
405            && ( (rflags & QR_GUESSNAME) == 0) ) {
406                 rflags = set_room_attr(rflags,
407                         "Accessible by entering a password",QR_PASSWORDED);
408                 }
409         if ( (rflags & QR_PRIVATE)
410            && ((rflags&QR_PASSWORDED)==QR_PASSWORDED) ) {
411                 strprompt("Room password",rpass,9);
412                 }
413
414         if ((rflags&QR_PRIVATE)==QR_PRIVATE) {
415                 rbump = boolprompt("Cause current users to forget room", 0);
416                 }
417
418         rflags = set_room_attr(rflags,"Preferred users only",QR_PREFONLY);
419         rflags = set_room_attr(rflags,"Read-only room",QR_READONLY);
420         rflags = set_room_attr(rflags,"Directory room",QR_DIRECTORY);
421         rflags = set_room_attr(rflags,"Permanent room",QR_PERMANENT);
422         if (rflags & QR_DIRECTORY) {
423                 strprompt("Directory name",rdir,14);
424                 rflags = set_room_attr(rflags,"Uploading allowed",QR_UPLOAD);
425                 rflags = set_room_attr(rflags,"Downloading allowed",
426                                                                 QR_DOWNLOAD);
427                 rflags = set_room_attr(rflags,"Visible directory",QR_VISDIR);
428                 }
429         rflags = set_room_attr(rflags,"Network shared room",QR_NETWORK);
430         rflags = set_room_attr(rflags,
431                 "Automatically make all messages anonymous",QR_ANONONLY);
432         if ( (rflags & QR_ANONONLY) == 0) {
433                 rflags = set_room_attr(rflags,
434                         "Ask users whether to make messages anonymous",
435                         QR_ANONOPT);
436                 }
437         rorder = intprompt("Listing order", rorder, 1, 127);
438
439         /* Ask about the room aide */
440         do {
441                 strprompt("Room aide (or 'none')",raide,29);
442                 if (!strucmp(raide,"none")) {
443                         strcpy(raide,"");
444                         strcpy(buf,"200");
445                         }
446                 else {
447                         snprintf(buf,sizeof buf,"QUSR %s",raide);
448                         serv_puts(buf);
449                         serv_gets(buf);
450                         if (buf[0]!='2') printf("%s\n",&buf[4]);
451                         }
452                 } while(buf[0]!='2');
453
454         if (!strucmp(raide,"none")) strcpy(raide,"");
455
456
457         /* Angels and demons dancing in my head... */
458         do {
459                 sprintf(buf, "%d", expire_mode);
460                 strprompt("Message expire policy (? for list)", buf, 1);
461                 if (buf[0] == '?') {
462                         printf("\n");
463                         printf("0. Use the default for this floor\n");
464                         printf("1. Never automatically expire messages\n");
465                         printf("2. Expire by message count\n");
466                         printf("3. Expire by message age\n");
467                         }
468                 } while((buf[0]<48)||(buf[0]>51));
469         expire_mode = buf[0] - 48;
470
471         /* ...lunatics and monsters underneath my bed */
472         if (expire_mode == 2) {
473                 sprintf(buf, "%d", expire_value);
474                 strprompt("Keep how many messages online?", buf, 10);
475                 expire_value = atol(buf);
476                 }
477
478         if (expire_mode == 3) {
479                 sprintf(buf, "%d", expire_value);
480                 strprompt("Keep messages for how many days?", buf, 10);
481                 expire_value = atol(buf);
482                 }
483
484         /* Give 'em a chance to change their minds */
485         printf("Save changes (y/n)? ");
486
487         if (yesno()==1) {
488                 snprintf(buf,sizeof buf,"SETA %s",raide);
489                 serv_puts(buf);
490                 serv_gets(buf);
491                 if (buf[0]!='2') printf("%s\n",&buf[4]);
492
493                 snprintf(buf, sizeof buf, "SPEX room|%d|%d",
494                         expire_mode, expire_value);
495                 serv_puts(buf);
496                 serv_gets(buf);
497
498                 snprintf(buf,sizeof buf,"SETR %s|%s|%s|%d|%d|%d|%d",
499                         rname,rpass,rdir,rflags,rbump,rfloor,rorder);
500                 serv_puts(buf);
501                 serv_gets(buf);
502                 printf("%s\n",&buf[4]);
503                 if (buf[0]=='2') dotgoto(rname,2);
504                 }
505         }
506
507
508 /*
509  * un-goto the previous room
510  */
511 void ungoto(void) { 
512         char buf[256];
513         
514         if (!strcmp(ugname,"")) return;
515         snprintf(buf,sizeof buf,"GOTO %s",ugname);
516         serv_puts(buf);
517         serv_gets(buf);
518         if (buf[0]!='2') {
519                 printf("%s\n",&buf[4]);
520                 return;
521                 }
522         sprintf(buf,"SLRP %ld",uglsn);
523         serv_puts(buf);
524         serv_gets(buf);
525         if (buf[0]!='2') printf("%s\n",&buf[4]);
526         safestrncpy(buf,ugname,sizeof buf);
527         strcpy(ugname,"");
528         dotgoto(buf,0);
529         }
530
531
532 /* Here's the code for simply transferring the file to the client,
533  * for folks who have their own clientware.  It's a lot simpler than
534  * the [XYZ]modem code below...
535  * (This function assumes that a download file is already open on the server)
536  */
537 void download_to_local_disk(char *supplied_filename, long total_bytes)
538 {
539         char buf[256];
540         char dbuf[4096];
541         long transmitted_bytes = 0L;
542         long aa,bb;
543         FILE *savefp;
544         int broken = 0;
545         int packet;
546         char filename[256];
547
548         strcpy(filename, supplied_filename);
549         if (strlen(filename)==0) {
550                 newprompt("Filename: ", filename, 250);
551                 }
552
553         printf("Enter the name of the directory to save '%s'\n",
554                 filename);
555         printf("to, or press return for the current directory.\n");
556         newprompt("Directory: ",dbuf,256);
557         if (strlen(dbuf)==0) strcpy(dbuf,".");
558         strcat(dbuf,"/");
559         strcat(dbuf,filename);
560         
561         savefp = fopen(dbuf,"w");
562         if (savefp == NULL) {
563                 printf("Cannot open '%s': %s\n",dbuf,strerror(errno));
564                 /* close the download file at the server */
565                 serv_puts("CLOS");
566                 serv_gets(buf);
567                 if (buf[0]!='2') {
568                         printf("%s\n",&buf[4]);
569                         }
570                 return;
571                 }
572         progress(0,total_bytes);
573         while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
574                 bb = total_bytes - transmitted_bytes;
575                 aa = ((bb < 4096) ? bb : 4096);
576                 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
577                 serv_puts(buf);
578                 serv_gets(buf);
579                 if (buf[0]!='6') {
580                         printf("%s\n",&buf[4]);
581                         return;
582                         }
583                 packet = extract_int(&buf[4],0);
584                 serv_read(dbuf,packet);
585                 if (fwrite(dbuf,packet,1,savefp) < 1) broken = 1;
586                 transmitted_bytes = transmitted_bytes + (long)packet;
587                 progress(transmitted_bytes,total_bytes);
588                 }
589         fclose(savefp);
590         /* close the download file at the server */
591         serv_puts("CLOS");
592         serv_gets(buf);
593         if (buf[0]!='2') {
594                 printf("%s\n",&buf[4]);
595                 }
596         return;
597         }
598
599
600 /*
601  * download()  -  download a file or files.  The argument passed to this
602  *                function determines which protocol to use.
603  *  proto - 0 = paginate, 1 = xmodem, 2 = raw, 3 = ymodem, 4 = zmodem, 5 = save
604  */
605 void download(int proto)
606 {
607         char buf[256];
608         char filename[256];
609         char tempname[256];
610         char transmit_cmd[256];
611         long total_bytes = 0L;
612         char dbuf[4096];
613         long transmitted_bytes = 0L;
614         long aa,bb;
615         int packet;
616         FILE *tpipe = NULL;
617         int broken = 0;
618
619         if ((room_flags & QR_DOWNLOAD) == 0) {
620                 printf("*** You cannot download from this room.\n");
621                 return;
622                 }
623         
624         newprompt("Enter filename: ",filename,255);
625
626         snprintf(buf,sizeof buf,"OPEN %s",filename);
627         serv_puts(buf);
628         serv_gets(buf);
629         if (buf[0]!='2') {
630                 printf("%s\n",&buf[4]);
631                 return;
632                 }
633         total_bytes = extract_long(&buf[4],0);
634
635         /* Save to local disk, for folks with their own copy of the client */
636         if (proto == 5) {
637                 download_to_local_disk(filename, total_bytes);
638                 return;
639                 }
640
641         /* Meta-download for public clients */
642         printf("Fetching file from Citadel server...\n");
643         mkdir(tempdir, 0700);
644         snprintf(tempname, sizeof tempname, "%s/%s", tempdir, filename);
645         tpipe = fopen(tempname, "wb");
646         while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
647                 progress(transmitted_bytes, total_bytes);
648                 bb = total_bytes - transmitted_bytes;
649                 aa = ((bb < 4096) ? bb : 4096);
650                 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
651                 serv_puts(buf);
652                 serv_gets(buf);
653                 if (buf[0]!='6') {
654                         printf("%s\n",&buf[4]);
655                 }
656                 packet = extract_int(&buf[4],0);
657                 serv_read(dbuf,packet);
658                 if (fwrite(dbuf,packet,1,tpipe) < 1) broken = 1;
659                 transmitted_bytes = transmitted_bytes + (long)packet;
660         }
661         fclose(tpipe);
662         progress(transmitted_bytes, total_bytes);
663
664         /* close the download file at the server */
665         serv_puts("CLOS");
666         serv_gets(buf);
667         if (buf[0]!='2') {
668                 printf("%s\n",&buf[4]);
669         }
670
671         if (proto==0)           sprintf(transmit_cmd, "SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",tempname);
672         else if (proto==1)      sprintf(transmit_cmd, "exec sx %s", tempname);
673         else if (proto==3)      sprintf(transmit_cmd, "exec sb %s", tempname);
674         else if (proto==4)      sprintf(transmit_cmd, "exec sz %s", tempname);
675         else                    sprintf(transmit_cmd, "exec cat %s", tempname);
676
677         sttybbs(SB_RESTORE);
678         system(transmit_cmd);
679         sttybbs(SB_NO_INTR);
680         
681         /* clean up the temporary directory */
682         nukedir(tempdir);
683         putc(7,stdout);
684 }
685
686
687 /*
688  * read directory of this room
689  */
690 void roomdir(void) {
691         char flnm[256];
692         char flsz[32];
693         char comment[256];
694         char buf[256];
695
696         serv_puts("RDIR");
697         serv_gets(buf);
698         if (buf[0]!='1') {
699                 printf("%s\n",&buf[4]);
700                 return;
701                 }
702
703         extract(comment,&buf[4],0);
704         extract(flnm,&buf[4],1);
705         printf("\nDirectory of %s on %s\n",flnm,comment);
706         printf("-----------------------\n");
707         while (serv_gets(buf), strcmp(buf,"000")) {
708                 extract(flnm,buf,0);
709                 extract(flsz,buf,1);
710                 extract(comment,buf,2);
711                 if (strlen(flnm)<=14)
712                         printf("%-14s %8s %s\n",flnm,flsz,comment);
713                 else
714                         printf("%s\n%14s %8s %s\n",flnm,"",flsz,comment);
715                 }
716         }
717
718
719 /*
720  * add a user to a private room
721  */
722 void invite(void) {
723         char aaa[31],bbb[256];
724
725         if ((room_flags & QR_PRIVATE)==0) {
726                 printf("This is not a private room.\n");
727                 return;
728                 }
729
730         newprompt("Name of user? ",aaa,30);
731         if (aaa[0]==0) return;
732
733         snprintf(bbb,sizeof bbb,"INVT %s",aaa);
734         serv_puts(bbb);
735         serv_gets(bbb);
736         printf("%s\n",&bbb[4]);
737         }
738
739
740 /*
741  * kick a user out of a room
742  */
743 void kickout(void) {
744         char aaa[31],bbb[256];
745
746         newprompt("Name of user? ",aaa,30);
747         if (aaa[0]==0) return;
748
749         snprintf(bbb,sizeof bbb,"KICK %s",aaa);
750         serv_puts(bbb);
751         serv_gets(bbb);
752         printf("%s\n",&bbb[4]);
753         }
754
755
756 /*
757  * aide command: kill the current room
758  */
759 void killroom(void) {
760         char aaa[100];
761
762         serv_puts("KILL 0");
763         serv_gets(aaa);
764         if (aaa[0]!='2') {
765                 printf("%s\n",&aaa[4]);
766                 return;
767                 }
768
769         printf("Are you sure you want to kill this room? ");
770         if (yesno()==0) return;
771
772         serv_puts("KILL 1");
773         serv_gets(aaa);
774         printf("%s\n",&aaa[4]);
775         if (aaa[0]!='2') return;
776         dotgoto("_BASEROOM_",0);
777         }
778
779 void forget(void) {     /* forget the current room */
780         char cmd[256];
781
782         printf("Are you sure you want to forget this room? ");
783         if (yesno()==0) return;
784
785         serv_puts("FORG");
786         serv_gets(cmd);
787         if (cmd[0]!='2') {
788                 printf("%s\n",&cmd[4]);
789                 return;
790                 }
791
792         /* now return to the lobby */
793         dotgoto("_BASEROOM_",0);
794         }
795
796
797 /*
798  * create a new room
799  */
800 void entroom(void) {
801         char cmd[256];
802         char new_room_name[ROOMNAMELEN];
803         int new_room_type;
804         char new_room_pass[10];
805         int new_room_floor;
806         int a,b;
807
808         serv_puts("CRE8 0");
809         serv_gets(cmd);
810         
811         if (cmd[0]!='2') {
812                 printf("%s\n",&cmd[4]);
813                 return;
814                 }
815         
816         newprompt("Name for new room? ",new_room_name,ROOMNAMELEN-1);
817         if (strlen(new_room_name)==0) return;
818         for (a=0; a<strlen(new_room_name); ++a)
819                 if (new_room_name[a] == '|') new_room_name[a]='_';
820
821         new_room_floor = select_floor((int)curr_floor);
822
823         IFNEXPERT formout("roomaccess");
824         do {
825                 printf( "<?>Help\n<1>Public room\n<2>Guess-name room\n"
826                         "<3>Passworded room\n<4>Invitation-only room\n"
827                         "<5>Personal room\n");
828                 printf("Enter room type: ");
829                 do {
830                         b=inkey();
831                         } while (((b<'1')||(b>'5')) && (b!='?'));
832                 if (b=='?') {
833                         printf("?\n");
834                         formout("roomaccess");
835                         }
836                 } while ((b<'1')||(b>'5'));
837         b=b-48;
838         printf("%d\n",b);
839         new_room_type = b - 1;
840         if (new_room_type==2) {
841                 newprompt("Enter a room password: ",new_room_pass,9);
842                 for (a=0; a<strlen(new_room_pass); ++a)
843                         if (new_room_pass[a] == '|') new_room_pass[a]='_';
844                 }
845         else strcpy(new_room_pass,"");
846
847         printf("\042%s\042, a",new_room_name);
848         if (b==1) printf(" public room.");
849         if (b==2) printf(" guess-name room.");
850         if (b==3) printf(" passworded room, password: %s",new_room_pass);
851         if (b==4) printf("n invitation-only room.");
852         if (b==5) printf(" personal room.");
853         printf("\nInstall it? (y/n) : ");
854         a=yesno();
855         if (a==0) return;
856
857         snprintf(cmd, sizeof cmd, "CRE8 1|%s|%d|%s|%d", new_room_name,
858                 new_room_type, new_room_pass, new_room_floor);
859         serv_puts(cmd);
860         serv_gets(cmd);
861         if (cmd[0]!='2') {
862                 printf("%s\n",&cmd[4]);
863                 return;
864                 }
865
866         /* command succeeded... now GO to the new room! */
867         dotgoto(new_room_name,0);
868         }
869
870
871
872 void readinfo(void) {   /* read info file for current room */
873         char cmd[256];
874         
875         sprintf(cmd,"RINF");
876         serv_puts(cmd);
877         serv_gets(cmd);
878
879         if (cmd[0]!='1') return;
880
881         fmout(screenwidth,NULL,
882                 ((userflags & US_PAGINATOR) ? 1 : 0),
883                 screenheight,0,1);
884         }
885
886
887 /*
888  * <W>ho knows room...
889  */
890 void whoknows(void) {
891         char buf[256];
892         serv_puts("WHOK");
893         serv_gets(buf);
894         if (buf[0]!='1') {
895                 printf("%s\n",&buf[5]);
896                 return;
897                 }
898         sigcaught = 0;
899         sttybbs(SB_YES_INTR);
900         while (serv_gets(buf), strncmp(buf,"000",3)) {
901                 if (sigcaught==0) printf("%s\n",buf);
902                 }
903         sttybbs(SB_NO_INTR);
904         }
905
906
907 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
908 {
909         FILE *fp;
910         char cmd[256];
911         int b,cksum,editor_exit;
912
913
914         if (strlen(editor_path)==0) {
915                 printf("Do you wish to re-enter %s? ",desc);
916                 if (yesno()==0) return;
917                 }
918
919         fp = fopen(temp,"w");
920         fclose(fp);
921
922         serv_puts(check_cmd);
923         serv_gets(cmd);
924         if (cmd[0]!='2') {
925                 printf("%s\n",&cmd[4]);
926                 return;
927                 }
928
929         if (strlen(editor_path)>0) {
930                 serv_puts(read_cmd);
931                 serv_gets(cmd);
932                 if (cmd[0]=='1') {
933                         fp = fopen(temp,"w");
934                         while (serv_gets(cmd), strcmp(cmd,"000")) {
935                                 fprintf(fp,"%s\n",cmd);
936                                 }
937                         fclose(fp);
938                         }
939                 }
940
941         cksum = file_checksum(temp);
942
943         if (strlen(editor_path)>0) {
944                 editor_pid=fork();
945                 if (editor_pid==0) {
946                         chmod(temp,0600);
947                         sttybbs(SB_RESTORE);
948                         execlp(editor_path,editor_path,temp,NULL);
949                         exit(1);
950                         }
951                 if (editor_pid>0) do {
952                         editor_exit = 0;
953                         b=wait(&editor_exit);
954                         } while((b!=editor_pid)&&(b>=0));
955                 editor_pid = (-1);
956                 printf("Executed %s\n", editor_path);
957                 sttybbs(0);
958                 }
959         else {
960                 printf("Entering %s.  ",desc);
961                 printf("Press return twice when finished.\n");
962                 fp=fopen(temp,"r+");
963                 citedit(fp);
964                 fclose(fp);
965                 }
966
967         if (file_checksum(temp) == cksum) {
968                 printf("*** Aborted.\n");
969                 }
970
971         else {
972                 serv_puts(write_cmd);
973                 serv_gets(cmd);
974                 if (cmd[0]!='4') {
975                         printf("%s\n",&cmd[4]);
976                         return;
977                         }
978
979                 fp=fopen(temp,"r");
980                 while (fgets(cmd,255,fp)!=NULL) {
981                         cmd[strlen(cmd)-1] = 0;
982                         serv_puts(cmd);
983                         }
984                 fclose(fp);
985                 serv_puts("000");
986                 }
987
988         unlink(temp);
989         }
990
991
992 void enterinfo(void) {          /* edit info file for current room */
993         do_edit("the Info file for this room","RINF","EINF 0","EINF 1");
994         }
995
996 void enter_bio(void) {
997         char cmd[256];
998         snprintf(cmd,sizeof cmd,"RBIO %s",fullname);
999         do_edit("your Bio",cmd,"NOOP","EBIO");
1000         }
1001
1002 /*
1003  * create a new floor
1004  */
1005 void create_floor(void) {
1006         char buf[256];
1007         char newfloorname[256];
1008
1009         serv_puts("CFLR xx|0");
1010         serv_gets(buf);
1011         if (buf[0]!='2') {
1012                 printf("%s\n",&buf[4]);
1013                 return;
1014                 }
1015
1016         newprompt("Name for new floor: ",newfloorname,255);
1017         snprintf(buf,sizeof buf,"CFLR %s|1",newfloorname);
1018         serv_puts(buf);
1019         serv_gets(buf);
1020         if (buf[0]=='2') {
1021                 printf("Floor has been created.\n");
1022                 }
1023         else {
1024                 printf("%s\n",&buf[4]);
1025                 }
1026         }
1027
1028 /*
1029  * edit the current floor
1030  */
1031 void edit_floor(void) {
1032         char buf[256];
1033         int expire_mode = 0;
1034         int expire_value = 0;
1035
1036         if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1037
1038         /* Fetch the expire policy (this will silently fail on old servers,
1039          * resulting in "default" policy)
1040          */
1041         serv_puts("GPEX floor");
1042         serv_gets(buf);
1043         if (buf[0]=='2') {
1044                 expire_mode = extract_int(&buf[4], 0);
1045                 expire_value = extract_int(&buf[4], 1);
1046                 }
1047
1048         /* Interact with the user */
1049         strprompt("Floor name",&floorlist[(int)curr_floor][0],255);
1050
1051         /* Angels and demons dancing in my head... */
1052         do {
1053                 sprintf(buf, "%d", expire_mode);
1054                 strprompt("Floor default essage expire policy (? for list)",
1055                         buf, 1);
1056                 if (buf[0] == '?') {
1057                         printf("\n");
1058                         printf("0. Use the system default\n");
1059                         printf("1. Never automatically expire messages\n");
1060                         printf("2. Expire by message count\n");
1061                         printf("3. Expire by message age\n");
1062                         }
1063                 } while((buf[0]<48)||(buf[0]>51));
1064         expire_mode = buf[0] - 48;
1065
1066         /* ...lunatics and monsters underneath my bed */
1067         if (expire_mode == 2) {
1068                 sprintf(buf, "%d", expire_value);
1069                 strprompt("Keep how many messages online?", buf, 10);
1070                 expire_value = atol(buf);
1071                 }
1072
1073         if (expire_mode == 3) {
1074                 sprintf(buf, "%d", expire_value);
1075                 strprompt("Keep messages for how many days?", buf, 10);
1076                 expire_value = atol(buf);
1077                 }
1078
1079         /* Save it */
1080         snprintf(buf, sizeof buf, "SPEX floor|%d|%d",
1081                 expire_mode, expire_value);
1082         serv_puts(buf);
1083         serv_gets(buf);
1084
1085         snprintf(buf,sizeof buf,"EFLR %d|%s",curr_floor,
1086                  &floorlist[(int)curr_floor][0]);
1087         serv_puts(buf);
1088         serv_gets(buf);
1089         printf("%s\n",&buf[4]);
1090         load_floorlist();
1091         }
1092
1093
1094
1095
1096 /*
1097  * kill the current floor 
1098  */
1099 void kill_floor(void) {
1100         int floornum_to_delete,a;
1101         char buf[256];
1102
1103         if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1104         do {
1105                 floornum_to_delete = (-1);
1106                 printf("(Press return to abort)\n");
1107                 newprompt("Delete which floor? ",buf,255);
1108                 if (strlen(buf)==0) return;
1109                 for (a=0; a<128; ++a)
1110                         if (!strucmp(&floorlist[a][0],buf))
1111                                 floornum_to_delete = a;
1112                 if (floornum_to_delete < 0) {
1113                         printf("No such floor.  Select one of:\n");
1114                         for (a=0; a<128; ++a)
1115                                 if (floorlist[a][0]!=0)
1116                                         printf("%s\n",&floorlist[a][0]);
1117                         }
1118                 } while (floornum_to_delete < 0);
1119         sprintf(buf,"KFLR %d|1",floornum_to_delete);
1120         serv_puts(buf);
1121         serv_gets(buf);
1122         printf("%s\n",&buf[4]);
1123         }