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