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