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