* Holy war on strlen: use IsEmptyStr where apropriate.
[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 #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->QRflags = set_room_attr(ipc, attr->QRflags,
600                                "Automatically make all messages anonymous",
601                                QR_ANONONLY);
602         if ((attr->QRflags & QR_ANONONLY) == 0) {
603                 attr->QRflags = set_room_attr(ipc, attr->QRflags,
604                                        "Ask users whether to make messages anonymous",
605                                        QR_ANONOPT);
606         }
607         attr->QRorder = intprompt("Listing order", attr->QRorder, 0, 127);
608
609         /* Ask about the room aide */
610         do {
611                 strprompt("Room aide (or 'none')", raide, 29);
612                 if (!strcasecmp(raide, "none")) {
613                         strcpy(raide, "");
614                         break;
615                 } else {
616                         r = CtdlIPCQueryUsername(ipc, raide, buf);
617                         if (r / 100 != 2)
618                                 scr_printf("%s\n", buf);
619                 }
620         } while (r / 100 != 2);
621
622         /* FIXME: Duplicate code??? */
623         if (!strcasecmp(raide, "none")) {
624                 strcpy(raide, "");
625         }
626
627         /* Angels and demons dancing in my head... */
628         do {
629                 snprintf(buf, sizeof buf, "%d", attr->QRep.expire_mode);
630                 strprompt("Message expire policy (? for list)", buf, 1);
631                 if (buf[0] == '?') {
632                         scr_printf("\n"
633                                 "0. Use the default for this floor\n"
634                                 "1. Never automatically expire messages\n"
635                                 "2. Expire by message count\n"
636                                 "3. Expire by message age\n");
637                 }
638         } while ((buf[0] < 48) || (buf[0] > 51));
639         attr->QRep.expire_mode = buf[0] - 48;
640
641         /* ...lunatics and monsters underneath my bed */
642         if (attr->QRep.expire_mode == 2) {
643                 snprintf(buf, sizeof buf, "%d", attr->QRep.expire_value);
644                 strprompt("Keep how many messages online?", buf, 10);
645                 attr->QRep.expire_value = atol(buf);
646         }
647
648         if (attr->QRep.expire_mode == 3) {
649                 snprintf(buf, sizeof buf, "%d", attr->QRep.expire_value);
650                 strprompt("Keep messages for how many days?", buf, 10);
651                 attr->QRep.expire_value = atol(buf);
652         }
653
654         /* Give 'em a chance to change their minds */
655         scr_printf("Save changes (y/n)? ");
656
657         if (yesno() == 1) {
658                 r = CtdlIPCSetRoomAide(ipc, raide, buf);
659                 if (r / 100 != 2) {
660                         scr_printf("%s\n", buf);
661                 }
662
663                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 0, eptr, buf);
664                 if (r / 100 != 2) {
665                         scr_printf("%s\n", buf);
666                 }
667
668                 r = CtdlIPCSetRoomAttributes(ipc, rbump, attr, buf);
669                 scr_printf("%s\n", buf);
670                 strncpy(buf, attr->QRname, ROOMNAMELEN);
671                 free(attr);
672                 if (r / 100 == 2)
673                         dotgoto(ipc, buf, 2, 0);
674         }
675         else free(attr);
676 }
677
678
679 /*
680  * un-goto the previous room, or a specified room
681  */
682 void dotungoto(CtdlIPC *ipc, char *towhere)
683   {
684     /* Find this 'towhere' room in the list ungoto from this room to
685        that at the messagepointer position in that room in our ungoto list.
686        I suppose I could be a real dick and just ungoto that many places
687        in our list. */
688     int found = -1;
689     int lp;
690         char buf[SIZ];
691         struct ctdlipcroom *rret = NULL;        /* ignored */
692         int r;
693
694         if (uglistsize == 0)
695       {
696                 scr_printf("No rooms to ungoto.\n");
697                 return;
698       }
699         if (towhere == NULL)
700       {
701                 scr_printf("Must specify a room to ungoto.\n");
702                 return;
703       }
704         if (IsEmptyStr(towhere))
705       {
706                 scr_printf("Must specify a room to ungoto.\n");
707                 return;
708       }
709     for (lp = uglistsize-1; lp >= 0; lp--)
710       {
711         if (strcasecmp(towhere, uglist[lp]) == 0)
712           {
713             found = lp;
714             break;
715           }
716       }
717     if (found == -1)
718       {
719                 scr_printf("Room: %s not in ungoto list.\n", towhere);
720         return;
721       }
722
723         r = CtdlIPCGotoRoom(ipc, uglist[found], "", &rret, buf);
724         if (rret) free(rret);   /* ignored */
725         if (r / 100 != 2) {
726                 scr_printf("%s\n", buf);
727                 return;
728         }
729         r = CtdlIPCSetLastRead(ipc, uglistlsn[found] ? uglistlsn[found] : 1, buf);
730         if (r / 100 != 2) {
731                 scr_printf("%s\n", buf);
732         }
733         safestrncpy(buf, uglist[found], sizeof(buf));
734     /* we queue ungoto information here, because we're not really
735        ungotoing, we're really going to a random spot in some arbitrary
736        room list. */
737         dotgoto(ipc, buf, 0, 0);
738   }
739
740 void ungoto(CtdlIPC *ipc)
741 {
742         char buf[SIZ];
743         struct ctdlipcroom *rret = NULL;        /* ignored */
744         int r;
745
746         if (uglistsize == 0)
747                 return;
748
749         r = CtdlIPCGotoRoom(ipc, uglist[uglistsize-1], "", &rret, buf);
750         if (rret) free(rret);   /* ignored */
751         if (r / 100 != 2) {
752                 scr_printf("%s\n", buf);
753                 return;
754         }
755         r = CtdlIPCSetLastRead(ipc, uglistlsn[uglistsize-1] ? uglistlsn[uglistsize-1] : 1, buf);
756         if (r / 100 != 2) {
757                 scr_printf("%s\n", buf);
758         }
759         safestrncpy(buf, uglist[uglistsize-1], sizeof(buf));
760         uglistsize--;
761         free(uglist[uglistsize]);
762         /* Don't queue ungoto info or we end up in a loop */
763         dotgoto(ipc, buf, 0, 1);
764 }
765
766
767 /*
768  * saves filelen bytes from file at pathname
769  */
770 int save_buffer(void *file, size_t filelen, const char *pathname)
771 {
772         size_t block = 0;
773         size_t bytes_written = 0;
774         FILE *fp;
775
776         fp = fopen(pathname, "w");
777         if (!fp) {
778                 err_printf("Cannot open '%s': %s\n", pathname, strerror(errno));
779                 return 0;
780         }
781         do {
782                 block = fwrite((char *)file + bytes_written, 1,
783                                 filelen - bytes_written, fp);
784                 bytes_written += block;
785         } while (errno == EINTR && bytes_written < filelen);
786         fclose(fp);
787
788         if (bytes_written < filelen) {
789                 err_printf("Trouble saving '%s': %s\n", pathname,
790                                 strerror(errno));
791                 return 0;
792         }
793         return 1;
794 }
795
796
797 /*
798  * Save supplied_filename in dest directory; gets the name only
799  */
800 void destination_directory(char *dest, const char *supplied_filename)
801 {
802         static char save_dir[SIZ] = { 0 };
803
804         if (IsEmptyStr(save_dir)) {
805                 if (getenv("HOME") == NULL) {
806                         strcpy(save_dir, ".");
807                 }
808                 else {
809                         sprintf(save_dir, "%s/Desktop", getenv("HOME"));
810                         if (access(save_dir, W_OK) != 0) {
811                                 sprintf(save_dir, "%s", getenv("HOME"));
812                                 if (access(save_dir, W_OK) != 0) {
813                                         sprintf(save_dir, ".");
814                                 }
815                         }
816                 }
817         }
818
819         sprintf(dest, "%s/%s", save_dir, supplied_filename);
820         strprompt("Save as", dest, PATH_MAX);
821
822         /* Remember the directory for next time */
823         strcpy(save_dir, dest);
824         if (strrchr(save_dir, '/') != NULL) {
825                 strcpy(strrchr(save_dir, '/'), "");
826         }
827         else {
828                 strcpy(save_dir, ".");
829         }
830 }
831
832
833 /*
834  * download()  -  download a file or files.  The argument passed to this
835  *                function determines which protocol to use.
836  *  proto - 0 = paginate, 1 = xmodem, 2 = raw, 3 = ymodem, 4 = zmodem, 5 = save
837  */
838 void download(CtdlIPC *ipc, int proto)
839 {
840         char buf[SIZ];
841         char filename[PATH_MAX];
842         char tempname[PATH_MAX];
843         char transmit_cmd[SIZ];
844         FILE *tpipe = NULL;
845         int broken = 0;
846         int r;
847         void *file = NULL;      /* The downloaded file */
848         size_t filelen = 0L;    /* The downloaded file length */
849
850         if ((room_flags & QR_DOWNLOAD) == 0) {
851                 scr_printf("*** You cannot download from this room.\n");
852                 return;
853         }
854
855         newprompt("Enter filename: ", filename, PATH_MAX);
856
857         /* Save to local disk, for folks with their own copy of the client */
858         if (proto == 5) {
859                 destination_directory(tempname, filename);
860                 r = CtdlIPCFileDownload(ipc, filename, &file, 0, progress, buf);
861                 if (r / 100 != 2) {
862                         scr_printf("%s\n", buf);
863                         return;
864                 }
865                 save_buffer(file, (size_t)extract_long(buf, 0), tempname);
866                 free(file);
867                 return;
868         }
869
870         r = CtdlIPCFileDownload(ipc, filename, &file, 0, progress, buf);
871         if (r / 100 != 2) {
872                 scr_printf("%s\n", buf);
873                 return;
874         }
875         filelen = extract_unsigned_long(buf, 0);
876
877         /* Meta-download for public clients */
878         /* scr_printf("Fetching file from Citadel server...\n"); */
879         mkdir(tempdir, 0700);
880         snprintf(tempname, sizeof tempname, "%s/%s", tempdir, filename);
881         tpipe = fopen(tempname, "wb");
882         if (fwrite(file, filelen, 1, tpipe) < filelen) {
883                 /* FIXME: restart syscall on EINTR */
884                 broken = 1;
885         }
886         fclose(tpipe);
887         if (file) free(file);
888
889         if (proto == 0) {
890                 /* FIXME: display internally instead */
891                 snprintf(transmit_cmd, sizeof transmit_cmd,
892                         "SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",
893                         tempname);
894         }
895         else if (proto == 1)
896                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sx %s", tempname);
897         else if (proto == 3)
898                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sb %s", tempname);
899         else if (proto == 4)
900                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sz %s", tempname);
901         else
902                 /* FIXME: display internally instead */
903                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec cat %s", tempname);
904
905         screen_reset();
906         stty_ctdl(SB_RESTORE);
907         system(transmit_cmd);
908         stty_ctdl(SB_NO_INTR);
909         screen_set();
910
911         /* clean up the temporary directory */
912         nukedir(tempdir);
913         ctdl_beep();    /* Beep beep! */
914 }
915
916
917 /*
918  * read directory of this room
919  */
920 void roomdir(CtdlIPC *ipc)
921 {
922         char flnm[256];
923         char flsz[32];
924         char comment[256];
925         char buf[256];
926         char *listing = NULL;   /* Returned directory listing */
927         int r;
928
929         r = CtdlIPCReadDirectory(ipc, &listing, buf);
930         if (r / 100 != 1) {
931                 pprintf("%s\n", buf);
932                 return;
933         }
934
935         extract_token(comment, buf, 0, '|', sizeof comment);
936         extract_token(flnm, buf, 1, '|', sizeof flnm);
937         pprintf("\nDirectory of %s on %s\n", flnm, comment);
938         pprintf("-----------------------\n");
939         while (listing && *listing && !IsEmptyStr(listing)) {
940                 extract_token(buf, listing, 0, '\n', sizeof buf);
941                 remove_token(listing, 0, '\n');
942
943                 extract_token(flnm, buf, 0, '|', sizeof flnm);
944                 extract_token(flsz, buf, 1, '|', sizeof flsz);
945                 extract_token(comment, buf, 2, '|', sizeof comment);
946                 if (strlen(flnm) <= 14)
947                         pprintf("%-14s %8s %s\n", flnm, flsz, comment);
948                 else
949                         pprintf("%s\n%14s %8s %s\n", flnm, "", flsz,
950                                 comment);
951         }
952         if (listing) free(listing);
953 }
954
955
956 /*
957  * add a user to a private room
958  */
959 void invite(CtdlIPC *ipc)
960 {
961         char username[USERNAME_SIZE];
962         char buf[SIZ];
963         int r;                          /* IPC response code */
964
965         newprompt("Name of user? ", username, USERNAME_SIZE);
966         if (username[0] == 0)
967                 return;
968
969         r = CtdlIPCInviteUserToRoom(ipc, username, buf);
970         scr_printf("%s\n", buf);
971 }
972
973
974 /*
975  * kick a user out of a room
976  */
977 void kickout(CtdlIPC *ipc)
978 {
979         char username[USERNAME_SIZE];
980         char buf[SIZ];
981         int r;                          /* IPC response code */
982
983         newprompt("Name of user? ", username, USERNAME_SIZE);
984         if (username[0] == 0)
985                 return;
986
987         r = CtdlIPCKickoutUserFromRoom(ipc, username, buf);
988         scr_printf("%s\n", buf);
989 }
990
991
992 /*
993  * aide command: kill the current room
994  */
995 void killroom(CtdlIPC *ipc)
996 {
997         char aaa[100];
998         int r;
999
1000         r = CtdlIPCDeleteRoom(ipc, 0, aaa);
1001         if (r / 100 != 2) {
1002                 scr_printf("%s\n", aaa);
1003                 return;
1004         }
1005
1006         scr_printf("Are you sure you want to kill this room? ");
1007         if (yesno() == 0)
1008                 return;
1009
1010         r = CtdlIPCDeleteRoom(ipc, 1, aaa);
1011         scr_printf("%s\n", aaa);
1012         if (r / 100 != 2)
1013                 return;
1014         dotgoto(ipc, "_BASEROOM_", 0, 0);
1015 }
1016
1017 void forget(CtdlIPC *ipc)
1018 {                               /* forget the current room */
1019         char buf[SIZ];
1020
1021         scr_printf("Are you sure you want to forget this room? ");
1022         if (yesno() == 0)
1023                 return;
1024
1025         remove_march(room_name, 0);
1026         if (CtdlIPCForgetRoom(ipc, buf) / 100 != 2) {
1027                 scr_printf("%s\n", buf);
1028                 return;
1029         }
1030
1031         /* now return to the lobby */
1032         dotgoto(ipc, "_BASEROOM_", 0, 0);
1033 }
1034
1035
1036 /*
1037  * create a new room
1038  */
1039 void entroom(CtdlIPC *ipc)
1040 {
1041         char buf[SIZ];
1042         char new_room_name[ROOMNAMELEN];
1043         int new_room_type;
1044         char new_room_pass[10];
1045         int new_room_floor;
1046         int a, b;
1047         int r;                          /* IPC response code */
1048
1049         /* Check permission to create room */
1050         r = CtdlIPCCreateRoom(ipc, 0, "", 1, "", 0, buf);
1051         if (r / 100 != 2) {
1052                 scr_printf("%s\n", buf);
1053                 return;
1054         }
1055
1056         newprompt("Name for new room? ", new_room_name, ROOMNAMELEN - 1);
1057         if (IsEmptyStr(new_room_name)) {
1058                 return;
1059         }
1060         for (a = 0; a < strlen(new_room_name); ++a) {
1061                 if (new_room_name[a] == '|') {
1062                         new_room_name[a] = '_';
1063                 }
1064         }
1065
1066         new_room_floor = select_floor(ipc, (int) curr_floor);
1067
1068         IFNEXPERT formout(ipc, "roomaccess");
1069         do {
1070                 scr_printf("<?>Help\n<1>Public room\n<2>Guess-name room\n"
1071                        "<3>Passworded room\n<4>Invitation-only room\n"
1072                        "<5>Personal room\n"
1073                         "Enter room type: ");
1074                 do {
1075                         b = inkey();
1076                 } while (((b < '1') || (b > '5')) && (b != '?'));
1077                 if (b == '?') {
1078                         scr_printf("?\n");
1079                         formout(ipc, "roomaccess");
1080                 }
1081         } while ((b < '1') || (b > '5'));
1082         b -= '0';                       /* Portable */
1083         scr_printf("%d\n", b);
1084         new_room_type = b - 1;
1085         if (new_room_type == 2) {
1086                 newprompt("Enter a room password: ", new_room_pass, 9);
1087                 for (a = 0; a < strlen(new_room_pass); ++a)
1088                         if (new_room_pass[a] == '|')
1089                                 new_room_pass[a] = '_';
1090         } else {
1091                 strcpy(new_room_pass, "");
1092         }
1093
1094         scr_printf("\042%s\042, a", new_room_name);
1095         if (b == 1)
1096                 scr_printf(" public room.");
1097         if (b == 2)
1098                 scr_printf(" guess-name room.");
1099         if (b == 3)
1100                 scr_printf(" passworded room, password: %s", new_room_pass);
1101         if (b == 4)
1102                 scr_printf("n invitation-only room.");
1103         if (b == 5)
1104                 scr_printf(" personal room.");
1105         scr_printf("\nInstall it? (y/n) : ");
1106         if (yesno() == 0) {
1107                 return;
1108         }
1109
1110         r = CtdlIPCCreateRoom(ipc, 1, new_room_name, new_room_type,
1111                               new_room_pass, new_room_floor, buf);
1112         if (r / 100 != 2) {
1113                 scr_printf("%s\n", buf);
1114                 return;
1115         }
1116
1117         /* command succeeded... now GO to the new room! */
1118         dotgoto(ipc, new_room_name, 0, 0);
1119 }
1120
1121
1122
1123 void readinfo(CtdlIPC *ipc)
1124 {                               /* read info file for current room */
1125         char buf[SIZ];
1126         char raide[64];
1127         int r;                  /* IPC response code */
1128         char *text = NULL;
1129
1130         /* Name of currernt room aide */
1131         r = CtdlIPCGetRoomAide(ipc, buf);
1132         if (r / 100 == 2)
1133                 safestrncpy(raide, buf, sizeof raide);
1134         else
1135                 strcpy(raide, "");
1136
1137         if (!IsEmptyStr(raide))
1138                 scr_printf("Room aide is %s.\n\n", raide);
1139
1140         r = CtdlIPCRoomInfo(ipc, &text, buf);
1141         if (r / 100 != 1)
1142                 return;
1143
1144         if (text) {
1145                 fmout(screenwidth, NULL, text, NULL,
1146                       ((userflags & US_PAGINATOR) ? 1 : 0), screenheight, 
1147                       (*raide) ? 2 : 0, 1);
1148                 free(text);
1149         }
1150 }
1151
1152
1153 /*
1154  * <W>ho knows room...
1155  */
1156 void whoknows(CtdlIPC *ipc)
1157 {
1158         char buf[256];
1159         char *listing = NULL;
1160         int r;
1161
1162         r = CtdlIPCWhoKnowsRoom(ipc, &listing, buf);
1163         if (r / 100 != 1) {
1164                 pprintf("%s\n", buf);
1165                 return;
1166         }
1167         while (!IsEmptyStr(listing)) {
1168                 extract_token(buf, listing, 0, '\n', sizeof buf);
1169                 remove_token(listing, 0, '\n');
1170                 if (sigcaught == 0)
1171                         pprintf("%s\n", buf);
1172         }
1173         free(listing);
1174 }
1175
1176
1177 void do_edit(CtdlIPC *ipc,
1178                 char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
1179 {
1180         FILE *fp;
1181         char cmd[SIZ];
1182         int b, cksum, editor_exit;
1183
1184         if (IsEmptyStr(editor_paths[0])) {
1185                 scr_printf("Do you wish to re-enter %s? ", desc);
1186                 if (yesno() == 0)
1187                         return;
1188         }
1189
1190         fp = fopen(temp, "w");
1191         fclose(fp);
1192
1193         CtdlIPC_chat_send(ipc, check_cmd);
1194         CtdlIPC_chat_recv(ipc, cmd);
1195         if (cmd[0] != '2') {
1196                 scr_printf("%s\n", &cmd[4]);
1197                 return;
1198         }
1199
1200         if (!IsEmptyStr(editor_paths[0])) {
1201                 CtdlIPC_chat_send(ipc, read_cmd);
1202                 CtdlIPC_chat_recv(ipc, cmd);
1203                 if (cmd[0] == '1') {
1204                         fp = fopen(temp, "w");
1205                         while (CtdlIPC_chat_recv(ipc, cmd), strcmp(cmd, "000")) {
1206                                 fprintf(fp, "%s\n", cmd);
1207                         }
1208                         fclose(fp);
1209                 }
1210         }
1211
1212         cksum = file_checksum(temp);
1213
1214         if (!IsEmptyStr(editor_paths[0])) {
1215                 char tmp[SIZ];
1216
1217                 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", desc);
1218                 putenv(tmp);
1219                 screen_reset();
1220                 stty_ctdl(SB_RESTORE);
1221                 editor_pid = fork();
1222                 if (editor_pid == 0) {
1223                         chmod(temp, 0600);
1224                         execlp(editor_paths[0], editor_paths[0], temp, NULL);
1225                         exit(1);
1226                 }
1227                 if (editor_pid > 0)
1228                         do {
1229                                 editor_exit = 0;
1230                                 b = ka_wait(&editor_exit);
1231                         } while ((b != editor_pid) && (b >= 0));
1232                 editor_pid = (-1);
1233                 scr_printf("Executed %s\n", editor_paths[0]);
1234                 stty_ctdl(0);
1235                 screen_set();
1236         } else {
1237                 scr_printf("Entering %s.  "
1238                         "Press return twice when finished.\n", desc);
1239                 fp = fopen(temp, "r+");
1240                 citedit(ipc, fp);
1241                 fclose(fp);
1242         }
1243
1244         if (file_checksum(temp) == cksum) {
1245                 scr_printf("*** Aborted.\n");
1246         }
1247
1248         else {
1249                 CtdlIPC_chat_send(ipc, write_cmd);
1250                 CtdlIPC_chat_recv(ipc, cmd);
1251                 if (cmd[0] != '4') {
1252                         scr_printf("%s\n", &cmd[4]);
1253                         return;
1254                 }
1255
1256                 fp = fopen(temp, "r");
1257                 while (fgets(cmd, SIZ - 1, fp) != NULL) {
1258                         cmd[strlen(cmd) - 1] = 0;
1259                         CtdlIPC_chat_send(ipc, cmd);
1260                 }
1261                 fclose(fp);
1262                 CtdlIPC_chat_send(ipc, "000");
1263         }
1264
1265         unlink(temp);
1266 }
1267
1268
1269 void enterinfo(CtdlIPC *ipc)
1270 {                               /* edit info file for current room */
1271         do_edit(ipc, "the Info file for this room", "RINF", "EINF 0", "EINF 1");
1272 }
1273
1274 void enter_bio(CtdlIPC *ipc)
1275 {
1276         char cmd[SIZ];
1277         snprintf(cmd, sizeof cmd, "RBIO %s", fullname);
1278         do_edit(ipc, "your Bio", cmd, "NOOP", "EBIO");
1279 }
1280
1281 /*
1282  * create a new floor
1283  */
1284 void create_floor(CtdlIPC *ipc)
1285 {
1286         char buf[SIZ];
1287         char newfloorname[SIZ];
1288         int r;                  /* IPC response code */
1289
1290         load_floorlist(ipc);
1291
1292         r = CtdlIPCCreateFloor(ipc, 0, "", buf);
1293         if ( (r / 100 != 2) && (r != ERROR + ILLEGAL_VALUE) ) {
1294                 scr_printf("%s\n", buf);
1295                 return;
1296         }
1297
1298         newprompt("Name for new floor: ", newfloorname, 255);
1299         if (!*newfloorname) return;
1300         r = CtdlIPCCreateFloor(ipc, 1, newfloorname, buf);
1301         if (r / 100 == 2) {
1302                 scr_printf("Floor has been created.\n");
1303         } else {
1304                 scr_printf("%s\n", buf);
1305         }
1306
1307         load_floorlist(ipc);
1308 }
1309
1310 /*
1311  * edit the current floor
1312  */
1313 void edit_floor(CtdlIPC *ipc)
1314 {
1315         char buf[SIZ];
1316         struct ExpirePolicy *ep = NULL;
1317         int r;                          /* IPC response code */
1318
1319         load_floorlist(ipc);
1320
1321         /* Fetch the expire policy (this will silently fail on old servers,
1322          * resulting in "default" policy)
1323          */
1324         r = CtdlIPCGetMessageExpirationPolicy(ipc, 1, &ep, buf);
1325
1326         /* Interact with the user */
1327         scr_printf("You are editing the floor called \"%s\"\n", 
1328                 &floorlist[(int) curr_floor][0] );
1329         strprompt("Floor name", &floorlist[(int) curr_floor][0], 255);
1330
1331         /* Angels and demons dancing in my head... */
1332         do {
1333                 snprintf(buf, sizeof buf, "%d", ep->expire_mode);
1334                 strprompt
1335                     ("Floor default message expire policy (? for list)",
1336                      buf, 1);
1337                 if (buf[0] == '?') {
1338                         scr_printf("\n"
1339                                 "0. Use the system default\n"
1340                                 "1. Never automatically expire messages\n"
1341                                 "2. Expire by message count\n"
1342                                 "3. Expire by message age\n");
1343                 }
1344         } while ((buf[0] < '0') || (buf[0] > '3'));
1345         ep->expire_mode = buf[0] - '0';
1346
1347         /* ...lunatics and monsters underneath my bed */
1348         if (ep->expire_mode == 2) {
1349                 snprintf(buf, sizeof buf, "%d", ep->expire_value);
1350                 strprompt("Keep how many messages online?", buf, 10);
1351                 ep->expire_value = atol(buf);
1352         }
1353
1354         if (ep->expire_mode == 3) {
1355                 snprintf(buf, sizeof buf, "%d", ep->expire_value);
1356                 strprompt("Keep messages for how many days?", buf, 10);
1357                 ep->expire_value = atol(buf);
1358         }
1359
1360         /* Save it */
1361         r = CtdlIPCSetMessageExpirationPolicy(ipc, 1, ep, buf);
1362         r = CtdlIPCEditFloor(ipc, curr_floor, &floorlist[(int)curr_floor][0], buf);
1363         scr_printf("%s\n", buf);
1364         load_floorlist(ipc);
1365 }
1366
1367
1368
1369
1370 /*
1371  * kill the current floor 
1372  */
1373 void kill_floor(CtdlIPC *ipc)
1374 {
1375         int floornum_to_delete, a;
1376         char buf[SIZ];
1377
1378         load_floorlist(ipc);
1379         do {
1380                 floornum_to_delete = (-1);
1381                 scr_printf("(Press return to abort)\n");
1382                 newprompt("Delete which floor? ", buf, 255);
1383                 if (IsEmptyStr(buf))
1384                         return;
1385                 for (a = 0; a < 128; ++a)
1386                         if (!strcasecmp(&floorlist[a][0], buf))
1387                                 floornum_to_delete = a;
1388                 if (floornum_to_delete < 0) {
1389                         scr_printf("No such floor.  Select one of:\n");
1390                         for (a = 0; a < 128; ++a)
1391                                 if (floorlist[a][0] != 0)
1392                                         scr_printf("%s\n", &floorlist[a][0]);
1393                 }
1394         } while (floornum_to_delete < 0);
1395         CtdlIPCDeleteFloor(ipc, 1, floornum_to_delete, buf);
1396         scr_printf("%s\n", buf);
1397         load_floorlist(ipc);
1398 }