5 * Client-side functions which perform room operations
17 #include <sys/types.h>
31 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
34 void sttybbs(int cmd);
35 void hit_any_key(void);
37 void strprompt(char *prompt, char *str, int len);
38 void newprompt(char *prompt, char *str, int len);
39 void dotgoto(char *towhere, int display_name);
40 void serv_read(char *buf, int bytes);
41 void formout(char *name);
43 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
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][256];
71 void load_floorlist(void) {
75 for (a=0; a<128; ++a) floorlist[a][0] = 0;
80 strcpy(floorlist[0],"Main Floor");
83 while (serv_gets(buf), strcmp(buf,"000")) {
84 extract(floorlist[extract_int(buf,0)],buf,1);
89 void room_tree_list(struct roomlisting *rp) {
91 char rmname[ROOMNAMELEN];
99 if (rp->lnext != NULL) {
100 room_tree_list(rp->lnext);
103 if (sigcaught == 0) {
104 strcpy(rmname, rp->rlname);
106 if ((c + strlen(rmname) + 4) > screenwidth) {
108 /* line break, check the paginator */
112 if (f & QR_MAILBOX) {
113 color(BRIGHT_YELLOW);
115 else if (f & QR_PRIVATE) {
121 pprintf("%s",rmname);
122 if ((f & QR_DIRECTORY) && (f & QR_NETWORK)) pprintf("} ");
123 else if (f & QR_DIRECTORY) pprintf("] ");
124 else if (f & QR_NETWORK) pprintf(") ");
126 c = c + strlen(rmname) + 3;
129 if (rp->rnext != NULL) {
130 room_tree_list(rp->rnext);
138 * Room ordering stuff (compare first by floor, then by order)
140 int rordercmp(struct roomlisting *r1, struct roomlisting *r2)
142 if ((r1==NULL)&&(r2==NULL)) return(0);
143 if (r1==NULL) return(-1);
144 if (r2==NULL) return(1);
145 if (r1->rlfloor < r2->rlfloor) return(-1);
146 if (r1->rlfloor > r2->rlfloor) return(1);
147 if (r1->rlorder < r2->rlorder) return(-1);
148 if (r1->rlorder > r2->rlorder) return(1);
154 * Common code for all room listings
156 void listrms(char *variety)
160 struct roomlisting *rl = NULL;
161 struct roomlisting *rp;
162 struct roomlisting *rs;
165 /* Ask the server for a room list */
168 if (buf[0]!='1') return;
169 while (serv_gets(buf), strcmp(buf, "000")) {
170 rp = malloc(sizeof(struct roomlisting));
171 extract(rp->rlname, buf, 0);
172 rp->rlflags = extract_int(buf, 1);
173 rp->rlfloor = extract_int(buf, 2);
174 rp->rlorder = extract_int(buf, 3);
182 else while (rp != NULL) {
183 if (rordercmp(rp, rs)<0) {
184 if (rs->lnext == NULL) {
193 if (rs->rnext == NULL) {
205 sttybbs(SB_YES_INTR);
206 room_tree_list(NULL);
213 void list_other_floors(void) {
217 for (a=0; a<128; ++a) if ((strlen(floorlist[a])>0)&&(a!=curr_floor)) {
218 if ((c + strlen(floorlist[a]) + 4) > screenwidth) {
222 pprintf("%s: ",floorlist[a]);
223 c = c + strlen(floorlist[a]) + 3;
229 * List known rooms. kn_floor_mode should be set to 0 for a 'flat' listing,
230 * 1 to list rooms on the current floor, or 1 to list rooms on all floors.
232 void knrooms(int kn_floor_mode)
239 if (kn_floor_mode == 0) {
241 pprintf("\n Rooms with unread messages:\n");
244 pprintf("\n\n No unseen messages in:\n");
249 if (kn_floor_mode == 1) {
251 pprintf("\n Rooms with unread messages on %s:\n",
252 floorlist[(int)curr_floor]);
253 sprintf(buf,"LKRN %d", curr_floor);
256 pprintf("\n\n Rooms with no new messages on %s:\n",
257 floorlist[(int)curr_floor]);
258 sprintf(buf,"LKRO %d",curr_floor);
261 pprintf("\n\n Other floors:\n");
266 if (kn_floor_mode == 2) {
267 for (a=0; a<128; ++a) if (floorlist[a][0]!=0) {
269 pprintf("\n Rooms on %s:\n",floorlist[a]);
270 sprintf(buf,"LKRA %d",a);
277 IFNEXPERT hit_any_key();
281 void listzrooms(void) { /* list public forgotten rooms */
283 pprintf("\n Forgotten public rooms:\n");
287 IFNEXPERT hit_any_key();
291 int set_room_attr(int ibuf, char *prompt, unsigned int sbit)
295 a = boolprompt(prompt, (ibuf&sbit));
297 if (!a) ibuf=(ibuf^sbit);
304 * Select a floor (used in several commands)
305 * The supplied argument is the 'default' floor number.
306 * This function returns the selected floor number.
308 int select_floor(int rfloor)
313 if (floor_mode == 1) {
314 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
318 safestrncpy(floorstr,floorlist[rfloor],sizeof floorstr);
319 strprompt("Which floor",floorstr,256);
320 for (a=0; a<128; ++a) {
321 if (!strcasecmp(floorstr,&floorlist[a][0]))
323 if ((newfloor<0)&&(!strncasecmp(floorstr,
324 &floorlist[a][0],strlen(floorstr))))
326 if ((newfloor<0)&&(pattern(&floorlist[a][0],
327 floorstr)>=0)) newfloor = a;
330 printf("\n One of:\n");
331 for (a=0; a<128; ++a)
332 if (floorlist[a][0]!=0)
336 } while(newfloor < 0);
346 * .<A>ide <E>dit room
348 void editthisroom(void) {
349 char rname[ROOMNAMELEN];
359 int expire_value = 0;
361 /* Fetch the existing room config */
365 printf("%s\n",&buf[4]);
369 extract(rname,&buf[4],0);
370 extract(rpass,&buf[4],1);
371 extract(rdir, &buf[4],2);
372 rflags = extract_int(&buf[4],3);
373 rfloor = extract_int(&buf[4],4);
374 rorder = extract_int(&buf[4],5);
377 /* Fetch the name of the current room aide */
380 if (buf[0]=='2') safestrncpy(raide,&buf[4],sizeof raide);
381 else strcpy(raide,"");
382 if (strlen(raide)==0) strcpy(raide,"none");
384 /* Fetch the expire policy (this will silently fail on old servers,
385 * resulting in "default" policy)
387 serv_puts("GPEX room");
390 expire_mode = extract_int(&buf[4], 0);
391 expire_value = extract_int(&buf[4], 1);
394 /* Now interact with the user. */
395 strprompt("Room name",rname,ROOMNAMELEN-1);
397 rfloor = select_floor(rfloor);
398 rflags = set_room_attr(rflags,"Private room",QR_PRIVATE);
399 if (rflags & QR_PRIVATE)
400 rflags = set_room_attr(rflags,
401 "Accessible by guessing room name",QR_GUESSNAME);
403 /* if it's public, clear the privacy classes */
404 if ((rflags & QR_PRIVATE)==0) {
405 if (rflags & QR_GUESSNAME) rflags = rflags - QR_GUESSNAME;
406 if (rflags & QR_PASSWORDED) rflags = rflags - QR_PASSWORDED;
409 /* if it's private, choose the privacy classes */
410 if ( (rflags & QR_PRIVATE)
411 && ( (rflags & QR_GUESSNAME) == 0) ) {
412 rflags = set_room_attr(rflags,
413 "Accessible by entering a password",QR_PASSWORDED);
415 if ( (rflags & QR_PRIVATE)
416 && ((rflags&QR_PASSWORDED)==QR_PASSWORDED) ) {
417 strprompt("Room password",rpass,9);
420 if ((rflags&QR_PRIVATE)==QR_PRIVATE) {
421 rbump = boolprompt("Cause current users to forget room", 0);
424 rflags = set_room_attr(rflags,"Preferred users only",QR_PREFONLY);
425 rflags = set_room_attr(rflags,"Read-only room",QR_READONLY);
426 rflags = set_room_attr(rflags,"Directory room",QR_DIRECTORY);
427 rflags = set_room_attr(rflags,"Permanent room",QR_PERMANENT);
428 if (rflags & QR_DIRECTORY) {
429 strprompt("Directory name",rdir,14);
430 rflags = set_room_attr(rflags,"Uploading allowed",QR_UPLOAD);
431 rflags = set_room_attr(rflags,"Downloading allowed",
433 rflags = set_room_attr(rflags,"Visible directory",QR_VISDIR);
435 rflags = set_room_attr(rflags,"Network shared room",QR_NETWORK);
436 rflags = set_room_attr(rflags,
437 "Automatically make all messages anonymous",QR_ANONONLY);
438 if ( (rflags & QR_ANONONLY) == 0) {
439 rflags = set_room_attr(rflags,
440 "Ask users whether to make messages anonymous",
443 rorder = intprompt("Listing order", rorder, 1, 127);
445 /* Ask about the room aide */
447 strprompt("Room aide (or 'none')",raide,29);
448 if (!strcasecmp(raide,"none")) {
453 snprintf(buf,sizeof buf,"QUSR %s",raide);
456 if (buf[0]!='2') printf("%s\n",&buf[4]);
458 } while(buf[0]!='2');
460 if (!strcasecmp(raide,"none")) strcpy(raide,"");
463 /* Angels and demons dancing in my head... */
465 sprintf(buf, "%d", expire_mode);
466 strprompt("Message expire policy (? for list)", buf, 1);
469 printf("0. Use the default for this floor\n");
470 printf("1. Never automatically expire messages\n");
471 printf("2. Expire by message count\n");
472 printf("3. Expire by message age\n");
474 } while((buf[0]<48)||(buf[0]>51));
475 expire_mode = buf[0] - 48;
477 /* ...lunatics and monsters underneath my bed */
478 if (expire_mode == 2) {
479 sprintf(buf, "%d", expire_value);
480 strprompt("Keep how many messages online?", buf, 10);
481 expire_value = atol(buf);
484 if (expire_mode == 3) {
485 sprintf(buf, "%d", expire_value);
486 strprompt("Keep messages for how many days?", buf, 10);
487 expire_value = atol(buf);
490 /* Give 'em a chance to change their minds */
491 printf("Save changes (y/n)? ");
494 snprintf(buf,sizeof buf,"SETA %s",raide);
497 if (buf[0]!='2') printf("%s\n",&buf[4]);
499 snprintf(buf, sizeof buf, "SPEX room|%d|%d",
500 expire_mode, expire_value);
504 snprintf(buf,sizeof buf,"SETR %s|%s|%s|%d|%d|%d|%d",
505 rname,rpass,rdir,rflags,rbump,rfloor,rorder);
508 printf("%s\n",&buf[4]);
509 if (buf[0]=='2') dotgoto(rname,2);
515 * un-goto the previous room
520 if (!strcmp(ugname,"")) return;
521 snprintf(buf,sizeof buf,"GOTO %s",ugname);
525 printf("%s\n",&buf[4]);
528 sprintf(buf,"SLRP %ld",uglsn);
531 if (buf[0]!='2') printf("%s\n",&buf[4]);
532 safestrncpy(buf,ugname,sizeof buf);
538 /* Here's the code for simply transferring the file to the client,
539 * for folks who have their own clientware. It's a lot simpler than
540 * the [XYZ]modem code below...
541 * (This function assumes that a download file is already open on the server)
543 void download_to_local_disk(char *supplied_filename, long total_bytes)
547 long transmitted_bytes = 0L;
554 strcpy(filename, supplied_filename);
555 if (strlen(filename)==0) {
556 newprompt("Filename: ", filename, 250);
559 printf("Enter the name of the directory to save '%s'\n",
561 printf("to, or press return for the current directory.\n");
562 newprompt("Directory: ",dbuf,256);
563 if (strlen(dbuf)==0) strcpy(dbuf,".");
565 strcat(dbuf,filename);
567 savefp = fopen(dbuf,"w");
568 if (savefp == NULL) {
569 printf("Cannot open '%s': %s\n",dbuf,strerror(errno));
570 /* close the download file at the server */
574 printf("%s\n",&buf[4]);
578 progress(0,total_bytes);
579 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
580 bb = total_bytes - transmitted_bytes;
581 aa = ((bb < 4096) ? bb : 4096);
582 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
586 printf("%s\n",&buf[4]);
589 packet = extract_int(&buf[4],0);
590 serv_read(dbuf,packet);
591 if (fwrite(dbuf,packet,1,savefp) < 1) broken = 1;
592 transmitted_bytes = transmitted_bytes + (long)packet;
593 progress(transmitted_bytes,total_bytes);
596 /* close the download file at the server */
600 printf("%s\n",&buf[4]);
607 * download() - download a file or files. The argument passed to this
608 * function determines which protocol to use.
609 * proto - 0 = paginate, 1 = xmodem, 2 = raw, 3 = ymodem, 4 = zmodem, 5 = save
611 void download(int proto)
616 char transmit_cmd[256];
617 long total_bytes = 0L;
619 long transmitted_bytes = 0L;
625 if ((room_flags & QR_DOWNLOAD) == 0) {
626 printf("*** You cannot download from this room.\n");
630 newprompt("Enter filename: ",filename,255);
632 snprintf(buf,sizeof buf,"OPEN %s",filename);
636 printf("%s\n",&buf[4]);
639 total_bytes = extract_long(&buf[4],0);
641 /* Save to local disk, for folks with their own copy of the client */
643 download_to_local_disk(filename, total_bytes);
647 /* Meta-download for public clients */
648 printf("Fetching file from Citadel server...\n");
649 mkdir(tempdir, 0700);
650 snprintf(tempname, sizeof tempname, "%s/%s", tempdir, filename);
651 tpipe = fopen(tempname, "wb");
652 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
653 progress(transmitted_bytes, total_bytes);
654 bb = total_bytes - transmitted_bytes;
655 aa = ((bb < 4096) ? bb : 4096);
656 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
660 printf("%s\n",&buf[4]);
662 packet = extract_int(&buf[4],0);
663 serv_read(dbuf,packet);
664 if (fwrite(dbuf,packet,1,tpipe) < 1) broken = 1;
665 transmitted_bytes = transmitted_bytes + (long)packet;
668 progress(transmitted_bytes, total_bytes);
670 /* close the download file at the server */
674 printf("%s\n",&buf[4]);
677 if (proto==0) sprintf(transmit_cmd, "SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",tempname);
678 else if (proto==1) sprintf(transmit_cmd, "exec sx %s", tempname);
679 else if (proto==3) sprintf(transmit_cmd, "exec sb %s", tempname);
680 else if (proto==4) sprintf(transmit_cmd, "exec sz %s", tempname);
681 else sprintf(transmit_cmd, "exec cat %s", tempname);
684 system(transmit_cmd);
687 /* clean up the temporary directory */
694 * read directory of this room
705 pprintf("%s\n",&buf[4]);
709 extract(comment,&buf[4],0);
710 extract(flnm,&buf[4],1);
711 pprintf("\nDirectory of %s on %s\n",flnm,comment);
712 pprintf("-----------------------\n");
713 while (serv_gets(buf), strcmp(buf,"000")) {
716 extract(comment,buf,2);
717 if (strlen(flnm)<=14)
718 pprintf("%-14s %8s %s\n",flnm,flsz,comment);
720 pprintf("%s\n%14s %8s %s\n",flnm,"",flsz,comment);
726 * add a user to a private room
729 char aaa[31],bbb[256];
731 if ((room_flags & QR_PRIVATE)==0) {
732 printf("This is not a private room.\n");
736 newprompt("Name of user? ",aaa,30);
737 if (aaa[0]==0) return;
739 snprintf(bbb,sizeof bbb,"INVT %s",aaa);
742 printf("%s\n",&bbb[4]);
747 * kick a user out of a room
750 char aaa[31],bbb[256];
752 newprompt("Name of user? ",aaa,30);
753 if (aaa[0]==0) return;
755 snprintf(bbb,sizeof bbb,"KICK %s",aaa);
758 printf("%s\n",&bbb[4]);
763 * aide command: kill the current room
765 void killroom(void) {
771 printf("%s\n",&aaa[4]);
775 printf("Are you sure you want to kill this room? ");
776 if (yesno()==0) return;
780 printf("%s\n",&aaa[4]);
781 if (aaa[0]!='2') return;
782 dotgoto("_BASEROOM_",0);
785 void forget(void) { /* forget the current room */
788 printf("Are you sure you want to forget this room? ");
789 if (yesno()==0) return;
794 printf("%s\n",&cmd[4]);
798 /* now return to the lobby */
799 dotgoto("_BASEROOM_",0);
808 char new_room_name[ROOMNAMELEN];
810 char new_room_pass[10];
818 printf("%s\n",&cmd[4]);
822 newprompt("Name for new room? ",new_room_name,ROOMNAMELEN-1);
823 if (strlen(new_room_name)==0) return;
824 for (a=0; a<strlen(new_room_name); ++a)
825 if (new_room_name[a] == '|') new_room_name[a]='_';
827 new_room_floor = select_floor((int)curr_floor);
829 IFNEXPERT formout("roomaccess");
831 printf( "<?>Help\n<1>Public room\n<2>Guess-name room\n"
832 "<3>Passworded room\n<4>Invitation-only room\n"
833 "<5>Personal room\n");
834 printf("Enter room type: ");
837 } while (((b<'1')||(b>'5')) && (b!='?'));
840 formout("roomaccess");
842 } while ((b<'1')||(b>'5'));
845 new_room_type = b - 1;
846 if (new_room_type==2) {
847 newprompt("Enter a room password: ",new_room_pass,9);
848 for (a=0; a<strlen(new_room_pass); ++a)
849 if (new_room_pass[a] == '|') new_room_pass[a]='_';
851 else strcpy(new_room_pass,"");
853 printf("\042%s\042, a",new_room_name);
854 if (b==1) printf(" public room.");
855 if (b==2) printf(" guess-name room.");
856 if (b==3) printf(" passworded room, password: %s",new_room_pass);
857 if (b==4) printf("n invitation-only room.");
858 if (b==5) printf(" personal room.");
859 printf("\nInstall it? (y/n) : ");
863 snprintf(cmd, sizeof cmd, "CRE8 1|%s|%d|%s|%d", new_room_name,
864 new_room_type, new_room_pass, new_room_floor);
868 printf("%s\n",&cmd[4]);
872 /* command succeeded... now GO to the new room! */
873 dotgoto(new_room_name,0);
878 void readinfo(void) { /* read info file for current room */
885 if (cmd[0]!='1') return;
887 fmout(screenwidth,NULL,
888 ((userflags & US_PAGINATOR) ? 1 : 0),
894 * <W>ho knows room...
896 void whoknows(void) {
901 pprintf("%s\n",&buf[5]);
905 sttybbs(SB_YES_INTR);
906 while (serv_gets(buf), strncmp(buf,"000",3)) {
907 if (sigcaught==0) pprintf("%s\n",buf);
913 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
917 int b,cksum,editor_exit;
920 if (strlen(editor_path)==0) {
921 printf("Do you wish to re-enter %s? ",desc);
922 if (yesno()==0) return;
925 fp = fopen(temp,"w");
928 serv_puts(check_cmd);
931 printf("%s\n",&cmd[4]);
935 if (strlen(editor_path)>0) {
939 fp = fopen(temp,"w");
940 while (serv_gets(cmd), strcmp(cmd,"000")) {
941 fprintf(fp,"%s\n",cmd);
947 cksum = file_checksum(temp);
949 if (strlen(editor_path)>0) {
954 execlp(editor_path,editor_path,temp,NULL);
957 if (editor_pid>0) do {
959 b=wait(&editor_exit);
960 } while((b!=editor_pid)&&(b>=0));
962 printf("Executed %s\n", editor_path);
966 printf("Entering %s. ",desc);
967 printf("Press return twice when finished.\n");
973 if (file_checksum(temp) == cksum) {
974 printf("*** Aborted.\n");
978 serv_puts(write_cmd);
981 printf("%s\n",&cmd[4]);
986 while (fgets(cmd,255,fp)!=NULL) {
987 cmd[strlen(cmd)-1] = 0;
998 void enterinfo(void) { /* edit info file for current room */
999 do_edit("the Info file for this room","RINF","EINF 0","EINF 1");
1002 void enter_bio(void) {
1004 snprintf(cmd,sizeof cmd,"RBIO %s",fullname);
1005 do_edit("your Bio",cmd,"NOOP","EBIO");
1009 * create a new floor
1011 void create_floor(void) {
1013 char newfloorname[256];
1015 serv_puts("CFLR xx|0");
1018 printf("%s\n",&buf[4]);
1022 newprompt("Name for new floor: ",newfloorname,255);
1023 snprintf(buf,sizeof buf,"CFLR %s|1",newfloorname);
1027 printf("Floor has been created.\n");
1030 printf("%s\n",&buf[4]);
1035 * edit the current floor
1037 void edit_floor(void) {
1039 int expire_mode = 0;
1040 int expire_value = 0;
1042 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1044 /* Fetch the expire policy (this will silently fail on old servers,
1045 * resulting in "default" policy)
1047 serv_puts("GPEX floor");
1050 expire_mode = extract_int(&buf[4], 0);
1051 expire_value = extract_int(&buf[4], 1);
1054 /* Interact with the user */
1055 strprompt("Floor name",&floorlist[(int)curr_floor][0],255);
1057 /* Angels and demons dancing in my head... */
1059 sprintf(buf, "%d", expire_mode);
1060 strprompt("Floor default essage expire policy (? for list)",
1062 if (buf[0] == '?') {
1064 printf("0. Use the system default\n");
1065 printf("1. Never automatically expire messages\n");
1066 printf("2. Expire by message count\n");
1067 printf("3. Expire by message age\n");
1069 } while((buf[0]<48)||(buf[0]>51));
1070 expire_mode = buf[0] - 48;
1072 /* ...lunatics and monsters underneath my bed */
1073 if (expire_mode == 2) {
1074 sprintf(buf, "%d", expire_value);
1075 strprompt("Keep how many messages online?", buf, 10);
1076 expire_value = atol(buf);
1079 if (expire_mode == 3) {
1080 sprintf(buf, "%d", expire_value);
1081 strprompt("Keep messages for how many days?", buf, 10);
1082 expire_value = atol(buf);
1086 snprintf(buf, sizeof buf, "SPEX floor|%d|%d",
1087 expire_mode, expire_value);
1091 snprintf(buf,sizeof buf,"EFLR %d|%s",curr_floor,
1092 &floorlist[(int)curr_floor][0]);
1095 printf("%s\n",&buf[4]);
1103 * kill the current floor
1105 void kill_floor(void) {
1106 int floornum_to_delete,a;
1109 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1111 floornum_to_delete = (-1);
1112 printf("(Press return to abort)\n");
1113 newprompt("Delete which floor? ",buf,255);
1114 if (strlen(buf)==0) return;
1115 for (a=0; a<128; ++a)
1116 if (!strcasecmp(&floorlist[a][0],buf))
1117 floornum_to_delete = a;
1118 if (floornum_to_delete < 0) {
1119 printf("No such floor. Select one of:\n");
1120 for (a=0; a<128; ++a)
1121 if (floorlist[a][0]!=0)
1122 printf("%s\n",&floorlist[a][0]);
1124 } while (floornum_to_delete < 0);
1125 sprintf(buf,"KFLR %d|1",floornum_to_delete);
1128 printf("%s\n",&buf[4]);