1 /* Citadel/UX room-oriented routines */
11 #include <sys/types.h>
20 #define IFEXPERT if (userflags&US_EXPERT)
21 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
22 #define IFAIDE if (axlevel>=6)
23 #define IFNAIDE if (axlevel<6)
26 void sttybbs(int cmd);
27 void extract(char *dest, char *source, int parmnum);
28 int extract_int(char *source, int parmnum);
29 void hit_any_key(void);
31 void strprompt(char *prompt, char *str, int len);
32 void newprompt(char *prompt, char *str, int len);
33 int struncmp(char *lstr, char *rstr, int len);
34 void dotgoto(char *towhere, int display_name);
35 long extract_long(char *source, int parmnum);
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 citedit(FILE *fp, long int base_pos);
41 void progress(long int curr, long int cmax);
42 int pattern(char *search, char *patn);
43 int file_checksum(char *filename);
44 int nukedir(char *dirname);
45 void color(int colornum);
47 extern unsigned room_flags;
48 extern char room_name[];
50 extern char tempdir[];
51 extern int editor_pid;
52 extern char editor_path[];
53 extern int screenwidth;
54 extern int screenheight;
55 extern char fullname[];
57 extern char sigcaught;
58 extern char floor_mode;
59 extern char curr_floor;
66 extern char floorlist[128][256];
69 void load_floorlist(void) {
73 for (a=0; a<128; ++a) floorlist[a][0] = 0;
78 strcpy(floorlist[0],"Main Floor");
81 while (serv_gets(buf), strcmp(buf,"000")) {
82 extract(floorlist[extract_int(buf,0)],buf,1);
86 void listrms(char *variety)
94 if (buf[0]!='1') return;
98 while (serv_gets(buf), strcmp(buf,"000")) if (sigcaught==0) {
99 extract(rmname,buf,0);
100 if ((c + strlen(rmname) + 4) > screenwidth) {
104 f = extract_int(buf,1);
105 if (f & QR_MAILBOX) {
108 else if (f & QR_PRIVATE) {
115 if ((f & QR_DIRECTORY) && (f & QR_NETWORK)) printf("} ");
116 else if (f & QR_DIRECTORY) printf("] ");
117 else if (f & QR_NETWORK) printf(") ");
119 c = c + strlen(rmname) + 3;
126 void list_other_floors(void) {
130 for (a=0; a<128; ++a) if ((strlen(floorlist[a])>0)&&(a!=curr_floor)) {
131 if ((c + strlen(floorlist[a]) + 4) > screenwidth) {
135 printf("%s: ",floorlist[a]);
136 c = c + strlen(floorlist[a]) + 3;
142 * List known rooms. kn_floor_mode should be set to 0 for a 'flat' listing,
143 * 1 to list rooms on the current floor, or 1 to list rooms on all floors.
145 void knrooms(int kn_floor_mode)
152 if (kn_floor_mode == 0) {
154 printf("\n Rooms with unread messages:\n");
157 printf("\n\n No unseen messages in:\n");
162 if (kn_floor_mode == 1) {
164 printf("\n Rooms with unread messages on %s:\n",
165 floorlist[(int)curr_floor]);
166 sprintf(buf,"LKRN %d",curr_floor);
169 printf("\n\n Rooms with no new messages on %s:\n",
170 floorlist[(int)curr_floor]);
171 sprintf(buf,"LKRO %d",curr_floor);
174 printf("\n\n Other floors:\n");
179 if (kn_floor_mode == 2) {
180 for (a=0; a<128; ++a) if (floorlist[a][0]!=0) {
182 printf("\n Rooms on %s:\n",floorlist[a]);
183 sprintf(buf,"LKRA %d",a);
190 IFNEXPERT hit_any_key();
194 void listzrooms(void) { /* list public forgotten rooms */
196 printf("\n Forgotten public rooms:\n");
200 IFNEXPERT hit_any_key();
204 int set_room_attr(int ibuf, char *prompt, unsigned int sbit)
208 a = boolprompt(prompt, (ibuf&sbit));
210 if (!a) ibuf=(ibuf^sbit);
217 * Select a floor (used in several commands)
218 * The supplied argument is the 'default' floor number.
219 * This function returns the selected floor number.
221 int select_floor(int rfloor)
226 if (floor_mode == 1) {
227 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
231 safestrncpy(floorstr,floorlist[rfloor],sizeof floorstr);
232 strprompt("Which floor",floorstr,256);
233 for (a=0; a<128; ++a) {
234 if (!strucmp(floorstr,&floorlist[a][0]))
236 if ((newfloor<0)&&(!struncmp(floorstr,
237 &floorlist[a][0],strlen(floorstr))))
239 if ((newfloor<0)&&(pattern(&floorlist[a][0],
240 floorstr)>=0)) newfloor = a;
243 printf("\n One of:\n");
244 for (a=0; a<128; ++a)
245 if (floorlist[a][0]!=0)
249 } while(newfloor < 0);
259 * .<A>ide <E>dit room
261 void editthisroom(void) {
262 char rname[ROOMNAMELEN];
271 int expire_value = 0;
273 /* Fetch the existing room config */
277 printf("%s\n",&buf[4]);
281 extract(rname,&buf[4],0);
282 extract(rpass,&buf[4],1);
283 extract(rdir, &buf[4],2);
284 rflags = extract_int(&buf[4],3);
285 rfloor = extract_int(&buf[4],4);
288 /* Fetch the name of the current room aide */
291 if (buf[0]=='2') safestrncpy(raide,&buf[4],sizeof raide);
292 else strcpy(raide,"");
293 if (strlen(raide)==0) strcpy(raide,"none");
295 /* Fetch the expire policy (this will silently fail on old servers,
296 * resulting in "default" policy)
298 serv_puts("GPEX room");
301 expire_mode = extract_int(&buf[4], 0);
302 expire_value = extract_int(&buf[4], 1);
305 /* Now interact with the user. */
306 strprompt("Room name",rname,ROOMNAMELEN-1);
308 rfloor = select_floor(rfloor);
309 rflags = set_room_attr(rflags,"Private room",QR_PRIVATE);
310 if (rflags & QR_PRIVATE)
311 rflags = set_room_attr(rflags,
312 "Accessible by guessing room name",QR_GUESSNAME);
314 /* if it's public, clear the privacy classes */
315 if ((rflags & QR_PRIVATE)==0) {
316 if (rflags & QR_GUESSNAME) rflags = rflags - QR_GUESSNAME;
317 if (rflags & QR_PASSWORDED) rflags = rflags - QR_PASSWORDED;
320 /* if it's private, choose the privacy classes */
321 if ( (rflags & QR_PRIVATE)
322 && ( (rflags & QR_GUESSNAME) == 0) ) {
323 rflags = set_room_attr(rflags,
324 "Accessible by entering a password",QR_PASSWORDED);
326 if ( (rflags & QR_PRIVATE)
327 && ((rflags&QR_PASSWORDED)==QR_PASSWORDED) ) {
328 strprompt("Room password",rpass,9);
331 if ((rflags&QR_PRIVATE)==QR_PRIVATE) {
332 rbump = boolprompt("Cause current users to forget room", 0);
335 rflags = set_room_attr(rflags,"Preferred users only",QR_PREFONLY);
336 rflags = set_room_attr(rflags,"Read-only room",QR_READONLY);
337 rflags = set_room_attr(rflags,"Directory room",QR_DIRECTORY);
338 rflags = set_room_attr(rflags,"Permanent room",QR_PERMANENT);
339 if (rflags & QR_DIRECTORY) {
340 strprompt("Directory name",rdir,14);
341 rflags = set_room_attr(rflags,"Uploading allowed",QR_UPLOAD);
342 rflags = set_room_attr(rflags,"Downloading allowed",
344 rflags = set_room_attr(rflags,"Visible directory",QR_VISDIR);
346 rflags = set_room_attr(rflags,"Network shared room",QR_NETWORK);
347 rflags = set_room_attr(rflags,
348 "Automatically make all messages anonymous",QR_ANONONLY);
349 if ( (rflags & QR_ANONONLY) == 0) {
350 rflags = set_room_attr(rflags,
351 "Ask users whether to make messages anonymous",
356 /* Ask about the room aide */
358 strprompt("Room aide (or 'none')",raide,29);
359 if (!strucmp(raide,"none")) {
364 snprintf(buf,sizeof buf,"QUSR %s",raide);
367 if (buf[0]!='2') printf("%s\n",&buf[4]);
369 } while(buf[0]!='2');
371 if (!strucmp(raide,"none")) strcpy(raide,"");
374 /* Angels and demons dancing in my head... */
376 sprintf(buf, "%d", expire_mode);
377 strprompt("Message expire policy (? for list)", buf, 1);
380 printf("0. Use the default for this floor\n");
381 printf("1. Never automatically expire messages\n");
382 printf("2. Expire by message count\n");
383 printf("3. Expire by message age\n");
385 } while((buf[0]<48)||(buf[0]>51));
386 expire_mode = buf[0] - 48;
388 /* ...lunatics and monsters underneath my bed */
389 if (expire_mode == 2) {
390 sprintf(buf, "%d", expire_value);
391 strprompt("Keep how many messages online?", buf, 10);
392 expire_value = atol(buf);
395 if (expire_mode == 3) {
396 sprintf(buf, "%d", expire_value);
397 strprompt("Keep messages for how many days?", buf, 10);
398 expire_value = atol(buf);
401 /* Give 'em a chance to change their minds */
402 printf("Save changes (y/n)? ");
405 snprintf(buf,sizeof buf,"SETA %s",raide);
408 if (buf[0]!='2') printf("%s\n",&buf[4]);
410 snprintf(buf, sizeof buf, "SPEX room|%d|%d",
411 expire_mode, expire_value);
415 snprintf(buf,sizeof buf,"SETR %s|%s|%s|%d|%d|%d",
416 rname,rpass,rdir,rflags,rbump,rfloor);
419 printf("%s\n",&buf[4]);
420 if (buf[0]=='2') dotgoto(rname,2);
426 * un-goto the previous room
431 if (!strcmp(ugname,"")) return;
432 snprintf(buf,sizeof buf,"GOTO %s",ugname);
436 printf("%s\n",&buf[4]);
439 sprintf(buf,"SLRP %ld",uglsn);
442 if (buf[0]!='2') printf("%s\n",&buf[4]);
443 safestrncpy(buf,ugname,sizeof buf);
450 * download() - download a file or files. The argument passed to this
451 * function determines which protocol to use.
453 void download(int proto)
457 - 0 = paginate, 1 = xmodem, 2 = raw, 3 = ymodem, 4 = zmodem, 5 = save
464 long total_bytes = 0L;
465 long transmitted_bytes = 0L;
474 if ((room_flags & QR_DOWNLOAD) == 0) {
475 printf("*** You cannot download from this room.\n");
479 newprompt("Enter filename: ",filename,255);
481 snprintf(buf,sizeof buf,"OPEN %s",filename);
485 printf("%s\n",&buf[4]);
488 total_bytes = extract_long(&buf[4],0);
491 /* Here's the code for simply transferring the file to the client,
492 * for folks who have their own clientware. It's a lot simpler than
493 * the [XYZ]modem code below...
496 printf("Enter the name of the directory to save '%s'\n",
498 printf("to, or press return for the current directory.\n");
499 newprompt("Directory: ",dbuf,256);
500 if (strlen(dbuf)==0) strcpy(dbuf,".");
502 strcat(dbuf,filename);
504 savefp = fopen(dbuf,"w");
505 if (savefp == NULL) {
506 printf("Cannot open '%s': %s\n",dbuf,strerror(errno));
507 /* close the download file at the server */
511 printf("%s\n",&buf[4]);
515 progress(0,total_bytes);
516 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
517 bb = total_bytes - transmitted_bytes;
518 aa = ((bb < 4096) ? bb : 4096);
519 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
523 printf("%s\n",&buf[4]);
526 packet = extract_int(&buf[4],0);
527 serv_read(dbuf,packet);
528 if (fwrite(dbuf,packet,1,savefp) < 1) broken = 1;
529 transmitted_bytes = transmitted_bytes + (long)packet;
530 progress(transmitted_bytes,total_bytes);
533 /* close the download file at the server */
537 printf("%s\n",&buf[4]);
544 snprintf(buf,sizeof buf,"%s/%s",tempdir,filename);
547 /* We do the remainder of this function as a separate process in
548 * order to allow recovery if the transfer is aborted. If the
549 * file transfer program aborts, the first child process receives a
550 * "broken pipe" signal and aborts. We *should* be able to catch
551 * this condition with signal(), but it doesn't seem to work on all
556 /* wait for the download to finish */
557 while (wait(&b)!=a) ;;
559 /* close the download file at the server */
563 printf("%s\n",&buf[4]);
565 /* clean up the temporary directory */
570 snprintf(buf,sizeof buf,"%s/%s",tempdir,filename); /* full pathname */
572 /* The next fork() creates a second child process that is used for
573 * the actual file transfer program (usually sz).
576 if (proto_pid == 0) {
579 signal(SIGINT,SIG_DFL);
580 signal(SIGQUIT,SIG_DFL);
581 snprintf(dbuf,sizeof dbuf,"SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",buf);
587 signal(SIGINT,SIG_DFL);
588 signal(SIGQUIT,SIG_DFL);
589 if (proto==1) execlp("sx","sx",buf,NULL);
590 if (proto==2) execlp("cat","cat",buf,NULL);
591 if (proto==3) execlp("sb","sb",buf,NULL);
592 if (proto==4) execlp("sz","sz",buf,NULL);
593 execlp("cat","cat",buf,NULL);
597 tpipe = fopen(buf,"w");
599 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
600 bb = total_bytes - transmitted_bytes;
601 aa = ((bb < 4096) ? bb : 4096);
602 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
606 printf("%s\n",&buf[4]);
609 packet = extract_int(&buf[4],0);
610 serv_read(dbuf,packet);
611 if (fwrite(dbuf,packet,1,tpipe) < 1) broken = 1;
612 transmitted_bytes = transmitted_bytes + (long)packet;
614 if (tpipe!=NULL) fclose(tpipe);
616 /* Hang out and wait for the file transfer program to finish */
617 while (wait(&a) != proto_pid) ;;
621 exit(0); /* transfer control back to the main program */
626 * read directory of this room
637 printf("%s\n",&buf[4]);
641 extract(comment,&buf[4],0);
642 extract(flnm,&buf[4],1);
643 printf("\nDirectory of %s on %s\n",flnm,comment);
644 printf("-----------------------\n");
645 while (serv_gets(buf), strcmp(buf,"000")) {
648 extract(comment,buf,2);
649 if (strlen(flnm)<=14)
650 printf("%-14s %8s %s\n",flnm,flsz,comment);
652 printf("%s\n%14s %8s %s\n",flnm,"",flsz,comment);
658 * add a user to a private room
661 char aaa[31],bbb[256];
663 if ((room_flags & QR_PRIVATE)==0) {
664 printf("This is not a private room.\n");
668 newprompt("Name of user? ",aaa,30);
669 if (aaa[0]==0) return;
671 snprintf(bbb,sizeof bbb,"INVT %s",aaa);
674 printf("%s\n",&bbb[4]);
679 * kick a user out of a room
682 char aaa[31],bbb[256];
684 newprompt("Name of user? ",aaa,30);
685 if (aaa[0]==0) return;
687 snprintf(bbb,sizeof bbb,"KICK %s",aaa);
690 printf("%s\n",&bbb[4]);
695 * aide command: kill the current room
697 void killroom(void) {
703 printf("%s\n",&aaa[4]);
707 printf("Are you sure you want to kill this room? ");
708 if (yesno()==0) return;
712 printf("%s\n",&aaa[4]);
713 if (aaa[0]!='2') return;
714 dotgoto("_BASEROOM_",0);
717 void forget(void) { /* forget the current room */
720 printf("Are you sure you want to forget this room? ");
721 if (yesno()==0) return;
726 printf("%s\n",&cmd[4]);
730 /* now return to the lobby */
731 dotgoto("_BASEROOM_",0);
740 char new_room_name[ROOMNAMELEN];
742 char new_room_pass[10];
750 printf("%s\n",&cmd[4]);
754 newprompt("Name for new room? ",new_room_name,ROOMNAMELEN-1);
755 if (strlen(new_room_name)==0) return;
756 for (a=0; a<strlen(new_room_name); ++a)
757 if (new_room_name[a] == '|') new_room_name[a]='_';
759 new_room_floor = select_floor((int)curr_floor);
761 IFNEXPERT formout("roomaccess");
763 printf("<?>Help\n<1>Public room\n<2>Guess-name room\n");
764 printf("<3>Passworded room\n<4>Invitation-only room\n");
765 printf("Enter room type: ");
768 } while (((b<'1')||(b>'4')) && (b!='?'));
771 formout("roomaccess");
773 } while ((b<'1')||(b>'4'));
776 new_room_type = b - 1;
777 if (new_room_type==2) {
778 newprompt("Enter a room password: ",new_room_pass,9);
779 for (a=0; a<strlen(new_room_pass); ++a)
780 if (new_room_pass[a] == '|') new_room_pass[a]='_';
782 else strcpy(new_room_pass,"");
784 printf("\042%s\042, a",new_room_name);
785 if (b==1) printf(" public room.");
786 if (b==2) printf(" guess-name room.");
787 if (b==3) printf(" passworded room, password: %s",new_room_pass);
788 if (b==4) printf("n invitation-only room.");
789 printf("\nInstall it? (y/n) : ");
793 snprintf(cmd, sizeof cmd, "CRE8 1|%s|%d|%s|%d", new_room_name,
794 new_room_type, new_room_pass, new_room_floor);
798 printf("%s\n",&cmd[4]);
802 /* command succeeded... now GO to the new room! */
803 dotgoto(new_room_name,0);
808 void readinfo(void) { /* read info file for current room */
815 if (cmd[0]!='1') return;
817 fmout(screenwidth,NULL,
818 ((userflags & US_PAGINATOR) ? 1 : 0),
824 * <W>ho knows room...
826 void whoknows(void) {
831 printf("%s\n",&buf[5]);
835 sttybbs(SB_YES_INTR);
836 while (serv_gets(buf), strncmp(buf,"000",3)) {
837 if (sigcaught==0) printf("%s\n",buf);
843 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
847 int b,cksum,editor_exit;
850 if (strlen(editor_path)==0) {
851 printf("Do you wish to re-enter %s? ",desc);
852 if (yesno()==0) return;
855 fp = fopen(temp,"w");
858 serv_puts(check_cmd);
861 printf("%s\n",&cmd[4]);
865 if (strlen(editor_path)>0) {
869 fp = fopen(temp,"w");
870 while (serv_gets(cmd), strcmp(cmd,"000")) {
871 fprintf(fp,"%s\n",cmd);
877 cksum = file_checksum(temp);
879 if (strlen(editor_path)>0) {
884 execlp(editor_path,editor_path,temp,NULL);
887 if (editor_pid>0) do {
889 b=wait(&editor_exit);
890 } while((b!=editor_pid)&&(b>=0));
892 printf("Executed %s\n", editor_path);
896 printf("Entering %s. ",desc);
897 printf("Press return twice when finished.\n");
903 if (file_checksum(temp) == cksum) {
904 printf("*** Aborted.\n");
908 serv_puts(write_cmd);
911 printf("%s\n",&cmd[4]);
916 while (fgets(cmd,255,fp)!=NULL) {
917 cmd[strlen(cmd)-1] = 0;
928 void enterinfo(void) { /* edit info file for current room */
929 do_edit("the Info file for this room","RINF","EINF 0","EINF 1");
932 void enter_bio(void) {
934 snprintf(cmd,sizeof cmd,"RBIO %s",fullname);
935 do_edit("your Bio",cmd,"NOOP","EBIO");
941 void create_floor(void) {
943 char newfloorname[256];
945 serv_puts("CFLR xx|0");
948 printf("%s\n",&buf[4]);
952 newprompt("Name for new floor: ",newfloorname,255);
953 snprintf(buf,sizeof buf,"CFLR %s|1",newfloorname);
957 printf("Floor has been created.\n");
960 printf("%s\n",&buf[4]);
965 * edit the current floor
967 void edit_floor(void) {
970 int expire_value = 0;
972 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
974 /* Fetch the expire policy (this will silently fail on old servers,
975 * resulting in "default" policy)
977 serv_puts("GPEX floor");
980 expire_mode = extract_int(&buf[4], 0);
981 expire_value = extract_int(&buf[4], 1);
984 /* Interact with the user */
985 strprompt("Floor name",&floorlist[(int)curr_floor][0],255);
987 /* Angels and demons dancing in my head... */
989 sprintf(buf, "%d", expire_mode);
990 strprompt("Floor default essage expire policy (? for list)",
994 printf("0. Use the system default\n");
995 printf("1. Never automatically expire messages\n");
996 printf("2. Expire by message count\n");
997 printf("3. Expire by message age\n");
999 } while((buf[0]<48)||(buf[0]>51));
1000 expire_mode = buf[0] - 48;
1002 /* ...lunatics and monsters underneath my bed */
1003 if (expire_mode == 2) {
1004 sprintf(buf, "%d", expire_value);
1005 strprompt("Keep how many messages online?", buf, 10);
1006 expire_value = atol(buf);
1009 if (expire_mode == 3) {
1010 sprintf(buf, "%d", expire_value);
1011 strprompt("Keep messages for how many days?", buf, 10);
1012 expire_value = atol(buf);
1016 snprintf(buf, sizeof buf, "SPEX floor|%d|%d",
1017 expire_mode, expire_value);
1021 snprintf(buf,sizeof buf,"EFLR %d|%s",curr_floor,
1022 &floorlist[(int)curr_floor][0]);
1025 printf("%s\n",&buf[4]);
1033 * kill the current floor
1035 void kill_floor(void) {
1036 int floornum_to_delete,a;
1039 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1041 floornum_to_delete = (-1);
1042 printf("(Press return to abort)\n");
1043 newprompt("Delete which floor? ",buf,255);
1044 if (strlen(buf)==0) return;
1045 for (a=0; a<128; ++a)
1046 if (!strucmp(&floorlist[a][0],buf))
1047 floornum_to_delete = a;
1048 if (floornum_to_delete < 0) {
1049 printf("No such floor. Select one of:\n");
1050 for (a=0; a<128; ++a)
1051 if (floorlist[a][0]!=0)
1052 printf("%s\n",&floorlist[a][0]);
1054 } while (floornum_to_delete < 0);
1055 sprintf(buf,"KFLR %d|1",floornum_to_delete);
1058 printf("%s\n",&buf[4]);