]> code.citadel.org Git - citadel.git/blob - citadel/rooms.c
* Don't run the wait indicator when in an external editor
[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
566         if (uglistsize == 0)
567                 return;
568
569         snprintf(buf, sizeof buf, "GOTO %s", uglist[uglistsize-1]); 
570         CtdlIPC_putline(ipc, buf);
571         CtdlIPC_getline(ipc, buf);
572         if (buf[0] != '2') {
573                 scr_printf("%s\n", &buf[4]);
574                 return;
575         }
576         snprintf(buf, sizeof buf, "SLRP %ld", uglistlsn[uglistsize-1]); 
577         CtdlIPC_putline(ipc, buf);
578         CtdlIPC_getline(ipc, buf);
579         if (buf[0] != '2') {
580                 scr_printf("%s\n", &buf[4]);
581         }
582     safestrncpy (buf, uglist[uglistsize-1], sizeof(buf));
583     uglistsize--;
584     free(uglist[uglistsize]);
585         /* Don't queue ungoto info or we end up in a loop */
586         dotgoto(ipc, buf, 0, 1);
587 }
588
589
590 /*
591  * saves filelen bytes from file at pathname
592  */
593 int save_buffer(void *file, size_t filelen, const char *pathname)
594 {
595         size_t block = 0;
596         size_t bytes_written = 0;
597         FILE *fp;
598
599         fp = fopen(pathname, "w");
600         if (!fp) {
601                 err_printf("Cannot open '%s': %s\n", pathname, strerror(errno));
602                 return 0;
603         }
604         do {
605                 block = fwrite(file + bytes_written, 1,
606                                 filelen - bytes_written, fp);
607                 bytes_written += block;
608         } while (errno == EINTR && bytes_written < filelen);
609         fclose(fp);
610
611         if (bytes_written < filelen) {
612                 err_printf("Trouble saving '%s': %s\n", pathname,
613                                 strerror(errno));
614                 return 0;
615         }
616         return 1;
617 }
618
619
620 /*
621  * Save supplied_filename in dest directory; gets the name only
622  */
623 void destination_directory(char *dest, const char *supplied_filename)
624 {
625         scr_printf("Enter the name of the directory to save '%s'\n"
626                 "to, or press return for the current directory.\n",
627                 supplied_filename);
628         newprompt("Directory: ", dest, PATH_MAX);
629         if (strlen(dest) == 0) {
630                 dest[0] = '.';
631                 dest[1] = 0;
632         }
633         strcat(dest, "/");
634         strcat(dest, supplied_filename);
635 }
636
637
638 /*
639  * download()  -  download a file or files.  The argument passed to this
640  *                function determines which protocol to use.
641  *  proto - 0 = paginate, 1 = xmodem, 2 = raw, 3 = ymodem, 4 = zmodem, 5 = save
642  */
643 void download(CtdlIPC *ipc, int proto)
644 {
645         char buf[SIZ];
646         char filename[PATH_MAX];
647         char tempname[PATH_MAX];
648         char transmit_cmd[SIZ];
649         FILE *tpipe = NULL;
650         int broken = 0;
651         int r;
652         void *file = NULL;      /* The downloaded file */
653         size_t filelen = 0L;    /* The downloaded file length */
654
655         if ((room_flags & QR_DOWNLOAD) == 0) {
656                 scr_printf("*** You cannot download from this room.\n");
657                 return;
658         }
659
660         newprompt("Enter filename: ", filename, PATH_MAX);
661
662         /* Save to local disk, for folks with their own copy of the client */
663         if (proto == 5) {
664                 destination_directory(tempname, filename);
665                 r = CtdlIPCFileDownload(ipc, filename, &file, 0, progress, buf);
666                 if (r / 100 != 2) {
667                         scr_printf("%s\n", buf);
668                         return;
669                 }
670                 save_buffer(file, (size_t)extract_long(buf, 0), tempname);
671                 free(file);
672                 return;
673         }
674
675         r = CtdlIPCFileDownload(ipc, filename, &file, 0, progress, buf);
676         if (r / 100 != 2) {
677                 scr_printf("%s\n", buf);
678                 return;
679         }
680         filelen = extract_unsigned_long(buf, 0);
681
682         /* Meta-download for public clients */
683         /* scr_printf("Fetching file from Citadel server...\n"); */
684         mkdir(tempdir, 0700);
685         snprintf(tempname, sizeof tempname, "%s/%s", tempdir, filename);
686         tpipe = fopen(tempname, "wb");
687         if (fwrite(file, filelen, 1, tpipe) < filelen) {
688                 /* FIXME: restart syscall on EINTR */
689                 broken = 1;
690         }
691         fclose(tpipe);
692         if (file) free(file);
693
694         if (proto == 0) {
695                 /* FIXME: display internally instead */
696                 snprintf(transmit_cmd, sizeof transmit_cmd,
697                         "SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",
698                         tempname);
699         }
700         else if (proto == 1)
701                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sx %s", tempname);
702         else if (proto == 3)
703                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sb %s", tempname);
704         else if (proto == 4)
705                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sz %s", tempname);
706         else
707                 /* FIXME: display internally instead */
708                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec cat %s", tempname);
709
710         screen_reset();
711         sttybbs(SB_RESTORE);
712         system(transmit_cmd);
713         sttybbs(SB_NO_INTR);
714         screen_set();
715
716         /* clean up the temporary directory */
717         nukedir(tempdir);
718         scr_putc(7);    /* Beep beep! */
719 }
720
721
722 /*
723  * read directory of this room
724  */
725 void roomdir(CtdlIPC *ipc)
726 {
727         char flnm[SIZ];
728         char flsz[32];
729         char comment[SIZ];
730         char buf[SIZ];
731
732         CtdlIPC_putline(ipc, "RDIR");
733         CtdlIPC_getline(ipc, buf);
734         if (buf[0] != '1') {
735                 pprintf("%s\n", &buf[4]);
736                 return;
737         }
738
739         extract(comment, &buf[4], 0);
740         extract(flnm, &buf[4], 1);
741         pprintf("\nDirectory of %s on %s\n", flnm, comment);
742         pprintf("-----------------------\n");
743         while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
744                 extract(flnm, buf, 0);
745                 extract(flsz, buf, 1);
746                 extract(comment, buf, 2);
747                 if (strlen(flnm) <= 14)
748                         pprintf("%-14s %8s %s\n", flnm, flsz, comment);
749                 else
750                         pprintf("%s\n%14s %8s %s\n", flnm, "", flsz,
751                                 comment);
752         }
753 }
754
755
756 /*
757  * add a user to a private room
758  */
759 void invite(CtdlIPC *ipc)
760 {
761         char username[USERNAME_SIZE];
762         char buf[SIZ];
763         int r;                          /* IPC response code */
764
765         newprompt("Name of user? ", username, USERNAME_SIZE);
766         if (username[0] == 0)
767                 return;
768
769         r = CtdlIPCInviteUserToRoom(ipc, username, buf);
770         scr_printf("%s\n", buf);
771 }
772
773
774 /*
775  * kick a user out of a room
776  */
777 void kickout(CtdlIPC *ipc)
778 {
779         char username[USERNAME_SIZE];
780         char buf[SIZ];
781         int r;                          /* IPC response code */
782
783         newprompt("Name of user? ", username, USERNAME_SIZE);
784         if (username[0] == 0)
785                 return;
786
787         r = CtdlIPCKickoutUserFromRoom(ipc, username, buf);
788         scr_printf("%s\n", buf);
789 }
790
791
792 /*
793  * aide command: kill the current room
794  */
795 void killroom(CtdlIPC *ipc)
796 {
797         char aaa[100];
798         int r;
799
800         r = CtdlIPCDeleteRoom(ipc, 0, aaa);
801         if (r / 100 != 2) {
802                 scr_printf("%s\n", aaa);
803                 return;
804         }
805
806         scr_printf("Are you sure you want to kill this room? ");
807         if (yesno() == 0)
808                 return;
809
810         r = CtdlIPCDeleteRoom(ipc, 1, aaa);
811         scr_printf("%s\n", aaa);
812         if (r / 100 != 2)
813                 return;
814         dotgoto(ipc, "_BASEROOM_", 0, 0);
815 }
816
817 void forget(CtdlIPC *ipc)
818 {                               /* forget the current room */
819         char buf[SIZ];
820
821         scr_printf("Are you sure you want to forget this room? ");
822         if (yesno() == 0)
823                 return;
824
825         if (CtdlIPCForgetRoom(ipc, buf) / 100 != 2) {
826                 scr_printf("%s\n", buf);
827                 return;
828         }
829
830         /* now return to the lobby */
831         dotgoto(ipc, "_BASEROOM_", 0, 0);
832 }
833
834
835 /*
836  * create a new room
837  */
838 void entroom(CtdlIPC *ipc)
839 {
840         char buf[SIZ];
841         char new_room_name[ROOMNAMELEN];
842         int new_room_type;
843         char new_room_pass[10];
844         int new_room_floor;
845         int a, b;
846         int r;                          /* IPC response code */
847
848         /* Check permission to create room */
849         r = CtdlIPCCreateRoom(ipc, 0, "", 1, "", 0, buf);
850         if (r / 100 != 2) {
851                 scr_printf("%s\n", buf);
852                 return;
853         }
854
855         newprompt("Name for new room? ", new_room_name, ROOMNAMELEN - 1);
856         if (strlen(new_room_name) == 0) {
857                 return;
858         }
859         for (a = 0; a < strlen(new_room_name); ++a) {
860                 if (new_room_name[a] == '|') {
861                         new_room_name[a] = '_';
862                 }
863         }
864
865         new_room_floor = select_floor(ipc, (int) curr_floor);
866
867         IFNEXPERT formout(ipc, "roomaccess");
868         do {
869                 scr_printf("<?>Help\n<1>Public room\n<2>Guess-name room\n"
870                        "<3>Passworded room\n<4>Invitation-only room\n"
871                        "<5>Personal room\n"
872                         "Enter room type: ");
873                 do {
874                         b = inkey();
875                 } while (((b < '1') || (b > '5')) && (b != '?'));
876                 if (b == '?') {
877                         scr_printf("?\n");
878                         formout(ipc, "roomaccess");
879                 }
880         } while ((b < '1') || (b > '5'));
881         b -= '0';                       /* Portable */
882         scr_printf("%d\n", b);
883         new_room_type = b - 1;
884         if (new_room_type == 2) {
885                 newprompt("Enter a room password: ", new_room_pass, 9);
886                 for (a = 0; a < strlen(new_room_pass); ++a)
887                         if (new_room_pass[a] == '|')
888                                 new_room_pass[a] = '_';
889         } else {
890                 strcpy(new_room_pass, "");
891         }
892
893         scr_printf("\042%s\042, a", new_room_name);
894         if (b == 1)
895                 scr_printf(" public room.");
896         if (b == 2)
897                 scr_printf(" guess-name room.");
898         if (b == 3)
899                 scr_printf(" passworded room, password: %s", new_room_pass);
900         if (b == 4)
901                 scr_printf("n invitation-only room.");
902         if (b == 5)
903                 scr_printf(" personal room.");
904         scr_printf("\nInstall it? (y/n) : ");
905         if (yesno() == 0) {
906                 return;
907         }
908
909         r = CtdlIPCCreateRoom(ipc, 1, new_room_name, new_room_type,
910                               new_room_pass, new_room_floor, buf);
911         if (r / 100 != 2) {
912                 scr_printf("%s\n", buf);
913                 return;
914         }
915
916         /* command succeeded... now GO to the new room! */
917         dotgoto(ipc, new_room_name, 0, 0);
918 }
919
920
921
922 void readinfo(CtdlIPC *ipc)
923 {                               /* read info file for current room */
924         char buf[SIZ];
925         char raide[64];
926         int r;                  /* IPC response code */
927         char *text = NULL;
928
929         /* Name of currernt room aide */
930         r = CtdlIPCGetRoomAide(ipc, buf);
931         if (r / 100 == 2)
932                 safestrncpy(raide, buf, sizeof raide);
933         else
934                 strcpy(raide, "");
935
936         if (strlen(raide) > 0)
937                 scr_printf("Room aide is %s.\n\n", raide);
938
939         r = CtdlIPCRoomInfo(ipc, &text, buf);
940         if (r / 100 != 1)
941                 return;
942
943         if (text) {
944                 fmout(screenwidth, NULL, text, NULL,
945                       ((userflags & US_PAGINATOR) ? 1 : 0), screenheight, 
946                       (*raide) ? 2 : 0, 1);
947                 free(text);
948         }
949 }
950
951
952 /*
953  * <W>ho knows room...
954  */
955 void whoknows(CtdlIPC *ipc)
956 {
957         char buf[SIZ];
958         CtdlIPC_putline(ipc, "WHOK");
959         CtdlIPC_getline(ipc, buf);
960         if (buf[0] != '1') {
961                 pprintf("%s\n", &buf[4]);
962                 return;
963         }
964         while (CtdlIPC_getline(ipc, buf), strncmp(buf, "000", 3)) {
965                 if (sigcaught == 0)
966                         pprintf("%s\n", buf);
967         }
968 }
969
970
971 void do_edit(CtdlIPC *ipc,
972                 char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
973 {
974         FILE *fp;
975         char cmd[SIZ];
976         int b, cksum, editor_exit;
977
978         if (strlen(editor_path) == 0) {
979                 scr_printf("Do you wish to re-enter %s? ", desc);
980                 if (yesno() == 0)
981                         return;
982         }
983
984         fp = fopen(temp, "w");
985         fclose(fp);
986
987         CtdlIPC_putline(ipc, check_cmd);
988         CtdlIPC_getline(ipc, cmd);
989         if (cmd[0] != '2') {
990                 scr_printf("%s\n", &cmd[4]);
991                 return;
992         }
993
994         if (strlen(editor_path) > 0) {
995                 CtdlIPC_putline(ipc, read_cmd);
996                 CtdlIPC_getline(ipc, cmd);
997                 if (cmd[0] == '1') {
998                         fp = fopen(temp, "w");
999                         while (CtdlIPC_getline(ipc, cmd), strcmp(cmd, "000")) {
1000                                 fprintf(fp, "%s\n", cmd);
1001                         }
1002                         fclose(fp);
1003                 }
1004         }
1005
1006         cksum = file_checksum(temp);
1007
1008         if (strlen(editor_path) > 0) {
1009                 char tmp[SIZ];
1010
1011                 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", desc);
1012                 putenv(tmp);
1013                 screen_reset();
1014                 sttybbs(SB_RESTORE);
1015                 editor_pid = fork();
1016                 if (editor_pid == 0) {
1017                         chmod(temp, 0600);
1018                         execlp(editor_path, editor_path, temp, NULL);
1019                         exit(1);
1020                 }
1021                 if (editor_pid > 0)
1022                         do {
1023                                 editor_exit = 0;
1024                                 b = ka_wait(&editor_exit);
1025                         } while ((b != editor_pid) && (b >= 0));
1026                 editor_pid = (-1);
1027                 scr_printf("Executed %s\n", editor_path);
1028                 sttybbs(0);
1029                 screen_set();
1030         } else {
1031                 scr_printf("Entering %s.  "
1032                         "Press return twice when finished.\n", desc);
1033                 fp = fopen(temp, "r+");
1034                 citedit(ipc, fp);
1035                 fclose(fp);
1036         }
1037
1038         if (file_checksum(temp) == cksum) {
1039                 scr_printf("*** Aborted.\n");
1040         }
1041
1042         else {
1043                 CtdlIPC_putline(ipc, write_cmd);
1044                 CtdlIPC_getline(ipc, cmd);
1045                 if (cmd[0] != '4') {
1046                         scr_printf("%s\n", &cmd[4]);
1047                         return;
1048                 }
1049
1050                 fp = fopen(temp, "r");
1051                 while (fgets(cmd, SIZ - 1, fp) != NULL) {
1052                         cmd[strlen(cmd) - 1] = 0;
1053                         CtdlIPC_putline(ipc, cmd);
1054                 }
1055                 fclose(fp);
1056                 CtdlIPC_putline(ipc, "000");
1057         }
1058
1059         unlink(temp);
1060 }
1061
1062
1063 void enterinfo(CtdlIPC *ipc)
1064 {                               /* edit info file for current room */
1065         do_edit(ipc, "the Info file for this room", "RINF", "EINF 0", "EINF 1");
1066 }
1067
1068 void enter_bio(CtdlIPC *ipc)
1069 {
1070         char cmd[SIZ];
1071         snprintf(cmd, sizeof cmd, "RBIO %s", fullname);
1072         do_edit(ipc, "your Bio", cmd, "NOOP", "EBIO");
1073 }
1074
1075 /*
1076  * create a new floor
1077  */
1078 void create_floor(CtdlIPC *ipc)
1079 {
1080         char buf[SIZ];
1081         char newfloorname[SIZ];
1082         int r;                  /* IPC response code */
1083
1084         load_floorlist(ipc);
1085
1086         r = CtdlIPCCreateFloor(ipc, 0, "", buf);
1087         if (r / 100 != 2) {
1088                 scr_printf("%s\n", buf);
1089                 return;
1090         }
1091
1092         newprompt("Name for new floor: ", newfloorname, 255);
1093         if (!*newprompt) return;
1094         r = CtdlIPCCreateFloor(ipc, 1, newfloorname, buf);
1095         if (r / 100 == 2) {
1096                 scr_printf("Floor has been created.\n");
1097         } else {
1098                 scr_printf("%s\n", buf);
1099         }
1100
1101         load_floorlist(ipc);
1102 }
1103
1104 /*
1105  * edit the current floor
1106  */
1107 void edit_floor(CtdlIPC *ipc)
1108 {
1109         char buf[SIZ];
1110         struct ExpirePolicy *ep = NULL;
1111         int r;                          /* IPC response code */
1112
1113         load_floorlist(ipc);
1114
1115         /* Fetch the expire policy (this will silently fail on old servers,
1116          * resulting in "default" policy)
1117          */
1118         r = CtdlIPCGetMessageExpirationPolicy(ipc, 1, &ep, buf);
1119
1120         /* Interact with the user */
1121         scr_printf("You are editing the floor called \"%s\"\n", 
1122                 &floorlist[(int) curr_floor][0] );
1123         strprompt("Floor name", &floorlist[(int) curr_floor][0], 255);
1124
1125         /* Angels and demons dancing in my head... */
1126         do {
1127                 snprintf(buf, sizeof buf, "%d", ep->expire_mode);
1128                 strprompt
1129                     ("Floor default message expire policy (? for list)",
1130                      buf, 1);
1131                 if (buf[0] == '?') {
1132                         scr_printf("\n"
1133                                 "0. Use the system default\n"
1134                                 "1. Never automatically expire messages\n"
1135                                 "2. Expire by message count\n"
1136                                 "3. Expire by message age\n");
1137                 }
1138         } while ((buf[0] < '0') || (buf[0] > '3'));
1139         ep->expire_mode = buf[0] - '0';
1140
1141         /* ...lunatics and monsters underneath my bed */
1142         if (ep->expire_mode == 2) {
1143                 snprintf(buf, sizeof buf, "%d", ep->expire_value);
1144                 strprompt("Keep how many messages online?", buf, 10);
1145                 ep->expire_value = atol(buf);
1146         }
1147
1148         if (ep->expire_mode == 3) {
1149                 snprintf(buf, sizeof buf, "%d", ep->expire_value);
1150                 strprompt("Keep messages for how many days?", buf, 10);
1151                 ep->expire_value = atol(buf);
1152         }
1153
1154         /* Save it */
1155         r = CtdlIPCSetMessageExpirationPolicy(ipc, 1, ep, buf);
1156         r = CtdlIPCEditFloor(ipc, curr_floor, &floorlist[(int)curr_floor][0], buf);
1157         scr_printf("%s\n", buf);
1158         load_floorlist(ipc);
1159 }
1160
1161
1162
1163
1164 /*
1165  * kill the current floor 
1166  */
1167 void kill_floor(CtdlIPC *ipc)
1168 {
1169         int floornum_to_delete, a;
1170         char buf[SIZ];
1171
1172         load_floorlist(ipc);
1173         do {
1174                 floornum_to_delete = (-1);
1175                 scr_printf("(Press return to abort)\n");
1176                 newprompt("Delete which floor? ", buf, 255);
1177                 if (strlen(buf) == 0)
1178                         return;
1179                 for (a = 0; a < 128; ++a)
1180                         if (!strcasecmp(&floorlist[a][0], buf))
1181                                 floornum_to_delete = a;
1182                 if (floornum_to_delete < 0) {
1183                         scr_printf("No such floor.  Select one of:\n");
1184                         for (a = 0; a < 128; ++a)
1185                                 if (floorlist[a][0] != 0)
1186                                         scr_printf("%s\n", &floorlist[a][0]);
1187                 }
1188         } while (floornum_to_delete < 0);
1189         CtdlIPCDeleteFloor(ipc, 1, floornum_to_delete, buf);
1190         scr_printf("%s\n", buf);
1191         load_floorlist(ipc);
1192 }