1 /* Citadel/UX room-oriented routines */
10 #include <sys/types.h>
18 #define IFEXPERT if (userflags&US_EXPERT)
19 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
20 #define IFAIDE if (axlevel>=6)
21 #define IFNAIDE if (axlevel<6)
24 void sttybbs(int cmd);
25 void extract(char *dest, char *source, int parmnum);
26 int extract_int(char *source, int parmnum);
27 void hit_any_key(void);
30 void strprompt(char *prompt, char *str, int len);
31 void newprompt(char *prompt, char *str, int len);
32 int struncmp(char *lstr, char *rstr, int len);
33 void dotgoto(char *towhere, int display_name);
34 long extract_long(char *source, int parmnum);
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 citedit(FILE *fp, long int base_pos);
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);
44 void color(int colornum);
46 extern unsigned room_flags;
47 extern char room_name[];
49 extern char tempdir[];
50 extern int editor_pid;
51 extern char editor_path[];
52 extern int screenwidth;
53 extern int screenheight;
54 extern char fullname[];
56 extern char sigcaught;
57 extern char floor_mode;
58 extern char curr_floor;
65 extern char floorlist[128][256];
68 void load_floorlist(void) {
72 for (a=0; a<128; ++a) floorlist[a][0] = 0;
77 strcpy(floorlist[0],"Main Floor");
80 while (serv_gets(buf), strcmp(buf,"000")) {
81 extract(floorlist[extract_int(buf,0)],buf,1);
85 void listrms(char *variety)
93 if (buf[0]!='1') return;
97 while (serv_gets(buf), strcmp(buf,"000")) if (sigcaught==0) {
98 extract(rmname,buf,0);
99 if ((c + strlen(rmname) + 4) > screenwidth) {
103 f = extract_int(buf,1);
104 if (f & QR_PRIVATE) {
111 if ((f & QR_DIRECTORY) && (f & QR_NETWORK)) printf("} ");
112 else if (f & QR_DIRECTORY) printf("] ");
113 else if (f & QR_NETWORK) printf(") ");
115 c = c + strlen(rmname) + 3;
122 void list_other_floors(void) {
126 for (a=0; a<128; ++a) if ((strlen(floorlist[a])>0)&&(a!=curr_floor)) {
127 if ((c + strlen(floorlist[a]) + 4) > screenwidth) {
131 printf("%s: ",floorlist[a]);
132 c = c + strlen(floorlist[a]) + 3;
138 * List known rooms. kn_floor_mode should be set to 0 for a 'flat' listing,
139 * 1 to list rooms on the current floor, or 1 to list rooms on all floors.
141 void knrooms(int kn_floor_mode)
148 if (kn_floor_mode == 0) {
150 printf("\n Rooms with unread messages:\n");
153 printf("\n\n No unseen messages in:\n");
158 if (kn_floor_mode == 1) {
160 printf("\n Rooms with unread messages on %s:\n",
161 floorlist[(int)curr_floor]);
162 sprintf(buf,"LKRN %d",curr_floor);
165 printf("\n\n Rooms with no new messages on %s:\n",
166 floorlist[(int)curr_floor]);
167 sprintf(buf,"LKRO %d",curr_floor);
170 printf("\n\n Other floors:\n");
175 if (kn_floor_mode == 2) {
176 for (a=0; a<128; ++a) if (floorlist[a][0]!=0) {
178 printf("\n Rooms on %s:\n",floorlist[a]);
179 sprintf(buf,"LKRA %d",a);
186 IFNEXPERT hit_any_key();
190 void listzrooms(void) { /* list public forgotten rooms */
192 printf("\n Forgotten public rooms:\n");
196 IFNEXPERT hit_any_key();
200 int set_room_attr(int ibuf, char *prompt, unsigned int sbit)
204 printf("%s [%s]? ",prompt,((ibuf&sbit) ? "Yes":"No"));
205 a=yesno_d(ibuf&sbit);
207 if (!a) ibuf=(ibuf^sbit);
214 * Select a floor (used in several commands)
215 * The supplied argument is the 'default' floor number.
216 * This function returns the selected floor number.
218 int select_floor(int rfloor)
223 if (floor_mode == 1) {
224 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
228 safestrncpy(floorstr,floorlist[rfloor],sizeof floorstr);
229 strprompt("Which floor",floorstr,256);
230 for (a=0; a<128; ++a) {
231 if (!strucmp(floorstr,&floorlist[a][0]))
233 if ((newfloor<0)&&(!struncmp(floorstr,
234 &floorlist[a][0],strlen(floorstr))))
236 if ((newfloor<0)&&(pattern(&floorlist[a][0],
237 floorstr)>=0)) newfloor = a;
240 printf("\n One of:\n");
241 for (a=0; a<128; ++a)
242 if (floorlist[a][0]!=0)
246 } while(newfloor < 0);
256 * .<A>ide <E>dit room
258 void editthisroom(void) {
259 char rname[ROOMNAMELEN];
268 int expire_value = 0;
270 /* Fetch the existing room config */
274 printf("%s\n",&buf[4]);
278 extract(rname,&buf[4],0);
279 extract(rpass,&buf[4],1);
280 extract(rdir, &buf[4],2);
281 rflags = extract_int(&buf[4],3);
282 rfloor = extract_int(&buf[4],4);
285 /* Fetch the name of the current room aide */
288 if (buf[0]=='2') safestrncpy(raide,&buf[4],sizeof raide);
289 else strcpy(raide,"");
290 if (strlen(raide)==0) strcpy(raide,"none");
292 /* Fetch the expire policy (this will silently fail on old servers,
293 * resulting in "default" policy)
295 serv_puts("GPEX room");
298 expire_mode = extract_int(&buf[4], 0);
299 expire_value = extract_int(&buf[4], 1);
302 /* Now interact with the user. */
303 strprompt("Room name",rname,ROOMNAMELEN-1);
305 rfloor = select_floor(rfloor);
306 rflags = set_room_attr(rflags,"Private room",QR_PRIVATE);
307 if (rflags & QR_PRIVATE)
308 rflags = set_room_attr(rflags,
309 "Accessible by guessing room name",QR_GUESSNAME);
311 /* if it's public, clear the privacy classes */
312 if ((rflags & QR_PRIVATE)==0) {
313 if (rflags & QR_GUESSNAME) rflags = rflags - QR_GUESSNAME;
314 if (rflags & QR_PASSWORDED) rflags = rflags - QR_PASSWORDED;
317 /* if it's private, choose the privacy classes */
318 if ( (rflags & QR_PRIVATE)
319 && ( (rflags & QR_GUESSNAME) == 0) ) {
320 rflags = set_room_attr(rflags,
321 "Accessible by entering a password",QR_PASSWORDED);
323 if ( (rflags & QR_PRIVATE)
324 && ((rflags&QR_PASSWORDED)==QR_PASSWORDED) ) {
325 strprompt("Room password",rpass,9);
328 if ((rflags&QR_PRIVATE)==QR_PRIVATE) {
329 printf("Cause current users to forget room [No] ? ");
330 if (yesno_d(0)==1) rbump = 1;
333 rflags = set_room_attr(rflags,"Preferred users only",QR_PREFONLY);
334 rflags = set_room_attr(rflags,"Read-only room",QR_READONLY);
335 rflags = set_room_attr(rflags,"Directory room",QR_DIRECTORY);
336 rflags = set_room_attr(rflags,"Permanent room",QR_PERMANENT);
337 if (rflags & QR_DIRECTORY) {
338 strprompt("Directory name",rdir,14);
339 rflags = set_room_attr(rflags,"Uploading allowed",QR_UPLOAD);
340 rflags = set_room_attr(rflags,"Downloading allowed",
342 rflags = set_room_attr(rflags,"Visible directory",QR_VISDIR);
344 rflags = set_room_attr(rflags,"Network shared room",QR_NETWORK);
345 rflags = set_room_attr(rflags,
346 "Automatically make all messages anonymous",QR_ANONONLY);
347 if ( (rflags & QR_ANONONLY) == 0) {
348 rflags = set_room_attr(rflags,
349 "Ask users whether to make messages anonymous",
354 /* Ask about the room aide */
356 strprompt("Room aide (or 'none')",raide,29);
357 if (!strucmp(raide,"none")) {
362 snprintf(buf,sizeof buf,"QUSR %s",raide);
365 if (buf[0]!='2') printf("%s\n",&buf[4]);
367 } while(buf[0]!='2');
369 if (!strucmp(raide,"none")) strcpy(raide,"");
372 /* Angels and demons dancing in my head... */
374 sprintf(buf, "%d", expire_mode);
375 strprompt("Message expire policy (? for list)", buf, 1);
378 printf("0. Use the default for this floor\n");
379 printf("1. Never automatically expire messages\n");
380 printf("2. Expire by message count\n");
381 printf("3. Expire by message age\n");
383 } while((buf[0]<48)||(buf[0]>51));
384 expire_mode = buf[0] - 48;
386 /* ...lunatics and monsters underneath my bed */
387 if (expire_mode == 2) {
388 sprintf(buf, "%d", expire_value);
389 strprompt("Keep how many messages online?", buf, 10);
390 expire_value = atol(buf);
393 if (expire_mode == 3) {
394 sprintf(buf, "%d", expire_value);
395 strprompt("Keep messages for how many days?", buf, 10);
396 expire_value = atol(buf);
399 /* Give 'em a chance to change their minds */
400 printf("Save changes (y/n)? ");
403 snprintf(buf,sizeof buf,"SETA %s",raide);
406 if (buf[0]!='2') printf("%s\n",&buf[4]);
408 snprintf(buf, sizeof buf, "SPEX room|%d|%d",
409 expire_mode, expire_value);
412 if (buf[0]!='2') printf("%s\n",&buf[4]);
414 snprintf(buf,sizeof buf,"SETR %s|%s|%s|%d|%d|%d",
415 rname,rpass,rdir,rflags,rbump,rfloor);
418 printf("%s\n",&buf[4]);
419 if (buf[0]=='2') dotgoto(rname,2);
425 * un-goto the previous room
430 if (!strcmp(ugname,"")) return;
431 snprintf(buf,sizeof buf,"GOTO %s",ugname);
435 printf("%s\n",&buf[4]);
438 sprintf(buf,"SLRP %ld",uglsn);
441 if (buf[0]!='2') printf("%s\n",&buf[4]);
442 safestrncpy(buf,ugname,sizeof buf);
449 * download() - download a file or files. The argument passed to this
450 * function determines which protocol to use.
452 void download(int proto)
456 - 0 = paginate, 1 = xmodem, 2 = raw, 3 = ymodem, 4 = zmodem, 5 = save
463 long total_bytes = 0L;
464 long transmitted_bytes = 0L;
473 if ((room_flags & QR_DOWNLOAD) == 0) {
474 printf("*** You cannot download from this room.\n");
478 newprompt("Enter filename: ",filename,255);
480 snprintf(buf,sizeof buf,"OPEN %s",filename);
484 printf("%s\n",&buf[4]);
487 total_bytes = extract_long(&buf[4],0);
490 /* Here's the code for simply transferring the file to the client,
491 * for folks who have their own clientware. It's a lot simpler than
492 * the [XYZ]modem code below...
495 printf("Enter the name of the directory to save '%s'\n",
497 printf("to, or press return for the current directory.\n");
498 newprompt("Directory: ",dbuf,256);
499 if (strlen(dbuf)==0) strcpy(dbuf,".");
501 strcat(dbuf,filename);
503 savefp = fopen(dbuf,"w");
504 if (savefp == NULL) {
505 printf("Cannot open '%s': %s\n",dbuf,strerror(errno));
506 /* close the download file at the server */
510 printf("%s\n",&buf[4]);
514 progress(0,total_bytes);
515 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
516 bb = total_bytes - transmitted_bytes;
517 aa = ((bb < 4096) ? bb : 4096);
518 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
522 printf("%s\n",&buf[4]);
525 packet = extract_int(&buf[4],0);
526 serv_read(dbuf,packet);
527 if (fwrite(dbuf,packet,1,savefp) < 1) broken = 1;
528 transmitted_bytes = transmitted_bytes + (long)packet;
529 progress(transmitted_bytes,total_bytes);
532 /* close the download file at the server */
536 printf("%s\n",&buf[4]);
543 snprintf(buf,sizeof buf,"%s/%s",tempdir,filename);
546 /* We do the remainder of this function as a separate process in
547 * order to allow recovery if the transfer is aborted. If the
548 * file transfer program aborts, the first child process receives a
549 * "broken pipe" signal and aborts. We *should* be able to catch
550 * this condition with signal(), but it doesn't seem to work on all
555 /* wait for the download to finish */
556 while (wait(&b)!=a) ;;
558 /* close the download file at the server */
562 printf("%s\n",&buf[4]);
564 /* clean up the temporary directory */
569 snprintf(buf,sizeof buf,"%s/%s",tempdir,filename); /* full pathname */
571 /* The next fork() creates a second child process that is used for
572 * the actual file transfer program (usually sz).
575 if (proto_pid == 0) {
578 signal(SIGINT,SIG_DFL);
579 signal(SIGQUIT,SIG_DFL);
580 snprintf(dbuf,sizeof dbuf,"SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",buf);
586 signal(SIGINT,SIG_DFL);
587 signal(SIGQUIT,SIG_DFL);
588 if (proto==1) execlp("sx","sx",buf,NULL);
589 if (proto==2) execlp("cat","cat",buf,NULL);
590 if (proto==3) execlp("sb","sb",buf,NULL);
591 if (proto==4) execlp("sz","sz",buf,NULL);
592 execlp("cat","cat",buf,NULL);
596 tpipe = fopen(buf,"w");
598 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
599 bb = total_bytes - transmitted_bytes;
600 aa = ((bb < 4096) ? bb : 4096);
601 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
605 printf("%s\n",&buf[4]);
608 packet = extract_int(&buf[4],0);
609 serv_read(dbuf,packet);
610 if (fwrite(dbuf,packet,1,tpipe) < 1) broken = 1;
611 transmitted_bytes = transmitted_bytes + (long)packet;
613 if (tpipe!=NULL) fclose(tpipe);
615 /* Hang out and wait for the file transfer program to finish */
616 while (wait(&a) != proto_pid) ;;
620 exit(0); /* transfer control back to the main program */
625 * read directory of this room
636 printf("%s\n",&buf[4]);
640 extract(comment,&buf[4],0);
641 extract(flnm,&buf[4],1);
642 printf("\nDirectory of %s on %s\n",flnm,comment);
643 printf("-----------------------\n");
644 while (serv_gets(buf), strcmp(buf,"000")) {
647 extract(comment,buf,2);
648 if (strlen(flnm)<=14)
649 printf("%-14s %8s %s\n",flnm,flsz,comment);
651 printf("%s\n%14s %8s %s\n",flnm,"",flsz,comment);
657 * add a user to a private room
660 char aaa[31],bbb[256];
662 if ((room_flags & QR_PRIVATE)==0) {
663 printf("This is not a private room.\n");
667 newprompt("Name of user? ",aaa,30);
668 if (aaa[0]==0) return;
670 snprintf(bbb,sizeof bbb,"INVT %s",aaa);
673 printf("%s\n",&bbb[4]);
678 * kick a user out of a room
681 char aaa[31],bbb[256];
683 if ((room_flags & QR_PRIVATE)==0) {
684 printf("Note: this is not a private room. Kicking a user ");
685 printf("out of this room will only\nhave the same effect ");
686 printf("as if they <Z>apped the room.\n\n");
689 newprompt("Name of user? ",aaa,30);
690 if (aaa[0]==0) return;
692 snprintf(bbb,sizeof bbb,"KICK %s",aaa);
695 printf("%s\n",&bbb[4]);
700 * aide command: kill the current room
702 void killroom(void) {
708 printf("%s\n",&aaa[4]);
712 printf("Are you sure you want to kill this room? ");
713 if (yesno()==0) return;
717 printf("%s\n",&aaa[4]);
718 if (aaa[0]!='2') return;
719 dotgoto("_BASEROOM_",0);
722 void forget(void) { /* forget the current room */
725 printf("Are you sure you want to forget this room? ");
726 if (yesno()==0) return;
731 printf("%s\n",&cmd[4]);
735 /* now return to the lobby */
736 dotgoto("_BASEROOM_",0);
745 char new_room_name[ROOMNAMELEN];
747 char new_room_pass[10];
755 printf("%s\n",&cmd[4]);
759 newprompt("Name for new room? ",new_room_name,ROOMNAMELEN-1);
760 if (strlen(new_room_name)==0) return;
761 for (a=0; a<strlen(new_room_name); ++a)
762 if (new_room_name[a] == '|') new_room_name[a]='_';
764 new_room_floor = select_floor((int)curr_floor);
766 IFNEXPERT formout("roomaccess");
768 printf("<?>Help\n<1>Public room\n<2>Guess-name room\n");
769 printf("<3>Passworded room\n<4>Invitation-only room\n");
770 printf("Enter room type: ");
773 } while (((b<'1')||(b>'4')) && (b!='?'));
776 formout("roomaccess");
778 } while ((b<'1')||(b>'4'));
781 new_room_type = b - 1;
782 if (new_room_type==2) {
783 newprompt("Enter a room password: ",new_room_pass,9);
784 for (a=0; a<strlen(new_room_pass); ++a)
785 if (new_room_pass[a] == '|') new_room_pass[a]='_';
787 else strcpy(new_room_pass,"");
789 printf("\042%s\042, a",new_room_name);
790 if (b==1) printf(" public room.");
791 if (b==2) printf(" guess-name room.");
792 if (b==3) printf(" passworded room, password: %s",new_room_pass);
793 if (b==4) printf("n invitation-only room.");
794 printf("\nInstall it? (y/n) : ");
798 snprintf(cmd, sizeof cmd, "CRE8 1|%s|%d|%s|%d", new_room_name,
799 new_room_type, new_room_pass, new_room_floor);
803 printf("%s\n",&cmd[4]);
807 /* command succeeded... now GO to the new room! */
808 dotgoto(new_room_name,0);
813 void readinfo(void) { /* read info file for current room */
820 if (cmd[0]!='1') return;
822 fmout(screenwidth,NULL,
823 ((userflags & US_PAGINATOR) ? 1 : 0),
829 * <W>ho knows room...
831 void whoknows(void) {
836 printf("%s\n",&buf[5]);
840 sttybbs(SB_YES_INTR);
841 while (serv_gets(buf), strncmp(buf,"000",3)) {
842 if (sigcaught==0) printf("%s\n",buf);
848 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
852 int b,cksum,editor_exit;
855 if (strlen(editor_path)==0) {
856 printf("Do you wish to re-enter %s? ",desc);
857 if (yesno()==0) return;
860 fp = fopen(temp,"w");
863 serv_puts(check_cmd);
866 printf("%s\n",&cmd[4]);
870 if (strlen(editor_path)>0) {
874 fp = fopen(temp,"w");
875 while (serv_gets(cmd), strcmp(cmd,"000")) {
876 fprintf(fp,"%s\n",cmd);
882 cksum = file_checksum(temp);
884 if (strlen(editor_path)>0) {
889 execlp(editor_path,editor_path,temp,NULL);
892 if (editor_pid>0) do {
894 b=wait(&editor_exit);
895 } while((b!=editor_pid)&&(b>=0));
897 printf("Executed %s\n", editor_path);
901 printf("Entering %s. ",desc);
902 printf("Press return twice when finished.\n");
908 if (file_checksum(temp) == cksum) {
909 printf("*** Aborted.\n");
913 serv_puts(write_cmd);
916 printf("%s\n",&cmd[4]);
921 while (fgets(cmd,255,fp)!=NULL) {
922 cmd[strlen(cmd)-1] = 0;
933 void enterinfo(void) { /* edit info file for current room */
934 do_edit("the Info file for this room","RINF","EINF 0","EINF 1");
937 void enter_bio(void) {
939 snprintf(cmd,sizeof cmd,"RBIO %s",fullname);
940 do_edit("your Bio",cmd,"NOOP","EBIO");
946 void create_floor(void) {
948 char newfloorname[256];
950 serv_puts("CFLR xx|0");
953 printf("%s\n",&buf[4]);
957 newprompt("Name for new floor: ",newfloorname,255);
958 snprintf(buf,sizeof buf,"CFLR %s|1",newfloorname);
962 printf("Floor has been created.\n");
965 printf("%s\n",&buf[4]);
970 * edit the current floor
972 void edit_floor(void) {
975 int expire_value = 0;
977 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
979 /* Fetch the expire policy (this will silently fail on old servers,
980 * resulting in "default" policy)
982 serv_puts("GPEX floor");
985 expire_mode = extract_int(&buf[4], 0);
986 expire_value = extract_int(&buf[4], 1);
989 /* Interact with the user */
990 strprompt("Floor name",&floorlist[(int)curr_floor][0],255);
992 /* Angels and demons dancing in my head... */
994 sprintf(buf, "%d", expire_mode);
995 strprompt("Floor default essage expire policy (? for list)",
999 printf("0. Use the system default\n");
1000 printf("1. Never automatically expire messages\n");
1001 printf("2. Expire by message count\n");
1002 printf("3. Expire by message age\n");
1004 } while((buf[0]<48)||(buf[0]>51));
1005 expire_mode = buf[0] - 48;
1007 /* ...lunatics and monsters underneath my bed */
1008 if (expire_mode == 2) {
1009 sprintf(buf, "%d", expire_value);
1010 strprompt("Keep how many messages online?", buf, 10);
1011 expire_value = atol(buf);
1014 if (expire_mode == 3) {
1015 sprintf(buf, "%d", expire_value);
1016 strprompt("Keep messages for how many days?", buf, 10);
1017 expire_value = atol(buf);
1021 snprintf(buf, sizeof buf, "SPEX floor|%d|%d",
1022 expire_mode, expire_value);
1025 if (buf[0]!='2') printf("%s\n",&buf[4]);
1027 snprintf(buf,sizeof buf,"EFLR %d|%s",curr_floor,
1028 &floorlist[(int)curr_floor][0]);
1031 printf("%s\n",&buf[4]);
1039 * kill the current floor
1041 void kill_floor(void) {
1042 int floornum_to_delete,a;
1045 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1047 floornum_to_delete = (-1);
1048 printf("(Press return to abort)\n");
1049 newprompt("Delete which floor? ",buf,255);
1050 if (strlen(buf)==0) return;
1051 for (a=0; a<128; ++a)
1052 if (!strucmp(&floorlist[a][0],buf))
1053 floornum_to_delete = a;
1054 if (floornum_to_delete < 0) {
1055 printf("No such floor. Select one of:\n");
1056 for (a=0; a<128; ++a)
1057 if (floorlist[a][0]!=0)
1058 printf("%s\n",&floorlist[a][0]);
1060 } while (floornum_to_delete < 0);
1061 sprintf(buf,"KFLR %d|1",floornum_to_delete);
1064 printf("%s\n",&buf[4]);