5 * Client-side functions which perform room operations
17 #include <sys/types.h>
32 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
35 void sttybbs(int cmd);
36 void hit_any_key(void);
38 void strprompt(char *prompt, char *str, int len);
39 void newprompt(char *prompt, char *str, int len);
40 void dotgoto(char *towhere, int display_name);
41 void serv_read(char *buf, int bytes);
42 void formout(char *name);
44 void progress(long int curr, long int cmax);
45 int pattern(char *search, char *patn);
46 int file_checksum(char *filename);
47 int nukedir(char *dirname);
49 extern unsigned room_flags;
50 extern char room_name[];
52 extern char tempdir[];
53 extern int editor_pid;
54 extern char editor_path[];
55 extern int screenwidth;
56 extern int screenheight;
57 extern char fullname[];
59 extern char sigcaught;
60 extern char floor_mode;
61 extern char curr_floor;
68 extern char floorlist[128][SIZ];
71 void load_floorlist(void)
76 for (a = 0; a < 128; ++a)
82 strcpy(floorlist[0], "Main Floor");
85 while (serv_gets(buf), strcmp(buf, "000")) {
86 extract(floorlist[extract_int(buf, 0)], buf, 1);
91 void room_tree_list(struct roomlisting *rp)
94 char rmname[ROOMNAMELEN];
102 if (rp->lnext != NULL) {
103 room_tree_list(rp->lnext);
106 if (sigcaught == 0) {
107 strcpy(rmname, rp->rlname);
109 if ((c + strlen(rmname) + 4) > screenwidth) {
111 /* line break, check the paginator */
115 if (f & QR_MAILBOX) {
116 color(BRIGHT_YELLOW);
117 } else if (f & QR_PRIVATE) {
122 pprintf("%s", rmname);
123 if ((f & QR_DIRECTORY) && (f & QR_NETWORK))
125 else if (f & QR_DIRECTORY)
127 else if (f & QR_NETWORK)
131 c = c + strlen(rmname) + 3;
134 if (rp->rnext != NULL) {
135 room_tree_list(rp->rnext);
143 * Room ordering stuff (compare first by floor, then by order)
145 int rordercmp(struct roomlisting *r1, struct roomlisting *r2)
147 if ((r1 == NULL) && (r2 == NULL))
153 if (r1->rlfloor < r2->rlfloor)
155 if (r1->rlfloor > r2->rlfloor)
157 if (r1->rlorder < r2->rlorder)
159 if (r1->rlorder > r2->rlorder)
166 * Common code for all room listings
168 void listrms(char *variety)
172 struct roomlisting *rl = NULL;
173 struct roomlisting *rp;
174 struct roomlisting *rs;
177 /* Ask the server for a room list */
183 while (serv_gets(buf), strcmp(buf, "000")) {
184 rp = malloc(sizeof(struct roomlisting));
185 extract(rp->rlname, buf, 0);
186 rp->rlflags = extract_int(buf, 1);
187 rp->rlfloor = extract_int(buf, 2);
188 rp->rlorder = extract_int(buf, 3);
197 if (rordercmp(rp, rs) < 0) {
198 if (rs->lnext == NULL) {
205 if (rs->rnext == NULL) {
216 room_tree_list(NULL);
222 void list_other_floors(void)
227 for (a = 0; a < 128; ++a) {
228 if ((strlen(floorlist[a]) > 0) && (a != curr_floor)) {
229 if ((c + strlen(floorlist[a]) + 4) > screenwidth) {
233 pprintf("%s: ", floorlist[a]);
234 c = c + strlen(floorlist[a]) + 3;
241 * List known rooms. kn_floor_mode should be set to 0 for a 'flat' listing,
242 * 1 to list rooms on the current floor, or 1 to list rooms on all floors.
244 void knrooms(int kn_floor_mode)
251 if (kn_floor_mode == 0) {
253 pprintf("\n Rooms with unread messages:\n");
256 pprintf("\n\n No unseen messages in:\n");
261 if (kn_floor_mode == 1) {
263 pprintf("\n Rooms with unread messages on %s:\n",
264 floorlist[(int) curr_floor]);
265 sprintf(buf, "LKRN %d", curr_floor);
268 pprintf("\n\n Rooms with no new messages on %s:\n",
269 floorlist[(int) curr_floor]);
270 sprintf(buf, "LKRO %d", curr_floor);
273 pprintf("\n\n Other floors:\n");
278 if (kn_floor_mode == 2) {
279 for (a = 0; a < 128; ++a) {
280 if (floorlist[a][0] != 0) {
282 pprintf("\n Rooms on %s:\n",
284 sprintf(buf, "LKRA %d", a);
292 IFNEXPERT hit_any_key();
296 void listzrooms(void)
297 { /* list public forgotten rooms */
299 pprintf("\n Forgotten public rooms:\n");
303 IFNEXPERT hit_any_key();
307 int set_room_attr(int ibuf, char *prompt, unsigned int sbit)
311 a = boolprompt(prompt, (ibuf & sbit));
312 ibuf = (ibuf | sbit);
314 ibuf = (ibuf ^ sbit);
322 * Select a floor (used in several commands)
323 * The supplied argument is the 'default' floor number.
324 * This function returns the selected floor number.
326 int select_floor(int rfloor)
331 if (floor_mode == 1) {
332 if (floorlist[(int) curr_floor][0] == 0) {
338 safestrncpy(floorstr, floorlist[rfloor],
340 strprompt("Which floor", floorstr, SIZ);
341 for (a = 0; a < 128; ++a) {
343 (floorstr, &floorlist[a][0]))
348 (floorstr, &floorlist[a][0],
352 && (pattern(&floorlist[a][0], floorstr)
357 scr_printf("\n One of:\n");
358 for (a = 0; a < 128; ++a) {
359 if (floorlist[a][0] != 0) {
365 } while (newfloor < 0);
375 * .<A>ide <E>dit room
377 void editthisroom(void)
379 char rname[ROOMNAMELEN];
389 int expire_value = 0;
391 /* Fetch the existing room config */
395 scr_printf("%s\n", &buf[4]);
399 extract(rname, &buf[4], 0);
400 extract(rpass, &buf[4], 1);
401 extract(rdir, &buf[4], 2);
402 rflags = extract_int(&buf[4], 3);
403 rfloor = extract_int(&buf[4], 4);
404 rorder = extract_int(&buf[4], 5);
407 /* Fetch the name of the current room aide */
411 safestrncpy(raide, &buf[4], sizeof raide);
416 if (strlen(raide) == 0) {
417 strcpy(raide, "none");
420 /* Fetch the expire policy (this will silently fail on old servers,
421 * resulting in "default" policy)
423 serv_puts("GPEX room");
426 expire_mode = extract_int(&buf[4], 0);
427 expire_value = extract_int(&buf[4], 1);
430 /* Now interact with the user. */
431 strprompt("Room name", rname, ROOMNAMELEN - 1);
433 rfloor = select_floor(rfloor);
434 rflags = set_room_attr(rflags, "Private room", QR_PRIVATE);
435 if (rflags & QR_PRIVATE) {
436 rflags = set_room_attr(rflags,
437 "Accessible by guessing room name",
441 /* if it's public, clear the privacy classes */
442 if ((rflags & QR_PRIVATE) == 0) {
443 if (rflags & QR_GUESSNAME) {
444 rflags = rflags - QR_GUESSNAME;
446 if (rflags & QR_PASSWORDED) {
447 rflags = rflags - QR_PASSWORDED;
451 /* if it's private, choose the privacy classes */
452 if ((rflags & QR_PRIVATE)
453 && ((rflags & QR_GUESSNAME) == 0)) {
454 rflags = set_room_attr(rflags,
455 "Accessible by entering a password",
458 if ((rflags & QR_PRIVATE)
459 && ((rflags & QR_PASSWORDED) == QR_PASSWORDED)) {
460 strprompt("Room password", rpass, 9);
463 if ((rflags & QR_PRIVATE) == QR_PRIVATE) {
465 boolprompt("Cause current users to forget room", 0);
469 set_room_attr(rflags, "Preferred users only", QR_PREFONLY);
470 rflags = set_room_attr(rflags, "Read-only room", QR_READONLY);
471 rflags = set_room_attr(rflags, "Directory room", QR_DIRECTORY);
472 rflags = set_room_attr(rflags, "Permanent room", QR_PERMANENT);
473 if (rflags & QR_DIRECTORY) {
474 strprompt("Directory name", rdir, 14);
476 set_room_attr(rflags, "Uploading allowed", QR_UPLOAD);
478 set_room_attr(rflags, "Downloading allowed",
481 set_room_attr(rflags, "Visible directory", QR_VISDIR);
483 rflags = set_room_attr(rflags, "Network shared room", QR_NETWORK);
484 rflags = set_room_attr(rflags,
485 "Automatically make all messages anonymous",
487 if ((rflags & QR_ANONONLY) == 0) {
488 rflags = set_room_attr(rflags,
489 "Ask users whether to make messages anonymous",
492 rorder = intprompt("Listing order", rorder, 1, 127);
494 /* Ask about the room aide */
496 strprompt("Room aide (or 'none')", raide, 29);
497 if (!strcasecmp(raide, "none")) {
501 snprintf(buf, sizeof buf, "QUSR %s", raide);
505 scr_printf("%s\n", &buf[4]);
507 } while (buf[0] != '2');
509 if (!strcasecmp(raide, "none")) {
514 /* Angels and demons dancing in my head... */
516 sprintf(buf, "%d", expire_mode);
517 strprompt("Message expire policy (? for list)", buf, 1);
520 "0. Use the default for this floor\n"
521 "1. Never automatically expire messages\n"
522 "2. Expire by message count\n"
523 "3. Expire by message age\n");
525 } while ((buf[0] < 48) || (buf[0] > 51));
526 expire_mode = buf[0] - 48;
528 /* ...lunatics and monsters underneath my bed */
529 if (expire_mode == 2) {
530 sprintf(buf, "%d", expire_value);
531 strprompt("Keep how many messages online?", buf, 10);
532 expire_value = atol(buf);
535 if (expire_mode == 3) {
536 sprintf(buf, "%d", expire_value);
537 strprompt("Keep messages for how many days?", buf, 10);
538 expire_value = atol(buf);
541 /* Give 'em a chance to change their minds */
542 scr_printf("Save changes (y/n)? ");
545 snprintf(buf, sizeof buf, "SETA %s", raide);
549 scr_printf("%s\n", &buf[4]);
552 snprintf(buf, sizeof buf, "SPEX room|%d|%d",
553 expire_mode, expire_value);
557 snprintf(buf, sizeof buf, "SETR %s|%s|%s|%d|%d|%d|%d",
558 rname, rpass, rdir, rflags, rbump, rfloor,
562 scr_printf("%s\n", &buf[4]);
570 * un-goto the previous room
576 if (!strcmp(ugname, ""))
578 snprintf(buf, sizeof buf, "GOTO %s", ugname);
582 scr_printf("%s\n", &buf[4]);
585 sprintf(buf, "SLRP %ld", uglsn);
589 scr_printf("%s\n", &buf[4]);
591 safestrncpy(buf, ugname, sizeof buf);
597 /* Here's the code for simply transferring the file to the client,
598 * for folks who have their own clientware. It's a lot simpler than
599 * the [XYZ]modem code below...
600 * (This function assumes that a download file is already open on the server)
602 void download_to_local_disk(char *supplied_filename, long total_bytes)
606 long transmitted_bytes = 0L;
613 strcpy(filename, supplied_filename);
614 if (strlen(filename) == 0) {
615 newprompt("Filename: ", filename, 250);
618 scr_printf("Enter the name of the directory to save '%s'\n"
619 "to, or press return for the current directory.\n", filename);
620 newprompt("Directory: ", dbuf, sizeof dbuf);
621 if (strlen(dbuf) == 0)
624 strcat(dbuf, filename);
626 savefp = fopen(dbuf, "w");
627 if (savefp == NULL) {
628 scr_printf("Cannot open '%s': %s\n", dbuf, strerror(errno));
629 /* close the download file at the server */
633 scr_printf("%s\n", &buf[4]);
637 progress(0, total_bytes);
638 while ((transmitted_bytes < total_bytes) && (broken == 0)) {
639 bb = total_bytes - transmitted_bytes;
640 aa = ((bb < 4096) ? bb : 4096);
641 sprintf(buf, "READ %ld|%ld", transmitted_bytes, aa);
645 scr_printf("%s\n", &buf[4]);
648 packet = extract_int(&buf[4], 0);
649 serv_read(dbuf, packet);
650 if (fwrite(dbuf, packet, 1, savefp) < 1)
652 transmitted_bytes = transmitted_bytes + (long) packet;
653 progress(transmitted_bytes, total_bytes);
656 /* close the download file at the server */
660 scr_printf("%s\n", &buf[4]);
667 * download() - download a file or files. The argument passed to this
668 * function determines which protocol to use.
669 * proto - 0 = paginate, 1 = xmodem, 2 = raw, 3 = ymodem, 4 = zmodem, 5 = save
671 void download(int proto)
676 char transmit_cmd[SIZ];
677 long total_bytes = 0L;
679 long transmitted_bytes = 0L;
685 if ((room_flags & QR_DOWNLOAD) == 0) {
686 scr_printf("*** You cannot download from this room.\n");
690 newprompt("Enter filename: ", filename, 255);
692 snprintf(buf, sizeof buf, "OPEN %s", filename);
696 scr_printf("%s\n", &buf[4]);
699 total_bytes = extract_long(&buf[4], 0);
701 /* Save to local disk, for folks with their own copy of the client */
703 download_to_local_disk(filename, total_bytes);
707 /* Meta-download for public clients */
708 scr_printf("Fetching file from Citadel server...\n");
709 mkdir(tempdir, 0700);
710 snprintf(tempname, sizeof tempname, "%s/%s", tempdir, filename);
711 tpipe = fopen(tempname, "wb");
712 while ((transmitted_bytes < total_bytes) && (broken == 0)) {
713 progress(transmitted_bytes, total_bytes);
714 bb = total_bytes - transmitted_bytes;
715 aa = ((bb < 4096) ? bb : 4096);
716 sprintf(buf, "READ %ld|%ld", transmitted_bytes, aa);
720 scr_printf("%s\n", &buf[4]);
722 packet = extract_int(&buf[4], 0);
723 serv_read(dbuf, packet);
724 if (fwrite(dbuf, packet, 1, tpipe) < 1) {
727 transmitted_bytes = transmitted_bytes + (long) packet;
730 progress(transmitted_bytes, total_bytes);
732 /* close the download file at the server */
736 scr_printf("%s\n", &buf[4]);
740 sprintf(transmit_cmd,
741 "SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",
745 sprintf(transmit_cmd, "exec sx %s", tempname);
747 sprintf(transmit_cmd, "exec sb %s", tempname);
749 sprintf(transmit_cmd, "exec sz %s", tempname);
751 sprintf(transmit_cmd, "exec cat %s", tempname);
755 system(transmit_cmd);
759 /* clean up the temporary directory */
766 * read directory of this room
778 pprintf("%s\n", &buf[4]);
782 extract(comment, &buf[4], 0);
783 extract(flnm, &buf[4], 1);
784 pprintf("\nDirectory of %s on %s\n", flnm, comment);
785 pprintf("-----------------------\n");
786 while (serv_gets(buf), strcmp(buf, "000")) {
787 extract(flnm, buf, 0);
788 extract(flsz, buf, 1);
789 extract(comment, buf, 2);
790 if (strlen(flnm) <= 14)
791 pprintf("%-14s %8s %s\n", flnm, flsz, comment);
793 pprintf("%s\n%14s %8s %s\n", flnm, "", flsz,
800 * add a user to a private room
804 char aaa[31], bbb[SIZ];
806 /* Because kicking people out of public rooms now sets a LOCKOUT
807 * flag, we need to be able to invite people into public rooms
808 * in order to let them back in again.
813 * if ((room_flags & QR_PRIVATE)==0) {
814 * scr_printf("This is not a private room.\n");
819 newprompt("Name of user? ", aaa, 30);
823 snprintf(bbb, sizeof bbb, "INVT %s", aaa);
826 scr_printf("%s\n", &bbb[4]);
831 * kick a user out of a room
835 char username[31], cmd[SIZ];
837 newprompt("Name of user? ", username, 30);
838 if (strlen(username) == 0) {
842 snprintf(cmd, sizeof cmd, "KICK %s", username);
845 scr_printf("%s\n", &cmd[4]);
850 * aide command: kill the current room
859 scr_printf("%s\n", &aaa[4]);
863 scr_printf("Are you sure you want to kill this room? ");
869 scr_printf("%s\n", &aaa[4]);
872 dotgoto("_BASEROOM_", 0);
876 { /* forget the current room */
879 scr_printf("Are you sure you want to forget this room? ");
886 scr_printf("%s\n", &cmd[4]);
890 /* now return to the lobby */
891 dotgoto("_BASEROOM_", 0);
901 char new_room_name[ROOMNAMELEN];
903 char new_room_pass[10];
911 scr_printf("%s\n", &cmd[4]);
915 newprompt("Name for new room? ", new_room_name, ROOMNAMELEN - 1);
916 if (strlen(new_room_name) == 0) {
919 for (a = 0; a < strlen(new_room_name); ++a) {
920 if (new_room_name[a] == '|') {
921 new_room_name[a] = '_';
925 new_room_floor = select_floor((int) curr_floor);
927 IFNEXPERT formout("roomaccess");
929 scr_printf("<?>Help\n<1>Public room\n<2>Guess-name room\n"
930 "<3>Passworded room\n<4>Invitation-only room\n"
932 "Enter room type: ");
935 } while (((b < '1') || (b > '5')) && (b != '?'));
938 formout("roomaccess");
940 } while ((b < '1') || (b > '5'));
942 scr_printf("%d\n", b);
943 new_room_type = b - 1;
944 if (new_room_type == 2) {
945 newprompt("Enter a room password: ", new_room_pass, 9);
946 for (a = 0; a < strlen(new_room_pass); ++a)
947 if (new_room_pass[a] == '|')
948 new_room_pass[a] = '_';
950 strcpy(new_room_pass, "");
953 scr_printf("\042%s\042, a", new_room_name);
955 scr_printf(" public room.");
957 scr_printf(" guess-name room.");
959 scr_printf(" passworded room, password: %s", new_room_pass);
961 scr_printf("n invitation-only room.");
963 scr_printf(" personal room.");
964 scr_printf("\nInstall it? (y/n) : ");
969 snprintf(cmd, sizeof cmd, "CRE8 1|%s|%d|%s|%d", new_room_name,
970 new_room_type, new_room_pass, new_room_floor);
974 scr_printf("%s\n", &cmd[4]);
978 /* command succeeded... now GO to the new room! */
979 dotgoto(new_room_name, 0);
985 { /* read info file for current room */
988 sprintf(cmd, "RINF");
996 fmout(screenwidth, NULL, NULL,
997 ((userflags & US_PAGINATOR) ? 1 : 0), screenheight, 0, 1);
1002 * <W>ho knows room...
1009 if (buf[0] != '1') {
1010 pprintf("%s\n", &buf[5]);
1013 while (serv_gets(buf), strncmp(buf, "000", 3)) {
1015 pprintf("%s\n", buf);
1020 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
1024 int b, cksum, editor_exit;
1027 if (strlen(editor_path) == 0) {
1028 scr_printf("Do you wish to re-enter %s? ", desc);
1033 fp = fopen(temp, "w");
1036 serv_puts(check_cmd);
1038 if (cmd[0] != '2') {
1039 scr_printf("%s\n", &cmd[4]);
1043 if (strlen(editor_path) > 0) {
1044 serv_puts(read_cmd);
1046 if (cmd[0] == '1') {
1047 fp = fopen(temp, "w");
1048 while (serv_gets(cmd), strcmp(cmd, "000")) {
1049 fprintf(fp, "%s\n", cmd);
1055 cksum = file_checksum(temp);
1057 if (strlen(editor_path) > 0) {
1058 setenv("WINDOW_TITLE", desc, 1);
1059 editor_pid = fork();
1060 if (editor_pid == 0) {
1063 sttybbs(SB_RESTORE);
1064 execlp(editor_path, editor_path, temp, NULL);
1070 b = wait(&editor_exit);
1071 } while ((b != editor_pid) && (b >= 0));
1073 scr_printf("Executed %s\n", editor_path);
1077 scr_printf("Entering %s. "
1078 "Press return twice when finished.\n", desc);
1079 fp = fopen(temp, "r+");
1084 if (file_checksum(temp) == cksum) {
1085 scr_printf("*** Aborted.\n");
1089 serv_puts(write_cmd);
1091 if (cmd[0] != '4') {
1092 scr_printf("%s\n", &cmd[4]);
1096 fp = fopen(temp, "r");
1097 while (fgets(cmd, SIZ - 1, fp) != NULL) {
1098 cmd[strlen(cmd) - 1] = 0;
1109 void enterinfo(void)
1110 { /* edit info file for current room */
1111 do_edit("the Info file for this room", "RINF", "EINF 0", "EINF 1");
1114 void enter_bio(void)
1117 snprintf(cmd, sizeof cmd, "RBIO %s", fullname);
1118 do_edit("your Bio", cmd, "NOOP", "EBIO");
1122 * create a new floor
1124 void create_floor(void)
1127 char newfloorname[SIZ];
1131 serv_puts("CFLR xx|0");
1133 if (buf[0] != '2') {
1134 scr_printf("%s\n", &buf[4]);
1138 newprompt("Name for new floor: ", newfloorname, 255);
1139 snprintf(buf, sizeof buf, "CFLR %s|1", newfloorname);
1142 if (buf[0] == '2') {
1143 scr_printf("Floor has been created.\n");
1145 scr_printf("%s\n", &buf[4]);
1152 * edit the current floor
1154 void edit_floor(void)
1157 int expire_mode = 0;
1158 int expire_value = 0;
1162 /* Fetch the expire policy (this will silently fail on old servers,
1163 * resulting in "default" policy)
1165 serv_puts("GPEX floor");
1167 if (buf[0] == '2') {
1168 expire_mode = extract_int(&buf[4], 0);
1169 expire_value = extract_int(&buf[4], 1);
1172 /* Interact with the user */
1173 strprompt("Floor name", &floorlist[(int) curr_floor][0], 255);
1175 /* Angels and demons dancing in my head... */
1177 sprintf(buf, "%d", expire_mode);
1179 ("Floor default essage expire policy (? for list)",
1181 if (buf[0] == '?') {
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");
1188 } while ((buf[0] < 48) || (buf[0] > 51));
1189 expire_mode = buf[0] - 48;
1191 /* ...lunatics and monsters underneath my bed */
1192 if (expire_mode == 2) {
1193 sprintf(buf, "%d", expire_value);
1194 strprompt("Keep how many messages online?", buf, 10);
1195 expire_value = atol(buf);
1198 if (expire_mode == 3) {
1199 sprintf(buf, "%d", expire_value);
1200 strprompt("Keep messages for how many days?", buf, 10);
1201 expire_value = atol(buf);
1205 snprintf(buf, sizeof buf, "SPEX floor|%d|%d",
1206 expire_mode, expire_value);
1210 snprintf(buf, sizeof buf, "EFLR %d|%s", curr_floor,
1211 &floorlist[(int) curr_floor][0]);
1214 scr_printf("%s\n", &buf[4]);
1222 * kill the current floor
1224 void kill_floor(void)
1226 int floornum_to_delete, a;
1231 floornum_to_delete = (-1);
1232 scr_printf("(Press return to abort)\n");
1233 newprompt("Delete which floor? ", buf, 255);
1234 if (strlen(buf) == 0)
1236 for (a = 0; a < 128; ++a)
1237 if (!strcasecmp(&floorlist[a][0], buf))
1238 floornum_to_delete = a;
1239 if (floornum_to_delete < 0) {
1240 scr_printf("No such floor. Select one of:\n");
1241 for (a = 0; a < 128; ++a)
1242 if (floorlist[a][0] != 0)
1243 scr_printf("%s\n", &floorlist[a][0]);
1245 } while (floornum_to_delete < 0);
1246 sprintf(buf, "KFLR %d|1", floornum_to_delete);
1249 scr_printf("%s\n", &buf[4]);