]> code.citadel.org Git - citadel.git/blob - citadel/rooms.c
* Extend GETR/SETR to allow twiddling of bits in the QRflags2 bucket
[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         unsigned rflags2;
391         int rbump;
392         char raide[32];
393         char buf[SIZ];
394         int rfloor;
395         int rorder;
396         int rview;
397         int expire_mode = 0;
398         int expire_value = 0;
399         int r;                          /* IPC response code */
400
401         /* Fetch the existing room config */
402         serv_puts("GETR");
403         serv_gets(buf);
404         if (buf[0] != '2') {
405                 scr_printf("%s\n", &buf[4]);
406                 return;
407         }
408
409         extract(rname, &buf[4], 0);
410         extract(rpass, &buf[4], 1);
411         extract(rdir, &buf[4], 2);
412         rflags = extract_int(&buf[4], 3);
413         rfloor = extract_int(&buf[4], 4);
414         rorder = extract_int(&buf[4], 5);
415         rview = extract_int(&buf[4], 6);
416         rflags2 = extract_int(&buf[4], 7);
417         rbump = 0;
418
419         /* Fetch the name of the current room aide */
420         serv_puts("GETA");
421         serv_gets(buf);
422         if (buf[0] == '2') {
423                 safestrncpy(raide, &buf[4], sizeof raide);
424         }
425         else {
426                 strcpy(raide, "");
427         }
428         if (strlen(raide) == 0) {
429                 strcpy(raide, "none");
430         }
431
432         /* Fetch the expire policy (this will silently fail on old servers,
433          * resulting in "default" policy)
434          */
435         serv_puts("GPEX room");
436         serv_gets(buf);
437         if (buf[0] == '2') {
438                 expire_mode = extract_int(&buf[4], 0);
439                 expire_value = extract_int(&buf[4], 1);
440         }
441
442         /* Now interact with the user. */
443         strprompt("Room name", rname, ROOMNAMELEN - 1);
444
445         rfloor = select_floor(rfloor);
446         rflags = set_room_attr(rflags, "Private room", QR_PRIVATE);
447         if (rflags & QR_PRIVATE) {
448                 rflags = set_room_attr(rflags,
449                                        "Accessible by guessing room name",
450                                        QR_GUESSNAME);
451         }
452
453         /* if it's public, clear the privacy classes */
454         if ((rflags & QR_PRIVATE) == 0) {
455                 if (rflags & QR_GUESSNAME) {
456                         rflags = rflags - QR_GUESSNAME;
457                 }
458                 if (rflags & QR_PASSWORDED) {
459                         rflags = rflags - QR_PASSWORDED;
460                 }
461         }
462
463         /* if it's private, choose the privacy classes */
464         if ((rflags & QR_PRIVATE)
465             && ((rflags & QR_GUESSNAME) == 0)) {
466                 rflags = set_room_attr(rflags,
467                                        "Accessible by entering a password",
468                                        QR_PASSWORDED);
469         }
470         if ((rflags & QR_PRIVATE)
471             && ((rflags & QR_PASSWORDED) == QR_PASSWORDED)) {
472                 strprompt("Room password", rpass, 9);
473         }
474
475         if ((rflags & QR_PRIVATE) == QR_PRIVATE) {
476                 rbump =
477                     boolprompt("Cause current users to forget room", 0);
478         }
479
480         rflags =
481             set_room_attr(rflags, "Preferred users only", QR_PREFONLY);
482         rflags = set_room_attr(rflags, "Read-only room", QR_READONLY);
483         rflags = set_room_attr(rflags, "Directory room", QR_DIRECTORY);
484         rflags = set_room_attr(rflags, "Permanent room", QR_PERMANENT);
485         if (rflags & QR_DIRECTORY) {
486                 strprompt("Directory name", rdir, 14);
487                 rflags =
488                     set_room_attr(rflags, "Uploading allowed", QR_UPLOAD);
489                 rflags =
490                     set_room_attr(rflags, "Downloading allowed",
491                                   QR_DOWNLOAD);
492                 rflags =
493                     set_room_attr(rflags, "Visible directory", QR_VISDIR);
494         }
495         rflags = set_room_attr(rflags, "Network shared room", QR_NETWORK);
496         rflags2 = set_room_attr(rflags2,
497                                 "Self-service list subscribe/unsubscribe",
498                                 QR2_SELFLIST);
499         rflags = set_room_attr(rflags,
500                                "Automatically make all messages anonymous",
501                                QR_ANONONLY);
502         if ((rflags & QR_ANONONLY) == 0) {
503                 rflags = set_room_attr(rflags,
504                                        "Ask users whether to make messages anonymous",
505                                        QR_ANONOPT);
506         }
507         rorder = intprompt("Listing order", rorder, 1, 127);
508
509         /* Ask about the room aide */
510         do {
511                 strprompt("Room aide (or 'none')", raide, 29);
512                 if (!strcasecmp(raide, "none")) {
513                         strcpy(raide, "");
514                         break;
515                 } else {
516                         r = CtdlIPCQueryUsername(raide, buf);
517                         if (r / 100 != 2)
518                                 scr_printf("%s\n", buf);
519                 }
520         } while (r / 100 != 2);
521
522         /* FIXME: Duplicate code??? */
523         if (!strcasecmp(raide, "none")) {
524                 strcpy(raide, "");
525         }
526
527         /* Angels and demons dancing in my head... */
528         do {
529                 snprintf(buf, sizeof buf, "%d", expire_mode);
530                 strprompt("Message expire policy (? for list)", buf, 1);
531                 if (buf[0] == '?') {
532                         scr_printf("\n"
533                                 "0. Use the default for this floor\n"
534                                 "1. Never automatically expire messages\n"
535                                 "2. Expire by message count\n"
536                                 "3. Expire by message age\n");
537                 }
538         } while ((buf[0] < 48) || (buf[0] > 51));
539         expire_mode = buf[0] - 48;
540
541         /* ...lunatics and monsters underneath my bed */
542         if (expire_mode == 2) {
543                 snprintf(buf, sizeof buf, "%d", expire_value);
544                 strprompt("Keep how many messages online?", buf, 10);
545                 expire_value = atol(buf);
546         }
547
548         if (expire_mode == 3) {
549                 snprintf(buf, sizeof buf, "%d", expire_value);
550                 strprompt("Keep messages for how many days?", buf, 10);
551                 expire_value = atol(buf);
552         }
553
554         /* Give 'em a chance to change their minds */
555         scr_printf("Save changes (y/n)? ");
556
557         if (yesno() == 1) {
558                 r = CtdlIPCSetRoomAide(raide, buf);
559                 if (r != 2) {
560                         scr_printf("%s\n", buf);
561                 }
562
563                 r = CtdlIPCSetMessageExpirationPolicy(0, expire_mode,
564                                                       expire_value, buf);
565
566                 snprintf(buf, sizeof buf, "SETR %s|%s|%s|%d|%d|%d|%d|%d|%d",
567                          rname, rpass, rdir, rflags, rbump, rfloor,
568                          rorder, rview, rflags2);
569                 serv_puts(buf);
570                 serv_gets(buf);
571                 scr_printf("%s\n", &buf[4]);
572                 if (buf[0] == '2')
573                         dotgoto(rname, 2, 0);
574         }
575 }
576
577
578 /*
579  * un-goto the previous room
580  */
581 void ungoto(void)
582 {
583         char buf[SIZ];
584
585         if (uglistsize == 0)
586                 return;
587
588         snprintf(buf, sizeof buf, "GOTO %s", uglist[uglistsize-1]); 
589         serv_puts(buf);
590         serv_gets(buf);
591         if (buf[0] != '2') {
592                 scr_printf("%s\n", &buf[4]);
593                 return;
594         }
595         snprintf(buf, sizeof buf, "SLRP %ld", uglistlsn[uglistsize-1]); 
596         serv_puts(buf);
597         serv_gets(buf);
598         if (buf[0] != '2') {
599                 scr_printf("%s\n", &buf[4]);
600         }
601     safestrncpy (buf, uglist[uglistsize-1], sizeof(buf));
602     uglistsize--;
603     free(uglist[uglistsize]);
604         dotgoto(buf, 0, 1); /* Don't queue ungoto info or we end up in a loop */
605 }
606
607
608 /* Here's the code for simply transferring the file to the client,
609  * for folks who have their own clientware.  It's a lot simpler than
610  * the [XYZ]modem code below...
611  * (This function assumes that a download file is already open on the server)
612  */
613 void download_to_local_disk(char *supplied_filename, long total_bytes)
614 {
615         char buf[SIZ];
616         char dbuf[4096];
617         long transmitted_bytes = 0L;
618         long aa, bb;
619         FILE *savefp;
620         int broken = 0;
621         int packet;
622         char filename[SIZ];
623
624         strcpy(filename, supplied_filename);
625         if (strlen(filename) == 0) {
626                 newprompt("Filename: ", filename, 250);
627         }
628
629         scr_printf("Enter the name of the directory to save '%s'\n"
630                 "to, or press return for the current directory.\n", filename);
631         newprompt("Directory: ", dbuf, sizeof dbuf);
632         if (strlen(dbuf) == 0)
633                 strcpy(dbuf, ".");
634         strcat(dbuf, "/");
635         strcat(dbuf, filename);
636
637         savefp = fopen(dbuf, "w");
638         if (savefp == NULL) {
639                 scr_printf("Cannot open '%s': %s\n", dbuf, strerror(errno));
640                 /* close the download file at the server */
641                 serv_puts("CLOS");
642                 serv_gets(buf);
643                 if (buf[0] != '2') {
644                         scr_printf("%s\n", &buf[4]);
645                 }
646                 return;
647         }
648         progress(0, total_bytes);
649         while ((transmitted_bytes < total_bytes) && (broken == 0)) {
650                 bb = total_bytes - transmitted_bytes;
651                 aa = ((bb < 4096) ? bb : 4096);
652                 snprintf(buf, sizeof buf, "READ %ld|%ld", transmitted_bytes, aa);
653                 serv_puts(buf);
654                 serv_gets(buf);
655                 if (buf[0] != '6') {
656                         scr_printf("%s\n", &buf[4]);
657                         return;
658                 }
659                 packet = extract_int(&buf[4], 0);
660                 serv_read(dbuf, packet);
661                 if (fwrite(dbuf, packet, 1, savefp) < 1)
662                         broken = 1;
663                 transmitted_bytes = transmitted_bytes + (long) packet;
664                 progress(transmitted_bytes, total_bytes);
665         }
666         fclose(savefp);
667         /* close the download file at the server */
668         serv_puts("CLOS");
669         serv_gets(buf);
670         if (buf[0] != '2') {
671                 scr_printf("%s\n", &buf[4]);
672         }
673         return;
674 }
675
676
677 /*
678  * download()  -  download a file or files.  The argument passed to this
679  *                function determines which protocol to use.
680  *  proto - 0 = paginate, 1 = xmodem, 2 = raw, 3 = ymodem, 4 = zmodem, 5 = save
681  */
682 void download(int proto)
683 {
684         char buf[SIZ];
685         char filename[SIZ];
686         char tempname[SIZ];
687         char transmit_cmd[SIZ];
688         long total_bytes = 0L;
689         char dbuf[4096];
690         long transmitted_bytes = 0L;
691         long aa, bb;
692         int packet;
693         FILE *tpipe = NULL;
694         int broken = 0;
695
696         if ((room_flags & QR_DOWNLOAD) == 0) {
697                 scr_printf("*** You cannot download from this room.\n");
698                 return;
699         }
700
701         newprompt("Enter filename: ", filename, 255);
702
703         snprintf(buf, sizeof buf, "OPEN %s", filename);
704         serv_puts(buf);
705         serv_gets(buf);
706         if (buf[0] != '2') {
707                 scr_printf("%s\n", &buf[4]);
708                 return;
709         }
710         total_bytes = extract_long(&buf[4], 0);
711
712         /* Save to local disk, for folks with their own copy of the client */
713         if (proto == 5) {
714                 download_to_local_disk(filename, total_bytes);
715                 return;
716         }
717
718         /* Meta-download for public clients */
719         scr_printf("Fetching file from Citadel server...\n");
720         mkdir(tempdir, 0700);
721         snprintf(tempname, sizeof tempname, "%s/%s", tempdir, filename);
722         tpipe = fopen(tempname, "wb");
723         while ((transmitted_bytes < total_bytes) && (broken == 0)) {
724                 progress(transmitted_bytes, total_bytes);
725                 bb = total_bytes - transmitted_bytes;
726                 aa = ((bb < 4096) ? bb : 4096);
727                 snprintf(buf, sizeof buf, "READ %ld|%ld", transmitted_bytes, aa);
728                 serv_puts(buf);
729                 serv_gets(buf);
730                 if (buf[0] != '6') {
731                         scr_printf("%s\n", &buf[4]);
732                 }
733                 packet = extract_int(&buf[4], 0);
734                 serv_read(dbuf, packet);
735                 if (fwrite(dbuf, packet, 1, tpipe) < 1) {
736                         broken = 1;
737                 }
738                 transmitted_bytes = transmitted_bytes + (long) packet;
739         }
740         fclose(tpipe);
741         progress(transmitted_bytes, total_bytes);
742
743         /* close the download file at the server */
744         serv_puts("CLOS");
745         serv_gets(buf);
746         if (buf[0] != '2') {
747                 scr_printf("%s\n", &buf[4]);
748         }
749
750         if (proto == 0) {
751                 snprintf(transmit_cmd, sizeof transmit_cmd,
752                         "SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",
753                         tempname);
754         }
755         else if (proto == 1)
756                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sx %s", tempname);
757         else if (proto == 3)
758                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sb %s", tempname);
759         else if (proto == 4)
760                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec sz %s", tempname);
761         else
762                 snprintf(transmit_cmd, sizeof transmit_cmd, "exec cat %s", tempname);
763
764         screen_reset();
765         sttybbs(SB_RESTORE);
766         system(transmit_cmd);
767         sttybbs(SB_NO_INTR);
768         screen_set();
769
770         /* clean up the temporary directory */
771         nukedir(tempdir);
772         scr_putc(7);
773 }
774
775
776 /*
777  * read directory of this room
778  */
779 void roomdir(void)
780 {
781         char flnm[SIZ];
782         char flsz[32];
783         char comment[SIZ];
784         char buf[SIZ];
785
786         serv_puts("RDIR");
787         serv_gets(buf);
788         if (buf[0] != '1') {
789                 pprintf("%s\n", &buf[4]);
790                 return;
791         }
792
793         extract(comment, &buf[4], 0);
794         extract(flnm, &buf[4], 1);
795         pprintf("\nDirectory of %s on %s\n", flnm, comment);
796         pprintf("-----------------------\n");
797         while (serv_gets(buf), strcmp(buf, "000")) {
798                 extract(flnm, buf, 0);
799                 extract(flsz, buf, 1);
800                 extract(comment, buf, 2);
801                 if (strlen(flnm) <= 14)
802                         pprintf("%-14s %8s %s\n", flnm, flsz, comment);
803                 else
804                         pprintf("%s\n%14s %8s %s\n", flnm, "", flsz,
805                                 comment);
806         }
807 }
808
809
810 /*
811  * add a user to a private room
812  */
813 void invite(void)
814 {
815         char username[USERNAME_SIZE];
816         char buf[SIZ];
817         int r;                          /* IPC response code */
818
819         newprompt("Name of user? ", username, USERNAME_SIZE);
820         if (username[0] == 0)
821                 return;
822
823         r = CtdlIPCInviteUserToRoom(username, buf);
824         scr_printf("%s\n", buf);
825 }
826
827
828 /*
829  * kick a user out of a room
830  */
831 void kickout(void)
832 {
833         char username[USERNAME_SIZE];
834         char buf[SIZ];
835         int r;                          /* IPC response code */
836
837         newprompt("Name of user? ", username, USERNAME_SIZE);
838         if (username[0] == 0)
839                 return;
840
841         r = CtdlIPCKickoutUserFromRoom(username, buf);
842         scr_printf("%s\n", buf);
843 }
844
845
846 /*
847  * aide command: kill the current room
848  */
849 void killroom(void)
850 {
851         char aaa[100];
852         int r;
853
854         r = CtdlIPCDeleteRoom(0, aaa);
855         if (r / 100 != 2) {
856                 scr_printf("%s\n", aaa);
857                 return;
858         }
859
860         scr_printf("Are you sure you want to kill this room? ");
861         if (yesno() == 0)
862                 return;
863
864         r = CtdlIPCDeleteRoom(1, aaa);
865         scr_printf("%s\n", aaa);
866         if (r / 100 != 2)
867                 return;
868         dotgoto("_BASEROOM_", 0, 0);
869 }
870
871 void forget(void)
872 {                               /* forget the current room */
873         char buf[SIZ];
874
875         scr_printf("Are you sure you want to forget this room? ");
876         if (yesno() == 0)
877                 return;
878
879         if (CtdlIPCForgetRoom(buf) / 100 != 2) {
880                 scr_printf("%s\n", buf);
881                 return;
882         }
883
884         /* now return to the lobby */
885         dotgoto("_BASEROOM_", 0, 0);
886 }
887
888
889 /*
890  * create a new room
891  */
892 void entroom(void)
893 {
894         char buf[SIZ];
895         char new_room_name[ROOMNAMELEN];
896         int new_room_type;
897         char new_room_pass[10];
898         int new_room_floor;
899         int a, b;
900         int r;                          /* IPC response code */
901
902         /* Check permission to create room */
903         r = CtdlIPCCreateRoom(0, "", 1, "", 0, buf);
904         if (r / 100 != 2) {
905                 scr_printf("%s\n", buf);
906                 return;
907         }
908
909         newprompt("Name for new room? ", new_room_name, ROOMNAMELEN - 1);
910         if (strlen(new_room_name) == 0) {
911                 return;
912         }
913         for (a = 0; a < strlen(new_room_name); ++a) {
914                 if (new_room_name[a] == '|') {
915                         new_room_name[a] = '_';
916                 }
917         }
918
919         new_room_floor = select_floor((int) curr_floor);
920
921         IFNEXPERT formout("roomaccess");
922         do {
923                 scr_printf("<?>Help\n<1>Public room\n<2>Guess-name room\n"
924                        "<3>Passworded room\n<4>Invitation-only room\n"
925                        "<5>Personal room\n"
926                         "Enter room type: ");
927                 do {
928                         b = inkey();
929                 } while (((b < '1') || (b > '5')) && (b != '?'));
930                 if (b == '?') {
931                         scr_printf("?\n");
932                         formout("roomaccess");
933                 }
934         } while ((b < '1') || (b > '5'));
935         b -= '0';                       /* Portable */
936         scr_printf("%d\n", b);
937         new_room_type = b - 1;
938         if (new_room_type == 2) {
939                 newprompt("Enter a room password: ", new_room_pass, 9);
940                 for (a = 0; a < strlen(new_room_pass); ++a)
941                         if (new_room_pass[a] == '|')
942                                 new_room_pass[a] = '_';
943         } else {
944                 strcpy(new_room_pass, "");
945         }
946
947         scr_printf("\042%s\042, a", new_room_name);
948         if (b == 1)
949                 scr_printf(" public room.");
950         if (b == 2)
951                 scr_printf(" guess-name room.");
952         if (b == 3)
953                 scr_printf(" passworded room, password: %s", new_room_pass);
954         if (b == 4)
955                 scr_printf("n invitation-only room.");
956         if (b == 5)
957                 scr_printf(" personal room.");
958         scr_printf("\nInstall it? (y/n) : ");
959         if (yesno() == 0) {
960                 return;
961         }
962
963         r = CtdlIPCCreateRoom(1, new_room_name, new_room_type,
964                               new_room_pass, new_room_floor, buf);
965         if (r / 100 != 2) {
966                 scr_printf("%s\n", buf);
967                 return;
968         }
969
970         /* command succeeded... now GO to the new room! */
971         dotgoto(new_room_name, 0, 0);
972 }
973
974
975
976 void readinfo(void)
977 {                               /* read info file for current room */
978         char buf[SIZ];
979         char raide[64];
980         int r;                  /* IPC response code */
981         char *text = NULL;
982
983         /* Name of currernt room aide */
984         r = CtdlIPCGetRoomAide(buf);
985         if (r / 100 == 2)
986                 safestrncpy(raide, buf, sizeof raide);
987         else
988                 strcpy(raide, "");
989
990         if (strlen(raide) > 0)
991                 scr_printf("Room aide is %s.\n\n", raide);
992
993         r = CtdlIPCRoomInfo(&text, buf);
994         if (r / 100 != 1)
995                 return;
996
997         if (text) {
998                 fmout(screenwidth, NULL, text, NULL,
999                       ((userflags & US_PAGINATOR) ? 1 : 0), screenheight, 
1000                       (*raide) ? 2 : 0, 1);
1001                 free(text);
1002         }
1003 }
1004
1005
1006 /*
1007  * <W>ho knows room...
1008  */
1009 void whoknows(void)
1010 {
1011         char buf[SIZ];
1012         serv_puts("WHOK");
1013         serv_gets(buf);
1014         if (buf[0] != '1') {
1015                 pprintf("%s\n", &buf[4]);
1016                 return;
1017         }
1018         while (serv_gets(buf), strncmp(buf, "000", 3)) {
1019                 if (sigcaught == 0)
1020                         pprintf("%s\n", buf);
1021         }
1022 }
1023
1024
1025 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
1026 {
1027         FILE *fp;
1028         char cmd[SIZ];
1029         int b, cksum, editor_exit;
1030
1031
1032         if (strlen(editor_path) == 0) {
1033                 scr_printf("Do you wish to re-enter %s? ", desc);
1034                 if (yesno() == 0)
1035                         return;
1036         }
1037
1038         fp = fopen(temp, "w");
1039         fclose(fp);
1040
1041         serv_puts(check_cmd);
1042         serv_gets(cmd);
1043         if (cmd[0] != '2') {
1044                 scr_printf("%s\n", &cmd[4]);
1045                 return;
1046         }
1047
1048         if (strlen(editor_path) > 0) {
1049                 serv_puts(read_cmd);
1050                 serv_gets(cmd);
1051                 if (cmd[0] == '1') {
1052                         fp = fopen(temp, "w");
1053                         while (serv_gets(cmd), strcmp(cmd, "000")) {
1054                                 fprintf(fp, "%s\n", cmd);
1055                         }
1056                         fclose(fp);
1057                 }
1058         }
1059
1060         cksum = file_checksum(temp);
1061
1062         if (strlen(editor_path) > 0) {
1063                 char tmp[SIZ];
1064
1065                 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", desc);
1066                 putenv(tmp);
1067                 editor_pid = fork();
1068                 if (editor_pid == 0) {
1069                         chmod(temp, 0600);
1070                         screen_reset();
1071                         sttybbs(SB_RESTORE);
1072                         execlp(editor_path, editor_path, temp, NULL);
1073                         exit(1);
1074                 }
1075                 if (editor_pid > 0)
1076                         do {
1077                                 editor_exit = 0;
1078                                 b = wait(&editor_exit);
1079                         } while ((b != editor_pid) && (b >= 0));
1080                 editor_pid = (-1);
1081                 scr_printf("Executed %s\n", editor_path);
1082                 sttybbs(0);
1083                 screen_set();
1084         } else {
1085                 scr_printf("Entering %s.  "
1086                         "Press return twice when finished.\n", desc);
1087                 fp = fopen(temp, "r+");
1088                 citedit(fp);
1089                 fclose(fp);
1090         }
1091
1092         if (file_checksum(temp) == cksum) {
1093                 scr_printf("*** Aborted.\n");
1094         }
1095
1096         else {
1097                 serv_puts(write_cmd);
1098                 serv_gets(cmd);
1099                 if (cmd[0] != '4') {
1100                         scr_printf("%s\n", &cmd[4]);
1101                         return;
1102                 }
1103
1104                 fp = fopen(temp, "r");
1105                 while (fgets(cmd, SIZ - 1, fp) != NULL) {
1106                         cmd[strlen(cmd) - 1] = 0;
1107                         serv_puts(cmd);
1108                 }
1109                 fclose(fp);
1110                 serv_puts("000");
1111         }
1112
1113         unlink(temp);
1114 }
1115
1116
1117 void enterinfo(void)
1118 {                               /* edit info file for current room */
1119         do_edit("the Info file for this room", "RINF", "EINF 0", "EINF 1");
1120 }
1121
1122 void enter_bio(void)
1123 {
1124         char cmd[SIZ];
1125         snprintf(cmd, sizeof cmd, "RBIO %s", fullname);
1126         do_edit("your Bio", cmd, "NOOP", "EBIO");
1127 }
1128
1129 /*
1130  * create a new floor
1131  */
1132 void create_floor(void)
1133 {
1134         char buf[SIZ];
1135         char newfloorname[SIZ];
1136         int r;                  /* IPC response code */
1137
1138         load_floorlist();
1139
1140         r = CtdlIPCCreateFloor(0, "", buf);
1141         if (r / 100 != 2) {
1142                 scr_printf("%s\n", buf);
1143                 return;
1144         }
1145
1146         newprompt("Name for new floor: ", newfloorname, 255);
1147         r = CtdlIPCCreateFloor(1, newfloorname, buf);
1148         if (r / 100 == 2) {
1149                 scr_printf("Floor has been created.\n");
1150         } else {
1151                 scr_printf("%s\n", buf);
1152         }
1153
1154         load_floorlist();
1155 }
1156
1157 /*
1158  * edit the current floor
1159  */
1160 void edit_floor(void)
1161 {
1162         char buf[SIZ];
1163         int expire_mode = 0;
1164         int expire_value = 0;
1165         int r;                          /* IPC response code */
1166
1167         load_floorlist();
1168
1169         /* Fetch the expire policy (this will silently fail on old servers,
1170          * resulting in "default" policy)
1171          */
1172         r = CtdlIPCGetMessageExpirationPolicy(1, buf);
1173         if (r / 100 == 2) {
1174                 expire_mode = extract_int(buf, 0);
1175                 expire_value = extract_int(buf, 1);
1176         }
1177
1178         /* Interact with the user */
1179         scr_printf("You are editing the floor called \"%s\"\n", 
1180                 &floorlist[(int) curr_floor][0] );
1181         strprompt("Floor name", &floorlist[(int) curr_floor][0], 255);
1182
1183         /* Angels and demons dancing in my head... */
1184         do {
1185                 snprintf(buf, sizeof buf, "%d", expire_mode);
1186                 strprompt
1187                     ("Floor default message expire policy (? for list)",
1188                      buf, 1);
1189                 if (buf[0] == '?') {
1190                         scr_printf("\n"
1191                                 "0. Use the system default\n"
1192                                 "1. Never automatically expire messages\n"
1193                                 "2. Expire by message count\n"
1194                                 "3. Expire by message age\n");
1195                 }
1196         } while ((buf[0] < '0') || (buf[0] > '3'));
1197         expire_mode = buf[0] - '0';
1198
1199         /* ...lunatics and monsters underneath my bed */
1200         if (expire_mode == 2) {
1201                 snprintf(buf, sizeof buf, "%d", expire_value);
1202                 strprompt("Keep how many messages online?", buf, 10);
1203                 expire_value = atol(buf);
1204         }
1205
1206         if (expire_mode == 3) {
1207                 snprintf(buf, sizeof buf, "%d", expire_value);
1208                 strprompt("Keep messages for how many days?", buf, 10);
1209                 expire_value = atol(buf);
1210         }
1211
1212         /* Save it */
1213         r = CtdlIPCSetMessageExpirationPolicy(1, expire_mode,
1214                                               expire_value, buf);
1215         r = CtdlIPCEditFloor(curr_floor, &floorlist[(int)curr_floor][0], buf);
1216         scr_printf("%s\n", buf);
1217         load_floorlist();
1218 }
1219
1220
1221
1222
1223 /*
1224  * kill the current floor 
1225  */
1226 void kill_floor(void)
1227 {
1228         int floornum_to_delete, a;
1229         char buf[SIZ];
1230
1231         load_floorlist();
1232         do {
1233                 floornum_to_delete = (-1);
1234                 scr_printf("(Press return to abort)\n");
1235                 newprompt("Delete which floor? ", buf, 255);
1236                 if (strlen(buf) == 0)
1237                         return;
1238                 for (a = 0; a < 128; ++a)
1239                         if (!strcasecmp(&floorlist[a][0], buf))
1240                                 floornum_to_delete = a;
1241                 if (floornum_to_delete < 0) {
1242                         scr_printf("No such floor.  Select one of:\n");
1243                         for (a = 0; a < 128; ++a)
1244                                 if (floorlist[a][0] != 0)
1245                                         scr_printf("%s\n", &floorlist[a][0]);
1246                 }
1247         } while (floornum_to_delete < 0);
1248         CtdlIPCDeleteFloor(1, floornum_to_delete, buf);
1249         scr_printf("%s\n", buf);
1250         load_floorlist();
1251 }