2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * Mail -- a mail program
43 * Collect input from standard input, handling
52 /* TODO longjmp() globbering as in cmd1.c and cmd3.c (see there)
53 * TODO Problem: Popen doesn't encapsulate all cases of open failures,
54 * TODO may leave child running if fdopen() fails! */
57 * Read a message from standard output and return a read file to it
62 * The following hokiness with global variables is so that on
63 * receipt of an interrupt signal, the partial message can be salted
64 * away on dead.letter.
67 static sighandler_type saveint
; /* Previous SIGINT value */
68 static sighandler_type savehup
; /* Previous SIGHUP value */
69 static sighandler_type savetstp
; /* Previous SIGTSTP value */
70 static sighandler_type savettou
; /* Previous SIGTTOU value */
71 static sighandler_type savettin
; /* Previous SIGTTIN value */
72 static FILE *collf
; /* File for saving away */
73 static int hadintr
; /* Have seen one SIGINT so far */
75 static sigjmp_buf colljmp
; /* To get back to work */
76 static int colljmp_p
; /* whether to long jump */
77 static sigjmp_buf collabort
; /* To end collection with error */
79 static sigjmp_buf pipejmp
; /* On broken pipe */
81 static void onpipe(int signo
);
82 static void insertcommand(FILE *fp
, char *cmd
);
83 static void print_collf(FILE *collf
, struct header
*hp
);
84 static int include_file(FILE *fbuf
, char *name
, int *linecount
,
85 int *charcount
, int echo
);
86 static struct attachment
*read_attachment_data(struct attachment
*ap
,
88 static struct attachment
*append_attachments(struct attachment
*attach
,
90 static int exwrite(char *name
, FILE *fp
, int f
);
91 static enum okay
makeheader(FILE *fp
, struct header
*hp
);
92 static void mesedit(int c
, struct header
*hp
);
93 static void mespipe(char *cmd
);
94 static int forward(char *ms
, FILE *fp
, int f
);
95 static void collstop(int s
);
96 static void collint(int s
);
97 static void collhup(int s
);
98 static int putesc(const char *s
, FILE *stream
);
105 siglongjmp(pipejmp
, 1);
109 * Execute cmd and insert its standard output into fp.
112 insertcommand(FILE *fp
, char *cmd
)
119 if (sigsetjmp(pipejmp
, 1))
123 if ((obuf
= Popen(cmd
, "r", cp
, 0)) == NULL
) {
127 safe_signal(SIGPIPE
, onpipe
);
128 while ((c
= getc(obuf
)) != EOF
)
131 safe_signal(SIGPIPE
, SIG_IGN
);
133 safe_signal(SIGPIPE
, dflpipe
);
140 print_collf(FILE *collf
, struct header
*hp
)
143 FILE *volatile obuf
= stdout
;
144 struct attachment
*ap
;
147 size_t linecnt
, maxlines
, linesize
= 0, linelen
, count
, count2
;
153 count
= count2
= fsize(collf
);
154 if (is_a_tty
[0] && is_a_tty
[1] && (cp
= value("crt")) != NULL
) {
156 fgetline(&lbuf
, &linesize
, &count2
, NULL
, collf
, 0);
159 maxlines
= (*cp
== '\0' ? screensize() : atoi(cp
));
171 maxlines
-= myaddrs(hp
) != NULL
|| hp
->h_from
!= NULL
;
172 maxlines
-= value("ORGANIZATION") != NULL
||
173 hp
->h_organization
!= NULL
;
174 maxlines
-= value("replyto") != NULL
|| hp
->h_replyto
!= NULL
;
175 maxlines
-= value("sender") != NULL
|| hp
->h_sender
!= NULL
;
176 if ((long)maxlines
< 0 || linecnt
> maxlines
) {
178 if (sigsetjmp(pipejmp
, 1))
180 obuf
= Popen(cp
, "w", NULL
, 1);
185 safe_signal(SIGPIPE
, onpipe
);
188 fprintf(obuf
, catgets(catd
, CATSET
, 62,
189 "-------\nMessage contains:\n"));
190 gf
= GIDENT
|GTO
|GSUBJECT
|GCC
|GBCC
|GNL
|GFILES
;
191 if (value("fullnames"))
193 puthead(hp
, obuf
, gf
, SEND_TODISP
, CONV_NONE
, NULL
, NULL
);
194 while (fgetline(&lbuf
, &linesize
, &count
, &linelen
, collf
, 1))
195 prout(lbuf
, linelen
, obuf
);
196 if (hp
->h_attach
!= NULL
) {
197 fputs(catgets(catd
, CATSET
, 63, "Attachments:"), obuf
);
198 for (ap
= hp
->h_attach
; ap
!= NULL
; ap
= ap
->a_flink
) {
200 fprintf(obuf
, " message %u", ap
->a_msgno
);
202 fprintf(obuf
, " %s", ap
->a_name
);
209 if (obuf
!= stdout
) {
210 safe_signal(SIGPIPE
, SIG_IGN
);
212 safe_signal(SIGPIPE
, dflpipe
);
219 include_file(FILE *fbuf
, char *name
, int *linecount
, int *charcount
, int echo
)
221 char *interactive
, *linebuf
= NULL
;
222 size_t linesize
= 0, linelen
, count
;
225 fbuf
= Fopen(name
, "r");
230 interactive
= value("interactive");
236 while (fgetline(&linebuf
, &linesize
, &count
, &linelen
, fbuf
, 0)
238 if (fwrite(linebuf
, sizeof *linebuf
, linelen
, collf
)
243 if (interactive
!= NULL
&& echo
)
244 fwrite(linebuf
, sizeof *linebuf
, linelen
, stdout
);
246 (*charcount
) += linelen
;
257 * Ask the user to edit file names and other data for the given
258 * attachment. NULL is returned if no file name is given.
260 static struct attachment
*
261 read_attachment_data(struct attachment
*ap
, unsigned number
)
263 char prefix
[80], *cp
;
266 ap
= csalloc(1, sizeof *ap
);
268 printf("#%u\tmessage %u\n", number
, ap
->a_msgno
);
271 snprintf(prefix
, sizeof prefix
, catgets(catd
, CATSET
, 50,
272 "#%u\tfilename: "), number
);
275 if ((ap
->a_name
= readtty(prefix
, ap
->a_name
)) == NULL
)
277 if ((exf
= expand(ap
->a_name
)) != NULL
)
279 if (access(ap
->a_name
, R_OK
) == 0)
283 if ((ap
->a_name
&& (value("attachment-ask-charset"))) ||
284 ((cp
= value("sendcharsets")) != NULL
&&
285 strchr(cp
, ',') != NULL
)) {
286 snprintf(prefix
, sizeof prefix
, "#%u\tcharset: ", number
);
287 ap
->a_charset
= readtty(prefix
, ap
->a_charset
);
290 * The "attachment-ask-content-*" variables are left undocumented
291 * since they are for RFC connoisseurs only.
293 if (ap
->a_name
&& value("attachment-ask-content-type")) {
294 if (ap
->a_content_type
== NULL
)
295 ap
->a_content_type
= mime_filecontent(ap
->a_name
);
296 snprintf(prefix
, sizeof prefix
, "#%u\tContent-Type: ", number
);
297 ap
->a_content_type
= readtty(prefix
, ap
->a_content_type
);
299 if (ap
->a_name
&& value("attachment-ask-content-disposition")) {
300 snprintf(prefix
, sizeof prefix
,
301 "#%u\tContent-Disposition: ", number
);
302 ap
->a_content_disposition
= readtty(prefix
,
303 ap
->a_content_disposition
);
305 if (ap
->a_name
&& value("attachment-ask-content-id")) {
306 snprintf(prefix
, sizeof prefix
, "#%u\tContent-ID: ", number
);
307 ap
->a_content_id
= readtty(prefix
, ap
->a_content_id
);
309 if (ap
->a_name
&& value("attachment-ask-content-description")) {
310 snprintf(prefix
, sizeof prefix
,
311 "#%u\tContent-Description: ", number
);
312 ap
->a_content_description
= readtty(prefix
,
313 ap
->a_content_description
);
315 return ap
->a_name
? ap
: NULL
;
319 * Interactively edit the attachment list.
322 edit_attachments(struct attachment
*attach
)
324 struct attachment
*ap
, *nap
;
327 for (ap
= attach
; ap
; ap
= ap
->a_flink
) {
328 if ((nap
= read_attachment_data(ap
, attno
)) == NULL
) {
330 ap
->a_blink
->a_flink
= ap
->a_flink
;
332 attach
= ap
->a_flink
;
334 ap
->a_flink
->a_blink
= ap
->a_blink
;
340 while ((nap
= read_attachment_data(NULL
, attno
)) != NULL
) {
341 for (ap
= attach
; ap
&& ap
->a_flink
; ap
= ap
->a_flink
);
354 * Put the given file to the end of the attachment list.
357 add_attachment(struct attachment
*attach
, char *file
, int expand_file
)
359 struct attachment
*ap
, *nap
;
366 file
= savestr(file
);
367 if (access(file
, R_OK
) != 0)
370 nap
= csalloc(1, sizeof *nap
);
372 if (attach
!= NULL
) {
373 for (ap
= attach
; ap
->a_flink
!= NULL
; ap
= ap
->a_flink
);
384 * Append the whitespace-separated file names to the end of
385 * the attachment list.
387 static struct attachment
*
388 append_attachments(struct attachment
*attach
, char *names
)
392 struct attachment
*ap
;
395 while (*cp
!= '\0' && blankchar(*cp
& 0377))
397 while (*cp
!= '\0') {
399 while (*cp
!= '\0' && !blankchar(*cp
& 0377))
403 if (*names
!= '\0') {
404 if ((ap
= add_attachment(attach
, names
, 1)) == NULL
)
411 while (*cp
!= '\0' && blankchar(*cp
& 0377))
418 collect(struct header
*hp
, int printheaders
, struct message
*mp
,
419 char *quotefile
, int doprefix
, int volatile tflag
)
426 struct ignoretab
*quoteig
;
427 int lc
, cc
, eofcount
, val
, c
, t
;
428 int volatile escape
, getfields
;
429 char *linebuf
= NULL
, *cp
, *quote
= NULL
, *tempMail
= NULL
;
432 enum sendaction action
;
434 sighandler_type savedtop
;
437 if (value("interactive") != NULL
)
442 * Start catching signals from here, but we're still die on interrupts
443 * until we're in the main loop.
446 sigaddset(&nset
, SIGINT
);
447 sigaddset(&nset
, SIGHUP
);
448 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
449 handlerpush(collint
);
450 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
451 safe_signal(SIGINT
, collint
);
452 if ((savehup
= safe_signal(SIGHUP
, SIG_IGN
)) != SIG_IGN
)
453 safe_signal(SIGHUP
, collhup
);
454 savetstp
= safe_signal(SIGTSTP
, collstop
);
455 savettou
= safe_signal(SIGTTOU
, collstop
);
456 savettin
= safe_signal(SIGTTIN
, collstop
);
457 if (sigsetjmp(collabort
, 1))
459 if (sigsetjmp(colljmp
, 1))
461 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
464 if ((collf
= Ftemp(&tempMail
, "Rs", "w+", 0600, 1)) == NULL
) {
465 perror(tr(51, "temporary mail file"));
471 if ((cp
= value("NAIL_HEAD")) != NULL
&& putesc(cp
, collf
) < 0)
475 * If we are going to prompt for a subject,
476 * refrain from printing a newline after
477 * the headers (since some people mind).
481 t
= GTO
|GSUBJECT
|GCC
|GNL
;
482 if (value("fullnames"))
484 if (hp
->h_subject
== NULL
&& (val
& val_INTERACT
) &&
485 (value("ask") != NULL
|| value("asksub") != NULL
))
486 t
&= ~GNL
, getfields
|= GSUBJECT
;
487 if (hp
->h_to
== NULL
&& (val
& val_INTERACT
))
488 t
&= ~GNL
, getfields
|= GTO
;
489 if (value("bsdcompat") == NULL
&& value("askatend") == NULL
&&
490 (val
& val_INTERACT
)) {
491 if (hp
->h_bcc
== NULL
&& value("askbcc"))
492 t
&= ~GNL
, getfields
|= GBCC
;
493 if (hp
->h_cc
== NULL
&& value("askcc"))
494 t
&= ~GNL
, getfields
|= GCC
;
497 (void)puthead(hp
, stdout
, t
, SEND_TODISP
, CONV_NONE
,
499 (void)fflush(stdout
);
504 * Quote an original message
506 if (mp
!= NULL
&& (doprefix
|| (quote
= value("quote")) != NULL
)) {
511 if ((cp
= value("fwdheading")) == NULL
)
512 cp
= "-------- Original Message --------";
513 if (*cp
&& fprintf(collf
, "%s\n", cp
) < 0)
515 } else if (strcmp(quote
, "noheading") == 0) {
517 } else if (strcmp(quote
, "headers") == 0) {
519 } else if (strcmp(quote
, "allheaders") == 0) {
521 action
= SEND_QUOTE_ALL
;
523 cp
= hfield1("from", mp
);
524 if (cp
!= NULL
&& (count
= (long)strlen(cp
)) > 0) {
525 if (mime_write(cp
, count
,
526 collf
, CONV_FROMHDR
, TD_NONE
,
530 if (fprintf(collf
, tr(52, " wrote:\n\n")) < 0)
536 cp
= value("indentprefix");
537 if (cp
!= NULL
&& *cp
== '\0')
539 if (send(mp
, collf
, quoteig
, (doprefix
? NULL
: cp
), action
,
544 /* Print what we have sofar also on the terminal */
546 while ((c
= getc(collf
)) != EOF
)
547 (void)putc(c
, stdout
);
548 if (fseek(collf
, 0, SEEK_END
))
551 escape
= ((cp
= value("escape")) != NULL
) ? *cp
: ESCAPE
;
555 if (! sigsetjmp(colljmp
, 1)) {
557 grabh(hp
, getfields
, 1);
558 if (quotefile
!= NULL
) {
559 if (include_file(NULL
, quotefile
, &lc
, &cc
, 1) != 0)
564 * Come here for printing the after-signal message.
565 * Duplicate messages won't be printed because
566 * the write is aborted if we get a SIGTTOU.
570 (void)fprintf(stderr
, tr(53,
571 "\n(Interrupt -- one more to kill letter)\n"));
573 printf(tr(54, "(continue)\n"));
574 (void)fflush(stdout
);
579 * No tilde escapes, interrupts not expected. Simply copy STDIN
581 if ((val
& val_INTERACT
) == 0 && tildeflag
<= 0 && ! tflag
) {
582 linebuf
= srealloc(linebuf
, linesize
= LINESIZE
);
583 while ((count
= fread(linebuf
, sizeof *linebuf
,
584 linesize
, stdin
)) > 0) {
585 if ((size_t)count
!= fwrite(linebuf
, sizeof *linebuf
,
595 * The interactive collect loop
599 count
= readline(stdin
, &linebuf
, &linesize
);
602 if ((val
& val_INTERACT
) &&
603 value("ignoreeof") != NULL
&& ++eofcount
< 25) {
605 "Use \".\" to terminate letter\n"));
610 if (tflag
&& count
== 0) {
612 if (makeheader(collf
, hp
) != OKAY
)
620 if (linebuf
[0] == '.' && linebuf
[1] == '\0' &&
621 (val
& val_INTERACT
) &&
622 (value("dot") != NULL
||
623 value("ignoreeof") != NULL
))
625 if (linebuf
[0] != escape
||
626 ((val
& val_INTERACT
) == 0 && tildeflag
== 0) ||
628 if (putline(collf
, linebuf
, count
) < 0)
637 * On double escape, just send the single one.
638 * Otherwise, it's an error.
641 if (putline(collf
, &linebuf
[1], count
- 1) < 0)
646 printf(tr(56, "Unknown tilde escape.\n"));
655 /* Shell escape, send the balance of line to sh -c */
660 /* Escape to command mode, but be nice! */
662 execute(&linebuf
[2], 1, count
- 2);
665 /* Simulate end of file on input */
668 /* Same as 'q', but no dead.letter saving */
675 * Force a quit of sending mail.
676 * Act like an interrupt happened.
683 /* Grab a bunch of headers */
685 grabh(hp
, GTO
|GSUBJECT
|GCC
|GBCC
,
686 value("bsdcompat") != NULL
&&
687 value("bsdorder") != NULL
);
688 while (hp
->h_to
== NULL
);
691 /* Grab extra headers */
693 grabh(hp
, GEXTRA
, 0);
694 while (check_from_and_sender(hp
->h_from
, hp
->h_sender
));
697 /* Add to the To list */
698 while ((hp
->h_to
= cat(hp
->h_to
, checkaddrs(
699 lextract(&linebuf
[2], GTO
|GFULL
))))
704 /* Set the Subject list */
706 while (whitechar(*cp
))
708 hp
->h_subject
= savestr(cp
);
711 /* Edit the attachment list */
712 if (linebuf
[2] != '\0')
713 hp
->h_attach
= append_attachments(hp
->h_attach
,
716 hp
->h_attach
= edit_attachments(hp
->h_attach
);
719 /* Add to the CC list */
720 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(
721 lextract(&linebuf
[2], GCC
|GFULL
)));
724 /* Add stuff to blind carbon copies list */
725 hp
->h_bcc
= cat(hp
->h_bcc
, checkaddrs(
726 lextract(&linebuf
[2], GBCC
|GFULL
)));
729 strncpy(linebuf
+ 2, getdeadletter(), linesize
- 2);
730 linebuf
[linesize
-1]='\0';
736 * Search for the file name,
737 * then open it and copy the contents to collf.
740 while (whitechar(*cp
))
743 printf(tr(57, "Interpolate what file?\n"));
747 insertcommand(collf
, cp
+ 1);
754 printf(tr(58, "%s: Directory\n"), cp
);
757 if ((fbuf
= Fopen(cp
, "r")) == NULL
) {
761 printf(tr(59, "\"%s\" "), cp
);
763 if (include_file(fbuf
, cp
, &lc
, &cc
, 0) != 0)
765 printf(tr(60, "%d/%d\n"), lc
, cc
);
768 /* Insert an environment variable into the file */
770 while (whitechar(*cp
))
772 if ((cp
= value(cp
)) == NULL
|| *cp
== '\0')
774 if (putesc(cp
, collf
) < 0)
776 if ((val
& val_INTERACT
) && putesc(cp
, stdout
) < 0)
781 /* Insert the contents of a signature variable */
782 if ((cp
= value(c
== 'a' ? "sign" : "Sign")) != NULL
&&
784 if (putesc(cp
, collf
) < 0)
786 if ((val
& val_INTERACT
) &&
787 putesc(cp
, stdout
) < 0)
792 /* Write the message on a file */
794 while (blankchar(*cp
))
796 if (*cp
== '\0' || (cp
= expand(cp
)) == NULL
) {
797 fprintf(stderr
, tr(61, "Write what file!?\n"));
801 if (exwrite(cp
, collf
, 1) < 0)
809 * Interpolate the named messages, if we
810 * are in receiving mail mode. Does the
811 * standard list processing garbage.
812 * If ~f is given, we don't shift over.
814 if (forward(linebuf
+ 2, collf
, c
) < 0)
819 * Print out the current state of the
820 * message without altering anything.
822 print_collf(collf
, hp
);
826 * Pipe message through command.
827 * Collect output as new message.
830 mespipe(&linebuf
[2]);
835 * Edit the current message.
836 * 'e' means to use EDITOR
837 * 'v' means to use VISUAL
840 mesedit(c
, value("editheaders") ? hp
: NULL
);
844 * Last the lengthy help string.
845 * (Very ugly, but take care for compiler supported
849 "-------------------- ~ ESCAPES ----------------------------\n"
850 "~~ Quote a single tilde\n"
851 "~@ [file ...] Edit attachment list\n"
852 "~b users Add users to \"blind\" cc list\n"
853 "~c users Add users to cc list\n"
854 "~d Read in dead.letter\n"
855 "~e Edit the message buffer\n"
856 "~f messages Read in messages without indenting lines\n"
857 "~F messages Same as ~f, but keep all header lines\n"
858 "~h Prompt for to list, subject, cc, and \"blind\" cc list\n");
860 "~r file Read a file into the message buffer\n"
861 "~p Print the message buffer\n"
862 "~q Abort message composition and save text to dead.letter\n"
863 "~m messages Read in messages with each line indented\n"
864 "~M messages Same as ~m, but keep all header lines\n"
865 "~s subject Set subject\n"
866 "~t users Add users to to list\n"
867 "~v Invoke display editor on message\n"
868 "~w file Write message onto file\n"
869 "~x Abort message composition and discard text written so far\n");
871 "~!command Invoke the shell\n"
872 "~:command Execute a regular command\n"
873 "-----------------------------------------------------------\n");
880 if ((cp
= value("NAIL_TAIL")) != NULL
) {
881 if (putesc(cp
, collf
) < 0)
883 if ((val
& val_INTERACT
) && putesc(cp
, stdout
) < 0)
891 sigaddset(&nset
, SIGINT
);
892 sigaddset(&nset
, SIGHUP
);
893 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
894 safe_signal(SIGINT
, saveint
);
895 safe_signal(SIGHUP
, savehup
);
896 safe_signal(SIGTSTP
, savetstp
);
897 safe_signal(SIGTTOU
, savettou
);
898 safe_signal(SIGTTIN
, savettin
);
899 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
902 if (tempMail
!= NULL
) {
914 * Write a file, ex-like if f set.
917 exwrite(char *name
, FILE *fp
, int f
)
925 printf("\"%s\" ", name
);
928 if ((of
= Fopen(name
, "a")) == NULL
) {
934 while ((c
= getc(fp
)) != EOF
) {
946 printf(catgets(catd
, CATSET
, 65, "%d/%ld\n"), lc
, cc
);
952 makeheader(FILE *fp
, struct header
*hp
)
958 if ((nf
= Ftemp(&tempEdit
, "Re", "w+", 0600, 1)) == NULL
) {
959 perror(catgets(catd
, CATSET
, 66, "temporary mail edit file"));
967 extract_header(fp
, hp
);
968 while ((c
= getc(fp
)) != EOF
)
974 if (check_from_and_sender(hp
->h_from
, hp
->h_sender
))
980 * Edit the message being collected on fp.
981 * On return, make the edit file the new temp file.
984 mesedit(int c
, struct header
*hp
)
986 sighandler_type sigint
= safe_signal(SIGINT
, SIG_IGN
);
987 FILE *nf
= run_editor(collf
, (off_t
)-1, c
, 0, hp
, NULL
, SEND_MBOX
,
995 fseek(nf
, 0L, SEEK_END
);
1000 safe_signal(SIGINT
, sigint
);
1004 * Pipe the message through the command.
1005 * Old message is on stdin of command;
1006 * New message collected from stdout.
1007 * Sh -c must return 0 to accept the new message.
1013 sighandler_type sigint
= safe_signal(SIGINT
, SIG_IGN
);
1017 if ((nf
= Ftemp(&tempEdit
, "Re", "w+", 0600, 1)) == NULL
) {
1018 perror(catgets(catd
, CATSET
, 66, "temporary mail edit file"));
1025 * stdin = current message.
1026 * stdout = new message.
1028 if ((shell
= value("SHELL")) == NULL
)
1030 if (run_command(shell
,
1031 0, fileno(collf
), fileno(nf
), "-c", cmd
, NULL
) < 0) {
1035 if (fsize(nf
) == 0) {
1036 fprintf(stderr
, catgets(catd
, CATSET
, 67,
1037 "No bytes from \"%s\" !?\n"), cmd
);
1044 fseek(nf
, 0L, SEEK_END
);
1048 safe_signal(SIGINT
, sigint
);
1052 * Interpolate the named messages into the current
1053 * message, preceding each line with a tab.
1054 * Return a count of the number of characters now in
1055 * the message, or -1 if an error is encountered writing
1056 * the message temporary. The flag argument is 'm' if we
1057 * should shift over and 'f' if not.
1060 forward(char *ms
, FILE *fp
, int f
)
1063 struct ignoretab
*ig
;
1065 enum sendaction action
;
1068 msgvec
= (int *)salloc((msgCount
+1) * sizeof *msgvec
);
1071 if (getmsglist(ms
, msgvec
, 0) < 0)
1074 *msgvec
= first(0, MMNORM
);
1076 printf(catgets(catd
, CATSET
, 68,
1077 "No appropriate messages\n"));
1082 if (f
== 'f' || f
== 'F')
1084 else if ((tabst
= value("indentprefix")) == NULL
)
1086 ig
= upperchar(f
) ? (struct ignoretab
*)NULL
: ignore
;
1087 action
= upperchar(f
) ? SEND_QUOTE_ALL
: SEND_QUOTE
;
1088 printf(catgets(catd
, CATSET
, 69, "Interpolating:"));
1089 for (; *msgvec
!= 0; msgvec
++) {
1090 struct message
*mp
= message
+ *msgvec
- 1;
1093 printf(" %d", *msgvec
);
1094 if (send(mp
, fp
, ig
, tabst
, action
, NULL
) < 0) {
1095 perror(catgets(catd
, CATSET
, 70,
1096 "temporary mail file"));
1105 * Print (continue) when continued after ^Z.
1111 sighandler_type old_action
= safe_signal(s
, SIG_DFL
);
1115 sigaddset(&nset
, s
);
1116 sigprocmask(SIG_UNBLOCK
, &nset
, (sigset_t
*)NULL
);
1118 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
1119 safe_signal(s
, old_action
);
1123 siglongjmp(colljmp
, 1);
1128 * On interrupt, come here to save the partial message in ~/dead.letter.
1129 * Then jump out of the collection loop.
1136 * the control flow is subtle, because we can be called from ~q.
1139 if (value("ignore") != NULL
) {
1146 siglongjmp(colljmp
, 1);
1150 if (value("save") != NULL
&& s
!= 0)
1151 savedeadletter(collf
);
1152 siglongjmp(collabort
, 1);
1161 savedeadletter(collf
);
1163 * Let's pretend nobody else wants to clean up,
1164 * a true statement at this time.
1170 savedeadletter(FILE *fp
)
1173 int c
, lines
= 0, bytes
= 0;
1178 cp
= getdeadletter();
1180 dbuf
= Fopen(cp
, "a");
1184 printf("\"%s\" ", cp
);
1185 while ((c
= getc(fp
)) != EOF
) {
1192 printf("%d/%d\n", lines
, bytes
);
1197 putesc(const char *s
, FILE *stream
)
1204 if (putc('\t', stream
) == EOF
)
1211 if (putc('\n', stream
) == EOF
)
1218 if (putc(s
[0], stream
) == EOF
)
1223 if (putc('\n', stream
) == EOF
)