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