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