* Convert room listings and some aide functions to new IPC code
[citadel.git] / citadel / rooms.c
1 /*
2  * $Id$
3  *
4  * 
5  * Client-side functions which perform room operations
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <signal.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/wait.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include "citadel.h"
23 #include "citadel_ipc.h"
24 #include "citadel_decls.h"
25 #include "rooms.h"
26 #include "commands.h"
27 #include "tools.h"
28 #include "messages.h"
29 #ifndef HAVE_SNPRINTF
30 #include "snprintf.h"
31 #endif
32 #include "screen.h"
33
34 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
35
36
37 void sttybbs(int cmd);
38 void hit_any_key(void);
39 void dotgoto(CtdlIPC *ipc, char *towhere, int display_name, int fromungoto);
40 void progress(long int curr, long int cmax);
41 int pattern(char *search, char *patn);
42 int file_checksum(char *filename);
43 int nukedir(char *dirname);
44
45 extern unsigned room_flags;
46 extern char room_name[];
47 extern char temp[];
48 extern char tempdir[];
49 extern int editor_pid;
50 extern char editor_path[];
51 extern int screenwidth;
52 extern int screenheight;
53 extern char fullname[];
54 extern char sigcaught;
55 extern char floor_mode;
56 extern char curr_floor;
57
58
59 extern int ugnum;
60 extern long uglsn;
61 extern char *uglist[];
62 extern long uglistlsn[];
63 extern int uglistsize;
64
65 extern char floorlist[128][SIZ];
66
67
68 void load_floorlist(CtdlIPC *ipc)
69 {
70         int a;
71         char buf[SIZ];
72         char *listing = NULL;
73         int r;                  /* IPC response code */
74
75         for (a = 0; a < 128; ++a)
76                 floorlist[a][0] = 0;
77
78         r = CtdlIPCFloorListing(ipc, &listing, buf);
79         if (r / 100 != 1) {
80                 strcpy(floorlist[0], "Main Floor");
81                 return;
82         }
83         while (*listing && strlen(listing)) {
84                 extract_token(buf, listing, 0, '\n');
85                 remove_token(listing, 0, '\n');
86                 extract(floorlist[extract_int(buf, 0)], buf, 1);
87         }
88         free(listing);
89 }
90
91
92 void room_tree_list(struct roomlisting *rp)
93 {
94         static int c = 0;
95         char rmname[ROOMNAMELEN];
96         int f;
97
98         if (rp == NULL) {
99                 c = 1;
100                 return;
101         }
102
103         if (rp->lnext != NULL) {
104                 room_tree_list(rp->lnext);
105         }
106
107         if (sigcaught == 0) {
108                 strcpy(rmname, rp->rlname);
109                 f = rp->rlflags;
110                 if ((c + strlen(rmname) + 4) > screenwidth) {
111
112                         /* line break, check the paginator */
113                         pprintf("\n");
114                         c = 1;
115                 }
116                 if (f & QR_MAILBOX) {
117                         color(BRIGHT_YELLOW);
118                 } else if (f & QR_PRIVATE) {
119                         color(BRIGHT_RED);
120                 } else {
121                         color(DIM_WHITE);
122                 }
123                 pprintf("%s", rmname);
124                 if ((f & QR_DIRECTORY) && (f & QR_NETWORK))
125                         pprintf("}  ");
126                 else if (f & QR_DIRECTORY)
127                         pprintf("]  ");
128                 else if (f & QR_NETWORK)
129                         pprintf(")  ");
130                 else
131                         pprintf(">  ");
132                 c = c + strlen(rmname) + 3;
133         }
134
135         if (rp->rnext != NULL) {
136                 room_tree_list(rp->rnext);
137         }
138
139         free(rp);
140 }
141
142
143 /* 
144  * Room ordering stuff (compare first by floor, then by order)
145  */
146 int rordercmp(struct roomlisting *r1, struct roomlisting *r2)
147 {
148         if ((r1 == NULL) && (r2 == NULL))
149                 return (0);
150         if (r1 == NULL)
151                 return (-1);
152         if (r2 == NULL)
153                 return (1);
154         if (r1->rlfloor < r2->rlfloor)
155                 return (-1);
156         if (r1->rlfloor > r2->rlfloor)
157                 return (1);
158         if (r1->rlorder < r2->rlorder)
159                 return (-1);
160         if (r1->rlorder > r2->rlorder)
161                 return (1);
162         return (0);
163 }
164
165
166 /*
167  * Common code for all room listings
168  */
169 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, 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[32];
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         long 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, 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_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                 broken = 1;
674         }
675         fclose(tpipe);
676         if (file) free(file);
677
678         if (proto == 0) {
679                 /* FIXME: display internally instead */
680                 snprintf(transmit_cmd, sizeof transmit_cmd,
681                         "SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",
682                         tempname);
683         }
684         else if (proto == 1)
685                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sx %s", tempname);
686         else if (proto == 3)
687                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sb %s", tempname);
688         else if (proto == 4)
689                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sz %s", tempname);
690         else
691                 /* FIXME: display internally instead */
692                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec cat %s", tempname);
693
694         screen_reset();
695         sttybbs(SB_RESTORE);
696         system(transmit_cmd);
697         sttybbs(SB_NO_INTR);
698         screen_set();
699
700         /* clean up the temporary directory */
701         nukedir(tempdir);
702         scr_putc(7);    /* Beep beep! */
703 }
704
705
706 /*
707  * read directory of this room
708  */
709 void roomdir(CtdlIPC *ipc)
710 {
711         char flnm[SIZ];
712         char flsz[32];
713         char comment[SIZ];
714         char buf[SIZ];
715
716         CtdlIPC_putline(ipc, "RDIR");
717         CtdlIPC_getline(ipc, buf);
718         if (buf[0] != '1') {
719                 pprintf("%s\n", &buf[4]);
720                 return;
721         }
722
723         extract(comment, &buf[4], 0);
724         extract(flnm, &buf[4], 1);
725         pprintf("\nDirectory of %s on %s\n", flnm, comment);
726         pprintf("-----------------------\n");
727         while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
728                 extract(flnm, buf, 0);
729                 extract(flsz, buf, 1);
730                 extract(comment, buf, 2);
731                 if (strlen(flnm) <= 14)
732                         pprintf("%-14s %8s %s\n", flnm, flsz, comment);
733                 else
734                         pprintf("%s\n%14s %8s %s\n", flnm, "", flsz,
735                                 comment);
736         }
737 }
738
739
740 /*
741  * add a user to a private room
742  */
743 void invite(CtdlIPC *ipc)
744 {
745         char username[USERNAME_SIZE];
746         char buf[SIZ];
747         int r;                          /* IPC response code */
748
749         newprompt("Name of user? ", username, USERNAME_SIZE);
750         if (username[0] == 0)
751                 return;
752
753         r = CtdlIPCInviteUserToRoom(ipc, username, buf);
754         scr_printf("%s\n", buf);
755 }
756
757
758 /*
759  * kick a user out of a room
760  */
761 void kickout(CtdlIPC *ipc)
762 {
763         char username[USERNAME_SIZE];
764         char buf[SIZ];
765         int r;                          /* IPC response code */
766
767         newprompt("Name of user? ", username, USERNAME_SIZE);
768         if (username[0] == 0)
769                 return;
770
771         r = CtdlIPCKickoutUserFromRoom(ipc, username, buf);
772         scr_printf("%s\n", buf);
773 }
774
775
776 /*
777  * aide command: kill the current room
778  */
779 void killroom(CtdlIPC *ipc)
780 {
781         char aaa[100];
782         int r;
783
784         r = CtdlIPCDeleteRoom(ipc, 0, aaa);
785         if (r / 100 != 2) {
786                 scr_printf("%s\n", aaa);
787                 return;
788         }
789
790         scr_printf("Are you sure you want to kill this room? ");
791         if (yesno() == 0)
792                 return;
793
794         r = CtdlIPCDeleteRoom(ipc, 1, aaa);
795         scr_printf("%s\n", aaa);
796         if (r / 100 != 2)
797                 return;
798         dotgoto(ipc, "_BASEROOM_", 0, 0);
799 }
800
801 void forget(CtdlIPC *ipc)
802 {                               /* forget the current room */
803         char buf[SIZ];
804
805         scr_printf("Are you sure you want to forget this room? ");
806         if (yesno() == 0)
807                 return;
808
809         if (CtdlIPCForgetRoom(ipc, buf) / 100 != 2) {
810                 scr_printf("%s\n", buf);
811                 return;
812         }
813
814         /* now return to the lobby */
815         dotgoto(ipc, "_BASEROOM_", 0, 0);
816 }
817
818
819 /*
820  * create a new room
821  */
822 void entroom(CtdlIPC *ipc)
823 {
824         char buf[SIZ];
825         char new_room_name[ROOMNAMELEN];
826         int new_room_type;
827         char new_room_pass[10];
828         int new_room_floor;
829         int a, b;
830         int r;                          /* IPC response code */
831
832         /* Check permission to create room */
833         r = CtdlIPCCreateRoom(ipc, 0, "", 1, "", 0, buf);
834         if (r / 100 != 2) {
835                 scr_printf("%s\n", buf);
836                 return;
837         }
838
839         newprompt("Name for new room? ", new_room_name, ROOMNAMELEN - 1);
840         if (strlen(new_room_name) == 0) {
841                 return;
842         }
843         for (a = 0; a < strlen(new_room_name); ++a) {
844                 if (new_room_name[a] == '|') {
845                         new_room_name[a] = '_';
846                 }
847         }
848
849         new_room_floor = select_floor(ipc, (int) curr_floor);
850
851         IFNEXPERT formout(ipc, "roomaccess");
852         do {
853                 scr_printf("<?>Help\n<1>Public room\n<2>Guess-name room\n"
854                        "<3>Passworded room\n<4>Invitation-only room\n"
855                        "<5>Personal room\n"
856                         "Enter room type: ");
857                 do {
858                         b = inkey();
859                 } while (((b < '1') || (b > '5')) && (b != '?'));
860                 if (b == '?') {
861                         scr_printf("?\n");
862                         formout(ipc, "roomaccess");
863                 }
864         } while ((b < '1') || (b > '5'));
865         b -= '0';                       /* Portable */
866         scr_printf("%d\n", b);
867         new_room_type = b - 1;
868         if (new_room_type == 2) {
869                 newprompt("Enter a room password: ", new_room_pass, 9);
870                 for (a = 0; a < strlen(new_room_pass); ++a)
871                         if (new_room_pass[a] == '|')
872                                 new_room_pass[a] = '_';
873         } else {
874                 strcpy(new_room_pass, "");
875         }
876
877         scr_printf("\042%s\042, a", new_room_name);
878         if (b == 1)
879                 scr_printf(" public room.");
880         if (b == 2)
881                 scr_printf(" guess-name room.");
882         if (b == 3)
883                 scr_printf(" passworded room, password: %s", new_room_pass);
884         if (b == 4)
885                 scr_printf("n invitation-only room.");
886         if (b == 5)
887                 scr_printf(" personal room.");
888         scr_printf("\nInstall it? (y/n) : ");
889         if (yesno() == 0) {
890                 return;
891         }
892
893         r = CtdlIPCCreateRoom(ipc, 1, new_room_name, new_room_type,
894                               new_room_pass, new_room_floor, buf);
895         if (r / 100 != 2) {
896                 scr_printf("%s\n", buf);
897                 return;
898         }
899
900         /* command succeeded... now GO to the new room! */
901         dotgoto(ipc, new_room_name, 0, 0);
902 }
903
904
905
906 void readinfo(CtdlIPC *ipc)
907 {                               /* read info file for current room */
908         char buf[SIZ];
909         char raide[64];
910         int r;                  /* IPC response code */
911         char *text = NULL;
912
913         /* Name of currernt room aide */
914         r = CtdlIPCGetRoomAide(ipc, buf);
915         if (r / 100 == 2)
916                 safestrncpy(raide, buf, sizeof raide);
917         else
918                 strcpy(raide, "");
919
920         if (strlen(raide) > 0)
921                 scr_printf("Room aide is %s.\n\n", raide);
922
923         r = CtdlIPCRoomInfo(ipc, &text, buf);
924         if (r / 100 != 1)
925                 return;
926
927         if (text) {
928                 fmout(screenwidth, NULL, text, NULL,
929                       ((userflags & US_PAGINATOR) ? 1 : 0), screenheight, 
930                       (*raide) ? 2 : 0, 1);
931                 free(text);
932         }
933 }
934
935
936 /*
937  * <W>ho knows room...
938  */
939 void whoknows(CtdlIPC *ipc)
940 {
941         char buf[SIZ];
942         CtdlIPC_putline(ipc, "WHOK");
943         CtdlIPC_getline(ipc, buf);
944         if (buf[0] != '1') {
945                 pprintf("%s\n", &buf[4]);
946                 return;
947         }
948         while (CtdlIPC_getline(ipc, buf), strncmp(buf, "000", 3)) {
949                 if (sigcaught == 0)
950                         pprintf("%s\n", buf);
951         }
952 }
953
954
955 void do_edit(CtdlIPC *ipc,
956                 char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
957 {
958         FILE *fp;
959         char cmd[SIZ];
960         int b, cksum, editor_exit;
961
962
963         if (strlen(editor_path) == 0) {
964                 scr_printf("Do you wish to re-enter %s? ", desc);
965                 if (yesno() == 0)
966                         return;
967         }
968
969         fp = fopen(temp, "w");
970         fclose(fp);
971
972         CtdlIPC_putline(ipc, check_cmd);
973         CtdlIPC_getline(ipc, cmd);
974         if (cmd[0] != '2') {
975                 scr_printf("%s\n", &cmd[4]);
976                 return;
977         }
978
979         if (strlen(editor_path) > 0) {
980                 CtdlIPC_putline(ipc, read_cmd);
981                 CtdlIPC_getline(ipc, cmd);
982                 if (cmd[0] == '1') {
983                         fp = fopen(temp, "w");
984                         while (CtdlIPC_getline(ipc, cmd), strcmp(cmd, "000")) {
985                                 fprintf(fp, "%s\n", cmd);
986                         }
987                         fclose(fp);
988                 }
989         }
990
991         cksum = file_checksum(temp);
992
993         if (strlen(editor_path) > 0) {
994                 char tmp[SIZ];
995
996                 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", desc);
997                 putenv(tmp);
998                 editor_pid = fork();
999                 if (editor_pid == 0) {
1000                         chmod(temp, 0600);
1001                         screen_reset();
1002                         sttybbs(SB_RESTORE);
1003                         execlp(editor_path, editor_path, temp, NULL);
1004                         exit(1);
1005                 }
1006                 if (editor_pid > 0)
1007                         do {
1008                                 editor_exit = 0;
1009                                 b = wait(&editor_exit);
1010                         } while ((b != editor_pid) && (b >= 0));
1011                 editor_pid = (-1);
1012                 scr_printf("Executed %s\n", editor_path);
1013                 sttybbs(0);
1014                 screen_set();
1015         } else {
1016                 scr_printf("Entering %s.  "
1017                         "Press return twice when finished.\n", desc);
1018                 fp = fopen(temp, "r+");
1019                 citedit(ipc, fp);
1020                 fclose(fp);
1021         }
1022
1023         if (file_checksum(temp) == cksum) {
1024                 scr_printf("*** Aborted.\n");
1025         }
1026
1027         else {
1028                 CtdlIPC_putline(ipc, write_cmd);
1029                 CtdlIPC_getline(ipc, cmd);
1030                 if (cmd[0] != '4') {
1031                         scr_printf("%s\n", &cmd[4]);
1032                         return;
1033                 }
1034
1035                 fp = fopen(temp, "r");
1036                 while (fgets(cmd, SIZ - 1, fp) != NULL) {
1037                         cmd[strlen(cmd) - 1] = 0;
1038                         CtdlIPC_putline(ipc, cmd);
1039                 }
1040                 fclose(fp);
1041                 CtdlIPC_putline(ipc, "000");
1042         }
1043
1044         unlink(temp);
1045 }
1046
1047
1048 void enterinfo(CtdlIPC *ipc)
1049 {                               /* edit info file for current room */
1050         do_edit(ipc, "the Info file for this room", "RINF", "EINF 0", "EINF 1");
1051 }
1052
1053 void enter_bio(CtdlIPC *ipc)
1054 {
1055         char cmd[SIZ];
1056         snprintf(cmd, sizeof cmd, "RBIO %s", fullname);
1057         do_edit(ipc, "your Bio", cmd, "NOOP", "EBIO");
1058 }
1059
1060 /*
1061  * create a new floor
1062  */
1063 void create_floor(CtdlIPC *ipc)
1064 {
1065         char buf[SIZ];
1066         char newfloorname[SIZ];
1067         int r;                  /* IPC response code */
1068
1069         load_floorlist(ipc);
1070
1071         r = CtdlIPCCreateFloor(ipc, 0, "", buf);
1072         if (r / 100 != 2) {
1073                 scr_printf("%s\n", buf);
1074                 return;
1075         }
1076
1077         newprompt("Name for new floor: ", newfloorname, 255);
1078         r = CtdlIPCCreateFloor(ipc, 1, newfloorname, buf);
1079         if (r / 100 == 2) {
1080                 scr_printf("Floor has been created.\n");
1081         } else {
1082                 scr_printf("%s\n", buf);
1083         }
1084
1085         load_floorlist(ipc);
1086 }
1087
1088 /*
1089  * edit the current floor
1090  */
1091 void edit_floor(CtdlIPC *ipc)
1092 {
1093         char buf[SIZ];
1094         struct ExpirePolicy *ep = NULL;
1095         int r;                          /* IPC response code */
1096
1097         load_floorlist(ipc);
1098
1099         /* Fetch the expire policy (this will silently fail on old servers,
1100          * resulting in "default" policy)
1101          */
1102         r = CtdlIPCGetMessageExpirationPolicy(ipc, 1, &ep, buf);
1103
1104         /* Interact with the user */
1105         scr_printf("You are editing the floor called \"%s\"\n", 
1106                 &floorlist[(int) curr_floor][0] );
1107         strprompt("Floor name", &floorlist[(int) curr_floor][0], 255);
1108
1109         /* Angels and demons dancing in my head... */
1110         do {
1111                 snprintf(buf, sizeof buf, "%d", ep->expire_mode);
1112                 strprompt
1113                     ("Floor default message expire policy (? for list)",
1114                      buf, 1);
1115                 if (buf[0] == '?') {
1116                         scr_printf("\n"
1117                                 "0. Use the system default\n"
1118                                 "1. Never automatically expire messages\n"
1119                                 "2. Expire by message count\n"
1120                                 "3. Expire by message age\n");
1121                 }
1122         } while ((buf[0] < '0') || (buf[0] > '3'));
1123         ep->expire_mode = buf[0] - '0';
1124
1125         /* ...lunatics and monsters underneath my bed */
1126         if (ep->expire_mode == 2) {
1127                 snprintf(buf, sizeof buf, "%d", ep->expire_value);
1128                 strprompt("Keep how many messages online?", buf, 10);
1129                 ep->expire_value = atol(buf);
1130         }
1131
1132         if (ep->expire_mode == 3) {
1133                 snprintf(buf, sizeof buf, "%d", ep->expire_value);
1134                 strprompt("Keep messages for how many days?", buf, 10);
1135                 ep->expire_value = atol(buf);
1136         }
1137
1138         /* Save it */
1139         r = CtdlIPCSetMessageExpirationPolicy(ipc, 1, ep, buf);
1140         r = CtdlIPCEditFloor(ipc, curr_floor, &floorlist[(int)curr_floor][0], buf);
1141         scr_printf("%s\n", buf);
1142         load_floorlist(ipc);
1143 }
1144
1145
1146
1147
1148 /*
1149  * kill the current floor 
1150  */
1151 void kill_floor(CtdlIPC *ipc)
1152 {
1153         int floornum_to_delete, a;
1154         char buf[SIZ];
1155
1156         load_floorlist(ipc);
1157         do {
1158                 floornum_to_delete = (-1);
1159                 scr_printf("(Press return to abort)\n");
1160                 newprompt("Delete which floor? ", buf, 255);
1161                 if (strlen(buf) == 0)
1162                         return;
1163                 for (a = 0; a < 128; ++a)
1164                         if (!strcasecmp(&floorlist[a][0], buf))
1165                                 floornum_to_delete = a;
1166                 if (floornum_to_delete < 0) {
1167                         scr_printf("No such floor.  Select one of:\n");
1168                         for (a = 0; a < 128; ++a)
1169                                 if (floorlist[a][0] != 0)
1170                                         scr_printf("%s\n", &floorlist[a][0]);
1171                 }
1172         } while (floornum_to_delete < 0);
1173         CtdlIPCDeleteFloor(ipc, 1, floornum_to_delete, buf);
1174         scr_printf("%s\n", buf);
1175         load_floorlist(ipc);
1176 }