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 void dotgoto(char *towhere, int display_name);
35 void serv_read(char *buf, int bytes);
36 void formout(char *name);
38 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
39 void progress(long int curr, long int cmax);
40 int pattern(char *search, char *patn);
41 int file_checksum(char *filename);
42 int nukedir(char *dirname);
44 extern unsigned room_flags;
45 extern char room_name[];
47 extern char tempdir[];
48 extern int editor_pid;
49 extern char editor_path[];
50 extern int screenwidth;
51 extern int screenheight;
52 extern char fullname[];
54 extern char sigcaught;
55 extern char floor_mode;
56 extern char curr_floor;
63 extern char floorlist[128][256];
66 void load_floorlist(void) {
70 for (a=0; a<128; ++a) floorlist[a][0] = 0;
75 strcpy(floorlist[0],"Main Floor");
78 while (serv_gets(buf), strcmp(buf,"000")) {
79 extract(floorlist[extract_int(buf,0)],buf,1);
84 void room_tree_list(struct roomlisting *rp) {
86 char rmname[ROOMNAMELEN];
94 if (rp->lnext != NULL) {
95 room_tree_list(rp->lnext);
99 strcpy(rmname, rp->rlname);
101 if ((c + strlen(rmname) + 4) > screenwidth) {
103 /* line break, check the paginator */
107 if (f & QR_MAILBOX) {
108 color(BRIGHT_YELLOW);
110 else if (f & QR_PRIVATE) {
116 pprintf("%s",rmname);
117 if ((f & QR_DIRECTORY) && (f & QR_NETWORK)) pprintf("} ");
118 else if (f & QR_DIRECTORY) pprintf("] ");
119 else if (f & QR_NETWORK) pprintf(") ");
121 c = c + strlen(rmname) + 3;
124 if (rp->rnext != NULL) {
125 room_tree_list(rp->rnext);
133 * Room ordering stuff (compare first by floor, then by order)
135 int rordercmp(struct roomlisting *r1, struct roomlisting *r2)
137 if ((r1==NULL)&&(r2==NULL)) return(0);
138 if (r1==NULL) return(-1);
139 if (r2==NULL) return(1);
140 if (r1->rlfloor < r2->rlfloor) return(-1);
141 if (r1->rlfloor > r2->rlfloor) return(1);
142 if (r1->rlorder < r2->rlorder) return(-1);
143 if (r1->rlorder > r2->rlorder) return(1);
149 * Common code for all room listings
151 void listrms(char *variety)
155 struct roomlisting *rl = NULL;
156 struct roomlisting *rp;
157 struct roomlisting *rs;
160 /* Ask the server for a room list */
163 if (buf[0]!='1') return;
164 while (serv_gets(buf), strcmp(buf, "000")) {
165 rp = malloc(sizeof(struct roomlisting));
166 extract(rp->rlname, buf, 0);
167 rp->rlflags = extract_int(buf, 1);
168 rp->rlfloor = extract_int(buf, 2);
169 rp->rlorder = extract_int(buf, 3);
177 else while (rp != NULL) {
178 if (rordercmp(rp, rs)<0) {
179 if (rs->lnext == NULL) {
188 if (rs->rnext == NULL) {
200 sttybbs(SB_YES_INTR);
201 room_tree_list(NULL);
208 void list_other_floors(void) {
212 for (a=0; a<128; ++a) if ((strlen(floorlist[a])>0)&&(a!=curr_floor)) {
213 if ((c + strlen(floorlist[a]) + 4) > screenwidth) {
217 pprintf("%s: ",floorlist[a]);
218 c = c + strlen(floorlist[a]) + 3;
224 * List known rooms. kn_floor_mode should be set to 0 for a 'flat' listing,
225 * 1 to list rooms on the current floor, or 1 to list rooms on all floors.
227 void knrooms(int kn_floor_mode)
234 if (kn_floor_mode == 0) {
236 pprintf("\n Rooms with unread messages:\n");
239 pprintf("\n\n No unseen messages in:\n");
244 if (kn_floor_mode == 1) {
246 pprintf("\n Rooms with unread messages on %s:\n",
247 floorlist[(int)curr_floor]);
248 sprintf(buf,"LKRN %d", curr_floor);
251 pprintf("\n\n Rooms with no new messages on %s:\n",
252 floorlist[(int)curr_floor]);
253 sprintf(buf,"LKRO %d",curr_floor);
256 pprintf("\n\n Other floors:\n");
261 if (kn_floor_mode == 2) {
262 for (a=0; a<128; ++a) if (floorlist[a][0]!=0) {
264 pprintf("\n Rooms on %s:\n",floorlist[a]);
265 sprintf(buf,"LKRA %d",a);
272 IFNEXPERT hit_any_key();
276 void listzrooms(void) { /* list public forgotten rooms */
278 pprintf("\n Forgotten public rooms:\n");
282 IFNEXPERT hit_any_key();
286 int set_room_attr(int ibuf, char *prompt, unsigned int sbit)
290 a = boolprompt(prompt, (ibuf&sbit));
292 if (!a) ibuf=(ibuf^sbit);
299 * Select a floor (used in several commands)
300 * The supplied argument is the 'default' floor number.
301 * This function returns the selected floor number.
303 int select_floor(int rfloor)
308 if (floor_mode == 1) {
309 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
313 safestrncpy(floorstr,floorlist[rfloor],sizeof floorstr);
314 strprompt("Which floor",floorstr,256);
315 for (a=0; a<128; ++a) {
316 if (!strcasecmp(floorstr,&floorlist[a][0]))
318 if ((newfloor<0)&&(!strncasecmp(floorstr,
319 &floorlist[a][0],strlen(floorstr))))
321 if ((newfloor<0)&&(pattern(&floorlist[a][0],
322 floorstr)>=0)) newfloor = a;
325 printf("\n One of:\n");
326 for (a=0; a<128; ++a)
327 if (floorlist[a][0]!=0)
331 } while(newfloor < 0);
341 * .<A>ide <E>dit room
343 void editthisroom(void) {
344 char rname[ROOMNAMELEN];
354 int expire_value = 0;
356 /* Fetch the existing room config */
360 printf("%s\n",&buf[4]);
364 extract(rname,&buf[4],0);
365 extract(rpass,&buf[4],1);
366 extract(rdir, &buf[4],2);
367 rflags = extract_int(&buf[4],3);
368 rfloor = extract_int(&buf[4],4);
369 rorder = extract_int(&buf[4],5);
372 /* Fetch the name of the current room aide */
375 if (buf[0]=='2') safestrncpy(raide,&buf[4],sizeof raide);
376 else strcpy(raide,"");
377 if (strlen(raide)==0) strcpy(raide,"none");
379 /* Fetch the expire policy (this will silently fail on old servers,
380 * resulting in "default" policy)
382 serv_puts("GPEX room");
385 expire_mode = extract_int(&buf[4], 0);
386 expire_value = extract_int(&buf[4], 1);
389 /* Now interact with the user. */
390 strprompt("Room name",rname,ROOMNAMELEN-1);
392 rfloor = select_floor(rfloor);
393 rflags = set_room_attr(rflags,"Private room",QR_PRIVATE);
394 if (rflags & QR_PRIVATE)
395 rflags = set_room_attr(rflags,
396 "Accessible by guessing room name",QR_GUESSNAME);
398 /* if it's public, clear the privacy classes */
399 if ((rflags & QR_PRIVATE)==0) {
400 if (rflags & QR_GUESSNAME) rflags = rflags - QR_GUESSNAME;
401 if (rflags & QR_PASSWORDED) rflags = rflags - QR_PASSWORDED;
404 /* if it's private, choose the privacy classes */
405 if ( (rflags & QR_PRIVATE)
406 && ( (rflags & QR_GUESSNAME) == 0) ) {
407 rflags = set_room_attr(rflags,
408 "Accessible by entering a password",QR_PASSWORDED);
410 if ( (rflags & QR_PRIVATE)
411 && ((rflags&QR_PASSWORDED)==QR_PASSWORDED) ) {
412 strprompt("Room password",rpass,9);
415 if ((rflags&QR_PRIVATE)==QR_PRIVATE) {
416 rbump = boolprompt("Cause current users to forget room", 0);
419 rflags = set_room_attr(rflags,"Preferred users only",QR_PREFONLY);
420 rflags = set_room_attr(rflags,"Read-only room",QR_READONLY);
421 rflags = set_room_attr(rflags,"Directory room",QR_DIRECTORY);
422 rflags = set_room_attr(rflags,"Permanent room",QR_PERMANENT);
423 if (rflags & QR_DIRECTORY) {
424 strprompt("Directory name",rdir,14);
425 rflags = set_room_attr(rflags,"Uploading allowed",QR_UPLOAD);
426 rflags = set_room_attr(rflags,"Downloading allowed",
428 rflags = set_room_attr(rflags,"Visible directory",QR_VISDIR);
430 rflags = set_room_attr(rflags,"Network shared room",QR_NETWORK);
431 rflags = set_room_attr(rflags,
432 "Automatically make all messages anonymous",QR_ANONONLY);
433 if ( (rflags & QR_ANONONLY) == 0) {
434 rflags = set_room_attr(rflags,
435 "Ask users whether to make messages anonymous",
438 rorder = intprompt("Listing order", rorder, 1, 127);
440 /* Ask about the room aide */
442 strprompt("Room aide (or 'none')",raide,29);
443 if (!strcasecmp(raide,"none")) {
448 snprintf(buf,sizeof buf,"QUSR %s",raide);
451 if (buf[0]!='2') printf("%s\n",&buf[4]);
453 } while(buf[0]!='2');
455 if (!strcasecmp(raide,"none")) strcpy(raide,"");
458 /* Angels and demons dancing in my head... */
460 sprintf(buf, "%d", expire_mode);
461 strprompt("Message expire policy (? for list)", buf, 1);
464 printf("0. Use the default for this floor\n");
465 printf("1. Never automatically expire messages\n");
466 printf("2. Expire by message count\n");
467 printf("3. Expire by message age\n");
469 } while((buf[0]<48)||(buf[0]>51));
470 expire_mode = buf[0] - 48;
472 /* ...lunatics and monsters underneath my bed */
473 if (expire_mode == 2) {
474 sprintf(buf, "%d", expire_value);
475 strprompt("Keep how many messages online?", buf, 10);
476 expire_value = atol(buf);
479 if (expire_mode == 3) {
480 sprintf(buf, "%d", expire_value);
481 strprompt("Keep messages for how many days?", buf, 10);
482 expire_value = atol(buf);
485 /* Give 'em a chance to change their minds */
486 printf("Save changes (y/n)? ");
489 snprintf(buf,sizeof buf,"SETA %s",raide);
492 if (buf[0]!='2') printf("%s\n",&buf[4]);
494 snprintf(buf, sizeof buf, "SPEX room|%d|%d",
495 expire_mode, expire_value);
499 snprintf(buf,sizeof buf,"SETR %s|%s|%s|%d|%d|%d|%d",
500 rname,rpass,rdir,rflags,rbump,rfloor,rorder);
503 printf("%s\n",&buf[4]);
504 if (buf[0]=='2') dotgoto(rname,2);
510 * un-goto the previous room
515 if (!strcmp(ugname,"")) return;
516 snprintf(buf,sizeof buf,"GOTO %s",ugname);
520 printf("%s\n",&buf[4]);
523 sprintf(buf,"SLRP %ld",uglsn);
526 if (buf[0]!='2') printf("%s\n",&buf[4]);
527 safestrncpy(buf,ugname,sizeof buf);
533 /* Here's the code for simply transferring the file to the client,
534 * for folks who have their own clientware. It's a lot simpler than
535 * the [XYZ]modem code below...
536 * (This function assumes that a download file is already open on the server)
538 void download_to_local_disk(char *supplied_filename, long total_bytes)
542 long transmitted_bytes = 0L;
549 strcpy(filename, supplied_filename);
550 if (strlen(filename)==0) {
551 newprompt("Filename: ", filename, 250);
554 printf("Enter the name of the directory to save '%s'\n",
556 printf("to, or press return for the current directory.\n");
557 newprompt("Directory: ",dbuf,256);
558 if (strlen(dbuf)==0) strcpy(dbuf,".");
560 strcat(dbuf,filename);
562 savefp = fopen(dbuf,"w");
563 if (savefp == NULL) {
564 printf("Cannot open '%s': %s\n",dbuf,strerror(errno));
565 /* close the download file at the server */
569 printf("%s\n",&buf[4]);
573 progress(0,total_bytes);
574 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
575 bb = total_bytes - transmitted_bytes;
576 aa = ((bb < 4096) ? bb : 4096);
577 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
581 printf("%s\n",&buf[4]);
584 packet = extract_int(&buf[4],0);
585 serv_read(dbuf,packet);
586 if (fwrite(dbuf,packet,1,savefp) < 1) broken = 1;
587 transmitted_bytes = transmitted_bytes + (long)packet;
588 progress(transmitted_bytes,total_bytes);
591 /* close the download file at the server */
595 printf("%s\n",&buf[4]);
602 * download() - download a file or files. The argument passed to this
603 * function determines which protocol to use.
604 * proto - 0 = paginate, 1 = xmodem, 2 = raw, 3 = ymodem, 4 = zmodem, 5 = save
606 void download(int proto)
611 char transmit_cmd[256];
612 long total_bytes = 0L;
614 long transmitted_bytes = 0L;
620 if ((room_flags & QR_DOWNLOAD) == 0) {
621 printf("*** You cannot download from this room.\n");
625 newprompt("Enter filename: ",filename,255);
627 snprintf(buf,sizeof buf,"OPEN %s",filename);
631 printf("%s\n",&buf[4]);
634 total_bytes = extract_long(&buf[4],0);
636 /* Save to local disk, for folks with their own copy of the client */
638 download_to_local_disk(filename, total_bytes);
642 /* Meta-download for public clients */
643 printf("Fetching file from Citadel server...\n");
644 mkdir(tempdir, 0700);
645 snprintf(tempname, sizeof tempname, "%s/%s", tempdir, filename);
646 tpipe = fopen(tempname, "wb");
647 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
648 progress(transmitted_bytes, total_bytes);
649 bb = total_bytes - transmitted_bytes;
650 aa = ((bb < 4096) ? bb : 4096);
651 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
655 printf("%s\n",&buf[4]);
657 packet = extract_int(&buf[4],0);
658 serv_read(dbuf,packet);
659 if (fwrite(dbuf,packet,1,tpipe) < 1) broken = 1;
660 transmitted_bytes = transmitted_bytes + (long)packet;
663 progress(transmitted_bytes, total_bytes);
665 /* close the download file at the server */
669 printf("%s\n",&buf[4]);
672 if (proto==0) sprintf(transmit_cmd, "SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",tempname);
673 else if (proto==1) sprintf(transmit_cmd, "exec sx %s", tempname);
674 else if (proto==3) sprintf(transmit_cmd, "exec sb %s", tempname);
675 else if (proto==4) sprintf(transmit_cmd, "exec sz %s", tempname);
676 else sprintf(transmit_cmd, "exec cat %s", tempname);
679 system(transmit_cmd);
682 /* clean up the temporary directory */
689 * read directory of this room
700 pprintf("%s\n",&buf[4]);
704 extract(comment,&buf[4],0);
705 extract(flnm,&buf[4],1);
706 pprintf("\nDirectory of %s on %s\n",flnm,comment);
707 pprintf("-----------------------\n");
708 while (serv_gets(buf), strcmp(buf,"000")) {
711 extract(comment,buf,2);
712 if (strlen(flnm)<=14)
713 pprintf("%-14s %8s %s\n",flnm,flsz,comment);
715 pprintf("%s\n%14s %8s %s\n",flnm,"",flsz,comment);
721 * add a user to a private room
724 char aaa[31],bbb[256];
726 if ((room_flags & QR_PRIVATE)==0) {
727 printf("This is not a private room.\n");
731 newprompt("Name of user? ",aaa,30);
732 if (aaa[0]==0) return;
734 snprintf(bbb,sizeof bbb,"INVT %s",aaa);
737 printf("%s\n",&bbb[4]);
742 * kick a user out of a room
745 char aaa[31],bbb[256];
747 newprompt("Name of user? ",aaa,30);
748 if (aaa[0]==0) return;
750 snprintf(bbb,sizeof bbb,"KICK %s",aaa);
753 printf("%s\n",&bbb[4]);
758 * aide command: kill the current room
760 void killroom(void) {
766 printf("%s\n",&aaa[4]);
770 printf("Are you sure you want to kill this room? ");
771 if (yesno()==0) return;
775 printf("%s\n",&aaa[4]);
776 if (aaa[0]!='2') return;
777 dotgoto("_BASEROOM_",0);
780 void forget(void) { /* forget the current room */
783 printf("Are you sure you want to forget this room? ");
784 if (yesno()==0) return;
789 printf("%s\n",&cmd[4]);
793 /* now return to the lobby */
794 dotgoto("_BASEROOM_",0);
803 char new_room_name[ROOMNAMELEN];
805 char new_room_pass[10];
813 printf("%s\n",&cmd[4]);
817 newprompt("Name for new room? ",new_room_name,ROOMNAMELEN-1);
818 if (strlen(new_room_name)==0) return;
819 for (a=0; a<strlen(new_room_name); ++a)
820 if (new_room_name[a] == '|') new_room_name[a]='_';
822 new_room_floor = select_floor((int)curr_floor);
824 IFNEXPERT formout("roomaccess");
826 printf( "<?>Help\n<1>Public room\n<2>Guess-name room\n"
827 "<3>Passworded room\n<4>Invitation-only room\n"
828 "<5>Personal room\n");
829 printf("Enter room type: ");
832 } while (((b<'1')||(b>'5')) && (b!='?'));
835 formout("roomaccess");
837 } while ((b<'1')||(b>'5'));
840 new_room_type = b - 1;
841 if (new_room_type==2) {
842 newprompt("Enter a room password: ",new_room_pass,9);
843 for (a=0; a<strlen(new_room_pass); ++a)
844 if (new_room_pass[a] == '|') new_room_pass[a]='_';
846 else strcpy(new_room_pass,"");
848 printf("\042%s\042, a",new_room_name);
849 if (b==1) printf(" public room.");
850 if (b==2) printf(" guess-name room.");
851 if (b==3) printf(" passworded room, password: %s",new_room_pass);
852 if (b==4) printf("n invitation-only room.");
853 if (b==5) printf(" personal room.");
854 printf("\nInstall it? (y/n) : ");
858 snprintf(cmd, sizeof cmd, "CRE8 1|%s|%d|%s|%d", new_room_name,
859 new_room_type, new_room_pass, new_room_floor);
863 printf("%s\n",&cmd[4]);
867 /* command succeeded... now GO to the new room! */
868 dotgoto(new_room_name,0);
873 void readinfo(void) { /* read info file for current room */
880 if (cmd[0]!='1') return;
882 fmout(screenwidth,NULL,
883 ((userflags & US_PAGINATOR) ? 1 : 0),
889 * <W>ho knows room...
891 void whoknows(void) {
896 pprintf("%s\n",&buf[5]);
900 sttybbs(SB_YES_INTR);
901 while (serv_gets(buf), strncmp(buf,"000",3)) {
902 if (sigcaught==0) pprintf("%s\n",buf);
908 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
912 int b,cksum,editor_exit;
915 if (strlen(editor_path)==0) {
916 printf("Do you wish to re-enter %s? ",desc);
917 if (yesno()==0) return;
920 fp = fopen(temp,"w");
923 serv_puts(check_cmd);
926 printf("%s\n",&cmd[4]);
930 if (strlen(editor_path)>0) {
934 fp = fopen(temp,"w");
935 while (serv_gets(cmd), strcmp(cmd,"000")) {
936 fprintf(fp,"%s\n",cmd);
942 cksum = file_checksum(temp);
944 if (strlen(editor_path)>0) {
949 execlp(editor_path,editor_path,temp,NULL);
952 if (editor_pid>0) do {
954 b=wait(&editor_exit);
955 } while((b!=editor_pid)&&(b>=0));
957 printf("Executed %s\n", editor_path);
961 printf("Entering %s. ",desc);
962 printf("Press return twice when finished.\n");
968 if (file_checksum(temp) == cksum) {
969 printf("*** Aborted.\n");
973 serv_puts(write_cmd);
976 printf("%s\n",&cmd[4]);
981 while (fgets(cmd,255,fp)!=NULL) {
982 cmd[strlen(cmd)-1] = 0;
993 void enterinfo(void) { /* edit info file for current room */
994 do_edit("the Info file for this room","RINF","EINF 0","EINF 1");
997 void enter_bio(void) {
999 snprintf(cmd,sizeof cmd,"RBIO %s",fullname);
1000 do_edit("your Bio",cmd,"NOOP","EBIO");
1004 * create a new floor
1006 void create_floor(void) {
1008 char newfloorname[256];
1010 serv_puts("CFLR xx|0");
1013 printf("%s\n",&buf[4]);
1017 newprompt("Name for new floor: ",newfloorname,255);
1018 snprintf(buf,sizeof buf,"CFLR %s|1",newfloorname);
1022 printf("Floor has been created.\n");
1025 printf("%s\n",&buf[4]);
1030 * edit the current floor
1032 void edit_floor(void) {
1034 int expire_mode = 0;
1035 int expire_value = 0;
1037 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1039 /* Fetch the expire policy (this will silently fail on old servers,
1040 * resulting in "default" policy)
1042 serv_puts("GPEX floor");
1045 expire_mode = extract_int(&buf[4], 0);
1046 expire_value = extract_int(&buf[4], 1);
1049 /* Interact with the user */
1050 strprompt("Floor name",&floorlist[(int)curr_floor][0],255);
1052 /* Angels and demons dancing in my head... */
1054 sprintf(buf, "%d", expire_mode);
1055 strprompt("Floor default essage expire policy (? for list)",
1057 if (buf[0] == '?') {
1059 printf("0. Use the system default\n");
1060 printf("1. Never automatically expire messages\n");
1061 printf("2. Expire by message count\n");
1062 printf("3. Expire by message age\n");
1064 } while((buf[0]<48)||(buf[0]>51));
1065 expire_mode = buf[0] - 48;
1067 /* ...lunatics and monsters underneath my bed */
1068 if (expire_mode == 2) {
1069 sprintf(buf, "%d", expire_value);
1070 strprompt("Keep how many messages online?", buf, 10);
1071 expire_value = atol(buf);
1074 if (expire_mode == 3) {
1075 sprintf(buf, "%d", expire_value);
1076 strprompt("Keep messages for how many days?", buf, 10);
1077 expire_value = atol(buf);
1081 snprintf(buf, sizeof buf, "SPEX floor|%d|%d",
1082 expire_mode, expire_value);
1086 snprintf(buf,sizeof buf,"EFLR %d|%s",curr_floor,
1087 &floorlist[(int)curr_floor][0]);
1090 printf("%s\n",&buf[4]);
1098 * kill the current floor
1100 void kill_floor(void) {
1101 int floornum_to_delete,a;
1104 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1106 floornum_to_delete = (-1);
1107 printf("(Press return to abort)\n");
1108 newprompt("Delete which floor? ",buf,255);
1109 if (strlen(buf)==0) return;
1110 for (a=0; a<128; ++a)
1111 if (!strcasecmp(&floorlist[a][0],buf))
1112 floornum_to_delete = a;
1113 if (floornum_to_delete < 0) {
1114 printf("No such floor. Select one of:\n");
1115 for (a=0; a<128; ++a)
1116 if (floorlist[a][0]!=0)
1117 printf("%s\n",&floorlist[a][0]);
1119 } while (floornum_to_delete < 0);
1120 sprintf(buf,"KFLR %d|1",floornum_to_delete);
1123 printf("%s\n",&buf[4]);