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