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