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