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