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