2 * Citadel/UX message support routines
3 * see copyright.txt for copyright information
25 #define MAXWORDBUF 256
30 char text[MAXWORDBUF];
33 void sttybbs(int cmd);
34 int struncmp(char *lstr, char *rstr, int len);
35 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
36 int haschar(char *st, int ch);
37 int checkpagin(int lp, int pagin, int height);
38 void getline(char *string, int lim);
39 void formout(char *name);
41 void newprompt(char *prompt, char *str, int len);
42 int file_checksum(char *filename);
43 void color(int colornum);
44 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd);
47 long msg_arr[MAXMSGS];
49 extern char room_name[];
50 extern unsigned room_flags;
51 extern long highest_msg_read;
52 extern struct CtdlServInfo serv_info;
55 extern int screenwidth;
56 extern int screenheight;
57 extern long maxmsgnum;
60 extern char is_room_aide;
61 extern char fullname[];
63 extern unsigned userflags;
64 extern char sigcaught;
65 extern char editor_path[];
66 extern char printcmd[];
67 extern int rc_allow_attachments;
68 extern int rc_display_message_numbers;
69 extern int rc_force_mail_prompts;
71 extern int editor_pid;
75 void ka_sigcatch(int signum) {
78 signal(SIGALRM, ka_sigcatch);
85 * server keep-alive version of wait() (needed for external editor)
87 pid_t ka_wait(int *kstatus)
92 signal(SIGALRM, ka_sigcatch);
96 } while (errno==EINTR);
97 signal(SIGALRM,SIG_IGN);
104 * version of system() that uses ka_wait()
106 int ka_system(char *shc)
115 perror("Cannot fork");
117 return((pid_t)childpid);
121 execlp("/bin/sh","sh","-c",shc,NULL);
127 waitpid = ka_wait(&retcode);
128 } while (waitpid != childpid);
138 * add a newline to the buffer...
140 void add_newline(struct cittext *textlist)
145 while (ptr->next != NULL) ptr = ptr->next;
147 while (ptr->text[strlen(ptr->text)-1]==32)
148 ptr->text[strlen(ptr->text)-1] = 0;
149 /* strcat(ptr->text,"\n"); */
151 ptr->next = (struct cittext *)
152 malloc(sizeof(struct cittext));
155 strcpy(ptr->text,"");
160 * add a word to the buffer...
162 void add_word(struct cittext *textlist, char *wordbuf)
168 while (ptr->next != NULL) ptr = ptr->next;
170 if (3+strlen(ptr->text)+strlen(wordbuf) > screenwidth) {
171 ptr->next = (struct cittext *)
172 malloc(sizeof(struct cittext));
175 strcpy(ptr->text,"");
178 strcat(ptr->text,wordbuf);
179 strcat(ptr->text," ");
184 * begin editing of an opened file pointed to by fp, beginning at position pos.
186 void citedit(FILE *fp, long int base_pos)
188 int a,prev,finished,b,last_space;
190 struct cittext *textlist = NULL;
192 char wordbuf[MAXWORDBUF];
194 time_t last_server_msg,now;
197 * we're going to keep track of the last time we talked to
198 * the server, so we can periodically send keep-alive messages
199 * out so it doesn't time out.
201 time(&last_server_msg);
203 /* first, load the text into the buffer */
204 fseek(fp,base_pos,0);
205 textlist = (struct cittext *)malloc(sizeof(struct cittext));
206 textlist->next = NULL;
207 strcpy(textlist->text,"");
211 while (a=getc(fp), a>=0) {
213 if ((a==32)||(a==9)||(a==13)||(a==10)) {
214 add_word(textlist,wordbuf);
216 if ((prev==13)||(prev==10)) {
217 add_word(textlist,"\n");
218 add_newline(textlist);
219 add_word(textlist,"");
223 wordbuf[strlen(wordbuf)+1] = 0;
224 wordbuf[strlen(wordbuf)] = a;
226 if (strlen(wordbuf)+3 > screenwidth) {
227 add_word(textlist,wordbuf);
235 prev = (appending ? 13 : (-1));
242 if ((a==32)&&(prev==13)) {
243 add_word(textlist,"\n");
244 add_newline(textlist);
247 if (strlen(wordbuf)>0) {
248 wordbuf[strlen(wordbuf)-1] = 0;
256 if (strlen(wordbuf)==0) finished = 1;
258 for (b=0; b<strlen(wordbuf); ++b)
259 if (wordbuf[b]==32) {
261 add_word(textlist,wordbuf);
262 strcpy(wordbuf,&wordbuf[b+1]);
265 add_word(textlist,wordbuf);
271 wordbuf[strlen(wordbuf)+1] = 0;
272 wordbuf[strlen(wordbuf)] = a;
274 if ((strlen(wordbuf)+3) > screenwidth) {
276 for (b=0; b<strlen(wordbuf); ++b)
277 if (wordbuf[b]==32) last_space = b;
279 for (b=0; b<strlen(wordbuf); ++b)
280 if (wordbuf[b]==32) {
282 add_word(textlist,wordbuf);
283 strcpy(wordbuf,&wordbuf[b+1]);
286 for (b=0; b<strlen(wordbuf); ++b) {
291 printf("\n%s",wordbuf);
294 add_word(textlist,wordbuf);
301 /* this check implements the server keep-alive */
303 if ( (now - last_server_msg) > S_KEEPALIVE ) {
306 last_server_msg = now;
309 } while (finished==0);
311 /* write the buffer back to disk */
312 fseek(fp,base_pos,0);
313 for (ptr=textlist; ptr!=NULL; ptr=ptr->next) {
314 fprintf(fp,"%s",ptr->text);
319 /* and deallocate the memory we used */
320 while (textlist!=NULL) {
328 int read_message(long int num, char pagin) /* Read a message from the server */
330 /* 0 = normal read, 1 = read with pagination, 2 = header */
344 sprintf(buf,"MSG0 %ld|%d",num,(pagin==READ_HEADER ? 1 : 0));
348 printf("*** msg #%ld: %s\n",num,buf);
350 lines_printed = checkpagin(lines_printed,pagin,screenheight);
355 strcpy(m_subject,"");
358 lines_printed = checkpagin(lines_printed,pagin,screenheight);
360 if (pagin == 1) color(3);
363 while(serv_gets(buf), strcmp(buf,"000")) {
368 checkpagin(lines_printed,
376 strcpy(reply_to,"nobody...xxxxx");
377 while(serv_gets(buf), struncmp(buf,"text",4)) {
378 if (!struncmp(buf,"nhdr=yes",8)) nhdr=1;
379 if (!struncmp(buf,"from=",5)) {
380 strcpy(from,&buf[5]);
382 if (nhdr==1) buf[0]='_';
383 if (!struncmp(buf,"type=",5))
384 format_type=atoi(&buf[5]);
385 if ((!struncmp(buf,"msgn=",5))&&(rc_display_message_numbers)) {
387 printf("[#%s] ",&buf[5]);
389 if (!struncmp(buf,"from=",5)) {
391 printf("from %s ",&buf[5]);
393 if (!struncmp(buf,"subj=",5))
394 strcpy(m_subject,&buf[5]);
395 if ((!struncmp(buf,"hnod=",5))
396 && (strucmp(&buf[5],serv_info.serv_humannode))) {
398 printf("(%s) ",&buf[5]);
400 if ((!struncmp(buf,"room=",5))
401 && (strucmp(&buf[5],room_name))) {
403 printf("in %s> ",&buf[5]);
406 if (!struncmp(buf,"node=",5)) {
407 if ( (room_flags&QR_NETWORK)
408 || ((strucmp(&buf[5],serv_info.serv_nodename)
409 &&(strucmp(&buf[5],serv_info.serv_fqdn)))))
412 printf("@%s ",&buf[5]);
414 if ((!strucmp(&buf[5],serv_info.serv_nodename))
415 ||(!strucmp(&buf[5],serv_info.serv_fqdn)))
417 strcpy(reply_to,from);
420 sprintf(reply_to,"%s @ %s",from,&buf[5]);
424 if (!struncmp(buf,"rcpt=",5)) {
426 printf("to %s ",&buf[5]);
428 if (!struncmp(buf,"time=",5)) {
430 tm=(struct tm *)localtime(&now);
431 strcpy(buf,asctime(tm)); buf[strlen(buf)-1]=0;
432 strcpy(&buf[16],&buf[19]);
434 printf("%s ",&buf[4]);
447 if (pagin == 1 ) color(2);
449 lines_printed = checkpagin(lines_printed,pagin,screenheight);
451 if (strlen(m_subject)>0) {
452 printf("Subject: %s\n",m_subject);
454 lines_printed = checkpagin(lines_printed,pagin,screenheight);
457 if (format_type == 0) {
458 fr=fmout(screenwidth,NULL,
459 ((pagin==1) ? 1 : 0),
460 screenheight,(-1),1);
463 while(serv_gets(buf), strcmp(buf,"000")) {
466 lines_printed = lines_printed + 1 +
467 (strlen(buf)/screenwidth);
469 checkpagin(lines_printed,pagin,screenheight);
476 lines_printed = checkpagin(lines_printed,pagin,screenheight);
478 if (pagin == 1) color(7);
484 * replace string function for the built-in editor
486 void replace_string(char *filename, long int startpos)
495 int substitutions = 0;
498 printf("Enter text to be replaced:\n: ");
499 getline(srch_str,128);
500 if (strlen(srch_str)==0) return;
502 printf("Enter text to replace it with:\n: ");
503 getline(rplc_str,128);
505 fp=fopen(filename,"r+");
506 if (fp==NULL) return;
509 fseek(fp,startpos,0);
511 while (a=getc(fp), a>0) {
513 buf[strlen(buf)+1] = 0;
514 buf[strlen(buf)] = a;
515 if ( strlen(buf) >= strlen(srch_str) ) {
516 ptr=&buf[strlen(buf)-strlen(srch_str)];
517 if (!struncmp(ptr,srch_str,strlen(srch_str))) {
518 strcpy(ptr,rplc_str);
522 if (strlen(buf)>384) {
525 fwrite((char *)buf,128,1,fp);
526 strcpy(buf,&buf[128]);
532 if (strlen(buf)>0) fwrite((char *)buf,strlen(buf),1,fp);
535 printf("<R>eplace made %d substitution(s).\n\n",substitutions);
539 int make_message(char *filename, char *recipient, int anon_type, int format_type, int mode)
540 /* temporary file name */
541 /* NULL if it's not mail */
542 /* see MES_ types in header file */
553 if (mode==2) if (strlen(editor_path)==0) {
554 printf("*** No editor available, using built-in editor\n");
559 strcpy(datestr,asctime(localtime(&now)));
560 datestr[strlen(datestr)-1] = 0;
562 if (room_flags & QR_ANONONLY) {
566 printf(" %s from %s",datestr,fullname);
567 if (strlen(recipient)>0) printf(" to %s",recipient);
573 if (mode==1) printf("(Press ctrl-d when finished)\n");
575 fp=fopen(filename,"r");
577 fmout(screenwidth,fp,0,screenheight,0,0);
582 fp=fopen(filename,"w");
590 fp=fopen(filename,"r+");
596 fp=fopen(filename,"w");
598 a=inkey(); if (a==255) a=32;
604 if (a==10) putc(13,stdout);
610 e_ex_code = 1; /* start with a failed exit code */
612 cksum = file_checksum(filename);
614 chmod(filename,0600);
616 execlp(editor_path,editor_path,filename,NULL);
619 if (editor_pid>0) do {
621 b=ka_wait(&e_ex_code);
622 } while((b!=editor_pid)&&(b>=0));
629 if (file_checksum(filename) == cksum) {
630 printf("*** Aborted message.\n");
633 if (e_ex_code==0) goto MEFIN;
636 MECR1: printf("Entry cmd (? for options) -> ");
638 if (b==NEXT_KEY) b='n';
639 if (b==STOP_KEY) b='s';
640 b=(b&127); b=tolower(b);
646 if (b=='a') { printf("Abort\n"); goto MEABT; }
647 if (b=='c') { printf("Continue\n"); goto ME1; }
648 if (b=='s') { printf("Save message\n"); goto MEFIN; }
650 printf("Print formatted\n");
651 printf(" %s from %s",datestr,fullname);
652 if (strlen(recipient)>0) printf(" to %s",recipient);
654 fp=fopen(filename,"r");
656 fmout(screenwidth,fp,
657 ((userflags & US_PAGINATOR) ? 1 : 0),
665 printf("Replace string\n");
666 replace_string(filename,0L);
670 printf("Hold message\n");
677 MEABT: printf("Are you sure? ");
678 if (yesno()==0) goto ME1;
679 MEABT2: unlink(filename);
684 * transmit message text to the server
686 void transmit_message(FILE *fp)
692 while (ch=getc(fp), (ch>=0)) {
694 if (!strcmp(buf,"000")) strcpy(buf,">000");
702 if ((ch==32)&&(strlen(buf)>200)) {
704 if (!strcmp(buf,"000")) strcpy(buf,">000");
708 if (strlen(buf)>250) {
709 if (!strcmp(buf,"000")) strcpy(buf,">000");
721 * entmsg() - edit and create a message
722 * returns 0 if message was saved
724 int entmsg(int is_reply, int c)
725 /* nonzero if this was a <R>eply command */
738 sprintf(cmd,"ENT0 0||0|%d",mode);
742 if ((strncmp(cmd,"570",3)) && (strncmp(cmd,"200",3))) {
743 printf("%s\n",&cmd[4]);
747 if (!strncmp(cmd,"570",3)) need_recp = 1;
749 if ((userflags & US_EXPERT) == 0) formout("entermsg");
755 strcpy(buf,reply_to);
758 printf("Enter recipient: ");
760 if (strlen(buf)==0) return(1);
763 else strcpy(buf,"sysop");
767 if (room_flags&QR_ANONOPT) {
768 printf("Anonymous (Y/N)? ");
772 /* if it's mail, we've got to check the validity of the recipient... */
774 sprintf(cmd,"ENT0 0|%s|%d|%d",buf,b,mode);
778 printf("%s\n",&cmd[4]);
783 /* learn the number of the newest message in in the room, so we can tell
784 * upon saving whether someone else has posted too
787 serv_puts("MSGS LAST|1");
790 printf("%s\n",&cmd[5]);
793 while (serv_gets(cmd), strcmp(cmd,"000")) {
794 msg_arr[num_msgs++] = atol(cmd);
798 /* now put together the message */
799 if ( make_message(temp,buf,b,0,c) != 0 ) return(2);
801 /* and send it to the server */
802 sprintf(cmd,"ENT0 1|%s|%d|%d||",buf,b,mode);
806 printf("%s\n",&cmd[4]);
811 transmit_message(fp);
817 highmsg = msg_arr[num_msgs - 1];
819 serv_puts("MSGS NEW");
822 printf("%s\n",&cmd[5]);
825 while (serv_gets(cmd), strcmp(cmd,"000")) {
826 msg_arr[num_msgs++] = atol(cmd);
830 /* get new highest message number in room to set lrp for goto... */
831 maxmsgnum = msg_arr[num_msgs - 1];
833 /* now see if anyone else has posted in here */
835 for (a=0; a<num_msgs; ++a) if (msg_arr[a]>highmsg) ++b;
837 /* in the Mail> room, this algorithm always counts one message
838 * higher than in public rooms, so we decrement it by one */
842 "*** 1 additional message has been entered in this room by another user.\n");
844 "*** %d additional messages have been entered in this room by other users.\n",b);
849 void process_quote(void) { /* do editing on quoted file */
852 int line,qstart,qend;
854 /* Unlink the second temp file as soon as it's opened, so it'll get
855 * deleted even if the program dies
857 qfile = fopen(temp2,"r");
860 /* Display the quotable text with line numbers added */
862 fgets(buf,128,qfile);
863 while (fgets(buf,128,qfile)!=NULL) {
864 printf("%2d %s",++line,buf);
866 printf("Begin quoting at [ 1] : ");
868 qstart = (buf[0]==0) ? (1) : atoi(buf);
869 printf(" End quoting at [%d] : ",line);
871 qend = (buf[0]==0) ? (line) : atoi(buf);
874 fgets(buf,128,qfile);
875 tfile=fopen(temp,"w");
876 while(fgets(buf,128,qfile)!=NULL) {
877 if ((++line>=qstart)&&(line<=qend)) fprintf(tfile," >%s",buf);
879 fprintf(tfile," \n");
886 void readmsgs(int c, int rdir, int q) /* read contents of a room */
887 /* 0=Read all 1=Read new 2=Read old 3=Read last q */
888 /* 1=Forward (-1)=Reverse */
889 /* Number of msgs to read (if c==3) */
899 char targ[ROOMNAMELEN];
902 signal(SIGINT,SIG_IGN);
903 signal(SIGQUIT,SIG_IGN);
905 if (c<0) b=(MAXMSGS-1);
908 sprintf(prtfile,"/tmp/CPrt%ld",(long)getpid());
913 case 0: strcat(cmd,"ALL");
915 case 1: strcat(cmd,"NEW");
917 case 2: strcat(cmd,"OLD");
919 case 3: sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
925 printf("%s\n",&cmd[5]);
928 while (serv_gets(cmd), strcmp(cmd,"000")) {
929 msg_arr[num_msgs++] = atol(cmd);
935 /* this loop cycles through each message... */
936 start = ( (rdir==1) ? 0 : (num_msgs-1) );
937 for (a=start; ((a<num_msgs)&&(a>=0)); a=a+rdir) {
938 while (msg_arr[a]==0L) {
939 a=a+rdir; if ((a==MAXMSGS)||(a==(-1))) return;
942 RAGAIN: pagin=((arcflag==0)&&(quotflag==0)&&
943 (userflags & US_PAGINATOR)) ? 1 : 0;
945 /* If we're doing a quote, set the screenwidth to 72 temporarily */
947 hold_sw = screenwidth;
951 /* If printing or archiving, set the screenwidth to 80 temporarily */
953 hold_sw = screenwidth;
957 /* now read the message... */
958 e=read_message(msg_arr[a],pagin);
960 /* ...and set the screenwidth back if we have to */
961 if ((quotflag)||(arcflag)) {
962 screenwidth = hold_sw;
964 RMSGREAD: fflush(stdout);
965 highest_msg_read = msg_arr[a];
967 freopen("/dev/tty","r+",stdout);
969 enable_color = hold_color;
973 freopen("/dev/tty","r+",stdout);
975 enable_color = hold_color;
978 freopen(prtfile,"r",stdin);
987 } while((g!=f)&&(g>=0));
988 printf("Message printed.\n");
991 if ( ((userflags&US_NOPROMPT)||(e==2))
992 && (((room_flags&QR_MAILBOX)==0)
993 ||(rc_force_mail_prompts==0)) ) {
997 printf("(%d) ",num_msgs-a-1);
998 if (is_mail==1) printf("<R>eply ");
999 if (strlen(printcmd)>0) printf("<P>rint ");
1000 if (rc_allow_attachments) printf("<F>ile ");
1001 printf("<B>ack <A>gain <Q>uote <H>eader <N>ext <S>top -> ");
1004 e=(inkey()&127); e=tolower(e);
1005 /* return key same as <N> */ if (e==13) e='n';
1006 /* space key same as <N> */ if (e==32) e='n';
1007 /* del/move for aides only */ if ((!is_room_aide)
1008 &&((room_flags&QR_MAILBOX)==0)) {
1009 if ((e=='d')||(e=='m')) e=0;
1011 /* print only if available */ if ((e=='p')&&(strlen(printcmd)==0)) e=0;
1012 /* can't move from Mail> */ if ((e=='m')&&(is_mail==1)) e=0;
1013 /* can't reply in public rms */ if ((e=='r')&&(is_mail!=1)) e=0;
1014 /* can't file if not allowed */ if ((e=='f')&&(rc_allow_attachments==0)) e=0;
1015 } while((e!='a')&&(e!='n')&&(e!='s')
1016 &&(e!='d')&&(e!='m')&&(e!='p')
1017 &&(e!='q')&&(e!='b')&&(e!='h')
1018 &&(e!='r')&&(e!='f'));
1020 case 's': printf("Stop\r"); break;
1021 case 'a': printf("Again\r"); break;
1022 case 'd': printf("Delete\r"); break;
1023 case 'm': printf("Move\r"); break;
1024 case 'n': printf("Next\r"); break;
1025 case 'p': printf("Print\r"); break;
1026 case 'q': printf("Quote\r"); break;
1027 case 'b': printf("Back\r"); break;
1028 case 'h': printf("Header\r"); break;
1029 case 'r': printf("Reply\r"); break;
1030 case 'f': printf("File attachment\r");
1033 if (userflags & US_DISAPPEAR)
1034 printf("%75s\r","");
1040 case 'p': fflush(stdout);
1041 freopen(prtfile,"w",stdout);
1043 hold_color = enable_color;
1046 case 'q': fflush(stdout);
1047 freopen(temp2,"w",stdout);
1049 hold_color = enable_color;
1053 case 'a': goto RAGAIN;
1054 case 'b': a=a-(rdir*2);
1056 case 'm': newprompt("Enter target room: ",
1057 targ,ROOMNAMELEN-1);
1058 if (strlen(targ)>0) {
1059 sprintf(cmd,"MOVE %ld|%s",
1063 printf("%s\n",&cmd[4]);
1064 if (cmd[0]=='2') msg_arr[a]=0L;
1069 if (cmd[0]!='2') goto RMSGREAD;
1071 case 'f': newprompt("Which section? ", filename,
1072 ((sizeof filename) -1));
1073 snprintf(cmd, sizeof cmd,
1074 "OPNA %ld|%s", msg_arr[a], filename);
1078 extract(filename, &cmd[4], 2);
1079 download_to_local_disk(filename,
1080 extract_int(&cmd[4], 0));
1083 printf("%s\n",&cmd[4]);
1086 case 'd': printf("*** Delete this message? ");
1088 sprintf(cmd,"DELE %ld",msg_arr[a]);
1091 printf("%s\n",&cmd[4]);
1092 if (cmd[0]=='2') msg_arr[a]=0L;
1098 case 'h': read_message(msg_arr[a],READ_HEADER);
1100 case 'r': entmsg(1,(DEFAULT_ENTRY==46 ? 2 : 0));
1103 } /* end for loop */
1104 } /* end read routine */
1110 * View and edit a system message
1112 void edit_system_message(char *which_message)
1118 sprintf(desc, "system message '%s'", which_message);
1119 sprintf(read_cmd, "MESG %s", which_message);
1120 sprintf(write_cmd, "EMSG %s", which_message);
1121 do_edit(desc, read_cmd, "NOOP", write_cmd);