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) {
106 if (f & QR_MAILBOX) {
107 color(BRIGHT_YELLOW);
109 else if (f & QR_PRIVATE) {
116 if ((f & QR_DIRECTORY) && (f & QR_NETWORK)) printf("} ");
117 else if (f & QR_DIRECTORY) printf("] ");
118 else if (f & QR_NETWORK) printf(") ");
120 c = c + strlen(rmname) + 3;
123 if (rp->rnext != NULL) {
124 room_tree_list(rp->rnext);
132 * Room ordering stuff (compare first by floor, then by order)
134 int rordercmp(struct roomlisting *r1, struct roomlisting *r2)
136 if ((r1==NULL)&&(r2==NULL)) return(0);
137 if (r1==NULL) return(-1);
138 if (r2==NULL) return(1);
139 if (r1->rlfloor < r2->rlfloor) return(-1);
140 if (r1->rlfloor > r2->rlfloor) return(1);
141 if (r1->rlorder < r2->rlorder) return(-1);
142 if (r1->rlorder > r2->rlorder) return(1);
148 * Common code for all room listings
150 void listrms(char *variety)
154 struct roomlisting *rl = NULL;
155 struct roomlisting *rp;
156 struct roomlisting *rs;
159 /* Ask the server for a room list */
162 if (buf[0]!='1') return;
163 while (serv_gets(buf), strcmp(buf, "000")) {
164 rp = malloc(sizeof(struct roomlisting));
165 extract(rp->rlname, buf, 0);
166 rp->rlflags = extract_int(buf, 1);
167 rp->rlfloor = extract_int(buf, 2);
168 rp->rlorder = extract_int(buf, 3);
176 else while (rp != NULL) {
177 if (rordercmp(rp, rs)<0) {
178 if (rs->lnext == NULL) {
187 if (rs->rnext == NULL) {
199 sttybbs(SB_YES_INTR);
200 room_tree_list(NULL);
207 void list_other_floors(void) {
211 for (a=0; a<128; ++a) if ((strlen(floorlist[a])>0)&&(a!=curr_floor)) {
212 if ((c + strlen(floorlist[a]) + 4) > screenwidth) {
216 printf("%s: ",floorlist[a]);
217 c = c + strlen(floorlist[a]) + 3;
223 * List known rooms. kn_floor_mode should be set to 0 for a 'flat' listing,
224 * 1 to list rooms on the current floor, or 1 to list rooms on all floors.
226 void knrooms(int kn_floor_mode)
233 if (kn_floor_mode == 0) {
235 printf("\n Rooms with unread messages:\n");
238 printf("\n\n No unseen messages in:\n");
243 if (kn_floor_mode == 1) {
245 printf("\n Rooms with unread messages on %s:\n",
246 floorlist[(int)curr_floor]);
247 sprintf(buf,"LKRN %d",curr_floor);
250 printf("\n\n Rooms with no new messages on %s:\n",
251 floorlist[(int)curr_floor]);
252 sprintf(buf,"LKRO %d",curr_floor);
255 printf("\n\n Other floors:\n");
260 if (kn_floor_mode == 2) {
261 for (a=0; a<128; ++a) if (floorlist[a][0]!=0) {
263 printf("\n Rooms on %s:\n",floorlist[a]);
264 sprintf(buf,"LKRA %d",a);
271 IFNEXPERT hit_any_key();
275 void listzrooms(void) { /* list public forgotten rooms */
277 printf("\n Forgotten public rooms:\n");
281 IFNEXPERT hit_any_key();
285 int set_room_attr(int ibuf, char *prompt, unsigned int sbit)
289 a = boolprompt(prompt, (ibuf&sbit));
291 if (!a) ibuf=(ibuf^sbit);
298 * Select a floor (used in several commands)
299 * The supplied argument is the 'default' floor number.
300 * This function returns the selected floor number.
302 int select_floor(int rfloor)
307 if (floor_mode == 1) {
308 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
312 safestrncpy(floorstr,floorlist[rfloor],sizeof floorstr);
313 strprompt("Which floor",floorstr,256);
314 for (a=0; a<128; ++a) {
315 if (!strucmp(floorstr,&floorlist[a][0]))
317 if ((newfloor<0)&&(!struncmp(floorstr,
318 &floorlist[a][0],strlen(floorstr))))
320 if ((newfloor<0)&&(pattern(&floorlist[a][0],
321 floorstr)>=0)) newfloor = a;
324 printf("\n One of:\n");
325 for (a=0; a<128; ++a)
326 if (floorlist[a][0]!=0)
330 } while(newfloor < 0);
340 * .<A>ide <E>dit room
342 void editthisroom(void) {
343 char rname[ROOMNAMELEN];
353 int expire_value = 0;
355 /* Fetch the existing room config */
359 printf("%s\n",&buf[4]);
363 extract(rname,&buf[4],0);
364 extract(rpass,&buf[4],1);
365 extract(rdir, &buf[4],2);
366 rflags = extract_int(&buf[4],3);
367 rfloor = extract_int(&buf[4],4);
368 rorder = extract_int(&buf[4],5);
371 /* Fetch the name of the current room aide */
374 if (buf[0]=='2') safestrncpy(raide,&buf[4],sizeof raide);
375 else strcpy(raide,"");
376 if (strlen(raide)==0) strcpy(raide,"none");
378 /* Fetch the expire policy (this will silently fail on old servers,
379 * resulting in "default" policy)
381 serv_puts("GPEX room");
384 expire_mode = extract_int(&buf[4], 0);
385 expire_value = extract_int(&buf[4], 1);
388 /* Now interact with the user. */
389 strprompt("Room name",rname,ROOMNAMELEN-1);
391 rfloor = select_floor(rfloor);
392 rflags = set_room_attr(rflags,"Private room",QR_PRIVATE);
393 if (rflags & QR_PRIVATE)
394 rflags = set_room_attr(rflags,
395 "Accessible by guessing room name",QR_GUESSNAME);
397 /* if it's public, clear the privacy classes */
398 if ((rflags & QR_PRIVATE)==0) {
399 if (rflags & QR_GUESSNAME) rflags = rflags - QR_GUESSNAME;
400 if (rflags & QR_PASSWORDED) rflags = rflags - QR_PASSWORDED;
403 /* if it's private, choose the privacy classes */
404 if ( (rflags & QR_PRIVATE)
405 && ( (rflags & QR_GUESSNAME) == 0) ) {
406 rflags = set_room_attr(rflags,
407 "Accessible by entering a password",QR_PASSWORDED);
409 if ( (rflags & QR_PRIVATE)
410 && ((rflags&QR_PASSWORDED)==QR_PASSWORDED) ) {
411 strprompt("Room password",rpass,9);
414 if ((rflags&QR_PRIVATE)==QR_PRIVATE) {
415 rbump = boolprompt("Cause current users to forget room", 0);
418 rflags = set_room_attr(rflags,"Preferred users only",QR_PREFONLY);
419 rflags = set_room_attr(rflags,"Read-only room",QR_READONLY);
420 rflags = set_room_attr(rflags,"Directory room",QR_DIRECTORY);
421 rflags = set_room_attr(rflags,"Permanent room",QR_PERMANENT);
422 if (rflags & QR_DIRECTORY) {
423 strprompt("Directory name",rdir,14);
424 rflags = set_room_attr(rflags,"Uploading allowed",QR_UPLOAD);
425 rflags = set_room_attr(rflags,"Downloading allowed",
427 rflags = set_room_attr(rflags,"Visible directory",QR_VISDIR);
429 rflags = set_room_attr(rflags,"Network shared room",QR_NETWORK);
430 rflags = set_room_attr(rflags,
431 "Automatically make all messages anonymous",QR_ANONONLY);
432 if ( (rflags & QR_ANONONLY) == 0) {
433 rflags = set_room_attr(rflags,
434 "Ask users whether to make messages anonymous",
437 rorder = intprompt("Listing order", rorder, 1, 127);
439 /* Ask about the room aide */
441 strprompt("Room aide (or 'none')",raide,29);
442 if (!strucmp(raide,"none")) {
447 snprintf(buf,sizeof buf,"QUSR %s",raide);
450 if (buf[0]!='2') printf("%s\n",&buf[4]);
452 } while(buf[0]!='2');
454 if (!strucmp(raide,"none")) strcpy(raide,"");
457 /* Angels and demons dancing in my head... */
459 sprintf(buf, "%d", expire_mode);
460 strprompt("Message expire policy (? for list)", buf, 1);
463 printf("0. Use the default for this floor\n");
464 printf("1. Never automatically expire messages\n");
465 printf("2. Expire by message count\n");
466 printf("3. Expire by message age\n");
468 } while((buf[0]<48)||(buf[0]>51));
469 expire_mode = buf[0] - 48;
471 /* ...lunatics and monsters underneath my bed */
472 if (expire_mode == 2) {
473 sprintf(buf, "%d", expire_value);
474 strprompt("Keep how many messages online?", buf, 10);
475 expire_value = atol(buf);
478 if (expire_mode == 3) {
479 sprintf(buf, "%d", expire_value);
480 strprompt("Keep messages for how many days?", buf, 10);
481 expire_value = atol(buf);
484 /* Give 'em a chance to change their minds */
485 printf("Save changes (y/n)? ");
488 snprintf(buf,sizeof buf,"SETA %s",raide);
491 if (buf[0]!='2') printf("%s\n",&buf[4]);
493 snprintf(buf, sizeof buf, "SPEX room|%d|%d",
494 expire_mode, expire_value);
498 snprintf(buf,sizeof buf,"SETR %s|%s|%s|%d|%d|%d|%d",
499 rname,rpass,rdir,rflags,rbump,rfloor,rorder);
502 printf("%s\n",&buf[4]);
503 if (buf[0]=='2') dotgoto(rname,2);
509 * un-goto the previous room
514 if (!strcmp(ugname,"")) return;
515 snprintf(buf,sizeof buf,"GOTO %s",ugname);
519 printf("%s\n",&buf[4]);
522 sprintf(buf,"SLRP %ld",uglsn);
525 if (buf[0]!='2') printf("%s\n",&buf[4]);
526 safestrncpy(buf,ugname,sizeof buf);
532 /* Here's the code for simply transferring the file to the client,
533 * for folks who have their own clientware. It's a lot simpler than
534 * the [XYZ]modem code below...
535 * (This function assumes that a download file is already open on the server)
537 void download_to_local_disk(char *supplied_filename, long total_bytes)
541 long transmitted_bytes = 0L;
548 strcpy(filename, supplied_filename);
549 if (strlen(filename)==0) {
550 newprompt("Filename: ", filename, 250);
553 printf("Enter the name of the directory to save '%s'\n",
555 printf("to, or press return for the current directory.\n");
556 newprompt("Directory: ",dbuf,256);
557 if (strlen(dbuf)==0) strcpy(dbuf,".");
559 strcat(dbuf,filename);
561 savefp = fopen(dbuf,"w");
562 if (savefp == NULL) {
563 printf("Cannot open '%s': %s\n",dbuf,strerror(errno));
564 /* close the download file at the server */
568 printf("%s\n",&buf[4]);
572 progress(0,total_bytes);
573 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
574 bb = total_bytes - transmitted_bytes;
575 aa = ((bb < 4096) ? bb : 4096);
576 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
580 printf("%s\n",&buf[4]);
583 packet = extract_int(&buf[4],0);
584 serv_read(dbuf,packet);
585 if (fwrite(dbuf,packet,1,savefp) < 1) broken = 1;
586 transmitted_bytes = transmitted_bytes + (long)packet;
587 progress(transmitted_bytes,total_bytes);
590 /* close the download file at the server */
594 printf("%s\n",&buf[4]);
601 * download() - download a file or files. The argument passed to this
602 * function determines which protocol to use.
603 * proto - 0 = paginate, 1 = xmodem, 2 = raw, 3 = ymodem, 4 = zmodem, 5 = save
605 void download(int proto)
610 char transmit_cmd[256];
611 long total_bytes = 0L;
613 long transmitted_bytes = 0L;
619 if ((room_flags & QR_DOWNLOAD) == 0) {
620 printf("*** You cannot download from this room.\n");
624 newprompt("Enter filename: ",filename,255);
626 snprintf(buf,sizeof buf,"OPEN %s",filename);
630 printf("%s\n",&buf[4]);
633 total_bytes = extract_long(&buf[4],0);
635 /* Save to local disk, for folks with their own copy of the client */
637 download_to_local_disk(filename, total_bytes);
641 /* Meta-download for public clients */
642 printf("Fetching file from Citadel server...\n");
643 mkdir(tempdir, 0700);
644 snprintf(tempname, sizeof tempname, "%s/%s", tempdir, filename);
645 tpipe = fopen(tempname, "wb");
646 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
647 progress(transmitted_bytes, total_bytes);
648 bb = total_bytes - transmitted_bytes;
649 aa = ((bb < 4096) ? bb : 4096);
650 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
654 printf("%s\n",&buf[4]);
656 packet = extract_int(&buf[4],0);
657 serv_read(dbuf,packet);
658 if (fwrite(dbuf,packet,1,tpipe) < 1) broken = 1;
659 transmitted_bytes = transmitted_bytes + (long)packet;
662 progress(transmitted_bytes, total_bytes);
664 /* close the download file at the server */
668 printf("%s\n",&buf[4]);
671 if (proto==0) sprintf(transmit_cmd, "SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",tempname);
672 else if (proto==1) sprintf(transmit_cmd, "exec sx %s", tempname);
673 else if (proto==3) sprintf(transmit_cmd, "exec sb %s", tempname);
674 else if (proto==4) sprintf(transmit_cmd, "exec sz %s", tempname);
675 else sprintf(transmit_cmd, "exec cat %s", tempname);
678 system(transmit_cmd);
681 /* clean up the temporary directory */
688 * read directory of this room
699 printf("%s\n",&buf[4]);
703 extract(comment,&buf[4],0);
704 extract(flnm,&buf[4],1);
705 printf("\nDirectory of %s on %s\n",flnm,comment);
706 printf("-----------------------\n");
707 while (serv_gets(buf), strcmp(buf,"000")) {
710 extract(comment,buf,2);
711 if (strlen(flnm)<=14)
712 printf("%-14s %8s %s\n",flnm,flsz,comment);
714 printf("%s\n%14s %8s %s\n",flnm,"",flsz,comment);
720 * add a user to a private room
723 char aaa[31],bbb[256];
725 if ((room_flags & QR_PRIVATE)==0) {
726 printf("This is not a private room.\n");
730 newprompt("Name of user? ",aaa,30);
731 if (aaa[0]==0) return;
733 snprintf(bbb,sizeof bbb,"INVT %s",aaa);
736 printf("%s\n",&bbb[4]);
741 * kick a user out of a room
744 char aaa[31],bbb[256];
746 newprompt("Name of user? ",aaa,30);
747 if (aaa[0]==0) return;
749 snprintf(bbb,sizeof bbb,"KICK %s",aaa);
752 printf("%s\n",&bbb[4]);
757 * aide command: kill the current room
759 void killroom(void) {
765 printf("%s\n",&aaa[4]);
769 printf("Are you sure you want to kill this room? ");
770 if (yesno()==0) return;
774 printf("%s\n",&aaa[4]);
775 if (aaa[0]!='2') return;
776 dotgoto("_BASEROOM_",0);
779 void forget(void) { /* forget the current room */
782 printf("Are you sure you want to forget this room? ");
783 if (yesno()==0) return;
788 printf("%s\n",&cmd[4]);
792 /* now return to the lobby */
793 dotgoto("_BASEROOM_",0);
802 char new_room_name[ROOMNAMELEN];
804 char new_room_pass[10];
812 printf("%s\n",&cmd[4]);
816 newprompt("Name for new room? ",new_room_name,ROOMNAMELEN-1);
817 if (strlen(new_room_name)==0) return;
818 for (a=0; a<strlen(new_room_name); ++a)
819 if (new_room_name[a] == '|') new_room_name[a]='_';
821 new_room_floor = select_floor((int)curr_floor);
823 IFNEXPERT formout("roomaccess");
825 printf( "<?>Help\n<1>Public room\n<2>Guess-name room\n"
826 "<3>Passworded room\n<4>Invitation-only room\n"
827 "<5>Personal room\n");
828 printf("Enter room type: ");
831 } while (((b<'1')||(b>'5')) && (b!='?'));
834 formout("roomaccess");
836 } while ((b<'1')||(b>'5'));
839 new_room_type = b - 1;
840 if (new_room_type==2) {
841 newprompt("Enter a room password: ",new_room_pass,9);
842 for (a=0; a<strlen(new_room_pass); ++a)
843 if (new_room_pass[a] == '|') new_room_pass[a]='_';
845 else strcpy(new_room_pass,"");
847 printf("\042%s\042, a",new_room_name);
848 if (b==1) printf(" public room.");
849 if (b==2) printf(" guess-name room.");
850 if (b==3) printf(" passworded room, password: %s",new_room_pass);
851 if (b==4) printf("n invitation-only room.");
852 if (b==5) printf(" personal room.");
853 printf("\nInstall it? (y/n) : ");
857 snprintf(cmd, sizeof cmd, "CRE8 1|%s|%d|%s|%d", new_room_name,
858 new_room_type, new_room_pass, new_room_floor);
862 printf("%s\n",&cmd[4]);
866 /* command succeeded... now GO to the new room! */
867 dotgoto(new_room_name,0);
872 void readinfo(void) { /* read info file for current room */
879 if (cmd[0]!='1') return;
881 fmout(screenwidth,NULL,
882 ((userflags & US_PAGINATOR) ? 1 : 0),
888 * <W>ho knows room...
890 void whoknows(void) {
895 printf("%s\n",&buf[5]);
899 sttybbs(SB_YES_INTR);
900 while (serv_gets(buf), strncmp(buf,"000",3)) {
901 if (sigcaught==0) printf("%s\n",buf);
907 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
911 int b,cksum,editor_exit;
914 if (strlen(editor_path)==0) {
915 printf("Do you wish to re-enter %s? ",desc);
916 if (yesno()==0) return;
919 fp = fopen(temp,"w");
922 serv_puts(check_cmd);
925 printf("%s\n",&cmd[4]);
929 if (strlen(editor_path)>0) {
933 fp = fopen(temp,"w");
934 while (serv_gets(cmd), strcmp(cmd,"000")) {
935 fprintf(fp,"%s\n",cmd);
941 cksum = file_checksum(temp);
943 if (strlen(editor_path)>0) {
948 execlp(editor_path,editor_path,temp,NULL);
951 if (editor_pid>0) do {
953 b=wait(&editor_exit);
954 } while((b!=editor_pid)&&(b>=0));
956 printf("Executed %s\n", editor_path);
960 printf("Entering %s. ",desc);
961 printf("Press return twice when finished.\n");
967 if (file_checksum(temp) == cksum) {
968 printf("*** Aborted.\n");
972 serv_puts(write_cmd);
975 printf("%s\n",&cmd[4]);
980 while (fgets(cmd,255,fp)!=NULL) {
981 cmd[strlen(cmd)-1] = 0;
992 void enterinfo(void) { /* edit info file for current room */
993 do_edit("the Info file for this room","RINF","EINF 0","EINF 1");
996 void enter_bio(void) {
998 snprintf(cmd,sizeof cmd,"RBIO %s",fullname);
999 do_edit("your Bio",cmd,"NOOP","EBIO");
1003 * create a new floor
1005 void create_floor(void) {
1007 char newfloorname[256];
1009 serv_puts("CFLR xx|0");
1012 printf("%s\n",&buf[4]);
1016 newprompt("Name for new floor: ",newfloorname,255);
1017 snprintf(buf,sizeof buf,"CFLR %s|1",newfloorname);
1021 printf("Floor has been created.\n");
1024 printf("%s\n",&buf[4]);
1029 * edit the current floor
1031 void edit_floor(void) {
1033 int expire_mode = 0;
1034 int expire_value = 0;
1036 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1038 /* Fetch the expire policy (this will silently fail on old servers,
1039 * resulting in "default" policy)
1041 serv_puts("GPEX floor");
1044 expire_mode = extract_int(&buf[4], 0);
1045 expire_value = extract_int(&buf[4], 1);
1048 /* Interact with the user */
1049 strprompt("Floor name",&floorlist[(int)curr_floor][0],255);
1051 /* Angels and demons dancing in my head... */
1053 sprintf(buf, "%d", expire_mode);
1054 strprompt("Floor default essage expire policy (? for list)",
1056 if (buf[0] == '?') {
1058 printf("0. Use the system default\n");
1059 printf("1. Never automatically expire messages\n");
1060 printf("2. Expire by message count\n");
1061 printf("3. Expire by message age\n");
1063 } while((buf[0]<48)||(buf[0]>51));
1064 expire_mode = buf[0] - 48;
1066 /* ...lunatics and monsters underneath my bed */
1067 if (expire_mode == 2) {
1068 sprintf(buf, "%d", expire_value);
1069 strprompt("Keep how many messages online?", buf, 10);
1070 expire_value = atol(buf);
1073 if (expire_mode == 3) {
1074 sprintf(buf, "%d", expire_value);
1075 strprompt("Keep messages for how many days?", buf, 10);
1076 expire_value = atol(buf);
1080 snprintf(buf, sizeof buf, "SPEX floor|%d|%d",
1081 expire_mode, expire_value);
1085 snprintf(buf,sizeof buf,"EFLR %d|%s",curr_floor,
1086 &floorlist[(int)curr_floor][0]);
1089 printf("%s\n",&buf[4]);
1097 * kill the current floor
1099 void kill_floor(void) {
1100 int floornum_to_delete,a;
1103 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1105 floornum_to_delete = (-1);
1106 printf("(Press return to abort)\n");
1107 newprompt("Delete which floor? ",buf,255);
1108 if (strlen(buf)==0) return;
1109 for (a=0; a<128; ++a)
1110 if (!strucmp(&floorlist[a][0],buf))
1111 floornum_to_delete = a;
1112 if (floornum_to_delete < 0) {
1113 printf("No such floor. Select one of:\n");
1114 for (a=0; a<128; ++a)
1115 if (floorlist[a][0]!=0)
1116 printf("%s\n",&floorlist[a][0]);
1118 } while (floornum_to_delete < 0);
1119 sprintf(buf,"KFLR %d|1",floornum_to_delete);
1122 printf("%s\n",&buf[4]);