1 /* Citadel/UX room-oriented routines */
11 #include <sys/types.h>
19 #define IFEXPERT if (userflags&US_EXPERT)
20 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
21 #define IFAIDE if (axlevel>=6)
22 #define IFNAIDE if (axlevel<6)
25 void sttybbs(int cmd);
26 void extract(char *dest, char *source, int parmnum);
27 int extract_int(char *source, int parmnum);
28 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_MAILBOX) {
107 else if (f & QR_PRIVATE) {
114 if ((f & QR_DIRECTORY) && (f & QR_NETWORK)) printf("} ");
115 else if (f & QR_DIRECTORY) printf("] ");
116 else if (f & QR_NETWORK) printf(") ");
118 c = c + strlen(rmname) + 3;
125 void list_other_floors(void) {
129 for (a=0; a<128; ++a) if ((strlen(floorlist[a])>0)&&(a!=curr_floor)) {
130 if ((c + strlen(floorlist[a]) + 4) > screenwidth) {
134 printf("%s: ",floorlist[a]);
135 c = c + strlen(floorlist[a]) + 3;
141 * List known rooms. kn_floor_mode should be set to 0 for a 'flat' listing,
142 * 1 to list rooms on the current floor, or 1 to list rooms on all floors.
144 void knrooms(int kn_floor_mode)
151 if (kn_floor_mode == 0) {
153 printf("\n Rooms with unread messages:\n");
156 printf("\n\n No unseen messages in:\n");
161 if (kn_floor_mode == 1) {
163 printf("\n Rooms with unread messages on %s:\n",
164 floorlist[(int)curr_floor]);
165 sprintf(buf,"LKRN %d",curr_floor);
168 printf("\n\n Rooms with no new messages on %s:\n",
169 floorlist[(int)curr_floor]);
170 sprintf(buf,"LKRO %d",curr_floor);
173 printf("\n\n Other floors:\n");
178 if (kn_floor_mode == 2) {
179 for (a=0; a<128; ++a) if (floorlist[a][0]!=0) {
181 printf("\n Rooms on %s:\n",floorlist[a]);
182 sprintf(buf,"LKRA %d",a);
189 IFNEXPERT hit_any_key();
193 void listzrooms(void) { /* list public forgotten rooms */
195 printf("\n Forgotten public rooms:\n");
199 IFNEXPERT hit_any_key();
203 int set_room_attr(int ibuf, char *prompt, unsigned int sbit)
207 a = boolprompt(prompt, (ibuf&sbit));
209 if (!a) ibuf=(ibuf^sbit);
216 * Select a floor (used in several commands)
217 * The supplied argument is the 'default' floor number.
218 * This function returns the selected floor number.
220 int select_floor(int rfloor)
225 if (floor_mode == 1) {
226 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
230 safestrncpy(floorstr,floorlist[rfloor],sizeof floorstr);
231 strprompt("Which floor",floorstr,256);
232 for (a=0; a<128; ++a) {
233 if (!strucmp(floorstr,&floorlist[a][0]))
235 if ((newfloor<0)&&(!struncmp(floorstr,
236 &floorlist[a][0],strlen(floorstr))))
238 if ((newfloor<0)&&(pattern(&floorlist[a][0],
239 floorstr)>=0)) newfloor = a;
242 printf("\n One of:\n");
243 for (a=0; a<128; ++a)
244 if (floorlist[a][0]!=0)
248 } while(newfloor < 0);
258 * .<A>ide <E>dit room
260 void editthisroom(void) {
261 char rname[ROOMNAMELEN];
270 int expire_value = 0;
272 /* Fetch the existing room config */
276 printf("%s\n",&buf[4]);
280 extract(rname,&buf[4],0);
281 extract(rpass,&buf[4],1);
282 extract(rdir, &buf[4],2);
283 rflags = extract_int(&buf[4],3);
284 rfloor = extract_int(&buf[4],4);
287 /* Fetch the name of the current room aide */
290 if (buf[0]=='2') safestrncpy(raide,&buf[4],sizeof raide);
291 else strcpy(raide,"");
292 if (strlen(raide)==0) strcpy(raide,"none");
294 /* Fetch the expire policy (this will silently fail on old servers,
295 * resulting in "default" policy)
297 serv_puts("GPEX room");
300 expire_mode = extract_int(&buf[4], 0);
301 expire_value = extract_int(&buf[4], 1);
304 /* Now interact with the user. */
305 strprompt("Room name",rname,ROOMNAMELEN-1);
307 rfloor = select_floor(rfloor);
308 rflags = set_room_attr(rflags,"Private room",QR_PRIVATE);
309 if (rflags & QR_PRIVATE)
310 rflags = set_room_attr(rflags,
311 "Accessible by guessing room name",QR_GUESSNAME);
313 /* if it's public, clear the privacy classes */
314 if ((rflags & QR_PRIVATE)==0) {
315 if (rflags & QR_GUESSNAME) rflags = rflags - QR_GUESSNAME;
316 if (rflags & QR_PASSWORDED) rflags = rflags - QR_PASSWORDED;
319 /* if it's private, choose the privacy classes */
320 if ( (rflags & QR_PRIVATE)
321 && ( (rflags & QR_GUESSNAME) == 0) ) {
322 rflags = set_room_attr(rflags,
323 "Accessible by entering a password",QR_PASSWORDED);
325 if ( (rflags & QR_PRIVATE)
326 && ((rflags&QR_PASSWORDED)==QR_PASSWORDED) ) {
327 strprompt("Room password",rpass,9);
330 if ((rflags&QR_PRIVATE)==QR_PRIVATE) {
331 rbump = boolprompt("Cause current users to forget room", 0);
334 rflags = set_room_attr(rflags,"Preferred users only",QR_PREFONLY);
335 rflags = set_room_attr(rflags,"Read-only room",QR_READONLY);
336 rflags = set_room_attr(rflags,"Directory room",QR_DIRECTORY);
337 rflags = set_room_attr(rflags,"Permanent room",QR_PERMANENT);
338 if (rflags & QR_DIRECTORY) {
339 strprompt("Directory name",rdir,14);
340 rflags = set_room_attr(rflags,"Uploading allowed",QR_UPLOAD);
341 rflags = set_room_attr(rflags,"Downloading allowed",
343 rflags = set_room_attr(rflags,"Visible directory",QR_VISDIR);
345 rflags = set_room_attr(rflags,"Network shared room",QR_NETWORK);
346 rflags = set_room_attr(rflags,
347 "Automatically make all messages anonymous",QR_ANONONLY);
348 if ( (rflags & QR_ANONONLY) == 0) {
349 rflags = set_room_attr(rflags,
350 "Ask users whether to make messages anonymous",
355 /* Ask about the room aide */
357 strprompt("Room aide (or 'none')",raide,29);
358 if (!strucmp(raide,"none")) {
363 snprintf(buf,sizeof buf,"QUSR %s",raide);
366 if (buf[0]!='2') printf("%s\n",&buf[4]);
368 } while(buf[0]!='2');
370 if (!strucmp(raide,"none")) strcpy(raide,"");
373 /* Angels and demons dancing in my head... */
375 sprintf(buf, "%d", expire_mode);
376 strprompt("Message expire policy (? for list)", buf, 1);
379 printf("0. Use the default for this floor\n");
380 printf("1. Never automatically expire messages\n");
381 printf("2. Expire by message count\n");
382 printf("3. Expire by message age\n");
384 } while((buf[0]<48)||(buf[0]>51));
385 expire_mode = buf[0] - 48;
387 /* ...lunatics and monsters underneath my bed */
388 if (expire_mode == 2) {
389 sprintf(buf, "%d", expire_value);
390 strprompt("Keep how many messages online?", buf, 10);
391 expire_value = atol(buf);
394 if (expire_mode == 3) {
395 sprintf(buf, "%d", expire_value);
396 strprompt("Keep messages for how many days?", buf, 10);
397 expire_value = atol(buf);
400 /* Give 'em a chance to change their minds */
401 printf("Save changes (y/n)? ");
404 snprintf(buf,sizeof buf,"SETA %s",raide);
407 if (buf[0]!='2') printf("%s\n",&buf[4]);
409 snprintf(buf, sizeof buf, "SPEX room|%d|%d",
410 expire_mode, expire_value);
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 newprompt("Name of user? ",aaa,30);
684 if (aaa[0]==0) return;
686 snprintf(bbb,sizeof bbb,"KICK %s",aaa);
689 printf("%s\n",&bbb[4]);
694 * aide command: kill the current room
696 void killroom(void) {
702 printf("%s\n",&aaa[4]);
706 printf("Are you sure you want to kill this room? ");
707 if (yesno()==0) return;
711 printf("%s\n",&aaa[4]);
712 if (aaa[0]!='2') return;
713 dotgoto("_BASEROOM_",0);
716 void forget(void) { /* forget the current room */
719 printf("Are you sure you want to forget this room? ");
720 if (yesno()==0) return;
725 printf("%s\n",&cmd[4]);
729 /* now return to the lobby */
730 dotgoto("_BASEROOM_",0);
739 char new_room_name[ROOMNAMELEN];
741 char new_room_pass[10];
749 printf("%s\n",&cmd[4]);
753 newprompt("Name for new room? ",new_room_name,ROOMNAMELEN-1);
754 if (strlen(new_room_name)==0) return;
755 for (a=0; a<strlen(new_room_name); ++a)
756 if (new_room_name[a] == '|') new_room_name[a]='_';
758 new_room_floor = select_floor((int)curr_floor);
760 IFNEXPERT formout("roomaccess");
762 printf("<?>Help\n<1>Public room\n<2>Guess-name room\n");
763 printf("<3>Passworded room\n<4>Invitation-only room\n");
764 printf("Enter room type: ");
767 } while (((b<'1')||(b>'4')) && (b!='?'));
770 formout("roomaccess");
772 } while ((b<'1')||(b>'4'));
775 new_room_type = b - 1;
776 if (new_room_type==2) {
777 newprompt("Enter a room password: ",new_room_pass,9);
778 for (a=0; a<strlen(new_room_pass); ++a)
779 if (new_room_pass[a] == '|') new_room_pass[a]='_';
781 else strcpy(new_room_pass,"");
783 printf("\042%s\042, a",new_room_name);
784 if (b==1) printf(" public room.");
785 if (b==2) printf(" guess-name room.");
786 if (b==3) printf(" passworded room, password: %s",new_room_pass);
787 if (b==4) printf("n invitation-only room.");
788 printf("\nInstall it? (y/n) : ");
792 snprintf(cmd, sizeof cmd, "CRE8 1|%s|%d|%s|%d", new_room_name,
793 new_room_type, new_room_pass, new_room_floor);
797 printf("%s\n",&cmd[4]);
801 /* command succeeded... now GO to the new room! */
802 dotgoto(new_room_name,0);
807 void readinfo(void) { /* read info file for current room */
814 if (cmd[0]!='1') return;
816 fmout(screenwidth,NULL,
817 ((userflags & US_PAGINATOR) ? 1 : 0),
823 * <W>ho knows room...
825 void whoknows(void) {
830 printf("%s\n",&buf[5]);
834 sttybbs(SB_YES_INTR);
835 while (serv_gets(buf), strncmp(buf,"000",3)) {
836 if (sigcaught==0) printf("%s\n",buf);
842 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
846 int b,cksum,editor_exit;
849 if (strlen(editor_path)==0) {
850 printf("Do you wish to re-enter %s? ",desc);
851 if (yesno()==0) return;
854 fp = fopen(temp,"w");
857 serv_puts(check_cmd);
860 printf("%s\n",&cmd[4]);
864 if (strlen(editor_path)>0) {
868 fp = fopen(temp,"w");
869 while (serv_gets(cmd), strcmp(cmd,"000")) {
870 fprintf(fp,"%s\n",cmd);
876 cksum = file_checksum(temp);
878 if (strlen(editor_path)>0) {
883 execlp(editor_path,editor_path,temp,NULL);
886 if (editor_pid>0) do {
888 b=wait(&editor_exit);
889 } while((b!=editor_pid)&&(b>=0));
891 printf("Executed %s\n", editor_path);
895 printf("Entering %s. ",desc);
896 printf("Press return twice when finished.\n");
902 if (file_checksum(temp) == cksum) {
903 printf("*** Aborted.\n");
907 serv_puts(write_cmd);
910 printf("%s\n",&cmd[4]);
915 while (fgets(cmd,255,fp)!=NULL) {
916 cmd[strlen(cmd)-1] = 0;
927 void enterinfo(void) { /* edit info file for current room */
928 do_edit("the Info file for this room","RINF","EINF 0","EINF 1");
931 void enter_bio(void) {
933 snprintf(cmd,sizeof cmd,"RBIO %s",fullname);
934 do_edit("your Bio",cmd,"NOOP","EBIO");
940 void create_floor(void) {
942 char newfloorname[256];
944 serv_puts("CFLR xx|0");
947 printf("%s\n",&buf[4]);
951 newprompt("Name for new floor: ",newfloorname,255);
952 snprintf(buf,sizeof buf,"CFLR %s|1",newfloorname);
956 printf("Floor has been created.\n");
959 printf("%s\n",&buf[4]);
964 * edit the current floor
966 void edit_floor(void) {
969 int expire_value = 0;
971 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
973 /* Fetch the expire policy (this will silently fail on old servers,
974 * resulting in "default" policy)
976 serv_puts("GPEX floor");
979 expire_mode = extract_int(&buf[4], 0);
980 expire_value = extract_int(&buf[4], 1);
983 /* Interact with the user */
984 strprompt("Floor name",&floorlist[(int)curr_floor][0],255);
986 /* Angels and demons dancing in my head... */
988 sprintf(buf, "%d", expire_mode);
989 strprompt("Floor default essage expire policy (? for list)",
993 printf("0. Use the system default\n");
994 printf("1. Never automatically expire messages\n");
995 printf("2. Expire by message count\n");
996 printf("3. Expire by message age\n");
998 } while((buf[0]<48)||(buf[0]>51));
999 expire_mode = buf[0] - 48;
1001 /* ...lunatics and monsters underneath my bed */
1002 if (expire_mode == 2) {
1003 sprintf(buf, "%d", expire_value);
1004 strprompt("Keep how many messages online?", buf, 10);
1005 expire_value = atol(buf);
1008 if (expire_mode == 3) {
1009 sprintf(buf, "%d", expire_value);
1010 strprompt("Keep messages for how many days?", buf, 10);
1011 expire_value = atol(buf);
1015 snprintf(buf, sizeof buf, "SPEX floor|%d|%d",
1016 expire_mode, expire_value);
1020 snprintf(buf,sizeof buf,"EFLR %d|%s",curr_floor,
1021 &floorlist[(int)curr_floor][0]);
1024 printf("%s\n",&buf[4]);
1032 * kill the current floor
1034 void kill_floor(void) {
1035 int floornum_to_delete,a;
1038 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1040 floornum_to_delete = (-1);
1041 printf("(Press return to abort)\n");
1042 newprompt("Delete which floor? ",buf,255);
1043 if (strlen(buf)==0) return;
1044 for (a=0; a<128; ++a)
1045 if (!strucmp(&floorlist[a][0],buf))
1046 floornum_to_delete = a;
1047 if (floornum_to_delete < 0) {
1048 printf("No such floor. Select one of:\n");
1049 for (a=0; a<128; ++a)
1050 if (floorlist[a][0]!=0)
1051 printf("%s\n",&floorlist[a][0]);
1053 } while (floornum_to_delete < 0);
1054 sprintf(buf,"KFLR %d|1",floornum_to_delete);
1057 printf("%s\n",&buf[4]);