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