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