2 * Citadel/UX message support routines
3 * see copyright.txt for copyright information
21 #define MAXWORDBUF 256
26 char text[MAXWORDBUF];
30 struct AttachedFile *next;
35 void sttybbs(int cmd);
36 int struncmp(char *lstr, char *rstr, int len);
37 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
38 int haschar(char *st, int ch);
39 int checkpagin(int lp, int pagin, int height);
40 void getline(char *string, int lim);
41 void formout(char *name);
43 void newprompt(char *prompt, char *str, int len);
44 int file_checksum(char *filename);
45 void color(int colornum);
46 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd);
49 long msg_arr[MAXMSGS];
51 extern char room_name[];
52 extern unsigned room_flags;
53 extern long highest_msg_read;
54 extern struct CtdlServInfo serv_info;
57 extern int screenwidth;
58 extern int screenheight;
59 extern long maxmsgnum;
62 extern char is_room_aide;
63 extern char fullname[];
65 extern unsigned userflags;
66 extern char sigcaught;
67 extern char editor_path[];
68 extern char printcmd[];
69 extern int rc_allow_attachments;
70 extern int rc_display_message_numbers;
71 extern int rc_force_mail_prompts;
73 extern int editor_pid;
77 void ka_sigcatch(int signum) {
80 signal(SIGALRM, ka_sigcatch);
87 * server keep-alive version of wait() (needed for external editor)
89 pid_t ka_wait(pid_t *kstatus)
94 signal(SIGALRM, ka_sigcatch);
98 } while (errno==EINTR);
99 signal(SIGALRM,SIG_IGN);
106 * version of system() that uses ka_wait()
108 int ka_system(char *shc)
117 perror("Cannot fork");
119 return((pid_t)childpid);
123 execlp("/bin/sh","sh","-c",shc,NULL);
129 waitpid = ka_wait(&retcode);
130 } while (waitpid != childpid);
140 * add a newline to the buffer...
142 void add_newline(struct cittext *textlist)
147 while (ptr->next != NULL) ptr = ptr->next;
149 while (ptr->text[strlen(ptr->text)-1]==32)
150 ptr->text[strlen(ptr->text)-1] = 0;
151 /* strcat(ptr->text,"\n"); */
153 ptr->next = (struct cittext *)
154 malloc(sizeof(struct cittext));
157 strcpy(ptr->text,"");
162 * add a word to the buffer...
164 void add_word(struct cittext *textlist, char *wordbuf)
170 while (ptr->next != NULL) ptr = ptr->next;
172 if (3+strlen(ptr->text)+strlen(wordbuf) > screenwidth) {
173 ptr->next = (struct cittext *)
174 malloc(sizeof(struct cittext));
177 strcpy(ptr->text,"");
180 strcat(ptr->text,wordbuf);
181 strcat(ptr->text," ");
186 * begin editing of an opened file pointed to by fp, beginning at position pos.
188 void citedit(FILE *fp, long int base_pos)
190 int a,prev,finished,b,last_space;
192 struct cittext *textlist = NULL;
194 char wordbuf[MAXWORDBUF];
196 time_t last_server_msg,now;
199 * we're going to keep track of the last time we talked to
200 * the server, so we can periodically send keep-alive messages
201 * out so it doesn't time out.
203 time(&last_server_msg);
205 /* first, load the text into the buffer */
206 fseek(fp,base_pos,0);
207 textlist = (struct cittext *)malloc(sizeof(struct cittext));
208 textlist->next = NULL;
209 strcpy(textlist->text,"");
213 while (a=getc(fp), a>=0) {
215 if ((a==32)||(a==9)||(a==13)||(a==10)) {
216 add_word(textlist,wordbuf);
218 if ((prev==13)||(prev==10)) {
219 add_word(textlist,"\n");
220 add_newline(textlist);
221 add_word(textlist,"");
225 wordbuf[strlen(wordbuf)+1] = 0;
226 wordbuf[strlen(wordbuf)] = a;
228 if (strlen(wordbuf)+3 > screenwidth) {
229 add_word(textlist,wordbuf);
237 prev = (appending ? 13 : (-1));
244 if ((a==32)&&(prev==13)) {
245 add_word(textlist,"\n");
246 add_newline(textlist);
249 if (strlen(wordbuf)>0) {
250 wordbuf[strlen(wordbuf)-1] = 0;
258 if (strlen(wordbuf)==0) finished = 1;
260 for (b=0; b<strlen(wordbuf); ++b)
261 if (wordbuf[b]==32) {
263 add_word(textlist,wordbuf);
264 strcpy(wordbuf,&wordbuf[b+1]);
267 add_word(textlist,wordbuf);
273 wordbuf[strlen(wordbuf)+1] = 0;
274 wordbuf[strlen(wordbuf)] = a;
276 if ((strlen(wordbuf)+3) > screenwidth) {
278 for (b=0; b<strlen(wordbuf); ++b)
279 if (wordbuf[b]==32) last_space = b;
281 for (b=0; b<strlen(wordbuf); ++b)
282 if (wordbuf[b]==32) {
284 add_word(textlist,wordbuf);
285 strcpy(wordbuf,&wordbuf[b+1]);
288 for (b=0; b<strlen(wordbuf); ++b) {
293 printf("\n%s",wordbuf);
296 add_word(textlist,wordbuf);
303 /* this check implements the server keep-alive */
305 if ( (now - last_server_msg) > S_KEEPALIVE ) {
308 last_server_msg = now;
311 } while (finished==0);
313 /* write the buffer back to disk */
314 fseek(fp,base_pos,0);
315 for (ptr=textlist; ptr!=NULL; ptr=ptr->next) {
316 fprintf(fp,"%s",ptr->text);
321 /* and deallocate the memory we used */
322 while (textlist!=NULL) {
330 int read_message(long int num, char pagin) /* Read a message from the server */
332 /* 0 = normal read, 1 = read with pagination, 2 = header */
346 sprintf(buf,"MSG0 %ld|%d",num,(pagin==READ_HEADER ? 1 : 0));
350 printf("*** msg #%ld: %s\n",num,buf);
352 lines_printed = checkpagin(lines_printed,pagin,screenheight);
357 strcpy(m_subject,"");
360 lines_printed = checkpagin(lines_printed,pagin,screenheight);
362 if (pagin == 1) color(3);
365 while(serv_gets(buf), strcmp(buf,"000")) {
370 checkpagin(lines_printed,
378 strcpy(reply_to,"nobody...xxxxx");
379 while(serv_gets(buf), struncmp(buf,"text",4)) {
380 if (!struncmp(buf,"nhdr=yes",8)) nhdr=1;
381 if (!struncmp(buf,"from=",5)) {
382 strcpy(from,&buf[5]);
384 if (nhdr==1) buf[0]='_';
385 if (!struncmp(buf,"type=",5))
386 format_type=atoi(&buf[5]);
387 if ((!struncmp(buf,"msgn=",5))&&(rc_display_message_numbers)) {
388 printf("[#%s] ",&buf[5]);
390 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)))
397 printf("(%s) ",&buf[5]);
398 if ((!struncmp(buf,"room=",5))
399 && (strucmp(&buf[5],room_name)))
400 printf("in %s> ",&buf[5]);
402 if (!struncmp(buf,"node=",5)) {
403 if ( (room_flags&QR_NETWORK)
404 || ((strucmp(&buf[5],serv_info.serv_nodename)
405 &&(strucmp(&buf[5],serv_info.serv_fqdn)))))
407 printf("@%s ",&buf[5]);
409 if ((!strucmp(&buf[5],serv_info.serv_nodename))
410 ||(!strucmp(&buf[5],serv_info.serv_fqdn)))
412 strcpy(reply_to,from);
415 sprintf(reply_to,"%s @ %s",from,&buf[5]);
419 if (!struncmp(buf,"rcpt=",5))
420 printf("to %s ",&buf[5]);
421 if (!struncmp(buf,"time=",5)) {
423 tm=(struct tm *)localtime(&now);
424 strcpy(buf,asctime(tm)); buf[strlen(buf)-1]=0;
425 strcpy(&buf[16],&buf[19]);
426 printf("%s ",&buf[4]);
439 if (pagin == 1 ) color(2);
441 lines_printed = checkpagin(lines_printed,pagin,screenheight);
443 if (strlen(m_subject)>0) {
444 printf("Subject: %s\n",m_subject);
446 lines_printed = checkpagin(lines_printed,pagin,screenheight);
449 if (format_type == 0) {
450 fr=fmout(screenwidth,NULL,
451 ((pagin==1) ? 1 : 0),
452 screenheight,(-1),1);
455 while(serv_gets(buf), strcmp(buf,"000")) {
458 lines_printed = lines_printed + 1 +
459 (strlen(buf)/screenwidth);
461 checkpagin(lines_printed,pagin,screenheight);
468 lines_printed = checkpagin(lines_printed,pagin,screenheight);
470 if (pagin == 1) color(7);
476 * replace string function for the built-in editor
478 void replace_string(char *filename, long int startpos)
487 int substitutions = 0;
490 printf("Enter text to be replaced:\n: ");
491 getline(srch_str,128);
492 if (strlen(srch_str)==0) return;
494 printf("Enter text to replace it with:\n: ");
495 getline(rplc_str,128);
497 fp=fopen(filename,"r+");
498 if (fp==NULL) return;
501 fseek(fp,startpos,0);
503 while (a=getc(fp), a>0) {
505 buf[strlen(buf)+1] = 0;
506 buf[strlen(buf)] = a;
507 if ( strlen(buf) >= strlen(srch_str) ) {
508 ptr=&buf[strlen(buf)-strlen(srch_str)];
509 if (!struncmp(ptr,srch_str,strlen(srch_str))) {
510 strcpy(ptr,rplc_str);
514 if (strlen(buf)>384) {
517 fwrite((char *)buf,128,1,fp);
518 strcpy(buf,&buf[128]);
524 if (strlen(buf)>0) fwrite((char *)buf,strlen(buf),1,fp);
527 printf("<R>eplace made %d substitution(s).\n\n",substitutions);
531 int make_message(char *filename, char *recipient, int anon_type, int format_type, int mode, char *boundary)
532 /* temporary file name */
533 /* NULL if it's not mail */
534 /* see MES_ types in header file */
544 struct AttachedFile *AttachList = NULL;
545 struct AttachedFile *Aptr;
548 if (mode==2) if (strlen(editor_path)==0) {
549 printf("*** No editor available, using built-in editor\n");
554 strcpy(datestr,asctime(localtime(&now)));
555 datestr[strlen(datestr)-1] = 0;
557 if (room_flags & QR_ANONONLY) {
561 printf(" %s from %s",datestr,fullname);
562 if (strlen(recipient)>0) printf(" to %s",recipient);
568 if (mode==1) printf("(Press ctrl-d when finished)\n");
570 fp=fopen(filename,"r");
572 fmout(screenwidth,fp,0,screenheight,0,0);
577 fp=fopen(filename,"w");
585 fp=fopen(filename,"r+");
592 fp=fopen(filename,"w");
594 a=inkey(); if (a==255) a=32;
600 if (a==10) putc(13,stdout);
606 e_ex_code = 1; /* start with a failed exit code */
608 cksum = file_checksum(filename);
610 chmod(filename,0600);
612 execlp(editor_path,editor_path,filename,NULL);
615 if (editor_pid>0) do {
617 b=ka_wait(&e_ex_code);
618 } while((b!=editor_pid)&&(b>=0));
625 if (file_checksum(filename) == cksum) {
626 printf("*** Aborted message.\n");
629 if (e_ex_code==0) goto MEFIN;
632 MECR1: printf("Entry cmd (? for options) -> ");
634 if (b==NEXT_KEY) b='n';
635 if (b==STOP_KEY) b='s';
636 b=(b&127); b=tolower(b);
642 if (b=='a') { printf("Abort\n"); goto MEABT; }
643 if (b=='c') { printf("Continue\n"); goto ME1; }
644 if (b=='s') { printf("Save message\n"); goto MEFIN; }
646 printf("Print formatted\n");
647 printf(" %s from %s",datestr,fullname);
648 if (strlen(recipient)>0) printf(" to %s",recipient);
650 fp=fopen(filename,"r");
652 fmout(screenwidth,fp,
653 ((userflags & US_PAGINATOR) ? 1 : 0),
661 printf("Replace string\n");
662 replace_string(filename,0L);
666 printf("Hold message\n");
669 if ((b=='f')&&(rc_allow_attachments==1)) {
670 printf("attach File\n");
671 if (strlen(boundary)==0) {
672 sprintf(boundary, "Citadel-Attachment-%ld.%d",
673 (long)time(NULL), getpid() );
675 newprompt("Filename: ", buf, 68);
676 if (access(buf, R_OK)==0) {
677 Aptr = (struct AttachedFile *)
678 malloc(sizeof(struct AttachedFile));
679 strcpy(Aptr->filename, buf);
680 Aptr->next = AttachList;
684 printf("*** Cannot open %s: %s\n",
685 buf, strerror(errno));
691 MEFIN: /* Now we're done typing the message. Before returning, append any
692 * attachments the user has selected
694 if (strlen(boundary)==0) goto SKIPAT;
695 fp = fopen(filename, "a");
696 while (AttachList != NULL) {
697 sprintf(buf, "uuencode %s <%s",
698 AttachList->filename, AttachList->filename);
699 atpipe = popen(buf, "r");
700 if (atpipe != NULL) {
701 fprintf(fp,"--%s\n", boundary);
702 fprintf(fp,"Content-type: application/octet-stream;\n");
703 fprintf(fp,"%cname=%c%s%c\n",
704 9, 34, AttachList->filename, 34);
705 fprintf(fp,"Content-Transfer-Encoding: x-uudecode\n");
706 fprintf(fp,"Content-Disposition: attachment;\n");
707 fprintf(fp,"%cfilename=%c%s%c\n\n",
708 9, 34, AttachList->filename, 34);
709 while (fgets(buf, 256, atpipe)!=NULL) {
710 buf[strlen(buf)-1]=0;
711 fprintf(fp, "%s\n", buf);
716 printf("*** Cannot open %s: %s\n",
717 AttachList->filename, strerror(errno));
719 Aptr = AttachList->next;
723 fprintf(fp, "--%s--\n", boundary); /* end of attachments */
728 MEABT: printf("Are you sure? ");
729 if (yesno()==0) goto ME1;
730 MEABT2: unlink(filename);
735 * transmit message text to the server
737 void transmit_message(FILE *fp)
743 while (ch=getc(fp), (ch>=0)) {
745 if (!strcmp(buf,"000")) strcpy(buf,">000");
753 if ((ch==32)&&(strlen(buf)>200)) {
755 if (!strcmp(buf,"000")) strcpy(buf,">000");
759 if (strlen(buf)>250) {
760 if (!strcmp(buf,"000")) strcpy(buf,">000");
772 * entmsg() - edit and create a message
773 * returns 0 if message was saved
775 int entmsg(int is_reply, int c)
776 /* nonzero if this was a <R>eply command */
790 sprintf(cmd,"ENT0 0||0|%d",mode);
794 if ((strncmp(cmd,"570",3)) && (strncmp(cmd,"200",3))) {
795 printf("%s\n",&cmd[4]);
799 if (!strncmp(cmd,"570",3)) need_recp = 1;
801 if ((userflags & US_EXPERT) == 0) formout("entermsg");
807 strcpy(buf,reply_to);
810 printf("Enter recipient: ");
812 if (strlen(buf)==0) return(1);
815 else strcpy(buf,"sysop");
819 if (room_flags&QR_ANONOPT) {
820 printf("Anonymous (Y/N)? ");
824 /* if it's mail, we've got to check the validity of the recipient... */
826 sprintf(cmd,"ENT0 0|%s|%d|%d",buf,b,mode);
830 printf("%s\n",&cmd[4]);
835 /* learn the number of the newest message in in the room, so we can tell
836 * upon saving whether someone else has posted too
839 serv_puts("MSGS LAST|1");
842 printf("%s\n",&cmd[5]);
845 while (serv_gets(cmd), strcmp(cmd,"000")) {
846 msg_arr[num_msgs++] = atol(cmd);
850 /* now put together the message */
851 strcpy(boundary, "");
852 if ( make_message(temp,buf,b,0,c,boundary) != 0 ) return(2);
854 /* and send it to the server */
855 sprintf(cmd,"ENT0 1|%s|%d|%d||%s|",buf,b,mode,boundary);
859 printf("%s\n",&cmd[4]);
864 transmit_message(fp);
870 highmsg = msg_arr[num_msgs - 1];
872 serv_puts("MSGS NEW");
875 printf("%s\n",&cmd[5]);
878 while (serv_gets(cmd), strcmp(cmd,"000")) {
879 msg_arr[num_msgs++] = atol(cmd);
883 /* get new highest message number in room to set lrp for goto... */
884 maxmsgnum = msg_arr[num_msgs - 1];
886 /* now see if anyone else has posted in here */
888 for (a=0; a<num_msgs; ++a) if (msg_arr[a]>highmsg) ++b;
890 /* in the Mail> room, this algorithm always counts one message
891 * higher than in public rooms, so we decrement it by one */
895 "*** 1 additional message has been entered in this room by another user.\n");
897 "*** %d additional messages have been entered in this room by other users.\n",b);
902 void process_quote(void) { /* do editing on quoted file */
905 int line,qstart,qend;
907 /* Unlink the second temp file as soon as it's opened, so it'll get
908 * deleted even if the program dies
910 qfile = fopen(temp2,"r");
913 /* Display the quotable text with line numbers added */
915 fgets(buf,128,qfile);
916 while (fgets(buf,128,qfile)!=NULL) {
917 printf("%2d %s",++line,buf);
919 printf("Begin quoting at [ 1] : ");
921 qstart = (buf[0]==0) ? (1) : atoi(buf);
922 printf(" End quoting at [%d] : ",line);
924 qend = (buf[0]==0) ? (line) : atoi(buf);
927 fgets(buf,128,qfile);
928 tfile=fopen(temp,"w");
929 while(fgets(buf,128,qfile)!=NULL) {
930 if ((++line>=qstart)&&(line<=qend)) fprintf(tfile," >%s",buf);
932 fprintf(tfile," \n");
939 void readmsgs(int c, int rdir, int q) /* read contents of a room */
940 /* 0=Read all 1=Read new 2=Read old 3=Read last q */
941 /* 1=Forward (-1)=Reverse */
942 /* Number of msgs to read (if c==3) */
951 char targ[ROOMNAMELEN];
953 signal(SIGINT,SIG_IGN);
954 signal(SIGQUIT,SIG_IGN);
956 if (c<0) b=(MAXMSGS-1);
959 sprintf(prtfile,"/tmp/CPrt%d",getpid());
964 case 0: strcat(cmd,"ALL");
966 case 1: strcat(cmd,"NEW");
968 case 2: strcat(cmd,"OLD");
970 case 3: sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
976 printf("%s\n",&cmd[5]);
979 while (serv_gets(cmd), strcmp(cmd,"000")) {
980 msg_arr[num_msgs++] = atol(cmd);
986 /* this loop cycles through each message... */
987 start = ( (rdir==1) ? 0 : (num_msgs-1) );
988 for (a=start; ((a<num_msgs)&&(a>=0)); a=a+rdir) {
989 while (msg_arr[a]==0L) {
990 a=a+rdir; if ((a==MAXMSGS)||(a==(-1))) return;
993 RAGAIN: pagin=((arcflag==0)&&(quotflag==0)&&
994 (userflags & US_PAGINATOR)) ? 1 : 0;
996 /* if we're doing a quote, set the screenwidth to 72 temporarily */
998 hold_sw = screenwidth;
1002 /* now read the message... */
1003 e=read_message(msg_arr[a],pagin);
1005 /* ...and set the screenwidth back if we have to */
1007 screenwidth = hold_sw;
1009 RMSGREAD: fflush(stdout);
1010 highest_msg_read = msg_arr[a];
1012 freopen("/dev/tty","r+",stdout);
1017 freopen("/dev/tty","r+",stdout);
1021 freopen(prtfile,"r",stdin);
1022 sttybbs(SB_RESTORE);
1023 ka_system(printcmd);
1024 sttybbs(SB_NO_INTR);
1030 } while((g!=f)&&(g>=0));
1031 printf("Message printed.\n");
1034 if ( ((userflags&US_NOPROMPT)||(e==2))
1035 && (((room_flags&QR_MAILBOX)==0)
1036 ||(rc_force_mail_prompts==0)) ) {
1040 printf("(%d) ",num_msgs-a-1);
1041 if (is_mail==1) printf("<R>eply ");
1042 if (strlen(printcmd)>0) printf("<P>rint ");
1043 printf("<B>ack <A>gain <Q>uote <H>eader <N>ext <S>top -> ");
1046 e=(inkey()&127); e=tolower(e);
1047 /* return key same as <N> */ if (e==13) e='n';
1048 /* space key same as <N> */ if (e==32) e='n';
1049 /* del/move for aides only */ if (!is_room_aide) if ((e=='d')||(e=='m')) e=0;
1050 /* print only if available */ if ((e=='p')&&(strlen(printcmd)==0)) e=0;
1051 /* can't move from Mail> */ if ((e=='m')&&(is_mail==1)) e=0;
1052 /* can't reply in public rms */ if ((e=='r')&&(is_mail!=1)) e=0;
1053 } while((e!='a')&&(e!='n')&&(e!='s')
1054 &&(e!='d')&&(e!='m')&&(e!='p')
1055 &&(e!='q')&&(e!='b')&&(e!='h')
1058 case 's': printf("Stop\r"); break;
1059 case 'a': printf("Again\r"); break;
1060 case 'd': printf("Delete\r"); break;
1061 case 'm': printf("Move\r"); break;
1062 case 'n': printf("Next\r"); break;
1063 case 'p': printf("Print\r"); break;
1064 case 'q': printf("Quote\r"); break;
1065 case 'b': printf("Back\r"); break;
1066 case 'h': printf("Header\r"); break;
1067 case 'r': printf("Reply\r"); break;
1069 if (userflags & US_DISAPPEAR)
1070 printf("%75s\r","");
1076 case 'p': fflush(stdout);
1077 freopen(prtfile,"w",stdout);
1080 case 'q': fflush(stdout);
1081 freopen(temp2,"w",stdout);
1085 case 'a': goto RAGAIN;
1086 case 'b': a=a-(rdir*2);
1088 case 'm': newprompt("Enter target room: ",
1089 targ,ROOMNAMELEN-1);
1090 if (strlen(targ)>0) {
1091 sprintf(cmd,"MOVE %ld|%s",
1095 printf("%s\n",&cmd[4]);
1096 if (cmd[0]=='2') msg_arr[a]=0L;
1101 if (cmd[0]!='2') goto RMSGREAD;
1103 case 'd': printf("*** Delete this message? ");
1105 sprintf(cmd,"DELE %ld",msg_arr[a]);
1108 printf("%s\n",&cmd[4]);
1109 if (cmd[0]=='2') msg_arr[a]=0L;
1115 case 'h': read_message(msg_arr[a],READ_HEADER);
1117 case 'r': entmsg(1,(DEFAULT_ENTRY==46 ? 2 : 0));
1120 } /* end for loop */
1121 } /* end read routine */
1127 * View and edit a system message
1129 void edit_system_message(char *which_message)
1135 sprintf(desc, "system message '%s'", which_message);
1136 sprintf(read_cmd, "MESG %s", which_message);
1137 sprintf(write_cmd, "EMSG %s", which_message);
1138 do_edit(desc, read_cmd, "NOOP", write_cmd);