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