war on cruft continues
[citadel.git] / textclient / src / rooms.c
1 /*
2  * Client-side functions which perform room operations
3  *
4  * Copyright (c) 1987-2016 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) && (f & QR_NETWORK))
126                         scr_printf("}  ");
127                 else if (f & QR_DIRECTORY)
128                         scr_printf("]  ");
129                 else if (f & QR_NETWORK)
130                         scr_printf(")  ");
131                 else
132                         scr_printf(">  ");
133                 c = c + strlen(rmname) + 3;
134         }
135
136         if (rp->rnext != NULL) {
137                 room_tree_list(rp->rnext);
138         }
139
140         free(rp);
141 }
142
143
144 /* 
145  * Room ordering stuff (compare first by floor, then by order)
146  */
147 int rordercmp(struct ctdlroomlisting *r1, struct ctdlroomlisting *r2)
148 {
149         if ((r1 == NULL) && (r2 == NULL))
150                 return (0);
151         if (r1 == NULL)
152                 return (-1);
153         if (r2 == NULL)
154                 return (1);
155         if (r1->rlfloor < r2->rlfloor)
156                 return (-1);
157         if (r1->rlfloor > r2->rlfloor)
158                 return (1);
159         if (r1->rlorder < r2->rlorder)
160                 return (-1);
161         if (r1->rlorder > r2->rlorder)
162                 return (1);
163         return (0);
164 }
165
166
167 /*
168  * Common code for all room listings
169  */
170 static void listrms(struct march *listing, int new_only, int floor_only, unsigned int flags, char *match)
171 {
172         struct march *mptr;
173         struct ctdlroomlisting *rl = NULL;
174         struct ctdlroomlisting *rp;
175         struct ctdlroomlisting *rs;
176         int list_it;
177
178         for (mptr = listing; mptr != NULL; mptr = mptr->next) {
179                 list_it = 1;
180
181                 if ( (new_only == LISTRMS_NEW_ONLY)
182                    && ((mptr->march_access & UA_HASNEWMSGS) == 0)) 
183                         list_it = 0;
184
185                 if ( (new_only == LISTRMS_OLD_ONLY)
186                    && ((mptr->march_access & UA_HASNEWMSGS) != 0)) 
187                         list_it = 0;
188
189                 if ( (floor_only >= 0)
190                    && (mptr->march_floor != floor_only))
191                         list_it = 0;
192
193                 if (flags && (mptr->march_flags & flags) == 0)
194                     list_it = 0;
195
196             if (match && (pattern(mptr->march_name, match) == -1))
197                         list_it = 0;
198
199                 if (list_it) {
200                         rp = malloc(sizeof(struct ctdlroomlisting));
201                         strncpy(rp->rlname, mptr->march_name, ROOMNAMELEN);
202                         rp->rlflags = mptr->march_flags;
203                         rp->rlfloor = mptr->march_floor;
204                         rp->rlorder = mptr->march_order;
205                         rp->lnext = NULL;
206                         rp->rnext = NULL;
207         
208                         rs = rl;
209                         if (rl == NULL) {
210                                 rl = rp;
211                         } else {
212                                 while (rp != NULL) {
213                                         if (rordercmp(rp, rs) < 0) {
214                                                 if (rs->lnext == NULL) {
215                                                         rs->lnext = rp;
216                                                         rp = NULL;
217                                                 } else {
218                                                         rs = rs->lnext;
219                                                 }
220                                         } else {
221                                                 if (rs->rnext == NULL) {
222                                                         rs->rnext = rp;
223                                                         rp = NULL;
224                                                 } else {
225                                                         rs = rs->rnext;
226                                                 }
227                                         }
228                                 }
229                         }
230                 }
231         }
232
233         room_tree_list(NULL);
234         room_tree_list(rl);
235         color(DIM_WHITE);
236 }
237
238
239 void list_other_floors(void)
240 {
241         int a, c;
242
243         c = 1;
244         for (a = 0; a < 128; ++a) {
245                 if ((strlen(floorlist[a]) > 0) && (a != curr_floor)) {
246                         if ((c + strlen(floorlist[a]) + 4) > screenwidth) {
247                                 scr_printf("\n");
248                                 c = 1;
249                         }
250                         scr_printf("%s:  ", floorlist[a]);
251                         c = c + strlen(floorlist[a]) + 3;
252                 }
253         }
254 }
255
256
257 /*
258  * List known rooms.  kn_floor_mode should be set to 0 for a 'flat' listing,
259  * 1 to list rooms on the current floor, or 2 to list rooms on all floors.
260  */
261 void knrooms(CtdlIPC *ipc, int kn_floor_mode)
262 {
263         int a;
264         struct march *listing = NULL;
265         struct march *mptr;
266         int r;          /* IPC response code */
267         char buf[SIZ];
268
269
270         /* Ask the server for a room list */
271         r = CtdlIPCKnownRooms(ipc, SubscribedRooms, (-1), &listing, buf);
272         if (r / 100 != 1) {
273                 listing = NULL;
274         }
275
276         load_floorlist(ipc);
277
278
279         if (kn_floor_mode == 0) {
280                 color(BRIGHT_CYAN);
281                 scr_printf("\n   Rooms with unread messages:\n");
282                 listrms(listing, LISTRMS_NEW_ONLY, -1, 0, NULL);
283                 color(BRIGHT_CYAN);
284                 scr_printf("\n\n   No unseen messages in:\n");
285                 listrms(listing, LISTRMS_OLD_ONLY, -1, 0, NULL);
286                 scr_printf("\n");
287         }
288
289         if (kn_floor_mode == 1) {
290                 color(BRIGHT_CYAN);
291                 scr_printf("\n   Rooms with unread messages on %s:\n",
292                         floorlist[(int) curr_floor]);
293                 listrms(listing, LISTRMS_NEW_ONLY, curr_floor, 0, NULL);
294                 color(BRIGHT_CYAN);
295                 scr_printf("\n\n   Rooms with no new messages on %s:\n",
296                         floorlist[(int) curr_floor]);
297                 listrms(listing, LISTRMS_OLD_ONLY, curr_floor, 0, NULL);
298                 color(BRIGHT_CYAN);
299                 scr_printf("\n\n   Other floors:\n");
300                 list_other_floors();
301                 scr_printf("\n");
302         }
303
304         if (kn_floor_mode == 2) {
305                 for (a = 0; a < 128; ++a) {
306                         if (floorlist[a][0] != 0) {
307                                 color(BRIGHT_CYAN);
308                                 scr_printf("\n   Rooms on %s:\n",
309                                         floorlist[a]);
310                                 listrms(listing, LISTRMS_ALL, a, 0, NULL);
311                                 scr_printf("\n");
312                         }
313                 }
314         }
315
316         /* Free the room list */
317         while (listing) {
318                 mptr = listing->next;
319                 free(listing);
320                 listing = mptr;
321         };
322
323         color(DIM_WHITE);
324 }
325
326
327 void listzrooms(CtdlIPC *ipc)
328 {                               /* list public forgotten rooms */
329         struct march *listing = NULL;
330         struct march *mptr;
331         int r;          /* IPC response code */
332         char buf[SIZ];
333
334
335         /* Ask the server for a room list */
336         r = CtdlIPCKnownRooms(ipc, UnsubscribedRooms, (-1), &listing, buf);
337         if (r / 100 != 1) {
338                 listing = NULL;
339         }
340
341         color(BRIGHT_CYAN);
342         scr_printf("\n   Forgotten public rooms:\n");
343         listrms(listing, LISTRMS_ALL, -1, 0, NULL);
344         scr_printf("\n");
345
346         /* Free the room list */
347         while (listing) {
348                 mptr = listing->next;
349                 free(listing);
350                 listing = mptr;
351         };
352
353         color(DIM_WHITE);
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         scr_printf("\n   Anonymous rooms:\n");
374             listrms(listing, LISTRMS_ALL, -1, QR_ANONONLY|QR_ANONOPT, NULL);
375         scr_printf("\n");
376                 break;
377     case 1:
378         scr_printf("\n   Directory rooms:\n");
379             listrms(listing, LISTRMS_ALL, -1, QR_DIRECTORY, NULL);
380         scr_printf("\n");
381                 break;
382     case 2:
383         scr_printf("\n   Matching \"%s\" rooms:\n", match);
384             listrms(listing, LISTRMS_ALL, -1, 0, match);
385         scr_printf("\n");
386                 break;
387     case 3:
388         scr_printf("\n   Preferred only rooms:\n");
389             listrms(listing, LISTRMS_ALL, -1, QR_PREFONLY, NULL);
390         scr_printf("\n");
391                 break;
392     case 4:
393         scr_printf("\n   Private rooms:\n");
394             listrms(listing, LISTRMS_ALL, -1, QR_PRIVATE, NULL);
395         scr_printf("\n");
396                 break;
397     case 5:
398         scr_printf("\n   Read only rooms:\n");
399             listrms(listing, LISTRMS_ALL, -1, QR_READONLY, NULL);
400         scr_printf("\n");
401                 break;
402     case 6:
403         scr_printf("\n   Shared rooms:\n");
404             listrms(listing, LISTRMS_ALL, -1, QR_NETWORK, NULL);
405         scr_printf("\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 }
418
419
420 int set_room_attr(CtdlIPC *ipc, unsigned int ibuf, char *prompt, unsigned int sbit)
421 {
422         int a;
423
424         a = boolprompt(prompt, (ibuf & sbit));
425         ibuf = (ibuf | sbit);
426         if (!a) {
427                 ibuf = (ibuf ^ sbit);
428         }
429         return (ibuf);
430 }
431
432
433
434 /*
435  * Select a floor (used in several commands)
436  * The supplied argument is the 'default' floor number.
437  * This function returns the selected floor number.
438  */
439 int select_floor(CtdlIPC *ipc, int rfloor)
440 {
441         int a, newfloor;
442         char floorstr[SIZ];
443
444         if (floor_mode == 1) {
445                 if (floorlist[(int) curr_floor][0] == 0) {
446                         load_floorlist(ipc);
447                 }
448
449                 do {
450                         newfloor = (-1);
451                         safestrncpy(floorstr, floorlist[rfloor],
452                                     sizeof floorstr);
453                         strprompt("Which floor", floorstr, 255);
454                         for (a = 0; a < 128; ++a) {
455                                 if (!strcasecmp
456                                     (floorstr, &floorlist[a][0]))
457                                         newfloor = a;
458                                 if ((newfloor < 0)
459                                     &&
460                                     (!strncasecmp
461                                      (floorstr, &floorlist[a][0],
462                                       strlen(floorstr))))
463                                         newfloor = a;
464                                 if ((newfloor < 0)
465                                     && (pattern(&floorlist[a][0], floorstr)
466                                         >= 0))
467                                         newfloor = a;
468                         }
469                         if (newfloor < 0) {
470                                 scr_printf("\n One of:\n");
471                                 for (a = 0; a < 128; ++a) {
472                                         if (floorlist[a][0] != 0) {
473                                                 scr_printf("%s\n",
474                                                        &floorlist[a][0]);
475                                         }
476                                 }
477                         }
478                 } while (newfloor < 0);
479                 return (newfloor);
480         }
481
482         else {
483                 scr_printf("Floor selection bypassed because you have "
484                         "floor mode disabled.\n");
485         }
486
487         return (rfloor);
488 }
489
490
491
492
493 /*
494  * .<A>ide <E>dit room
495  */
496 void editthisroom(CtdlIPC *ipc)
497 {
498         int rbump = 0;
499         char room_admin_name[USERNAME_SIZE];
500         char buf[SIZ];
501         struct ctdlroom *attr = NULL;
502         struct ExpirePolicy *eptr = NULL;
503         int r;                          /* IPC response code */
504
505         /* Fetch the existing room config */
506         r = CtdlIPCGetRoomAttributes(ipc, &attr, buf);
507         if (r / 100 != 2) {
508                 scr_printf("%s\n", buf);
509                 return;
510         }
511         eptr = &(attr->QRep);
512
513         /* Fetch the name of the current room admin */
514         r = CtdlIPCGetRoomAide(ipc, buf);
515         if (r / 100 == 2) {
516                 safestrncpy(room_admin_name, buf, sizeof room_admin_name);
517         } else {
518                 strcpy(room_admin_name, "");
519         }
520         if (IsEmptyStr(room_admin_name)) {
521                 strcpy(room_admin_name, "none");
522         }
523
524         /* Fetch the expire policy (this will silently fail on old servers,
525          * resulting in "default" policy)
526          */
527         r = CtdlIPCGetMessageExpirationPolicy(ipc, 0, &eptr, buf);
528
529         /* Now interact with the user. */
530
531         strprompt("Room name", attr->QRname, ROOMNAMELEN-1);
532         attr->QRfloor = select_floor(ipc, attr->QRfloor);
533         attr->QRflags = set_room_attr(ipc, attr->QRflags, "Private room", QR_PRIVATE);
534         if (attr->QRflags & QR_PRIVATE) {
535                 attr->QRflags = set_room_attr(ipc, attr->QRflags,
536                                        "Hidden room (accessible to anyone who knows the room name)",
537                                        QR_GUESSNAME);
538         }
539
540         /* if it's public, clear the privacy classes */
541         if ((attr->QRflags & QR_PRIVATE) == 0) {
542                 if (attr->QRflags & QR_GUESSNAME) {
543                         attr->QRflags = attr->QRflags - QR_GUESSNAME;
544                 }
545                 if (attr->QRflags & QR_PASSWORDED) {
546                         attr->QRflags = attr->QRflags - QR_PASSWORDED;
547                 }
548         }
549
550         /* if it's private, choose the privacy classes */
551         if ((attr->QRflags & QR_PRIVATE)
552             && ((attr->QRflags & QR_GUESSNAME) == 0)) {
553                 attr->QRflags = set_room_attr(ipc, attr->QRflags,
554                                        "Accessible by entering a password",
555                                        QR_PASSWORDED);
556         }
557         if ((attr->QRflags & QR_PRIVATE)
558             && ((attr->QRflags & QR_PASSWORDED) == QR_PASSWORDED)) {
559                 strprompt("Room password", attr->QRpasswd, 9);
560         }
561
562         if ((attr->QRflags & QR_PRIVATE) == QR_PRIVATE) {
563                 rbump = boolprompt("Cause current users to forget room", 0);
564         }
565
566         attr->QRflags = set_room_attr(ipc, attr->QRflags,
567                                         "Preferred users only", QR_PREFONLY);
568         attr->QRflags = set_room_attr(ipc, attr->QRflags,
569                                         "Read-only room", QR_READONLY);
570         attr->QRflags2 = set_room_attr(ipc, attr->QRflags2,
571                                 "Allow message deletion by anyone who can post",
572                                 QR2_COLLABDEL);
573         attr->QRflags = set_room_attr(ipc, attr->QRflags,
574                                         "Permanent room", QR_PERMANENT);
575         attr->QRflags2 = set_room_attr(ipc, attr->QRflags2,
576                                                                    "Subject Required (Force "
577                                                                    "users to specify a message "
578                                    "subject)", QR2_SUBJECTREQ);
579         attr->QRflags = set_room_attr(ipc, attr->QRflags,
580                                         "Directory room", QR_DIRECTORY);
581         if (attr->QRflags & QR_DIRECTORY) {
582                 strprompt("Directory name", attr->QRdirname, 14);
583                 attr->QRflags =
584                     set_room_attr(ipc, attr->QRflags,
585                                                 "Uploading allowed", QR_UPLOAD);
586                 attr->QRflags =
587                     set_room_attr(ipc, attr->QRflags, "Downloading allowed",
588                                   QR_DOWNLOAD);
589                 attr->QRflags =
590                     set_room_attr(ipc, attr->QRflags,
591                                                 "Visible directory", QR_VISDIR);
592         }
593         attr->QRflags = set_room_attr(ipc, attr->QRflags,
594                                         "Network shared room", QR_NETWORK);
595         attr->QRflags2 = set_room_attr(ipc, attr->QRflags2,
596                                 "Self-service list subscribe/unsubscribe",
597                                 QR2_SELFLIST);
598         attr->QRflags2 = set_room_attr(ipc, attr->QRflags2,
599                                 "public posting to this room via room_roomname@yourcitadel.org",
600                                 QR2_SMTP_PUBLIC);
601         attr->QRflags2 = set_room_attr(ipc, attr->QRflags2,
602                                 "moderated mailinglist",
603                                 QR2_MODERATED);
604         attr->QRflags = set_room_attr(ipc, attr->QRflags,
605                                "Automatically make all messages anonymous",
606                                QR_ANONONLY);
607         if ((attr->QRflags & QR_ANONONLY) == 0) {
608                 attr->QRflags = set_room_attr(ipc, attr->QRflags,
609                                        "Ask users whether to make messages anonymous",
610                                        QR_ANONOPT);
611         }
612         attr->QRorder = intprompt("Listing order", attr->QRorder, 0, 127);
613
614         /* Ask about the room admin */
615         do {
616                 strprompt("Room admin (or 'none')", room_admin_name, 29);
617                 if (!strcasecmp(room_admin_name, "none")) {
618                         strcpy(room_admin_name, "");
619                         break;
620                 } else {
621                         r = CtdlIPCQueryUsername(ipc, room_admin_name, buf);
622                         if (r / 100 != 2)
623                                 scr_printf("%s\n", buf);
624                 }
625         } while (r / 100 != 2);
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, room_admin_name, 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                 scr_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                 scr_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         int rv = 0;
848         void *file = NULL;      /* The downloaded file */
849         size_t filelen = 0L;    /* The downloaded file length */
850
851         if ((room_flags & QR_DOWNLOAD) == 0) {
852                 scr_printf("*** You cannot download from this room.\n");
853                 return;
854         }
855
856         newprompt("Enter filename: ", filename, PATH_MAX);
857
858         /* Save to local disk, for folks with their own copy of the client */
859         if (proto == 5) {
860                 destination_directory(tempname, filename);
861                 r = CtdlIPCFileDownload(ipc, filename, &file, 0, progress, buf);
862                 if (r / 100 != 2) {
863                         scr_printf("%s\n", buf);
864                         return;
865                 }
866                 save_buffer(file, (size_t)extract_long(buf, 0), tempname);
867                 free(file);
868                 return;
869         }
870
871         r = CtdlIPCFileDownload(ipc, filename, &file, 0, progress, buf);
872         if (r / 100 != 2) {
873                 scr_printf("%s\n", buf);
874                 return;
875         }
876         filelen = extract_unsigned_long(buf, 0);
877
878         /* Meta-download for public clients */
879         /* scr_printf("Fetching file from Citadel server...\n"); */
880         mkdir(tempdir, 0700);
881         snprintf(tempname, sizeof tempname, "%s/%s", tempdir, filename);
882         tpipe = fopen(tempname, "wb");
883         if (fwrite(file, filelen, 1, tpipe) < filelen) {
884                 /* FIXME: restart syscall on EINTR 
885                    broken = 1;*/
886         }
887         fclose(tpipe);
888         if (file) free(file);
889
890         if (proto == 0) {
891                 /* FIXME: display internally instead */
892                 snprintf(transmit_cmd, sizeof transmit_cmd,
893                         "SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",
894                         tempname);
895         }
896         else if (proto == 1)
897                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sx %s", tempname);
898         else if (proto == 3)
899                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sb %s", tempname);
900         else if (proto == 4)
901                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sz %s", tempname);
902         else
903                 /* FIXME: display internally instead */
904                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec cat %s", tempname);
905
906         stty_ctdl(SB_RESTORE);
907         rv = system(transmit_cmd);
908         if (rv != 0)
909                 scr_printf("failed to download '%s': %d\n", transmit_cmd, rv);
910         stty_ctdl(SB_NO_INTR);
911
912         /* clean up the temporary directory */
913         nukedir(tempdir);
914         ctdl_beep();    /* Beep beep! */
915 }
916
917
918 /*
919  * read directory of this room
920  */
921 void roomdir(CtdlIPC *ipc)
922 {
923         char flnm[256];
924         char flsz[32];
925         char comment[256];
926         char mimetype[256];
927         char buf[256];
928         char *listing = NULL;   /* Returned directory listing */
929         int r;
930
931         r = CtdlIPCReadDirectory(ipc, &listing, buf);
932         if (r / 100 != 1) {
933                 scr_printf("%s\n", buf);
934                 return;
935         }
936
937         extract_token(comment, buf, 0, '|', sizeof comment);
938         extract_token(flnm, buf, 1, '|', sizeof flnm);
939         scr_printf("\nDirectory of %s on %s\n", flnm, comment);
940         scr_printf("-----------------------\n");
941         while (listing && *listing && !IsEmptyStr(listing)) {
942                 extract_token(buf, listing, 0, '\n', sizeof buf);
943                 remove_token(listing, 0, '\n');
944
945                 extract_token(flnm, buf, 0, '|', sizeof flnm);
946                 extract_token(flsz, buf, 1, '|', sizeof flsz);
947                 extract_token(mimetype, buf, 2, '|', sizeof mimetype);
948                 extract_token(comment, buf, 3, '|', sizeof comment);
949                 if (strlen(flnm) <= 14)
950                         scr_printf("%-14s %8s %s [%s]\n", flnm, flsz, comment, mimetype);
951                 else
952                         scr_printf("%s\n%14s %8s %s [%s]\n", flnm, "", flsz,
953                                 comment, mimetype);
954         }
955         if (listing) free(listing);
956 }
957
958
959 /*
960  * add a user to a private room
961  */
962 void invite(CtdlIPC *ipc)
963 {
964         char username[USERNAME_SIZE];
965         char buf[SIZ];
966
967         newprompt("Name of user? ", username, USERNAME_SIZE);
968         if (username[0] == 0)
969                 return;
970
971         CtdlIPCInviteUserToRoom(ipc, username, buf);
972         scr_printf("%s\n", buf);
973 }
974
975
976 /*
977  * kick a user out of a room
978  */
979 void kickout(CtdlIPC *ipc)
980 {
981         char username[USERNAME_SIZE];
982         char buf[SIZ];
983
984         newprompt("Name of user? ", username, USERNAME_SIZE);
985         if (username[0] == 0)
986                 return;
987
988         CtdlIPCKickoutUserFromRoom(ipc, username, buf);
989         scr_printf("%s\n", buf);
990 }
991
992
993 /*
994  * aide command: kill the current room
995  */
996 void killroom(CtdlIPC *ipc)
997 {
998         char aaa[100];
999         int r;
1000
1001         r = CtdlIPCDeleteRoom(ipc, 0, aaa);
1002         if (r / 100 != 2) {
1003                 scr_printf("%s\n", aaa);
1004                 return;
1005         }
1006
1007         scr_printf("Are you sure you want to kill this room? ");
1008         if (yesno() == 0)
1009                 return;
1010
1011         r = CtdlIPCDeleteRoom(ipc, 1, aaa);
1012         scr_printf("%s\n", aaa);
1013         if (r / 100 != 2)
1014                 return;
1015         dotgoto(ipc, "_BASEROOM_", 0, 0);
1016 }
1017
1018 void forget(CtdlIPC *ipc)
1019 {                               /* forget the current room */
1020         char buf[SIZ];
1021
1022         scr_printf("Are you sure you want to forget this room? ");
1023         if (yesno() == 0)
1024                 return;
1025
1026         remove_march(room_name, 0);
1027         if (CtdlIPCForgetRoom(ipc, buf) / 100 != 2) {
1028                 scr_printf("%s\n", buf);
1029                 return;
1030         }
1031
1032         /* now return to the lobby */
1033         dotgoto(ipc, "_BASEROOM_", 0, 0);
1034 }
1035
1036
1037 /*
1038  * create a new room
1039  */
1040 void entroom(CtdlIPC *ipc)
1041 {
1042         char buf[SIZ];
1043         char new_room_name[ROOMNAMELEN];
1044         int new_room_type;
1045         char new_room_pass[10];
1046         int new_room_floor;
1047         int a, b;
1048         int r;                          /* IPC response code */
1049
1050         /* Check permission to create room */
1051         r = CtdlIPCCreateRoom(ipc, 0, "", 1, "", 0, buf);
1052         if (r / 100 != 2) {
1053                 scr_printf("%s\n", buf);
1054                 return;
1055         }
1056
1057         newprompt("Name for new room? ", new_room_name, ROOMNAMELEN - 1);
1058         if (IsEmptyStr(new_room_name)) {
1059                 return;
1060         }
1061         for (a = 0; !IsEmptyStr(&new_room_name[a]); ++a) {
1062                 if (new_room_name[a] == '|') {
1063                         new_room_name[a] = '_';
1064                 }
1065         }
1066
1067         new_room_floor = select_floor(ipc, (int) curr_floor);
1068
1069         IFNEXPERT formout(ipc, "roomaccess");
1070         do {
1071                 scr_printf("<?>Help\n"
1072                         "<1>Public room (shown to all users by default)\n"
1073                         "<2>Hidden room (accessible to anyone who knows the room name)\n"
1074                         "<3>Passworded room (hidden, plus requires a password to enter)\n"
1075                         "<4>Invitation-only room (requires access to be granted by an Admin)\n"
1076                         "<5>Personal room (accessible to you only)\n"
1077                         "Enter room type: "
1078                 );
1079                 do {
1080                         b = inkey();
1081                 } while (((b < '1') || (b > '5')) && (b != '?'));
1082                 if (b == '?') {
1083                         scr_printf("?\n");
1084                         formout(ipc, "roomaccess");
1085                 }
1086         } while ((b < '1') || (b > '5'));
1087         b -= '0';                       /* Portable */
1088         scr_printf("%d\n", b);
1089         new_room_type = b - 1;
1090         if (new_room_type == 2) {
1091                 newprompt("Enter a room password: ", new_room_pass, 9);
1092                 for (a = 0; !IsEmptyStr(&new_room_pass[a]); ++a)
1093                         if (new_room_pass[a] == '|')
1094                                 new_room_pass[a] = '_';
1095         } else {
1096                 strcpy(new_room_pass, "");
1097         }
1098
1099         scr_printf("\042%s\042, a", new_room_name);
1100         if (b == 1)
1101                 scr_printf(" public room.");
1102         if (b == 2)
1103                 scr_printf(" hidden room.");
1104         if (b == 3)
1105                 scr_printf(" passworded room, password: %s", new_room_pass);
1106         if (b == 4)
1107                 scr_printf("n invitation-only room.");
1108         if (b == 5)
1109                 scr_printf(" personal room.");
1110         scr_printf("\nInstall it? (y/n) : ");
1111         if (yesno() == 0) {
1112                 return;
1113         }
1114
1115         r = CtdlIPCCreateRoom(ipc, 1, new_room_name, new_room_type,
1116                               new_room_pass, new_room_floor, buf);
1117         if (r / 100 != 2) {
1118                 scr_printf("%s\n", buf);
1119                 return;
1120         }
1121
1122         /* command succeeded... now GO to the new room! */
1123         dotgoto(ipc, new_room_name, 0, 0);
1124 }
1125
1126
1127
1128 void readinfo(CtdlIPC *ipc)
1129 {                               /* read info file for current room */
1130         char buf[SIZ];
1131         char room_admin_name[64];
1132         int r;                  /* IPC response code */
1133         char *text = NULL;
1134
1135         /* Name of currernt room admin */
1136         r = CtdlIPCGetRoomAide(ipc, buf);
1137         if (r / 100 == 2)
1138                 safestrncpy(room_admin_name, buf, sizeof room_admin_name);
1139         else
1140                 strcpy(room_admin_name, "");
1141
1142         if (!IsEmptyStr(room_admin_name))
1143                 scr_printf("Room admin is %s.\n\n", room_admin_name);
1144
1145         r = CtdlIPCRoomInfo(ipc, &text, buf);
1146         if (r / 100 != 1)
1147                 return;
1148
1149         if (text) {
1150                 fmout(screenwidth, NULL, text, NULL, 1);
1151                 free(text);
1152         }
1153 }
1154
1155
1156 /*
1157  * <W>ho knows room...
1158  */
1159 void whoknows(CtdlIPC *ipc)
1160 {
1161         char buf[256];
1162         char *listing = NULL;
1163         int r;
1164
1165         r = CtdlIPCWhoKnowsRoom(ipc, &listing, buf);
1166         if (r / 100 != 1) {
1167                 scr_printf("%s\n", buf);
1168                 return;
1169         }
1170         while (!IsEmptyStr(listing)) {
1171                 extract_token(buf, listing, 0, '\n', sizeof buf);
1172                 remove_token(listing, 0, '\n');
1173                 if (sigcaught == 0)
1174                         scr_printf("%s\n", buf);
1175         }
1176         free(listing);
1177 }
1178
1179
1180 void do_edit(CtdlIPC *ipc,
1181                 char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
1182 {
1183         FILE *fp;
1184         char cmd[SIZ];
1185         int b, cksum, editor_exit;
1186
1187         if (IsEmptyStr(editor_path)) {
1188                 scr_printf("Do you wish to re-enter %s? ", desc);
1189                 if (yesno() == 0)
1190                         return;
1191         }
1192
1193         fp = fopen(temp, "w");
1194         fclose(fp);
1195
1196         CtdlIPC_chat_send(ipc, check_cmd);
1197         CtdlIPC_chat_recv(ipc, cmd);
1198         if (cmd[0] != '2') {
1199                 scr_printf("%s\n", &cmd[4]);
1200                 return;
1201         }
1202
1203         if (!IsEmptyStr(editor_path)) {
1204                 CtdlIPC_chat_send(ipc, read_cmd);
1205                 CtdlIPC_chat_recv(ipc, cmd);
1206                 if (cmd[0] == '1') {
1207                         fp = fopen(temp, "w");
1208                         while (CtdlIPC_chat_recv(ipc, cmd), strcmp(cmd, "000")) {
1209                                 fprintf(fp, "%s\n", cmd);
1210                         }
1211                         fclose(fp);
1212                 }
1213         }
1214
1215         cksum = file_checksum(temp);
1216
1217         if (!IsEmptyStr(editor_path)) {
1218                 char tmp[SIZ];
1219
1220                 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", desc);
1221                 putenv(tmp);
1222                 stty_ctdl(SB_RESTORE);
1223                 editor_pid = fork();
1224                 if (editor_pid == 0) {
1225                         chmod(temp, 0600);
1226                         execlp(editor_path, editor_path, temp, NULL);
1227                         exit(1);
1228                 }
1229                 if (editor_pid > 0)
1230                         do {
1231                                 editor_exit = 0;
1232                                 b = ka_wait(&editor_exit);
1233                         } while ((b != editor_pid) && (b >= 0));
1234                 editor_pid = (-1);
1235                 scr_printf("Executed %s\n", editor_path);
1236                 stty_ctdl(0);
1237         } else {
1238                 scr_printf("Entering %s.  Press return twice when finished.\n", desc);
1239                 fp = fopen(temp, "r+");
1240                 citedit(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
1318         load_floorlist(ipc);
1319
1320         /* Fetch the expire policy (this will silently fail on old servers,
1321          * resulting in "default" policy)
1322          */
1323         CtdlIPCGetMessageExpirationPolicy(ipc, 1, &ep, buf);
1324
1325         /* Interact with the user */
1326         scr_printf("You are editing the floor called \"%s\"\n", 
1327                 &floorlist[(int) curr_floor][0] );
1328         strprompt("Floor name", &floorlist[(int) curr_floor][0], 255);
1329
1330         /* Angels and demons dancing in my head... */
1331         do {
1332                 snprintf(buf, sizeof buf, "%d", ep->expire_mode);
1333                 strprompt
1334                     ("Floor default message expire policy (? for list)",
1335                      buf, 1);
1336                 if (buf[0] == '?') {
1337                         scr_printf("\n"
1338                                 "0. Use the system default\n"
1339                                 "1. Never automatically expire messages\n"
1340                                 "2. Expire by message count\n"
1341                                 "3. Expire by message age\n");
1342                 }
1343         } while ((buf[0] < '0') || (buf[0] > '3'));
1344         ep->expire_mode = buf[0] - '0';
1345
1346         /* ...lunatics and monsters underneath my bed */
1347         if (ep->expire_mode == 2) {
1348                 snprintf(buf, sizeof buf, "%d", ep->expire_value);
1349                 strprompt("Keep how many messages online?", buf, 10);
1350                 ep->expire_value = atol(buf);
1351         }
1352
1353         if (ep->expire_mode == 3) {
1354                 snprintf(buf, sizeof buf, "%d", ep->expire_value);
1355                 strprompt("Keep messages for how many days?", buf, 10);
1356                 ep->expire_value = atol(buf);
1357         }
1358
1359         /* Save it */
1360         CtdlIPCSetMessageExpirationPolicy(ipc, 1, ep, buf);
1361         CtdlIPCEditFloor(ipc, curr_floor, &floorlist[(int)curr_floor][0], buf);
1362         scr_printf("%s\n", buf);
1363         load_floorlist(ipc);
1364 }
1365
1366
1367
1368
1369 /*
1370  * kill the current floor 
1371  */
1372 void kill_floor(CtdlIPC *ipc)
1373 {
1374         int floornum_to_delete, a;
1375         char buf[SIZ];
1376
1377         load_floorlist(ipc);
1378         do {
1379                 floornum_to_delete = (-1);
1380                 scr_printf("(Press return to abort)\n");
1381                 newprompt("Delete which floor? ", buf, 255);
1382                 if (IsEmptyStr(buf))
1383                         return;
1384                 for (a = 0; a < 128; ++a)
1385                         if (!strcasecmp(&floorlist[a][0], buf))
1386                                 floornum_to_delete = a;
1387                 if (floornum_to_delete < 0) {
1388                         scr_printf("No such floor.  Select one of:\n");
1389                         for (a = 0; a < 128; ++a)
1390                                 if (floorlist[a][0] != 0)
1391                                         scr_printf("%s\n", &floorlist[a][0]);
1392                 }
1393         } while (floornum_to_delete < 0);
1394         CtdlIPCDeleteFloor(ipc, 1, floornum_to_delete, buf);
1395         scr_printf("%s\n", buf);
1396         load_floorlist(ipc);
1397 }