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