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