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
)
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
;
255 * Ask the user to edit file names and other data for the given
256 * attachment. NULL is returned if no file name is given.
258 static struct attachment
*
259 read_attachment_data(struct attachment
*ap
, unsigned number
)
261 char prefix
[80], *cp
;
264 ap
= csalloc(1, sizeof *ap
);
266 printf("#%u\tmessage %u\n", number
, ap
->a_msgno
);
269 snprintf(prefix
, sizeof prefix
, catgets(catd
, CATSET
, 50,
270 "#%u\tfilename: "), number
);
273 if ((ap
->a_name
= readtty(prefix
, ap
->a_name
)) == NULL
)
275 if ((exf
= expand(ap
->a_name
)) != NULL
)
277 if (access(ap
->a_name
, R_OK
) == 0)
281 if ((ap
->a_name
&& (value("attachment-ask-charset"))) ||
282 ((cp
= value("sendcharsets")) != NULL
&&
283 strchr(cp
, ',') != NULL
)) {
284 snprintf(prefix
, sizeof prefix
, "#%u\tcharset: ", number
);
285 ap
->a_charset
= readtty(prefix
, ap
->a_charset
);
288 * The "attachment-ask-content-*" variables are left undocumented
289 * since they are for RFC connoisseurs only.
291 if (ap
->a_name
&& value("attachment-ask-content-type")) {
292 if (ap
->a_content_type
== NULL
)
293 ap
->a_content_type
= mime_filecontent(ap
->a_name
);
294 snprintf(prefix
, sizeof prefix
, "#%u\tContent-Type: ", number
);
295 ap
->a_content_type
= readtty(prefix
, ap
->a_content_type
);
297 if (ap
->a_name
&& value("attachment-ask-content-disposition")) {
298 snprintf(prefix
, sizeof prefix
,
299 "#%u\tContent-Disposition: ", number
);
300 ap
->a_content_disposition
= readtty(prefix
,
301 ap
->a_content_disposition
);
303 if (ap
->a_name
&& value("attachment-ask-content-id")) {
304 snprintf(prefix
, sizeof prefix
, "#%u\tContent-ID: ", number
);
305 ap
->a_content_id
= readtty(prefix
, ap
->a_content_id
);
307 if (ap
->a_name
&& value("attachment-ask-content-description")) {
308 snprintf(prefix
, sizeof prefix
,
309 "#%u\tContent-Description: ", number
);
310 ap
->a_content_description
= readtty(prefix
,
311 ap
->a_content_description
);
313 return ap
->a_name
? ap
: NULL
;
317 * Interactively edit the attachment list.
320 edit_attachments(struct attachment
*attach
)
322 struct attachment
*ap
, *nap
;
325 for (ap
= attach
; ap
; ap
= ap
->a_flink
) {
326 if ((nap
= read_attachment_data(ap
, attno
)) == NULL
) {
328 ap
->a_blink
->a_flink
= ap
->a_flink
;
330 attach
= ap
->a_flink
;
332 ap
->a_flink
->a_blink
= ap
->a_blink
;
338 while ((nap
= read_attachment_data(NULL
, attno
)) != NULL
) {
339 for (ap
= attach
; ap
&& ap
->a_flink
; ap
= ap
->a_flink
);
352 * Put the given file to the end of the attachment list.
355 add_attachment(struct attachment
*attach
, char *file
, int expand_file
)
357 struct attachment
*ap
, *nap
;
364 file
= savestr(file
);
365 if (access(file
, R_OK
) != 0)
368 nap
= csalloc(1, sizeof *nap
);
370 if (attach
!= NULL
) {
371 for (ap
= attach
; ap
->a_flink
!= NULL
; ap
= ap
->a_flink
);
382 * Append the whitespace-separated file names to the end of
383 * the attachment list.
385 static struct attachment
*
386 append_attachments(struct attachment
*attach
, char *names
)
390 struct attachment
*ap
;
393 while (*cp
!= '\0' && blankchar(*cp
& 0377))
395 while (*cp
!= '\0') {
397 while (*cp
!= '\0' && !blankchar(*cp
& 0377))
401 if (*names
!= '\0') {
402 if ((ap
= add_attachment(attach
, names
, 1)) == NULL
)
409 while (*cp
!= '\0' && blankchar(*cp
& 0377))
416 collect(struct header
*hp
, int printheaders
, struct message
*mp
,
417 char *quotefile
, int doprefix
, int tflag
)
424 struct ignoretab
*quoteig
;
425 int lc
, cc
, escape
, eofcount
;
427 char *linebuf
= NULL
, *cp
, *quote
= NULL
;
429 char *tempMail
= NULL
;
433 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)) {
458 if (tempMail
!= NULL
) {
464 if (sigsetjmp(colljmp
, 1)) {
465 if (tempMail
!= NULL
) {
471 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
474 if ((collf
= Ftemp(&tempMail
, "Rs", "w+", 0600, 1)) == NULL
) {
475 perror(catgets(catd
, CATSET
, 51, "temporary mail file"));
481 if ((cp
= value("NAIL_HEAD")) != NULL
) {
488 * If we are going to prompt for a subject,
489 * refrain from printing a newline after
490 * the headers (since some people mind).
494 t
= GTO
|GSUBJECT
|GCC
|GNL
;
495 if (value("fullnames"))
497 if (hp
->h_subject
== NULL
&& (val
& val_INTERACT
) &&
498 (value("ask") != NULL
|| value("asksub") != NULL
))
499 t
&= ~GNL
, getfields
|= GSUBJECT
;
500 if (hp
->h_to
== NULL
&& (val
& val_INTERACT
))
501 t
&= ~GNL
, getfields
|= GTO
;
502 if (value("bsdcompat") == NULL
&& value("askatend") == NULL
&&
503 (val
& val_INTERACT
)) {
504 if (hp
->h_bcc
== NULL
&& value("askbcc"))
505 t
&= ~GNL
, getfields
|= GBCC
;
506 if (hp
->h_cc
== NULL
&& value("askcc"))
507 t
&= ~GNL
, getfields
|= GCC
;
510 puthead(hp
, stdout
, t
, SEND_TODISP
, CONV_NONE
,
517 * Quote an original message
519 if (mp
!= NULL
&& (doprefix
|| (quote
= value("quote")) != NULL
)) {
524 if ((cp
= value("fwdheading")) == NULL
)
525 cp
= "-------- Original Message --------";
527 fprintf(collf
, "%s\n", cp
);
528 fprintf(stdout
, "%s\n", cp
);
530 } else if (strcmp(quote
, "noheading") == 0) {
532 } else if (strcmp(quote
, "headers") == 0) {
534 } else if (strcmp(quote
, "allheaders") == 0) {
536 action
= SEND_QUOTE_ALL
;
538 cp
= hfield1("from", mp
);
540 mime_write(cp
, strlen(cp
),
541 collf
, CONV_FROMHDR
, TD_NONE
,
544 mime_write(cp
, strlen(cp
),
545 stdout
, CONV_FROMHDR
, TD_NONE
,
548 fwrite(catgets(catd
, CATSET
, 52,
549 " wrote:\n\n"), sizeof(char), 9, collf
);
550 fwrite(catgets(catd
, CATSET
, 52,
551 " wrote:\n\n"), sizeof(char), 9, stdout
);
554 cp
= value("indentprefix");
555 if (cp
!= NULL
&& *cp
== '\0')
557 send(mp
, collf
, quoteig
, doprefix
? NULL
: cp
, action
, NULL
);
558 send(mp
, stdout
, quoteig
, doprefix
? NULL
: cp
, action
, NULL
);
561 if ((cp
= value("escape")) != NULL
)
568 if (!sigsetjmp(colljmp
, 1)) {
570 grabh(hp
, getfields
, 1);
571 if (quotefile
!= NULL
) {
572 if (include_file(NULL
, quotefile
, &lc
, &cc
, 1) != 0)
577 * Come here for printing the after-signal message.
578 * Duplicate messages won't be printed because
579 * the write is aborted if we get a SIGTTOU.
584 fprintf(stderr
, catgets(catd
, CATSET
, 53,
585 "\n(Interrupt -- one more to kill letter)\n"));
587 printf(catgets(catd
, CATSET
, 54, "(continue)\n"));
591 if ((val
& val_INTERACT
) == 0 && tildeflag
<= 0 &&
592 !is_a_tty
[0] && !tflag
) {
594 * No tilde escapes, interrupts not expected. Copy
595 * standard input the simple way.
597 linebuf
= srealloc(linebuf
, linesize
= LINESIZE
);
598 while ((count
= fread(linebuf
, sizeof *linebuf
,
599 linesize
, stdin
)) > 0) {
600 if ((size_t)count
!= fwrite(linebuf
, sizeof *linebuf
,
608 count
= readline(stdin
, &linebuf
, &linesize
);
611 if ((val
& val_INTERACT
) &&
612 value("ignoreeof") != NULL
&& ++eofcount
< 25) {
613 printf(catgets(catd
, CATSET
, 55,
614 "Use \".\" to terminate letter\n"));
619 if (tflag
&& count
== 0) {
621 if (makeheader(collf
, hp
) != OKAY
)
629 if (linebuf
[0] == '.' && linebuf
[1] == '\0' &&
630 (val
& val_INTERACT
) &&
631 (value("dot") != NULL
|| value("ignoreeof") != NULL
))
633 if (linebuf
[0] != escape
||
634 ((val
& val_INTERACT
) == 0 && tildeflag
== 0) ||
636 if (putline(collf
, linebuf
, count
) < 0)
644 * On double escape, just send the single one.
645 * Otherwise, it's an error.
648 if (putline(collf
, &linebuf
[1], count
- 1) < 0)
653 printf(catgets(catd
, CATSET
, 56,
654 "Unknown tilde escape.\n"));
656 #ifdef DEBUG_COMMANDS
663 #endif /* DEBUG_COMMANDS */
666 * Shell escape, send the balance of the
674 * Escape to command mode, but be nice!
677 execute(&linebuf
[2], 1, count
- 2);
681 * Simulate end of file on input.
686 * Same as 'q', but no dead.letter saving.
694 * Force a quit of sending mail.
695 * Act like an interrupt happened.
703 * Grab a bunch of headers.
706 grabh(hp
, GTO
|GSUBJECT
|GCC
|GBCC
,
707 value("bsdcompat") != NULL
&&
708 value("bsdorder") != NULL
);
709 while (hp
->h_to
== NULL
);
713 * Grab extra headers.
716 grabh(hp
, GEXTRA
, 0);
717 while (check_from_and_sender(hp
->h_from
, hp
->h_sender
));
721 * Add to the To list.
723 while ((hp
->h_to
= cat(hp
->h_to
, checkaddrs(
724 lextract(&linebuf
[2], GTO
|GFULL
))))
729 * Set the Subject list.
732 while (whitechar(*cp
& 0377))
734 hp
->h_subject
= savestr(cp
);
738 * Edit the attachment list.
740 if (linebuf
[2] != '\0')
741 hp
->h_attach
= append_attachments(hp
->h_attach
,
744 hp
->h_attach
= edit_attachments(hp
->h_attach
);
748 * Add to the CC list.
750 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(
751 lextract(&linebuf
[2], GCC
|GFULL
)));
755 * Add stuff to blind carbon copies list.
757 hp
->h_bcc
= cat(hp
->h_bcc
, checkaddrs(
758 lextract(&linebuf
[2], GBCC
|GFULL
)));
761 strncpy(linebuf
+ 2, getdeadletter(), linesize
- 2);
762 linebuf
[linesize
-1]='\0';
768 * Search for the file name,
769 * then open it and copy the contents to collf.
772 while (whitechar(*cp
& 0377))
775 printf(catgets(catd
, CATSET
, 57,
776 "Interpolate what file?\n"));
780 insertcommand(collf
, cp
+ 1);
787 printf(catgets(catd
, CATSET
, 58,
788 "%s: Directory\n"), cp
);
791 if ((fbuf
= Fopen(cp
, "r")) == NULL
) {
795 printf(catgets(catd
, CATSET
, 59, "\"%s\" "), cp
);
797 if (include_file(fbuf
, cp
, &lc
, &cc
, 0) != 0)
799 printf(catgets(catd
, CATSET
, 60, "%d/%d\n"), lc
, cc
);
803 * Insert an environment variable into the file.
806 while (whitechar(*cp
& 0377))
808 if ((cp
= value(cp
)) == NULL
|| *cp
== '\0')
817 * Insert the contents of a signature variable.
819 if ((cp
= value(c
== 'a' ? "sign" : "Sign")) != NULL
&&
828 * Write the message on a file.
831 while (blankchar(*cp
& 0377))
834 fprintf(stderr
, catgets(catd
, CATSET
, 61,
835 "Write what file!?\n"));
838 if ((cp
= expand(cp
)) == NULL
)
841 exwrite(cp
, collf
, 1);
848 * Interpolate the named messages, if we
849 * are in receiving mail mode. Does the
850 * standard list processing garbage.
851 * If ~f is given, we don't shift over.
853 if (forward(linebuf
+ 2, collf
, c
) < 0)
858 * Print out the current state of the
859 * message without altering anything.
861 print_collf(collf
, hp
);
865 * Pipe message through command.
866 * Collect output as new message.
869 mespipe(&linebuf
[2]);
874 * Edit the current message.
875 * 'e' means to use EDITOR
876 * 'v' means to use VISUAL
879 mesedit(c
, value("editheaders") ? hp
: NULL
);
883 * Last the lengthy help string.
884 * (Very ugly, but take care for compiler supported
888 "-------------------- ~ ESCAPES ----------------------------\n"
889 "~~ Quote a single tilde\n"
890 "~@ [file ...] Edit attachment list\n"
891 "~b users Add users to \"blind\" cc list\n"
892 "~c users Add users to cc list\n"
893 "~d Read in dead.letter\n"
894 "~e Edit the message buffer\n"
895 "~f messages Read in messages without indenting lines\n"
896 "~F messages Same as ~f, but keep all header lines\n"
897 "~h Prompt for to list, subject, cc, and \"blind\" cc list\n");
899 "~r file Read a file into the message buffer\n"
900 "~p Print the message buffer\n"
901 "~q Abort message composition and save text to dead.letter\n"
902 "~m messages Read in messages with each line indented\n"
903 "~M messages Same as ~m, but keep all header lines\n"
904 "~s subject Set subject\n"
905 "~t users Add users to to list\n"
906 "~v Invoke display editor on message\n"
907 "~w file Write message onto file\n"
908 "~x Abort message composition and discard text written so far\n");
910 "~!command Invoke the shell\n"
911 "~:command Execute a regular command\n"
912 "-----------------------------------------------------------\n");
925 if ((cp
= value("NAIL_TAIL")) != NULL
) {
936 sigaddset(&nset
, SIGINT
);
937 sigaddset(&nset
, SIGHUP
);
939 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
941 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
943 safe_signal(SIGINT
, saveint
);
944 safe_signal(SIGHUP
, savehup
);
945 safe_signal(SIGTSTP
, savetstp
);
946 safe_signal(SIGTTOU
, savettou
);
947 safe_signal(SIGTTIN
, savettin
);
948 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
953 * Write a file, ex-like if f set.
956 exwrite(char *name
, FILE *fp
, int f
)
964 printf("\"%s\" ", name
);
967 if ((of
= Fopen(name
, "a")) == NULL
) {
973 while ((c
= getc(fp
)) != EOF
) {
985 printf(catgets(catd
, CATSET
, 65, "%d/%ld\n"), lc
, cc
);
991 makeheader(FILE *fp
, struct header
*hp
)
997 if ((nf
= Ftemp(&tempEdit
, "Re", "w+", 0600, 1)) == NULL
) {
998 perror(catgets(catd
, CATSET
, 66, "temporary mail edit file"));
1006 extract_header(fp
, hp
);
1007 while ((c
= getc(fp
)) != EOF
)
1013 if (check_from_and_sender(hp
->h_from
, hp
->h_sender
))
1019 * Edit the message being collected on fp.
1020 * On return, make the edit file the new temp file.
1023 mesedit(int c
, struct header
*hp
)
1025 sighandler_type sigint
= safe_signal(SIGINT
, SIG_IGN
);
1026 FILE *nf
= run_editor(collf
, (off_t
)-1, c
, 0, hp
, NULL
, SEND_MBOX
,
1034 fseek(nf
, 0L, SEEK_END
);
1039 safe_signal(SIGINT
, sigint
);
1043 * Pipe the message through the command.
1044 * Old message is on stdin of command;
1045 * New message collected from stdout.
1046 * Sh -c must return 0 to accept the new message.
1052 sighandler_type sigint
= safe_signal(SIGINT
, SIG_IGN
);
1056 if ((nf
= Ftemp(&tempEdit
, "Re", "w+", 0600, 1)) == NULL
) {
1057 perror(catgets(catd
, CATSET
, 66, "temporary mail edit file"));
1064 * stdin = current message.
1065 * stdout = new message.
1067 if ((shell
= value("SHELL")) == NULL
)
1069 if (run_command(shell
,
1070 0, fileno(collf
), fileno(nf
), "-c", cmd
, NULL
) < 0) {
1074 if (fsize(nf
) == 0) {
1075 fprintf(stderr
, catgets(catd
, CATSET
, 67,
1076 "No bytes from \"%s\" !?\n"), cmd
);
1083 fseek(nf
, 0L, SEEK_END
);
1087 safe_signal(SIGINT
, sigint
);
1091 * Interpolate the named messages into the current
1092 * message, preceding each line with a tab.
1093 * Return a count of the number of characters now in
1094 * the message, or -1 if an error is encountered writing
1095 * the message temporary. The flag argument is 'm' if we
1096 * should shift over and 'f' if not.
1099 forward(char *ms
, FILE *fp
, int f
)
1102 struct ignoretab
*ig
;
1104 enum sendaction action
;
1107 msgvec
= (int *)salloc((msgCount
+1) * sizeof *msgvec
);
1110 if (getmsglist(ms
, msgvec
, 0) < 0)
1113 *msgvec
= first(0, MMNORM
);
1115 printf(catgets(catd
, CATSET
, 68,
1116 "No appropriate messages\n"));
1121 if (f
== 'f' || f
== 'F')
1123 else if ((tabst
= value("indentprefix")) == NULL
)
1125 ig
= upperchar(f
) ? (struct ignoretab
*)NULL
: ignore
;
1126 action
= upperchar(f
) ? SEND_QUOTE_ALL
: SEND_QUOTE
;
1127 printf(catgets(catd
, CATSET
, 69, "Interpolating:"));
1128 for (; *msgvec
!= 0; msgvec
++) {
1129 struct message
*mp
= message
+ *msgvec
- 1;
1132 printf(" %d", *msgvec
);
1133 if (send(mp
, fp
, ig
, tabst
, action
, NULL
) < 0) {
1134 perror(catgets(catd
, CATSET
, 70,
1135 "temporary mail file"));
1144 * Print (continue) when continued after ^Z.
1150 sighandler_type old_action
= safe_signal(s
, SIG_DFL
);
1154 sigaddset(&nset
, s
);
1155 sigprocmask(SIG_UNBLOCK
, &nset
, (sigset_t
*)NULL
);
1157 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
1158 safe_signal(s
, old_action
);
1162 siglongjmp(colljmp
, 1);
1167 * On interrupt, come here to save the partial message in ~/dead.letter.
1168 * Then jump out of the collection loop.
1175 * the control flow is subtle, because we can be called from ~q.
1178 if (value("ignore") != NULL
) {
1185 siglongjmp(colljmp
, 1);
1189 if (value("save") != NULL
&& s
!= 0)
1190 savedeadletter(collf
);
1191 siglongjmp(collabort
, 1);
1200 savedeadletter(collf
);
1202 * Let's pretend nobody else wants to clean up,
1203 * a true statement at this time.
1209 savedeadletter(FILE *fp
)
1212 int c
, lines
= 0, bytes
= 0;
1217 cp
= getdeadletter();
1219 dbuf
= Fopen(cp
, "a");
1223 printf("\"%s\" ", cp
);
1224 while ((c
= getc(fp
)) != EOF
) {
1231 printf("%d/%d\n", lines
, bytes
);
1236 putesc(const char *s
, FILE *stream
)
1255 putc(s
[0]&0377, stream
);