1 /* Citadel/UX room-oriented routines */
12 #include <sys/types.h>
26 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
29 void sttybbs(int cmd);
30 void hit_any_key(void);
32 void strprompt(char *prompt, char *str, int len);
33 void newprompt(char *prompt, char *str, int len);
34 int struncmp(char *lstr, char *rstr, int len);
35 void dotgoto(char *towhere, int display_name);
36 void serv_read(char *buf, int bytes);
37 void formout(char *name);
39 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
40 void progress(long int curr, long int cmax);
41 int pattern(char *search, char *patn);
42 int file_checksum(char *filename);
43 int nukedir(char *dirname);
45 extern unsigned room_flags;
46 extern char room_name[];
48 extern char tempdir[];
49 extern int editor_pid;
50 extern char editor_path[];
51 extern int screenwidth;
52 extern int screenheight;
53 extern char fullname[];
55 extern char sigcaught;
56 extern char floor_mode;
57 extern char curr_floor;
64 extern char floorlist[128][256];
67 void load_floorlist(void) {
71 for (a=0; a<128; ++a) floorlist[a][0] = 0;
76 strcpy(floorlist[0],"Main Floor");
79 while (serv_gets(buf), strcmp(buf,"000")) {
80 extract(floorlist[extract_int(buf,0)],buf,1);
85 void room_tree_list(struct roomlisting *rp) {
87 char rmname[ROOMNAMELEN];
95 if (rp->lnext != NULL) {
96 room_tree_list(rp->lnext);
100 strcpy(rmname, rp->rlname);
102 if ((c + strlen(rmname) + 4) > screenwidth) {
104 /* line break, check the paginator */
108 if (f & QR_MAILBOX) {
109 color(BRIGHT_YELLOW);
111 else if (f & QR_PRIVATE) {
117 pprintf("%s",rmname);
118 if ((f & QR_DIRECTORY) && (f & QR_NETWORK)) pprintf("} ");
119 else if (f & QR_DIRECTORY) pprintf("] ");
120 else if (f & QR_NETWORK) pprintf(") ");
122 c = c + strlen(rmname) + 3;
125 if (rp->rnext != NULL) {
126 room_tree_list(rp->rnext);
134 * Room ordering stuff (compare first by floor, then by order)
136 int rordercmp(struct roomlisting *r1, struct roomlisting *r2)
138 if ((r1==NULL)&&(r2==NULL)) return(0);
139 if (r1==NULL) return(-1);
140 if (r2==NULL) return(1);
141 if (r1->rlfloor < r2->rlfloor) return(-1);
142 if (r1->rlfloor > r2->rlfloor) return(1);
143 if (r1->rlorder < r2->rlorder) return(-1);
144 if (r1->rlorder > r2->rlorder) return(1);
150 * Common code for all room listings
152 void listrms(char *variety)
156 struct roomlisting *rl = NULL;
157 struct roomlisting *rp;
158 struct roomlisting *rs;
161 /* Ask the server for a room list */
164 if (buf[0]!='1') return;
165 while (serv_gets(buf), strcmp(buf, "000")) {
166 rp = malloc(sizeof(struct roomlisting));
167 extract(rp->rlname, buf, 0);
168 rp->rlflags = extract_int(buf, 1);
169 rp->rlfloor = extract_int(buf, 2);
170 rp->rlorder = extract_int(buf, 3);
178 else while (rp != NULL) {
179 if (rordercmp(rp, rs)<0) {
180 if (rs->lnext == NULL) {
189 if (rs->rnext == NULL) {
201 sttybbs(SB_YES_INTR);
202 room_tree_list(NULL);
209 void list_other_floors(void) {
213 for (a=0; a<128; ++a) if ((strlen(floorlist[a])>0)&&(a!=curr_floor)) {
214 if ((c + strlen(floorlist[a]) + 4) > screenwidth) {
218 pprintf("%s: ",floorlist[a]);
219 c = c + strlen(floorlist[a]) + 3;
225 * List known rooms. kn_floor_mode should be set to 0 for a 'flat' listing,
226 * 1 to list rooms on the current floor, or 1 to list rooms on all floors.
228 void knrooms(int kn_floor_mode)
235 if (kn_floor_mode == 0) {
237 pprintf("\n Rooms with unread messages:\n");
240 pprintf("\n\n No unseen messages in:\n");
245 if (kn_floor_mode == 1) {
247 pprintf("\n Rooms with unread messages on %s:\n",
248 floorlist[(int)curr_floor]);
249 sprintf(buf,"LKRN %d", curr_floor);
252 pprintf("\n\n Rooms with no new messages on %s:\n",
253 floorlist[(int)curr_floor]);
254 sprintf(buf,"LKRO %d",curr_floor);
257 pprintf("\n\n Other floors:\n");
262 if (kn_floor_mode == 2) {
263 for (a=0; a<128; ++a) if (floorlist[a][0]!=0) {
265 pprintf("\n Rooms on %s:\n",floorlist[a]);
266 sprintf(buf,"LKRA %d",a);
273 IFNEXPERT hit_any_key();
277 void listzrooms(void) { /* list public forgotten rooms */
279 pprintf("\n Forgotten public rooms:\n");
283 IFNEXPERT hit_any_key();
287 int set_room_attr(int ibuf, char *prompt, unsigned int sbit)
291 a = boolprompt(prompt, (ibuf&sbit));
293 if (!a) ibuf=(ibuf^sbit);
300 * Select a floor (used in several commands)
301 * The supplied argument is the 'default' floor number.
302 * This function returns the selected floor number.
304 int select_floor(int rfloor)
309 if (floor_mode == 1) {
310 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
314 safestrncpy(floorstr,floorlist[rfloor],sizeof floorstr);
315 strprompt("Which floor",floorstr,256);
316 for (a=0; a<128; ++a) {
317 if (!strucmp(floorstr,&floorlist[a][0]))
319 if ((newfloor<0)&&(!struncmp(floorstr,
320 &floorlist[a][0],strlen(floorstr))))
322 if ((newfloor<0)&&(pattern(&floorlist[a][0],
323 floorstr)>=0)) newfloor = a;
326 printf("\n One of:\n");
327 for (a=0; a<128; ++a)
328 if (floorlist[a][0]!=0)
332 } while(newfloor < 0);
342 * .<A>ide <E>dit room
344 void editthisroom(void) {
345 char rname[ROOMNAMELEN];
355 int expire_value = 0;
357 /* Fetch the existing room config */
361 printf("%s\n",&buf[4]);
365 extract(rname,&buf[4],0);
366 extract(rpass,&buf[4],1);
367 extract(rdir, &buf[4],2);
368 rflags = extract_int(&buf[4],3);
369 rfloor = extract_int(&buf[4],4);
370 rorder = extract_int(&buf[4],5);
373 /* Fetch the name of the current room aide */
376 if (buf[0]=='2') safestrncpy(raide,&buf[4],sizeof raide);
377 else strcpy(raide,"");
378 if (strlen(raide)==0) strcpy(raide,"none");
380 /* Fetch the expire policy (this will silently fail on old servers,
381 * resulting in "default" policy)
383 serv_puts("GPEX room");
386 expire_mode = extract_int(&buf[4], 0);
387 expire_value = extract_int(&buf[4], 1);
390 /* Now interact with the user. */
391 strprompt("Room name",rname,ROOMNAMELEN-1);
393 rfloor = select_floor(rfloor);
394 rflags = set_room_attr(rflags,"Private room",QR_PRIVATE);
395 if (rflags & QR_PRIVATE)
396 rflags = set_room_attr(rflags,
397 "Accessible by guessing room name",QR_GUESSNAME);
399 /* if it's public, clear the privacy classes */
400 if ((rflags & QR_PRIVATE)==0) {
401 if (rflags & QR_GUESSNAME) rflags = rflags - QR_GUESSNAME;
402 if (rflags & QR_PASSWORDED) rflags = rflags - QR_PASSWORDED;
405 /* if it's private, choose the privacy classes */
406 if ( (rflags & QR_PRIVATE)
407 && ( (rflags & QR_GUESSNAME) == 0) ) {
408 rflags = set_room_attr(rflags,
409 "Accessible by entering a password",QR_PASSWORDED);
411 if ( (rflags & QR_PRIVATE)
412 && ((rflags&QR_PASSWORDED)==QR_PASSWORDED) ) {
413 strprompt("Room password",rpass,9);
416 if ((rflags&QR_PRIVATE)==QR_PRIVATE) {
417 rbump = boolprompt("Cause current users to forget room", 0);
420 rflags = set_room_attr(rflags,"Preferred users only",QR_PREFONLY);
421 rflags = set_room_attr(rflags,"Read-only room",QR_READONLY);
422 rflags = set_room_attr(rflags,"Directory room",QR_DIRECTORY);
423 rflags = set_room_attr(rflags,"Permanent room",QR_PERMANENT);
424 if (rflags & QR_DIRECTORY) {
425 strprompt("Directory name",rdir,14);
426 rflags = set_room_attr(rflags,"Uploading allowed",QR_UPLOAD);
427 rflags = set_room_attr(rflags,"Downloading allowed",
429 rflags = set_room_attr(rflags,"Visible directory",QR_VISDIR);
431 rflags = set_room_attr(rflags,"Network shared room",QR_NETWORK);
432 rflags = set_room_attr(rflags,
433 "Automatically make all messages anonymous",QR_ANONONLY);
434 if ( (rflags & QR_ANONONLY) == 0) {
435 rflags = set_room_attr(rflags,
436 "Ask users whether to make messages anonymous",
439 rorder = intprompt("Listing order", rorder, 1, 127);
441 /* Ask about the room aide */
443 strprompt("Room aide (or 'none')",raide,29);
444 if (!strucmp(raide,"none")) {
449 snprintf(buf,sizeof buf,"QUSR %s",raide);
452 if (buf[0]!='2') printf("%s\n",&buf[4]);
454 } while(buf[0]!='2');
456 if (!strucmp(raide,"none")) strcpy(raide,"");
459 /* Angels and demons dancing in my head... */
461 sprintf(buf, "%d", expire_mode);
462 strprompt("Message expire policy (? for list)", buf, 1);
465 printf("0. Use the default for this floor\n");
466 printf("1. Never automatically expire messages\n");
467 printf("2. Expire by message count\n");
468 printf("3. Expire by message age\n");
470 } while((buf[0]<48)||(buf[0]>51));
471 expire_mode = buf[0] - 48;
473 /* ...lunatics and monsters underneath my bed */
474 if (expire_mode == 2) {
475 sprintf(buf, "%d", expire_value);
476 strprompt("Keep how many messages online?", buf, 10);
477 expire_value = atol(buf);
480 if (expire_mode == 3) {
481 sprintf(buf, "%d", expire_value);
482 strprompt("Keep messages for how many days?", buf, 10);
483 expire_value = atol(buf);
486 /* Give 'em a chance to change their minds */
487 printf("Save changes (y/n)? ");
490 snprintf(buf,sizeof buf,"SETA %s",raide);
493 if (buf[0]!='2') printf("%s\n",&buf[4]);
495 snprintf(buf, sizeof buf, "SPEX room|%d|%d",
496 expire_mode, expire_value);
500 snprintf(buf,sizeof buf,"SETR %s|%s|%s|%d|%d|%d|%d",
501 rname,rpass,rdir,rflags,rbump,rfloor,rorder);
504 printf("%s\n",&buf[4]);
505 if (buf[0]=='2') dotgoto(rname,2);
511 * un-goto the previous room
516 if (!strcmp(ugname,"")) return;
517 snprintf(buf,sizeof buf,"GOTO %s",ugname);
521 printf("%s\n",&buf[4]);
524 sprintf(buf,"SLRP %ld",uglsn);
527 if (buf[0]!='2') printf("%s\n",&buf[4]);
528 safestrncpy(buf,ugname,sizeof buf);
534 /* Here's the code for simply transferring the file to the client,
535 * for folks who have their own clientware. It's a lot simpler than
536 * the [XYZ]modem code below...
537 * (This function assumes that a download file is already open on the server)
539 void download_to_local_disk(char *supplied_filename, long total_bytes)
543 long transmitted_bytes = 0L;
550 strcpy(filename, supplied_filename);
551 if (strlen(filename)==0) {
552 newprompt("Filename: ", filename, 250);
555 printf("Enter the name of the directory to save '%s'\n",
557 printf("to, or press return for the current directory.\n");
558 newprompt("Directory: ",dbuf,256);
559 if (strlen(dbuf)==0) strcpy(dbuf,".");
561 strcat(dbuf,filename);
563 savefp = fopen(dbuf,"w");
564 if (savefp == NULL) {
565 printf("Cannot open '%s': %s\n",dbuf,strerror(errno));
566 /* close the download file at the server */
570 printf("%s\n",&buf[4]);
574 progress(0,total_bytes);
575 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
576 bb = total_bytes - transmitted_bytes;
577 aa = ((bb < 4096) ? bb : 4096);
578 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
582 printf("%s\n",&buf[4]);
585 packet = extract_int(&buf[4],0);
586 serv_read(dbuf,packet);
587 if (fwrite(dbuf,packet,1,savefp) < 1) broken = 1;
588 transmitted_bytes = transmitted_bytes + (long)packet;
589 progress(transmitted_bytes,total_bytes);
592 /* close the download file at the server */
596 printf("%s\n",&buf[4]);
603 * download() - download a file or files. The argument passed to this
604 * function determines which protocol to use.
605 * proto - 0 = paginate, 1 = xmodem, 2 = raw, 3 = ymodem, 4 = zmodem, 5 = save
607 void download(int proto)
612 char transmit_cmd[256];
613 long total_bytes = 0L;
615 long transmitted_bytes = 0L;
621 if ((room_flags & QR_DOWNLOAD) == 0) {
622 printf("*** You cannot download from this room.\n");
626 newprompt("Enter filename: ",filename,255);
628 snprintf(buf,sizeof buf,"OPEN %s",filename);
632 printf("%s\n",&buf[4]);
635 total_bytes = extract_long(&buf[4],0);
637 /* Save to local disk, for folks with their own copy of the client */
639 download_to_local_disk(filename, total_bytes);
643 /* Meta-download for public clients */
644 printf("Fetching file from Citadel server...\n");
645 mkdir(tempdir, 0700);
646 snprintf(tempname, sizeof tempname, "%s/%s", tempdir, filename);
647 tpipe = fopen(tempname, "wb");
648 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
649 progress(transmitted_bytes, total_bytes);
650 bb = total_bytes - transmitted_bytes;
651 aa = ((bb < 4096) ? bb : 4096);
652 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
656 printf("%s\n",&buf[4]);
658 packet = extract_int(&buf[4],0);
659 serv_read(dbuf,packet);
660 if (fwrite(dbuf,packet,1,tpipe) < 1) broken = 1;
661 transmitted_bytes = transmitted_bytes + (long)packet;
664 progress(transmitted_bytes, total_bytes);
666 /* close the download file at the server */
670 printf("%s\n",&buf[4]);
673 if (proto==0) sprintf(transmit_cmd, "SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",tempname);
674 else if (proto==1) sprintf(transmit_cmd, "exec sx %s", tempname);
675 else if (proto==3) sprintf(transmit_cmd, "exec sb %s", tempname);
676 else if (proto==4) sprintf(transmit_cmd, "exec sz %s", tempname);
677 else sprintf(transmit_cmd, "exec cat %s", tempname);
680 system(transmit_cmd);
683 /* clean up the temporary directory */
690 * read directory of this room
701 pprintf("%s\n",&buf[4]);
705 extract(comment,&buf[4],0);
706 extract(flnm,&buf[4],1);
707 pprintf("\nDirectory of %s on %s\n",flnm,comment);
708 pprintf("-----------------------\n");
709 while (serv_gets(buf), strcmp(buf,"000")) {
712 extract(comment,buf,2);
713 if (strlen(flnm)<=14)
714 pprintf("%-14s %8s %s\n",flnm,flsz,comment);
716 pprintf("%s\n%14s %8s %s\n",flnm,"",flsz,comment);
722 * add a user to a private room
725 char aaa[31],bbb[256];
727 if ((room_flags & QR_PRIVATE)==0) {
728 printf("This is not a private room.\n");
732 newprompt("Name of user? ",aaa,30);
733 if (aaa[0]==0) return;
735 snprintf(bbb,sizeof bbb,"INVT %s",aaa);
738 printf("%s\n",&bbb[4]);
743 * kick a user out of a room
746 char aaa[31],bbb[256];
748 newprompt("Name of user? ",aaa,30);
749 if (aaa[0]==0) return;
751 snprintf(bbb,sizeof bbb,"KICK %s",aaa);
754 printf("%s\n",&bbb[4]);
759 * aide command: kill the current room
761 void killroom(void) {
767 printf("%s\n",&aaa[4]);
771 printf("Are you sure you want to kill this room? ");
772 if (yesno()==0) return;
776 printf("%s\n",&aaa[4]);
777 if (aaa[0]!='2') return;
778 dotgoto("_BASEROOM_",0);
781 void forget(void) { /* forget the current room */
784 printf("Are you sure you want to forget this room? ");
785 if (yesno()==0) return;
790 printf("%s\n",&cmd[4]);
794 /* now return to the lobby */
795 dotgoto("_BASEROOM_",0);
804 char new_room_name[ROOMNAMELEN];
806 char new_room_pass[10];
814 printf("%s\n",&cmd[4]);
818 newprompt("Name for new room? ",new_room_name,ROOMNAMELEN-1);
819 if (strlen(new_room_name)==0) return;
820 for (a=0; a<strlen(new_room_name); ++a)
821 if (new_room_name[a] == '|') new_room_name[a]='_';
823 new_room_floor = select_floor((int)curr_floor);
825 IFNEXPERT formout("roomaccess");
827 printf( "<?>Help\n<1>Public room\n<2>Guess-name room\n"
828 "<3>Passworded room\n<4>Invitation-only room\n"
829 "<5>Personal room\n");
830 printf("Enter room type: ");
833 } while (((b<'1')||(b>'5')) && (b!='?'));
836 formout("roomaccess");
838 } while ((b<'1')||(b>'5'));
841 new_room_type = b - 1;
842 if (new_room_type==2) {
843 newprompt("Enter a room password: ",new_room_pass,9);
844 for (a=0; a<strlen(new_room_pass); ++a)
845 if (new_room_pass[a] == '|') new_room_pass[a]='_';
847 else strcpy(new_room_pass,"");
849 printf("\042%s\042, a",new_room_name);
850 if (b==1) printf(" public room.");
851 if (b==2) printf(" guess-name room.");
852 if (b==3) printf(" passworded room, password: %s",new_room_pass);
853 if (b==4) printf("n invitation-only room.");
854 if (b==5) printf(" personal room.");
855 printf("\nInstall it? (y/n) : ");
859 snprintf(cmd, sizeof cmd, "CRE8 1|%s|%d|%s|%d", new_room_name,
860 new_room_type, new_room_pass, new_room_floor);
864 printf("%s\n",&cmd[4]);
868 /* command succeeded... now GO to the new room! */
869 dotgoto(new_room_name,0);
874 void readinfo(void) { /* read info file for current room */
881 if (cmd[0]!='1') return;
883 fmout(screenwidth,NULL,
884 ((userflags & US_PAGINATOR) ? 1 : 0),
890 * <W>ho knows room...
892 void whoknows(void) {
897 pprintf("%s\n",&buf[5]);
901 sttybbs(SB_YES_INTR);
902 while (serv_gets(buf), strncmp(buf,"000",3)) {
903 if (sigcaught==0) pprintf("%s\n",buf);
909 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
913 int b,cksum,editor_exit;
916 if (strlen(editor_path)==0) {
917 printf("Do you wish to re-enter %s? ",desc);
918 if (yesno()==0) return;
921 fp = fopen(temp,"w");
924 serv_puts(check_cmd);
927 printf("%s\n",&cmd[4]);
931 if (strlen(editor_path)>0) {
935 fp = fopen(temp,"w");
936 while (serv_gets(cmd), strcmp(cmd,"000")) {
937 fprintf(fp,"%s\n",cmd);
943 cksum = file_checksum(temp);
945 if (strlen(editor_path)>0) {
950 execlp(editor_path,editor_path,temp,NULL);
953 if (editor_pid>0) do {
955 b=wait(&editor_exit);
956 } while((b!=editor_pid)&&(b>=0));
958 printf("Executed %s\n", editor_path);
962 printf("Entering %s. ",desc);
963 printf("Press return twice when finished.\n");
969 if (file_checksum(temp) == cksum) {
970 printf("*** Aborted.\n");
974 serv_puts(write_cmd);
977 printf("%s\n",&cmd[4]);
982 while (fgets(cmd,255,fp)!=NULL) {
983 cmd[strlen(cmd)-1] = 0;
994 void enterinfo(void) { /* edit info file for current room */
995 do_edit("the Info file for this room","RINF","EINF 0","EINF 1");
998 void enter_bio(void) {
1000 snprintf(cmd,sizeof cmd,"RBIO %s",fullname);
1001 do_edit("your Bio",cmd,"NOOP","EBIO");
1005 * create a new floor
1007 void create_floor(void) {
1009 char newfloorname[256];
1011 serv_puts("CFLR xx|0");
1014 printf("%s\n",&buf[4]);
1018 newprompt("Name for new floor: ",newfloorname,255);
1019 snprintf(buf,sizeof buf,"CFLR %s|1",newfloorname);
1023 printf("Floor has been created.\n");
1026 printf("%s\n",&buf[4]);
1031 * edit the current floor
1033 void edit_floor(void) {
1035 int expire_mode = 0;
1036 int expire_value = 0;
1038 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1040 /* Fetch the expire policy (this will silently fail on old servers,
1041 * resulting in "default" policy)
1043 serv_puts("GPEX floor");
1046 expire_mode = extract_int(&buf[4], 0);
1047 expire_value = extract_int(&buf[4], 1);
1050 /* Interact with the user */
1051 strprompt("Floor name",&floorlist[(int)curr_floor][0],255);
1053 /* Angels and demons dancing in my head... */
1055 sprintf(buf, "%d", expire_mode);
1056 strprompt("Floor default essage expire policy (? for list)",
1058 if (buf[0] == '?') {
1060 printf("0. Use the system default\n");
1061 printf("1. Never automatically expire messages\n");
1062 printf("2. Expire by message count\n");
1063 printf("3. Expire by message age\n");
1065 } while((buf[0]<48)||(buf[0]>51));
1066 expire_mode = buf[0] - 48;
1068 /* ...lunatics and monsters underneath my bed */
1069 if (expire_mode == 2) {
1070 sprintf(buf, "%d", expire_value);
1071 strprompt("Keep how many messages online?", buf, 10);
1072 expire_value = atol(buf);
1075 if (expire_mode == 3) {
1076 sprintf(buf, "%d", expire_value);
1077 strprompt("Keep messages for how many days?", buf, 10);
1078 expire_value = atol(buf);
1082 snprintf(buf, sizeof buf, "SPEX floor|%d|%d",
1083 expire_mode, expire_value);
1087 snprintf(buf,sizeof buf,"EFLR %d|%s",curr_floor,
1088 &floorlist[(int)curr_floor][0]);
1091 printf("%s\n",&buf[4]);
1099 * kill the current floor
1101 void kill_floor(void) {
1102 int floornum_to_delete,a;
1105 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1107 floornum_to_delete = (-1);
1108 printf("(Press return to abort)\n");
1109 newprompt("Delete which floor? ",buf,255);
1110 if (strlen(buf)==0) return;
1111 for (a=0; a<128; ++a)
1112 if (!strucmp(&floorlist[a][0],buf))
1113 floornum_to_delete = a;
1114 if (floornum_to_delete < 0) {
1115 printf("No such floor. Select one of:\n");
1116 for (a=0; a<128; ++a)
1117 if (floorlist[a][0]!=0)
1118 printf("%s\n",&floorlist[a][0]);
1120 } while (floornum_to_delete < 0);
1121 sprintf(buf,"KFLR %d|1",floornum_to_delete);
1124 printf("%s\n",&buf[4]);