2 * Citadel/UX message support routines
3 * see copyright.txt for copyright information
10 #include <sys/types.h>
30 #define MAXWORDBUF 256
35 char text[MAXWORDBUF];
38 void sttybbs(int cmd);
39 int struncmp(char *lstr, char *rstr, int len);
40 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
41 int haschar(char *st, int ch);
42 int checkpagin(int lp, int pagin, int height);
43 void getline(char *string, int lim);
44 void formout(char *name);
46 void newprompt(char *prompt, char *str, int len);
47 int file_checksum(char *filename);
48 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd);
51 long msg_arr[MAXMSGS];
53 extern char room_name[];
54 extern unsigned room_flags;
55 extern long highest_msg_read;
56 extern struct CtdlServInfo serv_info;
59 extern int screenwidth;
60 extern int screenheight;
61 extern long maxmsgnum;
64 extern char is_room_aide;
65 extern char fullname[];
67 extern unsigned userflags;
68 extern char sigcaught;
69 extern char editor_path[];
70 extern char printcmd[];
71 extern int rc_allow_attachments;
72 extern int rc_display_message_numbers;
73 extern int rc_force_mail_prompts;
75 extern int editor_pid;
79 void ka_sigcatch(int signum) {
82 signal(SIGALRM, ka_sigcatch);
89 * server keep-alive version of wait() (needed for external editor)
91 pid_t ka_wait(int *kstatus)
96 signal(SIGALRM, ka_sigcatch);
100 } while (errno==EINTR);
101 signal(SIGALRM,SIG_IGN);
108 * version of system() that uses ka_wait()
110 int ka_system(char *shc)
119 perror("Cannot fork");
121 return((pid_t)childpid);
125 execlp("/bin/sh","sh","-c",shc,NULL);
131 waitpid = ka_wait(&retcode);
132 } while (waitpid != childpid);
142 * add a newline to the buffer...
144 void add_newline(struct cittext *textlist)
149 while (ptr->next != NULL) ptr = ptr->next;
151 while (ptr->text[strlen(ptr->text)-1]==32)
152 ptr->text[strlen(ptr->text)-1] = 0;
153 /* strcat(ptr->text,"\n"); */
155 ptr->next = (struct cittext *)
156 malloc(sizeof(struct cittext));
159 strcpy(ptr->text,"");
164 * add a word to the buffer...
166 void add_word(struct cittext *textlist, char *wordbuf)
172 while (ptr->next != NULL) ptr = ptr->next;
174 if (3+strlen(ptr->text)+strlen(wordbuf) > screenwidth) {
175 ptr->next = (struct cittext *)
176 malloc(sizeof(struct cittext));
179 strcpy(ptr->text,"");
182 strcat(ptr->text,wordbuf);
183 strcat(ptr->text," ");
188 * begin editing of an opened file pointed to by fp, beginning at position pos.
190 void citedit(FILE *fp, long int base_pos)
192 int a,prev,finished,b,last_space;
194 struct cittext *textlist = NULL;
196 char wordbuf[MAXWORDBUF];
198 time_t last_server_msg,now;
201 * we're going to keep track of the last time we talked to
202 * the server, so we can periodically send keep-alive messages
203 * out so it doesn't time out.
205 time(&last_server_msg);
207 /* first, load the text into the buffer */
208 fseek(fp,base_pos,0);
209 textlist = (struct cittext *)malloc(sizeof(struct cittext));
210 textlist->next = NULL;
211 strcpy(textlist->text,"");
215 while (a=getc(fp), a>=0) {
217 if ((a==32)||(a==9)||(a==13)||(a==10)) {
218 add_word(textlist,wordbuf);
220 if ((prev==13)||(prev==10)) {
221 add_word(textlist,"\n");
222 add_newline(textlist);
223 add_word(textlist,"");
227 wordbuf[strlen(wordbuf)+1] = 0;
228 wordbuf[strlen(wordbuf)] = a;
230 if (strlen(wordbuf)+3 > screenwidth) {
231 add_word(textlist,wordbuf);
239 prev = (appending ? 13 : (-1));
246 if ((a==32)&&(prev==13)) {
247 add_word(textlist,"\n");
248 add_newline(textlist);
251 if (strlen(wordbuf)>0) {
252 wordbuf[strlen(wordbuf)-1] = 0;
260 if (strlen(wordbuf)==0) finished = 1;
262 for (b=0; b<strlen(wordbuf); ++b)
263 if (wordbuf[b]==32) {
265 add_word(textlist,wordbuf);
266 strcpy(wordbuf,&wordbuf[b+1]);
269 add_word(textlist,wordbuf);
275 wordbuf[strlen(wordbuf)+1] = 0;
276 wordbuf[strlen(wordbuf)] = a;
278 if ((strlen(wordbuf)+3) > screenwidth) {
280 for (b=0; b<strlen(wordbuf); ++b)
281 if (wordbuf[b]==32) last_space = b;
283 for (b=0; b<strlen(wordbuf); ++b)
284 if (wordbuf[b]==32) {
286 add_word(textlist,wordbuf);
287 strcpy(wordbuf,&wordbuf[b+1]);
290 for (b=0; b<strlen(wordbuf); ++b) {
295 printf("\n%s",wordbuf);
298 add_word(textlist,wordbuf);
305 /* this check implements the server keep-alive */
307 if ( (now - last_server_msg) > S_KEEPALIVE ) {
310 last_server_msg = now;
313 } while (finished==0);
315 /* write the buffer back to disk */
316 fseek(fp,base_pos,0);
317 for (ptr=textlist; ptr!=NULL; ptr=ptr->next) {
318 fprintf(fp,"%s",ptr->text);
323 /* and deallocate the memory we used */
324 while (textlist!=NULL) {
332 int read_message(long int num, char pagin) /* Read a message from the server */
334 /* 0 = normal read, 1 = read with pagination, 2 = header */
348 sprintf(buf,"MSG0 %ld|%d",num,(pagin==READ_HEADER ? 1 : 0));
352 printf("*** msg #%ld: %s\n",num,buf);
354 lines_printed = checkpagin(lines_printed,pagin,screenheight);
359 strcpy(m_subject,"");
362 lines_printed = checkpagin(lines_printed,pagin,screenheight);
364 if (pagin == 1) color(BRIGHT_CYAN);
367 while(serv_gets(buf), strcmp(buf,"000")) {
372 checkpagin(lines_printed,
380 strcpy(reply_to,"nobody...xxxxx");
381 while(serv_gets(buf), struncmp(buf,"text",4)) {
382 if (!struncmp(buf,"nhdr=yes",8)) nhdr=1;
383 if (!struncmp(buf,"from=",5)) {
384 strcpy(from,&buf[5]);
386 if (nhdr==1) buf[0]='_';
387 if (!struncmp(buf,"type=",5))
388 format_type=atoi(&buf[5]);
389 if ((!struncmp(buf,"msgn=",5))&&(rc_display_message_numbers)) {
393 printf("#%s",&buf[5]);
397 if (!struncmp(buf,"from=",5)) {
401 printf("%s ",&buf[5]);
403 if (!struncmp(buf,"subj=",5))
404 strcpy(m_subject,&buf[5]);
405 if ((!struncmp(buf,"hnod=",5))
406 && (strucmp(&buf[5],serv_info.serv_humannode))) {
410 printf("%s",&buf[5]);
414 if ((!struncmp(buf,"room=",5))
415 && (strucmp(&buf[5],room_name))) {
418 color(BRIGHT_MAGENTA);
419 printf("%s> ",&buf[5]);
422 if (!struncmp(buf,"node=",5)) {
423 if ( (room_flags&QR_NETWORK)
424 || ((strucmp(&buf[5],serv_info.serv_nodename)
425 &&(strucmp(&buf[5],serv_info.serv_fqdn)))))
429 color(BRIGHT_YELLOW);
430 printf("%s ",&buf[5]);
432 if ((!strucmp(&buf[5],serv_info.serv_nodename))
433 ||(!strucmp(&buf[5],serv_info.serv_fqdn)))
435 strcpy(reply_to,from);
438 sprintf(reply_to,"%s @ %s",from,&buf[5]);
442 if (!struncmp(buf,"rcpt=",5)) {
446 printf("%s ",&buf[5]);
448 if (!struncmp(buf,"time=",5)) {
450 tm=(struct tm *)localtime(&now);
451 strcpy(buf,asctime(tm)); buf[strlen(buf)-1]=0;
452 strcpy(&buf[16],&buf[19]);
453 color(BRIGHT_MAGENTA);
454 printf("%s ",&buf[4]);
467 if (pagin == 1 ) color(DIM_WHITE);
469 lines_printed = checkpagin(lines_printed,pagin,screenheight);
471 if (strlen(m_subject)>0) {
472 printf("Subject: %s\n",m_subject);
474 lines_printed = checkpagin(lines_printed,pagin,screenheight);
477 if (format_type == 0) {
478 fr=fmout(screenwidth,NULL,
479 ((pagin==1) ? 1 : 0),
480 screenheight,(-1),1);
483 while(serv_gets(buf), strcmp(buf,"000")) {
486 lines_printed = lines_printed + 1 +
487 (strlen(buf)/screenwidth);
489 checkpagin(lines_printed,pagin,screenheight);
496 lines_printed = checkpagin(lines_printed,pagin,screenheight);
498 if (pagin == 1) color(DIM_WHITE);
504 * replace string function for the built-in editor
506 void replace_string(char *filename, long int startpos)
515 int substitutions = 0;
518 printf("Enter text to be replaced:\n: ");
519 getline(srch_str,128);
520 if (strlen(srch_str)==0) return;
522 printf("Enter text to replace it with:\n: ");
523 getline(rplc_str,128);
525 fp=fopen(filename,"r+");
526 if (fp==NULL) return;
529 fseek(fp,startpos,0);
531 while (a=getc(fp), a>0) {
533 buf[strlen(buf)+1] = 0;
534 buf[strlen(buf)] = a;
535 if ( strlen(buf) >= strlen(srch_str) ) {
536 ptr=&buf[strlen(buf)-strlen(srch_str)];
537 if (!struncmp(ptr,srch_str,strlen(srch_str))) {
538 strcpy(ptr,rplc_str);
542 if (strlen(buf)>384) {
545 fwrite((char *)buf,128,1,fp);
546 strcpy(buf,&buf[128]);
552 if (strlen(buf)>0) fwrite((char *)buf,strlen(buf),1,fp);
555 printf("<R>eplace made %d substitution(s).\n\n",substitutions);
559 int make_message(char *filename, char *recipient, int anon_type, int format_type, int mode)
560 /* temporary file name */
561 /* NULL if it's not mail */
562 /* see MES_ types in header file */
573 if (mode==2) if (strlen(editor_path)==0) {
574 printf("*** No editor available, using built-in editor\n");
579 strcpy(datestr,asctime(localtime(&now)));
580 datestr[strlen(datestr)-1] = 0;
582 if (room_flags & QR_ANONONLY) {
586 printf(" %s from %s",datestr,fullname);
587 if (strlen(recipient)>0) printf(" to %s",recipient);
593 if (mode==1) printf("(Press ctrl-d when finished)\n");
595 fp=fopen(filename,"r");
597 fmout(screenwidth,fp,0,screenheight,0,0);
602 fp=fopen(filename,"w");
610 fp=fopen(filename,"r+");
616 fp=fopen(filename,"w");
618 a=inkey(); if (a==255) a=32;
624 if (a==10) putc(13,stdout);
630 e_ex_code = 1; /* start with a failed exit code */
632 cksum = file_checksum(filename);
634 chmod(filename,0600);
636 execlp(editor_path,editor_path,filename,NULL);
639 if (editor_pid>0) do {
641 b=ka_wait(&e_ex_code);
642 } while((b!=editor_pid)&&(b>=0));
649 if (file_checksum(filename) == cksum) {
650 printf("*** Aborted message.\n");
653 if (e_ex_code==0) goto MEFIN;
656 MECR1: printf("Entry cmd (? for options) -> ");
658 if (b==NEXT_KEY) b='n';
659 if (b==STOP_KEY) b='s';
660 b=(b&127); b=tolower(b);
666 if (b=='a') { printf("Abort\n"); goto MEABT; }
667 if (b=='c') { printf("Continue\n"); goto ME1; }
668 if (b=='s') { printf("Save message\n"); goto MEFIN; }
670 printf("Print formatted\n");
671 printf(" %s from %s",datestr,fullname);
672 if (strlen(recipient)>0) printf(" to %s",recipient);
674 fp=fopen(filename,"r");
676 fmout(screenwidth,fp,
677 ((userflags & US_PAGINATOR) ? 1 : 0),
685 printf("Replace string\n");
686 replace_string(filename,0L);
690 printf("Hold message\n");
697 MEABT: printf("Are you sure? ");
698 if (yesno()==0) goto ME1;
699 MEABT2: unlink(filename);
704 * transmit message text to the server
706 void transmit_message(FILE *fp)
712 while (ch=getc(fp), (ch>=0)) {
714 if (!strcmp(buf,"000")) strcpy(buf,">000");
722 if ((ch==32)&&(strlen(buf)>200)) {
724 if (!strcmp(buf,"000")) strcpy(buf,">000");
728 if (strlen(buf)>250) {
729 if (!strcmp(buf,"000")) strcpy(buf,">000");
741 * entmsg() - edit and create a message
742 * returns 0 if message was saved
744 int entmsg(int is_reply, int c)
745 /* nonzero if this was a <R>eply command */
758 sprintf(cmd,"ENT0 0||0|%d",mode);
762 if ((strncmp(cmd,"570",3)) && (strncmp(cmd,"200",3))) {
763 printf("%s\n",&cmd[4]);
767 if (!strncmp(cmd,"570",3)) need_recp = 1;
769 if ((userflags & US_EXPERT) == 0) formout("entermsg");
775 strcpy(buf,reply_to);
778 printf("Enter recipient: ");
780 if (strlen(buf)==0) return(1);
783 else strcpy(buf,"sysop");
787 if (room_flags&QR_ANONOPT) {
788 printf("Anonymous (Y/N)? ");
792 /* if it's mail, we've got to check the validity of the recipient... */
794 sprintf(cmd,"ENT0 0|%s|%d|%d",buf,b,mode);
798 printf("%s\n",&cmd[4]);
803 /* learn the number of the newest message in in the room, so we can tell
804 * upon saving whether someone else has posted too
807 serv_puts("MSGS LAST|1");
810 printf("%s\n",&cmd[5]);
813 while (serv_gets(cmd), strcmp(cmd,"000")) {
814 msg_arr[num_msgs++] = atol(cmd);
818 /* now put together the message */
819 if ( make_message(temp,buf,b,0,c) != 0 ) return(2);
821 /* and send it to the server */
822 sprintf(cmd,"ENT0 1|%s|%d|%d||",buf,b,mode);
826 printf("%s\n",&cmd[4]);
831 transmit_message(fp);
837 highmsg = msg_arr[num_msgs - 1];
839 serv_puts("MSGS NEW");
842 printf("%s\n",&cmd[5]);
845 while (serv_gets(cmd), strcmp(cmd,"000")) {
846 msg_arr[num_msgs++] = atol(cmd);
850 /* get new highest message number in room to set lrp for goto... */
851 maxmsgnum = msg_arr[num_msgs - 1];
853 /* now see if anyone else has posted in here */
855 for (a=0; a<num_msgs; ++a) if (msg_arr[a]>highmsg) ++b;
857 /* in the Mail> room, this algorithm always counts one message
858 * higher than in public rooms, so we decrement it by one */
862 "*** 1 additional message has been entered in this room by another user.\n");
864 "*** %d additional messages have been entered in this room by other users.\n",b);
869 void process_quote(void) { /* do editing on quoted file */
872 int line,qstart,qend;
874 /* Unlink the second temp file as soon as it's opened, so it'll get
875 * deleted even if the program dies
877 qfile = fopen(temp2,"r");
880 /* Display the quotable text with line numbers added */
882 fgets(buf,128,qfile);
883 while (fgets(buf,128,qfile)!=NULL) {
884 printf("%2d %s",++line,buf);
886 printf("Begin quoting at [ 1] : ");
888 qstart = (buf[0]==0) ? (1) : atoi(buf);
889 printf(" End quoting at [%d] : ",line);
891 qend = (buf[0]==0) ? (line) : atoi(buf);
894 fgets(buf,128,qfile);
895 tfile=fopen(temp,"w");
896 while(fgets(buf,128,qfile)!=NULL) {
897 if ((++line>=qstart)&&(line<=qend)) fprintf(tfile," >%s",buf);
899 fprintf(tfile," \n");
906 void readmsgs(int c, int rdir, int q) /* read contents of a room */
907 /* 0=Read all 1=Read new 2=Read old 3=Read last q */
908 /* 1=Forward (-1)=Reverse */
909 /* Number of msgs to read (if c==3) */
919 char targ[ROOMNAMELEN];
922 signal(SIGINT,SIG_IGN);
923 signal(SIGQUIT,SIG_IGN);
925 if (c<0) b=(MAXMSGS-1);
928 sprintf(prtfile,"/tmp/CPrt%ld",(long)getpid());
933 case 0: strcat(cmd,"ALL");
935 case 1: strcat(cmd,"NEW");
937 case 2: strcat(cmd,"OLD");
939 case 3: sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
945 printf("%s\n",&cmd[5]);
948 while (serv_gets(cmd), strcmp(cmd,"000")) {
949 if (num_msgs == MAXMSGS) {
950 memcpy(&msg_arr[0], &msg_arr[1],
951 (sizeof(long) * (MAXMSGS - 1)) );
954 msg_arr[num_msgs++] = atol(cmd);
960 /* this loop cycles through each message... */
961 start = ( (rdir==1) ? 0 : (num_msgs-1) );
962 for (a=start; ((a<num_msgs)&&(a>=0)); a=a+rdir) {
963 while (msg_arr[a]==0L) {
964 a=a+rdir; if ((a==MAXMSGS)||(a==(-1))) return;
967 RAGAIN: pagin=((arcflag==0)&&(quotflag==0)&&
968 (userflags & US_PAGINATOR)) ? 1 : 0;
970 /* If we're doing a quote, set the screenwidth to 72 temporarily */
972 hold_sw = screenwidth;
976 /* If printing or archiving, set the screenwidth to 80 temporarily */
978 hold_sw = screenwidth;
982 /* now read the message... */
983 e=read_message(msg_arr[a],pagin);
985 /* ...and set the screenwidth back if we have to */
986 if ((quotflag)||(arcflag)) {
987 screenwidth = hold_sw;
989 RMSGREAD: fflush(stdout);
990 highest_msg_read = msg_arr[a];
992 freopen("/dev/tty","r+",stdout);
994 enable_color = hold_color;
998 freopen("/dev/tty","r+",stdout);
1000 enable_color = hold_color;
1003 freopen(prtfile,"r",stdin);
1004 sttybbs(SB_RESTORE);
1005 ka_system(printcmd);
1006 sttybbs(SB_NO_INTR);
1012 } while((g!=f)&&(g>=0));
1013 printf("Message printed.\n");
1016 if ( ((userflags&US_NOPROMPT)||(e==2))
1017 && (((room_flags&QR_MAILBOX)==0)
1018 ||(rc_force_mail_prompts==0)) ) {
1024 color(BRIGHT_WHITE);
1025 printf("%d",num_msgs-a-1);
1029 if (is_mail==1) keyopt("<R>eply ");
1030 keyopt("<B>ack <A>gain <Q>uote <N>ext <S>top <?>Help/others -> ");
1033 e=(inkey()&127); e=tolower(e);
1034 /* return key same as <N> */ if (e==13) e='n';
1035 /* space key same as <N> */ if (e==32) e='n';
1036 /* del/move for aides only */ if ((!is_room_aide)
1037 &&((room_flags&QR_MAILBOX)==0)) {
1038 if ((e=='d')||(e=='m')) e=0;
1040 /* print only if available */ if ((e=='p')&&(strlen(printcmd)==0)) e=0;
1041 /* can't move from Mail> */ if ((e=='m')&&(is_mail==1)) e=0;
1042 /* can't reply in public rms */ if ((e=='r')&&(is_mail!=1)) e=0;
1043 /* can't file if not allowed */ if ((e=='f')&&(rc_allow_attachments==0)) e=0;
1044 } while((e!='a')&&(e!='n')&&(e!='s')
1045 &&(e!='d')&&(e!='m')&&(e!='p')
1046 &&(e!='q')&&(e!='b')&&(e!='h')
1047 &&(e!='r')&&(e!='f')&&(e!='?'));
1049 case 's': printf("Stop\r"); break;
1050 case 'a': printf("Again\r"); break;
1051 case 'd': printf("Delete\r"); break;
1052 case 'm': printf("Move\r"); break;
1053 case 'n': printf("Next\r"); break;
1054 case 'p': printf("Print\r"); break;
1055 case 'q': printf("Quote\r"); break;
1056 case 'b': printf("Back\r"); break;
1057 case 'h': printf("Header\r"); break;
1058 case 'r': printf("Reply\r"); break;
1059 case 'f': printf("File\r"); break;
1060 case '?': printf("? <help>\r"); break;
1062 if (userflags & US_DISAPPEAR)
1063 printf("\r%79s\r","");
1069 case '?': printf("Options available here:\n");
1070 printf(" ? Help (prints this message)\n");
1071 printf(" S Stop reading immediately\n");
1072 printf(" A Again (repeats last message)\n");
1073 printf(" N Next (continue with next message)\n");
1074 printf(" B Back (go back to previous message)\n");
1076 ||(room_flags&QR_MAILBOX)) {
1077 printf(" D Delete this message\n");
1078 printf(" M Move message to another room\n");
1080 if (strlen(printcmd)>0)
1081 printf(" P Print this message\n");
1082 printf(" Q Quote portions of this message for your next post\n");
1083 printf(" H Headers (display message headers only)\n");
1085 printf(" R Reply to this message\n");
1086 if (rc_allow_attachments)
1087 printf(" F (save attachments to a file)\n");
1090 case 'p': fflush(stdout);
1091 freopen(prtfile,"w",stdout);
1093 hold_color = enable_color;
1096 case 'q': fflush(stdout);
1097 freopen(temp2,"w",stdout);
1099 hold_color = enable_color;
1103 case 'a': goto RAGAIN;
1104 case 'b': a=a-(rdir*2);
1106 case 'm': newprompt("Enter target room: ",
1107 targ,ROOMNAMELEN-1);
1108 if (strlen(targ)>0) {
1109 sprintf(cmd,"MOVE %ld|%s",
1113 printf("%s\n",&cmd[4]);
1114 if (cmd[0]=='2') msg_arr[a]=0L;
1119 if (cmd[0]!='2') goto RMSGREAD;
1121 case 'f': newprompt("Which section? ", filename,
1122 ((sizeof filename) -1));
1123 snprintf(cmd, sizeof cmd,
1124 "OPNA %ld|%s", msg_arr[a], filename);
1128 extract(filename, &cmd[4], 2);
1129 download_to_local_disk(filename,
1130 extract_int(&cmd[4], 0));
1133 printf("%s\n",&cmd[4]);
1136 case 'd': printf("*** Delete this message? ");
1138 sprintf(cmd,"DELE %ld",msg_arr[a]);
1141 printf("%s\n",&cmd[4]);
1142 if (cmd[0]=='2') msg_arr[a]=0L;
1148 case 'h': read_message(msg_arr[a],READ_HEADER);
1150 case 'r': entmsg(1,(DEFAULT_ENTRY==46 ? 2 : 0));
1153 } /* end for loop */
1154 } /* end read routine */
1160 * View and edit a system message
1162 void edit_system_message(char *which_message)
1168 sprintf(desc, "system message '%s'", which_message);
1169 sprintf(read_cmd, "MESG %s", which_message);
1170 sprintf(write_cmd, "EMSG %s", which_message);
1171 do_edit(desc, read_cmd, "NOOP", write_cmd);