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 hit_any_key(void);
29 void strprompt(char *prompt, char *str, int len);
30 void newprompt(char *prompt, char *str, int len);
31 int struncmp(char *lstr, char *rstr, int len);
32 void dotgoto(char *towhere, int display_name);
33 void serv_read(char *buf, int bytes);
34 void formout(char *name);
36 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
37 void citedit(FILE *fp, long int base_pos);
38 void progress(long int curr, long int cmax);
39 int pattern(char *search, char *patn);
40 int file_checksum(char *filename);
41 int nukedir(char *dirname);
42 void color(int colornum);
44 extern unsigned room_flags;
45 extern char room_name[];
47 extern char tempdir[];
48 extern int editor_pid;
49 extern char editor_path[];
50 extern int screenwidth;
51 extern int screenheight;
52 extern char fullname[];
54 extern char sigcaught;
55 extern char floor_mode;
56 extern char curr_floor;
63 extern char floorlist[128][256];
66 void load_floorlist(void) {
70 for (a=0; a<128; ++a) floorlist[a][0] = 0;
75 strcpy(floorlist[0],"Main Floor");
78 while (serv_gets(buf), strcmp(buf,"000")) {
79 extract(floorlist[extract_int(buf,0)],buf,1);
83 void listrms(char *variety)
91 if (buf[0]!='1') return;
95 while (serv_gets(buf), strcmp(buf,"000")) if (sigcaught==0) {
96 extract(rmname,buf,0);
97 if ((c + strlen(rmname) + 4) > screenwidth) {
101 f = extract_int(buf,1);
102 if (f & QR_MAILBOX) {
105 else if (f & QR_PRIVATE) {
112 if ((f & QR_DIRECTORY) && (f & QR_NETWORK)) printf("} ");
113 else if (f & QR_DIRECTORY) printf("] ");
114 else if (f & QR_NETWORK) printf(") ");
116 c = c + strlen(rmname) + 3;
123 void list_other_floors(void) {
127 for (a=0; a<128; ++a) if ((strlen(floorlist[a])>0)&&(a!=curr_floor)) {
128 if ((c + strlen(floorlist[a]) + 4) > screenwidth) {
132 printf("%s: ",floorlist[a]);
133 c = c + strlen(floorlist[a]) + 3;
139 * List known rooms. kn_floor_mode should be set to 0 for a 'flat' listing,
140 * 1 to list rooms on the current floor, or 1 to list rooms on all floors.
142 void knrooms(int kn_floor_mode)
149 if (kn_floor_mode == 0) {
151 printf("\n Rooms with unread messages:\n");
154 printf("\n\n No unseen messages in:\n");
159 if (kn_floor_mode == 1) {
161 printf("\n Rooms with unread messages on %s:\n",
162 floorlist[(int)curr_floor]);
163 sprintf(buf,"LKRN %d",curr_floor);
166 printf("\n\n Rooms with no new messages on %s:\n",
167 floorlist[(int)curr_floor]);
168 sprintf(buf,"LKRO %d",curr_floor);
171 printf("\n\n Other floors:\n");
176 if (kn_floor_mode == 2) {
177 for (a=0; a<128; ++a) if (floorlist[a][0]!=0) {
179 printf("\n Rooms on %s:\n",floorlist[a]);
180 sprintf(buf,"LKRA %d",a);
187 IFNEXPERT hit_any_key();
191 void listzrooms(void) { /* list public forgotten rooms */
193 printf("\n Forgotten public rooms:\n");
197 IFNEXPERT hit_any_key();
201 int set_room_attr(int ibuf, char *prompt, unsigned int sbit)
205 a = boolprompt(prompt, (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 rbump = boolprompt("Cause current users to forget room", 0);
332 rflags = set_room_attr(rflags,"Preferred users only",QR_PREFONLY);
333 rflags = set_room_attr(rflags,"Read-only room",QR_READONLY);
334 rflags = set_room_attr(rflags,"Directory room",QR_DIRECTORY);
335 rflags = set_room_attr(rflags,"Permanent room",QR_PERMANENT);
336 if (rflags & QR_DIRECTORY) {
337 strprompt("Directory name",rdir,14);
338 rflags = set_room_attr(rflags,"Uploading allowed",QR_UPLOAD);
339 rflags = set_room_attr(rflags,"Downloading allowed",
341 rflags = set_room_attr(rflags,"Visible directory",QR_VISDIR);
343 rflags = set_room_attr(rflags,"Network shared room",QR_NETWORK);
344 rflags = set_room_attr(rflags,
345 "Automatically make all messages anonymous",QR_ANONONLY);
346 if ( (rflags & QR_ANONONLY) == 0) {
347 rflags = set_room_attr(rflags,
348 "Ask users whether to make messages anonymous",
353 /* Ask about the room aide */
355 strprompt("Room aide (or 'none')",raide,29);
356 if (!strucmp(raide,"none")) {
361 snprintf(buf,sizeof buf,"QUSR %s",raide);
364 if (buf[0]!='2') printf("%s\n",&buf[4]);
366 } while(buf[0]!='2');
368 if (!strucmp(raide,"none")) strcpy(raide,"");
371 /* Angels and demons dancing in my head... */
373 sprintf(buf, "%d", expire_mode);
374 strprompt("Message expire policy (? for list)", buf, 1);
377 printf("0. Use the default for this floor\n");
378 printf("1. Never automatically expire messages\n");
379 printf("2. Expire by message count\n");
380 printf("3. Expire by message age\n");
382 } while((buf[0]<48)||(buf[0]>51));
383 expire_mode = buf[0] - 48;
385 /* ...lunatics and monsters underneath my bed */
386 if (expire_mode == 2) {
387 sprintf(buf, "%d", expire_value);
388 strprompt("Keep how many messages online?", buf, 10);
389 expire_value = atol(buf);
392 if (expire_mode == 3) {
393 sprintf(buf, "%d", expire_value);
394 strprompt("Keep messages for how many days?", buf, 10);
395 expire_value = atol(buf);
398 /* Give 'em a chance to change their minds */
399 printf("Save changes (y/n)? ");
402 snprintf(buf,sizeof buf,"SETA %s",raide);
405 if (buf[0]!='2') printf("%s\n",&buf[4]);
407 snprintf(buf, sizeof buf, "SPEX room|%d|%d",
408 expire_mode, expire_value);
412 snprintf(buf,sizeof buf,"SETR %s|%s|%s|%d|%d|%d",
413 rname,rpass,rdir,rflags,rbump,rfloor);
416 printf("%s\n",&buf[4]);
417 if (buf[0]=='2') dotgoto(rname,2);
423 * un-goto the previous room
428 if (!strcmp(ugname,"")) return;
429 snprintf(buf,sizeof buf,"GOTO %s",ugname);
433 printf("%s\n",&buf[4]);
436 sprintf(buf,"SLRP %ld",uglsn);
439 if (buf[0]!='2') printf("%s\n",&buf[4]);
440 safestrncpy(buf,ugname,sizeof buf);
447 * download() - download a file or files. The argument passed to this
448 * function determines which protocol to use.
450 void download(int proto)
454 - 0 = paginate, 1 = xmodem, 2 = raw, 3 = ymodem, 4 = zmodem, 5 = save
461 long total_bytes = 0L;
462 long transmitted_bytes = 0L;
471 if ((room_flags & QR_DOWNLOAD) == 0) {
472 printf("*** You cannot download from this room.\n");
476 newprompt("Enter filename: ",filename,255);
478 snprintf(buf,sizeof buf,"OPEN %s",filename);
482 printf("%s\n",&buf[4]);
485 total_bytes = extract_long(&buf[4],0);
488 /* Here's the code for simply transferring the file to the client,
489 * for folks who have their own clientware. It's a lot simpler than
490 * the [XYZ]modem code below...
493 printf("Enter the name of the directory to save '%s'\n",
495 printf("to, or press return for the current directory.\n");
496 newprompt("Directory: ",dbuf,256);
497 if (strlen(dbuf)==0) strcpy(dbuf,".");
499 strcat(dbuf,filename);
501 savefp = fopen(dbuf,"w");
502 if (savefp == NULL) {
503 printf("Cannot open '%s': %s\n",dbuf,strerror(errno));
504 /* close the download file at the server */
508 printf("%s\n",&buf[4]);
512 progress(0,total_bytes);
513 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
514 bb = total_bytes - transmitted_bytes;
515 aa = ((bb < 4096) ? bb : 4096);
516 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
520 printf("%s\n",&buf[4]);
523 packet = extract_int(&buf[4],0);
524 serv_read(dbuf,packet);
525 if (fwrite(dbuf,packet,1,savefp) < 1) broken = 1;
526 transmitted_bytes = transmitted_bytes + (long)packet;
527 progress(transmitted_bytes,total_bytes);
530 /* close the download file at the server */
534 printf("%s\n",&buf[4]);
541 snprintf(buf,sizeof buf,"%s/%s",tempdir,filename);
544 /* We do the remainder of this function as a separate process in
545 * order to allow recovery if the transfer is aborted. If the
546 * file transfer program aborts, the first child process receives a
547 * "broken pipe" signal and aborts. We *should* be able to catch
548 * this condition with signal(), but it doesn't seem to work on all
553 /* wait for the download to finish */
554 while (wait(&b)!=a) ;;
556 /* close the download file at the server */
560 printf("%s\n",&buf[4]);
562 /* clean up the temporary directory */
567 snprintf(buf,sizeof buf,"%s/%s",tempdir,filename); /* full pathname */
569 /* The next fork() creates a second child process that is used for
570 * the actual file transfer program (usually sz).
573 if (proto_pid == 0) {
576 signal(SIGINT,SIG_DFL);
577 signal(SIGQUIT,SIG_DFL);
578 snprintf(dbuf,sizeof dbuf,"SHELL=/dev/null; export SHELL; TERM=dumb; export TERM; exec more -d <%s",buf);
584 signal(SIGINT,SIG_DFL);
585 signal(SIGQUIT,SIG_DFL);
586 if (proto==1) execlp("sx","sx",buf,NULL);
587 if (proto==2) execlp("cat","cat",buf,NULL);
588 if (proto==3) execlp("sb","sb",buf,NULL);
589 if (proto==4) execlp("sz","sz",buf,NULL);
590 execlp("cat","cat",buf,NULL);
594 tpipe = fopen(buf,"w");
596 while ( (transmitted_bytes < total_bytes) && (broken == 0) ) {
597 bb = total_bytes - transmitted_bytes;
598 aa = ((bb < 4096) ? bb : 4096);
599 sprintf(buf,"READ %ld|%ld",transmitted_bytes,aa);
603 printf("%s\n",&buf[4]);
606 packet = extract_int(&buf[4],0);
607 serv_read(dbuf,packet);
608 if (fwrite(dbuf,packet,1,tpipe) < 1) broken = 1;
609 transmitted_bytes = transmitted_bytes + (long)packet;
611 if (tpipe!=NULL) fclose(tpipe);
613 /* Hang out and wait for the file transfer program to finish */
614 while (wait(&a) != proto_pid) ;;
618 exit(0); /* transfer control back to the main program */
623 * read directory of this room
634 printf("%s\n",&buf[4]);
638 extract(comment,&buf[4],0);
639 extract(flnm,&buf[4],1);
640 printf("\nDirectory of %s on %s\n",flnm,comment);
641 printf("-----------------------\n");
642 while (serv_gets(buf), strcmp(buf,"000")) {
645 extract(comment,buf,2);
646 if (strlen(flnm)<=14)
647 printf("%-14s %8s %s\n",flnm,flsz,comment);
649 printf("%s\n%14s %8s %s\n",flnm,"",flsz,comment);
655 * add a user to a private room
658 char aaa[31],bbb[256];
660 if ((room_flags & QR_PRIVATE)==0) {
661 printf("This is not a private room.\n");
665 newprompt("Name of user? ",aaa,30);
666 if (aaa[0]==0) return;
668 snprintf(bbb,sizeof bbb,"INVT %s",aaa);
671 printf("%s\n",&bbb[4]);
676 * kick a user out of a room
679 char aaa[31],bbb[256];
681 newprompt("Name of user? ",aaa,30);
682 if (aaa[0]==0) return;
684 snprintf(bbb,sizeof bbb,"KICK %s",aaa);
687 printf("%s\n",&bbb[4]);
692 * aide command: kill the current room
694 void killroom(void) {
700 printf("%s\n",&aaa[4]);
704 printf("Are you sure you want to kill this room? ");
705 if (yesno()==0) return;
709 printf("%s\n",&aaa[4]);
710 if (aaa[0]!='2') return;
711 dotgoto("_BASEROOM_",0);
714 void forget(void) { /* forget the current room */
717 printf("Are you sure you want to forget this room? ");
718 if (yesno()==0) return;
723 printf("%s\n",&cmd[4]);
727 /* now return to the lobby */
728 dotgoto("_BASEROOM_",0);
737 char new_room_name[ROOMNAMELEN];
739 char new_room_pass[10];
747 printf("%s\n",&cmd[4]);
751 newprompt("Name for new room? ",new_room_name,ROOMNAMELEN-1);
752 if (strlen(new_room_name)==0) return;
753 for (a=0; a<strlen(new_room_name); ++a)
754 if (new_room_name[a] == '|') new_room_name[a]='_';
756 new_room_floor = select_floor((int)curr_floor);
758 IFNEXPERT formout("roomaccess");
760 printf("<?>Help\n<1>Public room\n<2>Guess-name room\n");
761 printf("<3>Passworded room\n<4>Invitation-only room\n");
762 printf("Enter room type: ");
765 } while (((b<'1')||(b>'4')) && (b!='?'));
768 formout("roomaccess");
770 } while ((b<'1')||(b>'4'));
773 new_room_type = b - 1;
774 if (new_room_type==2) {
775 newprompt("Enter a room password: ",new_room_pass,9);
776 for (a=0; a<strlen(new_room_pass); ++a)
777 if (new_room_pass[a] == '|') new_room_pass[a]='_';
779 else strcpy(new_room_pass,"");
781 printf("\042%s\042, a",new_room_name);
782 if (b==1) printf(" public room.");
783 if (b==2) printf(" guess-name room.");
784 if (b==3) printf(" passworded room, password: %s",new_room_pass);
785 if (b==4) printf("n invitation-only room.");
786 printf("\nInstall it? (y/n) : ");
790 snprintf(cmd, sizeof cmd, "CRE8 1|%s|%d|%s|%d", new_room_name,
791 new_room_type, new_room_pass, new_room_floor);
795 printf("%s\n",&cmd[4]);
799 /* command succeeded... now GO to the new room! */
800 dotgoto(new_room_name,0);
805 void readinfo(void) { /* read info file for current room */
812 if (cmd[0]!='1') return;
814 fmout(screenwidth,NULL,
815 ((userflags & US_PAGINATOR) ? 1 : 0),
821 * <W>ho knows room...
823 void whoknows(void) {
828 printf("%s\n",&buf[5]);
832 sttybbs(SB_YES_INTR);
833 while (serv_gets(buf), strncmp(buf,"000",3)) {
834 if (sigcaught==0) printf("%s\n",buf);
840 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd)
844 int b,cksum,editor_exit;
847 if (strlen(editor_path)==0) {
848 printf("Do you wish to re-enter %s? ",desc);
849 if (yesno()==0) return;
852 fp = fopen(temp,"w");
855 serv_puts(check_cmd);
858 printf("%s\n",&cmd[4]);
862 if (strlen(editor_path)>0) {
866 fp = fopen(temp,"w");
867 while (serv_gets(cmd), strcmp(cmd,"000")) {
868 fprintf(fp,"%s\n",cmd);
874 cksum = file_checksum(temp);
876 if (strlen(editor_path)>0) {
881 execlp(editor_path,editor_path,temp,NULL);
884 if (editor_pid>0) do {
886 b=wait(&editor_exit);
887 } while((b!=editor_pid)&&(b>=0));
889 printf("Executed %s\n", editor_path);
893 printf("Entering %s. ",desc);
894 printf("Press return twice when finished.\n");
900 if (file_checksum(temp) == cksum) {
901 printf("*** Aborted.\n");
905 serv_puts(write_cmd);
908 printf("%s\n",&cmd[4]);
913 while (fgets(cmd,255,fp)!=NULL) {
914 cmd[strlen(cmd)-1] = 0;
925 void enterinfo(void) { /* edit info file for current room */
926 do_edit("the Info file for this room","RINF","EINF 0","EINF 1");
929 void enter_bio(void) {
931 snprintf(cmd,sizeof cmd,"RBIO %s",fullname);
932 do_edit("your Bio",cmd,"NOOP","EBIO");
938 void create_floor(void) {
940 char newfloorname[256];
942 serv_puts("CFLR xx|0");
945 printf("%s\n",&buf[4]);
949 newprompt("Name for new floor: ",newfloorname,255);
950 snprintf(buf,sizeof buf,"CFLR %s|1",newfloorname);
954 printf("Floor has been created.\n");
957 printf("%s\n",&buf[4]);
962 * edit the current floor
964 void edit_floor(void) {
967 int expire_value = 0;
969 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
971 /* Fetch the expire policy (this will silently fail on old servers,
972 * resulting in "default" policy)
974 serv_puts("GPEX floor");
977 expire_mode = extract_int(&buf[4], 0);
978 expire_value = extract_int(&buf[4], 1);
981 /* Interact with the user */
982 strprompt("Floor name",&floorlist[(int)curr_floor][0],255);
984 /* Angels and demons dancing in my head... */
986 sprintf(buf, "%d", expire_mode);
987 strprompt("Floor default essage expire policy (? for list)",
991 printf("0. Use the system default\n");
992 printf("1. Never automatically expire messages\n");
993 printf("2. Expire by message count\n");
994 printf("3. Expire by message age\n");
996 } while((buf[0]<48)||(buf[0]>51));
997 expire_mode = buf[0] - 48;
999 /* ...lunatics and monsters underneath my bed */
1000 if (expire_mode == 2) {
1001 sprintf(buf, "%d", expire_value);
1002 strprompt("Keep how many messages online?", buf, 10);
1003 expire_value = atol(buf);
1006 if (expire_mode == 3) {
1007 sprintf(buf, "%d", expire_value);
1008 strprompt("Keep messages for how many days?", buf, 10);
1009 expire_value = atol(buf);
1013 snprintf(buf, sizeof buf, "SPEX floor|%d|%d",
1014 expire_mode, expire_value);
1018 snprintf(buf,sizeof buf,"EFLR %d|%s",curr_floor,
1019 &floorlist[(int)curr_floor][0]);
1022 printf("%s\n",&buf[4]);
1030 * kill the current floor
1032 void kill_floor(void) {
1033 int floornum_to_delete,a;
1036 if (floorlist[(int)curr_floor][0]==0) load_floorlist();
1038 floornum_to_delete = (-1);
1039 printf("(Press return to abort)\n");
1040 newprompt("Delete which floor? ",buf,255);
1041 if (strlen(buf)==0) return;
1042 for (a=0; a<128; ++a)
1043 if (!strucmp(&floorlist[a][0],buf))
1044 floornum_to_delete = a;
1045 if (floornum_to_delete < 0) {
1046 printf("No such floor. Select one of:\n");
1047 for (a=0; a<128; ++a)
1048 if (floorlist[a][0]!=0)
1049 printf("%s\n",&floorlist[a][0]);
1051 } while (floornum_to_delete < 0);
1052 sprintf(buf,"KFLR %d|1",floornum_to_delete);
1055 printf("%s\n",&buf[4]);