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