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
53 * Read a message from standard output and return a read file to it
58 * The following hokiness with global variables is so that on
59 * receipt of an interrupt signal, the partial message can be salted
60 * away on dead.letter.
63 static sighandler_type saveint
; /* Previous SIGINT value */
64 static sighandler_type savehup
; /* Previous SIGHUP value */
65 static sighandler_type savetstp
; /* Previous SIGTSTP value */
66 static sighandler_type savettou
; /* Previous SIGTTOU value */
67 static sighandler_type savettin
; /* Previous SIGTTIN value */
68 static FILE *collf
; /* File for saving away */
69 static int hadintr
; /* Have seen one SIGINT so far */
71 static sigjmp_buf colljmp
; /* To get back to work */
72 static int colljmp_p
; /* whether to long jump */
73 static sigjmp_buf collabort
; /* To end collection with error */
75 static sigjmp_buf pipejmp
; /* On broken pipe */
77 static void onpipe(int signo
);
78 static void insertcommand(FILE *fp
, char *cmd
);
79 static void print_collf(FILE *collf
, struct header
*hp
);
80 static int include_file(FILE *fbuf
, char *name
, int *linecount
,
81 int *charcount
, int echo
);
82 static struct attachment
*read_attachment_data(struct attachment
*ap
,
84 static struct attachment
*append_attachments(struct attachment
*attach
,
86 static int exwrite(char *name
, FILE *fp
, int f
);
87 static enum okay
makeheader(FILE *fp
, struct header
*hp
);
88 static void mesedit(int c
, struct header
*hp
);
89 static void mespipe(char *cmd
);
90 static int forward(char *ms
, FILE *fp
, int f
);
91 static void collstop(int s
);
92 static void collint(int s
);
93 static void collhup(int s
);
94 static int putesc(const char *s
, FILE *stream
);
101 siglongjmp(pipejmp
, 1);
105 * Execute cmd and insert its standard output into fp.
108 insertcommand(FILE *fp
, char *cmd
)
115 if (sigsetjmp(pipejmp
, 1))
119 if ((obuf
= Popen(cmd
, "r", cp
, 0)) == NULL
) {
123 safe_signal(SIGPIPE
, onpipe
);
124 while ((c
= getc(obuf
)) != EOF
)
127 safe_signal(SIGPIPE
, SIG_IGN
);
129 safe_signal(SIGPIPE
, dflpipe
);
136 print_collf(FILE *collf
, struct header
*hp
)
139 FILE *volatile obuf
= stdout
;
140 struct attachment
*ap
;
143 size_t linecnt
, maxlines
, linesize
= 0, linelen
, count
, count2
;
149 count
= count2
= fsize(collf
);
150 if (is_a_tty
[0] && is_a_tty
[1] && (cp
= value("crt")) != NULL
) {
152 fgetline(&lbuf
, &linesize
, &count2
, NULL
, collf
, 0);
155 maxlines
= (*cp
== '\0' ? screensize() : atoi(cp
));
167 maxlines
-= myaddrs(hp
) != NULL
|| hp
->h_from
!= NULL
;
168 maxlines
-= value("ORGANIZATION") != NULL
||
169 hp
->h_organization
!= NULL
;
170 maxlines
-= value("replyto") != NULL
|| hp
->h_replyto
!= NULL
;
171 maxlines
-= value("sender") != NULL
|| hp
->h_sender
!= NULL
;
172 if ((long)maxlines
< 0 || linecnt
> maxlines
) {
174 if (sigsetjmp(pipejmp
, 1))
176 obuf
= Popen(cp
, "w", NULL
, 1);
181 safe_signal(SIGPIPE
, onpipe
);
184 fprintf(obuf
, catgets(catd
, CATSET
, 62,
185 "-------\nMessage contains:\n"));
186 gf
= GIDENT
|GTO
|GSUBJECT
|GCC
|GBCC
|GNL
|GFILES
;
187 if (value("fullnames"))
189 puthead(hp
, obuf
, gf
, SEND_TODISP
, CONV_NONE
, NULL
, NULL
);
190 while (fgetline(&lbuf
, &linesize
, &count
, &linelen
, collf
, 1))
191 prout(lbuf
, linelen
, obuf
);
192 if (hp
->h_attach
!= NULL
) {
193 fputs(catgets(catd
, CATSET
, 63, "Attachments:"), obuf
);
194 for (ap
= hp
->h_attach
; ap
!= NULL
; ap
= ap
->a_flink
) {
196 fprintf(obuf
, " message %u", ap
->a_msgno
);
198 fprintf(obuf
, " %s", ap
->a_name
);
205 if (obuf
!= stdout
) {
206 safe_signal(SIGPIPE
, SIG_IGN
);
208 safe_signal(SIGPIPE
, dflpipe
);
215 include_file(FILE *fbuf
, char *name
, int *linecount
, int *charcount
, int echo
)
217 char *interactive
, *linebuf
= NULL
;
218 size_t linesize
= 0, linelen
, count
;
221 fbuf
= Fopen(name
, "r");
226 interactive
= value("interactive");
232 while (fgetline(&linebuf
, &linesize
, &count
, &linelen
, fbuf
, 0)
234 if (fwrite(linebuf
, sizeof *linebuf
, linelen
, collf
)
239 if (interactive
!= NULL
&& echo
)
240 fwrite(linebuf
, sizeof *linebuf
, linelen
, stdout
);
242 (*charcount
) += linelen
;
253 * Ask the user to edit file names and other data for the given
254 * attachment. NULL is returned if no file name is given.
256 static struct attachment
*
257 read_attachment_data(struct attachment
*ap
, unsigned number
)
259 char prefix
[80], *cp
;
262 ap
= csalloc(1, sizeof *ap
);
264 printf("#%u\tmessage %u\n", number
, ap
->a_msgno
);
267 snprintf(prefix
, sizeof prefix
, catgets(catd
, CATSET
, 50,
268 "#%u\tfilename: "), number
);
271 if ((ap
->a_name
= readtty(prefix
, ap
->a_name
)) == NULL
)
273 if ((exf
= file_expand(ap
->a_name
)) == NULL
)
276 if (access(ap
->a_name
, R_OK
) == 0)
280 if ((ap
->a_name
&& (value("attachment-ask-charset"))) ||
281 ((cp
= value("sendcharsets")) != NULL
&&
282 strchr(cp
, ',') != NULL
)) {
283 snprintf(prefix
, sizeof prefix
, "#%u\tcharset: ", number
);
284 ap
->a_charset
= readtty(prefix
, ap
->a_charset
);
287 * The "attachment-ask-content-*" variables are left undocumented
288 * since they are for RFC connoisseurs only.
290 if (ap
->a_name
&& value("attachment-ask-content-type")) {
291 if (ap
->a_content_type
== NULL
)
292 ap
->a_content_type
= mime_filecontent(ap
->a_name
);
293 snprintf(prefix
, sizeof prefix
, "#%u\tContent-Type: ", number
);
294 ap
->a_content_type
= readtty(prefix
, ap
->a_content_type
);
296 if (ap
->a_name
&& value("attachment-ask-content-disposition")) {
297 snprintf(prefix
, sizeof prefix
,
298 "#%u\tContent-Disposition: ", number
);
299 ap
->a_content_disposition
= readtty(prefix
,
300 ap
->a_content_disposition
);
302 if (ap
->a_name
&& value("attachment-ask-content-id")) {
303 snprintf(prefix
, sizeof prefix
, "#%u\tContent-ID: ", number
);
304 ap
->a_content_id
= readtty(prefix
, ap
->a_content_id
);
306 if (ap
->a_name
&& value("attachment-ask-content-description")) {
307 snprintf(prefix
, sizeof prefix
,
308 "#%u\tContent-Description: ", number
);
309 ap
->a_content_description
= readtty(prefix
,
310 ap
->a_content_description
);
312 return ap
->a_name
? ap
: NULL
;
316 * Interactively edit the attachment list.
319 edit_attachments(struct attachment
*attach
)
321 struct attachment
*ap
, *nap
;
324 for (ap
= attach
; ap
; ap
= ap
->a_flink
) {
325 if ((nap
= read_attachment_data(ap
, attno
)) == NULL
) {
327 ap
->a_blink
->a_flink
= ap
->a_flink
;
329 attach
= ap
->a_flink
;
331 ap
->a_flink
->a_blink
= ap
->a_blink
;
337 while ((nap
= read_attachment_data(NULL
, attno
)) != NULL
) {
338 for (ap
= attach
; ap
&& ap
->a_flink
; ap
= ap
->a_flink
);
351 * Put the given file to the end of the attachment list.
354 add_attachment(struct attachment
*attach
, char *file
, int expand_file
)
356 struct attachment
*ap
, *nap
;
359 if ((file
= file_expand(file
)) == NULL
)
362 file
= savestr(file
);
363 if (access(file
, R_OK
) != 0)
366 nap
= csalloc(1, sizeof *nap
);
368 if (attach
!= NULL
) {
369 for (ap
= attach
; ap
->a_flink
!= NULL
; ap
= ap
->a_flink
);
380 * Append the whitespace-separated file names to the end of
381 * the attachment list.
383 static struct attachment
*
384 append_attachments(struct attachment
*attach
, char *names
)
388 struct attachment
*ap
;
391 while (*cp
!= '\0' && blankchar(*cp
& 0377))
393 while (*cp
!= '\0') {
395 while (*cp
!= '\0' && !blankchar(*cp
& 0377))
399 if (*names
!= '\0') {
400 if ((ap
= add_attachment(attach
, names
, 1)) == NULL
)
407 while (*cp
!= '\0' && blankchar(*cp
& 0377))
414 collect(struct header
*hp
, int printheaders
, struct message
*mp
,
415 char *quotefile
, int doprefix
, int volatile tflag
)
422 struct ignoretab
*quoteig
;
423 int lc
, cc
, eofcount
, val
, c
, t
;
424 int volatile escape
, getfields
;
425 char *linebuf
= NULL
, *cp
, *quote
= NULL
, *tempMail
= NULL
;
428 enum sendaction action
;
430 sighandler_type savedtop
;
433 if (value("interactive") != NULL
)
438 * Start catching signals from here, but we're still die on interrupts
439 * until we're in the main loop.
442 sigaddset(&nset
, SIGINT
);
443 sigaddset(&nset
, SIGHUP
);
444 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
445 handlerpush(collint
);
446 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
447 safe_signal(SIGINT
, collint
);
448 if ((savehup
= safe_signal(SIGHUP
, SIG_IGN
)) != SIG_IGN
)
449 safe_signal(SIGHUP
, collhup
);
450 savetstp
= safe_signal(SIGTSTP
, collstop
);
451 savettou
= safe_signal(SIGTTOU
, collstop
);
452 savettin
= safe_signal(SIGTTIN
, collstop
);
453 if (sigsetjmp(collabort
, 1))
455 if (sigsetjmp(colljmp
, 1))
457 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
460 if ((collf
= Ftemp(&tempMail
, "Rs", "w+", 0600, 1)) == NULL
) {
461 perror(tr(51, "temporary mail file"));
467 if ((cp
= value("NAIL_HEAD")) != NULL
&& putesc(cp
, collf
) < 0)
471 * If we are going to prompt for a subject,
472 * refrain from printing a newline after
473 * the headers (since some people mind).
477 t
= GTO
|GSUBJECT
|GCC
|GNL
;
478 if (value("fullnames"))
480 if (hp
->h_subject
== NULL
&& (val
& val_INTERACT
) &&
481 (value("ask") != NULL
|| value("asksub") != NULL
))
482 t
&= ~GNL
, getfields
|= GSUBJECT
;
483 if (hp
->h_to
== NULL
&& (val
& val_INTERACT
))
484 t
&= ~GNL
, getfields
|= GTO
;
485 if (value("bsdcompat") == NULL
&& value("askatend") == NULL
&&
486 (val
& val_INTERACT
)) {
487 if (hp
->h_bcc
== NULL
&& value("askbcc"))
488 t
&= ~GNL
, getfields
|= GBCC
;
489 if (hp
->h_cc
== NULL
&& value("askcc"))
490 t
&= ~GNL
, getfields
|= GCC
;
493 (void)puthead(hp
, stdout
, t
, SEND_TODISP
, CONV_NONE
,
495 (void)fflush(stdout
);
500 * Quote an original message
502 if (mp
!= NULL
&& (doprefix
|| (quote
= value("quote")) != NULL
)) {
507 if ((cp
= value("fwdheading")) == NULL
)
508 cp
= "-------- Original Message --------";
509 if (*cp
&& fprintf(collf
, "%s\n", cp
) < 0)
511 } else if (strcmp(quote
, "noheading") == 0) {
513 } else if (strcmp(quote
, "headers") == 0) {
515 } else if (strcmp(quote
, "allheaders") == 0) {
517 action
= SEND_QUOTE_ALL
;
519 cp
= hfield1("from", mp
);
520 if (cp
!= NULL
&& (count
= (long)strlen(cp
)) > 0) {
521 if (mime_write(cp
, count
,
522 collf
, CONV_FROMHDR
, TD_NONE
,
526 if (fprintf(collf
, tr(52, " wrote:\n\n")) < 0)
532 cp
= value("indentprefix");
533 if (cp
!= NULL
&& *cp
== '\0')
535 if (send(mp
, collf
, quoteig
, (doprefix
? NULL
: cp
), action
,
540 /* Print what we have sofar also on the terminal */
542 while ((c
= getc(collf
)) != EOF
)
543 (void)putc(c
, stdout
);
544 if (fseek(collf
, 0, SEEK_END
))
547 escape
= ((cp
= value("escape")) != NULL
) ? *cp
: ESCAPE
;
551 if (! sigsetjmp(colljmp
, 1)) {
553 grabh(hp
, getfields
, 1);
554 if (quotefile
!= NULL
) {
555 if (include_file(NULL
, quotefile
, &lc
, &cc
, 1) != 0)
560 * Come here for printing the after-signal message.
561 * Duplicate messages won't be printed because
562 * the write is aborted if we get a SIGTTOU.
566 (void)fprintf(stderr
, tr(53,
567 "\n(Interrupt -- one more to kill letter)\n"));
569 printf(tr(54, "(continue)\n"));
570 (void)fflush(stdout
);
575 * No tilde escapes, interrupts not expected. Simply copy STDIN
577 if ((val
& val_INTERACT
) == 0 && tildeflag
<= 0 && ! tflag
) {
578 linebuf
= srealloc(linebuf
, linesize
= LINESIZE
);
579 while ((count
= fread(linebuf
, sizeof *linebuf
,
580 linesize
, stdin
)) > 0) {
581 if ((size_t)count
!= fwrite(linebuf
, sizeof *linebuf
,
591 * The interactive collect loop
595 count
= readline(stdin
, &linebuf
, &linesize
);
598 if ((val
& val_INTERACT
) &&
599 value("ignoreeof") != NULL
&& ++eofcount
< 25) {
601 "Use \".\" to terminate letter\n"));
606 if (tflag
&& count
== 0) {
608 if (makeheader(collf
, hp
) != OKAY
)
616 if (linebuf
[0] == '.' && linebuf
[1] == '\0' &&
617 (val
& val_INTERACT
) &&
618 (value("dot") != NULL
||
619 value("ignoreeof") != NULL
))
621 if (linebuf
[0] != escape
||
622 ((val
& val_INTERACT
) == 0 && tildeflag
== 0) ||
624 if (putline(collf
, linebuf
, count
) < 0)
633 * On double escape, just send the single one.
634 * Otherwise, it's an error.
637 if (putline(collf
, &linebuf
[1], count
- 1) < 0)
642 printf(tr(56, "Unknown tilde escape.\n"));
651 /* Shell escape, send the balance of line to sh -c */
656 /* Escape to command mode, but be nice! */
658 execute(&linebuf
[2], 1, count
- 2);
661 /* Simulate end of file on input */
664 /* Same as 'q', but no dead.letter saving */
671 * Force a quit of sending mail.
672 * Act like an interrupt happened.
679 /* Grab a bunch of headers */
681 grabh(hp
, GTO
|GSUBJECT
|GCC
|GBCC
,
682 value("bsdcompat") != NULL
&&
683 value("bsdorder") != NULL
);
684 while (hp
->h_to
== NULL
);
687 /* Grab extra headers */
689 grabh(hp
, GEXTRA
, 0);
690 while (check_from_and_sender(hp
->h_from
, hp
->h_sender
));
693 /* Add to the To list */
694 while ((hp
->h_to
= cat(hp
->h_to
, checkaddrs(
695 lextract(&linebuf
[2], GTO
|GFULL
))))
700 /* Set the Subject list */
702 while (whitechar(*cp
))
704 hp
->h_subject
= savestr(cp
);
707 /* Edit the attachment list */
708 if (linebuf
[2] != '\0')
709 hp
->h_attach
= append_attachments(hp
->h_attach
,
712 hp
->h_attach
= edit_attachments(hp
->h_attach
);
715 /* Add to the CC list */
716 hp
->h_cc
= cat(hp
->h_cc
, checkaddrs(
717 lextract(&linebuf
[2], GCC
|GFULL
)));
720 /* Add stuff to blind carbon copies list */
721 hp
->h_bcc
= cat(hp
->h_bcc
, checkaddrs(
722 lextract(&linebuf
[2], GBCC
|GFULL
)));
725 strncpy(linebuf
+ 2, getdeadletter(), linesize
- 2);
726 linebuf
[linesize
-1]='\0';
732 * Search for the file name,
733 * then open it and copy the contents to collf.
736 while (whitechar(*cp
))
739 fprintf(stderr
, tr(57,
740 "Interpolate what file?\n"));
744 insertcommand(collf
, cp
+ 1);
747 if ((cp
= file_expand(cp
)) == NULL
)
750 fprintf(stderr
, tr(58, "%s: Directory\n"), cp
);
753 if ((fbuf
= Fopen(cp
, "r")) == NULL
) {
757 printf(tr(59, "\"%s\" "), cp
);
759 if (include_file(fbuf
, cp
, &lc
, &cc
, 0) != 0)
761 printf(tr(60, "%d/%d\n"), lc
, cc
);
764 /* Insert an environment variable into the file */
766 while (whitechar(*cp
))
768 if ((cp
= value(cp
)) == NULL
|| *cp
== '\0')
770 if (putesc(cp
, collf
) < 0)
772 if ((val
& val_INTERACT
) && putesc(cp
, stdout
) < 0)
777 /* Insert the contents of a signature variable */
778 if ((cp
= value(c
== 'a' ? "sign" : "Sign")) != NULL
&&
780 if (putesc(cp
, collf
) < 0)
782 if ((val
& val_INTERACT
) &&
783 putesc(cp
, stdout
) < 0)
788 /* Write the message on a file */
790 while (blankchar(*cp
))
792 if (*cp
== '\0' || (cp
= file_expand(cp
)) == NULL
) {
793 fprintf(stderr
, tr(61, "Write what file!?\n"));
797 if (exwrite(cp
, collf
, 1) < 0)
805 * Interpolate the named messages, if we
806 * are in receiving mail mode. Does the
807 * standard list processing garbage.
808 * If ~f is given, we don't shift over.
810 if (forward(linebuf
+ 2, collf
, c
) < 0)
815 * Print out the current state of the
816 * message without altering anything.
818 print_collf(collf
, hp
);
822 * Pipe message through command.
823 * Collect output as new message.
826 mespipe(&linebuf
[2]);
831 * Edit the current message.
832 * 'e' means to use EDITOR
833 * 'v' means to use VISUAL
836 mesedit(c
, value("editheaders") ? hp
: NULL
);
840 * Last the lengthy help string.
841 * (Very ugly, but take care for compiler supported
845 "-------------------- ~ ESCAPES ----------------------------\n"
846 "~~ Quote a single tilde\n"
847 "~@ [file ...] Edit attachment list\n"
848 "~b users Add users to \"blind\" cc list\n"
849 "~c users Add users to cc list\n"
850 "~d Read in dead.letter\n"
851 "~e Edit the message buffer\n"
852 "~f messages Read in messages without indenting lines\n"
853 "~F messages Same as ~f, but keep all header lines\n"
854 "~h Prompt for to list, subject, cc, and \"blind\" cc list\n");
856 "~r file Read a file into the message buffer\n"
857 "~p Print the message buffer\n"
858 "~q Abort message composition and save text to dead.letter\n"
859 "~m messages Read in messages with each line indented\n"
860 "~M messages Same as ~m, but keep all header lines\n"
861 "~s subject Set subject\n"
862 "~t users Add users to to list\n"
863 "~v Invoke display editor on message\n"
864 "~w file Write message onto file\n"
865 "~x Abort message composition and discard text written so far\n");
867 "~!command Invoke the shell\n"
868 "~:command Execute a regular command\n"
869 "-----------------------------------------------------------\n");
876 if ((cp
= value("NAIL_TAIL")) != NULL
) {
877 if (putesc(cp
, collf
) < 0)
879 if ((val
& val_INTERACT
) && putesc(cp
, stdout
) < 0)
887 sigaddset(&nset
, SIGINT
);
888 sigaddset(&nset
, SIGHUP
);
889 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
890 safe_signal(SIGINT
, saveint
);
891 safe_signal(SIGHUP
, savehup
);
892 safe_signal(SIGTSTP
, savetstp
);
893 safe_signal(SIGTTOU
, savettou
);
894 safe_signal(SIGTTIN
, savettin
);
895 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
898 if (tempMail
!= NULL
) {
910 * Write a file, ex-like if f set.
913 exwrite(char *name
, FILE *fp
, int f
)
921 printf("\"%s\" ", name
);
924 if ((of
= Fopen(name
, "a")) == NULL
) {
930 while ((c
= getc(fp
)) != EOF
) {
942 printf(catgets(catd
, CATSET
, 65, "%d/%ld\n"), lc
, cc
);
948 makeheader(FILE *fp
, struct header
*hp
)
954 if ((nf
= Ftemp(&tempEdit
, "Re", "w+", 0600, 1)) == NULL
) {
955 perror(catgets(catd
, CATSET
, 66, "temporary mail edit file"));
963 extract_header(fp
, hp
);
964 while ((c
= getc(fp
)) != EOF
)
970 if (check_from_and_sender(hp
->h_from
, hp
->h_sender
))
976 * Edit the message being collected on fp.
977 * On return, make the edit file the new temp file.
980 mesedit(int c
, struct header
*hp
)
982 sighandler_type sigint
= safe_signal(SIGINT
, SIG_IGN
);
983 FILE *nf
= run_editor(collf
, (off_t
)-1, c
, 0, hp
, NULL
, SEND_MBOX
,
991 fseek(nf
, 0L, SEEK_END
);
996 safe_signal(SIGINT
, sigint
);
1000 * Pipe the message through the command.
1001 * Old message is on stdin of command;
1002 * New message collected from stdout.
1003 * Sh -c must return 0 to accept the new message.
1009 sighandler_type sigint
= safe_signal(SIGINT
, SIG_IGN
);
1013 if ((nf
= Ftemp(&tempEdit
, "Re", "w+", 0600, 1)) == NULL
) {
1014 perror(catgets(catd
, CATSET
, 66, "temporary mail edit file"));
1021 * stdin = current message.
1022 * stdout = new message.
1024 if ((shell
= value("SHELL")) == NULL
)
1026 if (run_command(shell
,
1027 0, fileno(collf
), fileno(nf
), "-c", cmd
, NULL
) < 0) {
1031 if (fsize(nf
) == 0) {
1032 fprintf(stderr
, catgets(catd
, CATSET
, 67,
1033 "No bytes from \"%s\" !?\n"), cmd
);
1040 fseek(nf
, 0L, SEEK_END
);
1044 safe_signal(SIGINT
, sigint
);
1048 * Interpolate the named messages into the current
1049 * message, preceding each line with a tab.
1050 * Return a count of the number of characters now in
1051 * the message, or -1 if an error is encountered writing
1052 * the message temporary. The flag argument is 'm' if we
1053 * should shift over and 'f' if not.
1056 forward(char *ms
, FILE *fp
, int f
)
1059 struct ignoretab
*ig
;
1061 enum sendaction action
;
1064 msgvec
= (int *)salloc((msgCount
+1) * sizeof *msgvec
);
1067 if (getmsglist(ms
, msgvec
, 0) < 0)
1070 *msgvec
= first(0, MMNORM
);
1072 printf(catgets(catd
, CATSET
, 68,
1073 "No appropriate messages\n"));
1078 if (f
== 'f' || f
== 'F')
1080 else if ((tabst
= value("indentprefix")) == NULL
)
1082 ig
= upperchar(f
) ? (struct ignoretab
*)NULL
: ignore
;
1083 action
= upperchar(f
) ? SEND_QUOTE_ALL
: SEND_QUOTE
;
1084 printf(catgets(catd
, CATSET
, 69, "Interpolating:"));
1085 for (; *msgvec
!= 0; msgvec
++) {
1086 struct message
*mp
= message
+ *msgvec
- 1;
1089 printf(" %d", *msgvec
);
1090 if (send(mp
, fp
, ig
, tabst
, action
, NULL
) < 0) {
1091 perror(catgets(catd
, CATSET
, 70,
1092 "temporary mail file"));
1101 * Print (continue) when continued after ^Z.
1107 sighandler_type old_action
= safe_signal(s
, SIG_DFL
);
1111 sigaddset(&nset
, s
);
1112 sigprocmask(SIG_UNBLOCK
, &nset
, (sigset_t
*)NULL
);
1114 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
1115 safe_signal(s
, old_action
);
1119 siglongjmp(colljmp
, 1);
1124 * On interrupt, come here to save the partial message in ~/dead.letter.
1125 * Then jump out of the collection loop.
1132 * the control flow is subtle, because we can be called from ~q.
1135 if (value("ignore") != NULL
) {
1142 siglongjmp(colljmp
, 1);
1146 if (value("save") != NULL
&& s
!= 0)
1147 savedeadletter(collf
);
1148 siglongjmp(collabort
, 1);
1157 savedeadletter(collf
);
1159 * Let's pretend nobody else wants to clean up,
1160 * a true statement at this time.
1166 savedeadletter(FILE *fp
)
1169 int c
, lines
= 0, bytes
= 0;
1174 cp
= getdeadletter();
1176 dbuf
= Fopen(cp
, "a");
1180 printf("\"%s\" ", cp
);
1181 while ((c
= getc(fp
)) != EOF
) {
1188 printf("%d/%d\n", lines
, bytes
);
1193 putesc(const char *s
, FILE *stream
)
1200 if (putc('\t', stream
) == EOF
)
1207 if (putc('\n', stream
) == EOF
)
1214 if (putc(s
[0], stream
) == EOF
)
1219 if (putc('\n', stream
) == EOF
)