2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)collect.c 8.2 (Berkeley) 4/19/94
30 * $FreeBSD: src/usr.bin/mail/collect.c,v 1.4.6.3 2003/01/06 05:46:03 mikeh Exp $
31 * $DragonFly: src/usr.bin/mail/collect.c,v 1.4 2004/09/08 03:01:11 joerg Exp $
35 * Mail -- a mail program
37 * Collect input from standard input, handling
46 * Read a message from standard output and return a read file to it
51 * The following hokiness with global variables is so that on
52 * receipt of an interrupt signal, the partial message can be salted
53 * away on dead.letter.
56 static sig_t saveint
; /* Previous SIGINT value */
57 static sig_t savehup
; /* Previous SIGHUP value */
58 static sig_t savetstp
; /* Previous SIGTSTP value */
59 static sig_t savettou
; /* Previous SIGTTOU value */
60 static sig_t savettin
; /* Previous SIGTTIN value */
61 static FILE *collf
; /* File for saving away */
62 static int hadintr
; /* Have seen one SIGINT so far */
64 static jmp_buf colljmp
; /* To get back to work */
65 static int colljmp_p
; /* whether to long jump */
66 static jmp_buf collabort
; /* To end collection with error */
69 collect(struct header
*hp
, int printheaders
)
72 int lc
, cc
, escape
, eofcount
, fd
, c
, t
;
73 char linebuf
[LINESIZE
], tempname
[PATHSIZE
], *cp
, getsub
;
75 int longline
, lastlong
, rc
; /* So we don't make 2 or more lines
76 out of a long input line. */
80 * Start catching signals from here, but we're still die on interrupts
81 * until we're in the main loop.
84 sigaddset(&nset
, SIGINT
);
85 sigaddset(&nset
, SIGHUP
);
86 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
87 if ((saveint
= signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
88 signal(SIGINT
, collint
);
89 if ((savehup
= signal(SIGHUP
, SIG_IGN
)) != SIG_IGN
)
90 signal(SIGHUP
, collhup
);
91 savetstp
= signal(SIGTSTP
, collstop
);
92 savettou
= signal(SIGTTOU
, collstop
);
93 savettin
= signal(SIGTTIN
, collstop
);
94 if (setjmp(collabort
) || setjmp(colljmp
)) {
98 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
101 snprintf(tempname
, sizeof(tempname
), "%s/mail.RsXXXXXXXXXX", tmpdir
);
102 if ((fd
= mkstemp(tempname
)) == -1 ||
103 (collf
= Fdopen(fd
, "w+")) == NULL
) {
104 warn("%s", tempname
);
110 * If we are going to prompt for a subject,
111 * refrain from printing a newline after
112 * the headers (since some people mind).
114 t
= GTO
|GSUBJECT
|GCC
|GNL
;
116 if (hp
->h_subject
== NULL
&& value("interactive") != NULL
&&
117 (value("ask") != NULL
|| value("asksub") != NULL
))
120 puthead(hp
, stdout
, t
);
123 if ((cp
= value("escape")) != NULL
)
132 if (!setjmp(colljmp
)) {
137 * Come here for printing the after-signal message.
138 * Duplicate messages won't be printed because
139 * the write is aborted if we get a SIGTTOU.
145 "\n(Interrupt -- one more to kill letter)\n");
147 printf("(continue)\n");
153 c
= readline(stdin
, linebuf
, LINESIZE
);
156 if (value("interactive") != NULL
&&
157 value("ignoreeof") != NULL
&& ++eofcount
< 25) {
158 printf("Use \".\" to terminate letter\n");
164 longline
= c
== LINESIZE
- 1;
167 if (linebuf
[0] == '.' && linebuf
[1] == '\0' &&
168 value("interactive") != NULL
&& !lastlong
&&
169 (value("dot") != NULL
|| value("ignoreeof") != NULL
))
171 if (linebuf
[0] != escape
|| value("interactive") == NULL
||
173 if (putline(collf
, linebuf
, !longline
) < 0)
181 * On double escape, just send the single one.
182 * Otherwise, it's an error.
185 if (putline(collf
, &linebuf
[1], !longline
) < 0)
190 printf("Unknown tilde escape.\n");
200 * Shell escape, send the balance of the
208 * Escape to command mode, but be nice!
210 execute(&linebuf
[2], 1);
214 * Simulate end of file on input.
219 * Force a quit of sending mail.
220 * Act like an interrupt happened.
227 * Exit, do not save in dead.letter.
232 * Grab a bunch of headers.
234 grabh(hp
, GTO
|GSUBJECT
|GCC
|GBCC
);
238 * Add to the To list.
240 hp
->h_to
= cat(hp
->h_to
, extract(&linebuf
[2], GTO
));
244 * Set the Subject line.
247 while (isspace((unsigned char)*cp
))
249 hp
->h_subject
= savestr(cp
);
253 * Set the Reply-To line.
256 while (isspace((unsigned char)*cp
))
258 hp
->h_replyto
= savestr(cp
);
262 * Add to the CC list.
264 hp
->h_cc
= cat(hp
->h_cc
, extract(&linebuf
[2], GCC
));
268 * Add to the BCC list.
270 hp
->h_bcc
= cat(hp
->h_bcc
, extract(&linebuf
[2], GBCC
));
276 * Insert named variable in message.
281 while(isspace((unsigned char)*cp
))
294 if(*cp
!= '\0' && (cp
= value(cp
)) != NULL
) {
296 if(putline(collf
, cp
, 1) < 0)
303 * Read in the dead letter file.
305 if (strlcpy(linebuf
+ 2, getdeadletter(),
307 >= sizeof(linebuf
) - 2) {
308 printf("Line buffer overflow\n");
316 * Search for the file name,
317 * then open it and copy the contents to collf.
320 while (isspace((unsigned char)*cp
))
323 printf("Interpolate what file?\n");
331 * Insert stdout of command.
334 int nullfd
, tempfd
, rc
;
335 char tempname2
[PATHSIZE
];
337 if ((nullfd
= open("/dev/null", O_RDONLY
, 0))
343 snprintf(tempname2
, sizeof(tempname2
),
344 "%s/mail.ReXXXXXXXXXX", tmpdir
);
345 if ((tempfd
= mkstemp(tempname2
)) == -1 ||
346 (fbuf
= Fdopen(tempfd
, "w+")) == NULL
) {
347 warn("%s", tempname2
);
352 if ((sh
= value("SHELL")) == NULL
)
355 rc
= run_command(sh
, 0, nullfd
, fileno(fbuf
),
365 if (fsize(fbuf
) == 0) {
367 "No bytes from command \"%s\"\n",
374 } else if (isdir(cp
)) {
375 printf("%s: Directory\n", cp
);
377 } else if ((fbuf
= Fopen(cp
, "r")) == NULL
) {
381 printf("\"%s\" ", cp
);
385 while ((rc
= readline(fbuf
, linebuf
, LINESIZE
)) >= 0) {
386 if (rc
!= LINESIZE
- 1)
388 if ((t
= putline(collf
, linebuf
,
389 rc
!= LINESIZE
- 1)) < 0) {
396 printf("%d/%d\n", lc
, cc
);
400 * Write the message on a file.
403 while (*cp
== ' ' || *cp
== '\t')
406 fprintf(stderr
, "Write what file!?\n");
409 if ((cp
= expand(cp
)) == NULL
)
412 exwrite(cp
, collf
, 1);
419 * Interpolate the named messages, if we
420 * are in receiving mail mode. Does the
421 * standard list processing garbage.
422 * If ~f is given, we don't shift over.
424 if (forward(linebuf
+ 2, collf
, tempname
, c
) < 0)
428 if ((fbuf
= Fopen(_PATH_TILDE
, "r")) == NULL
) {
429 warn("%s", _PATH_TILDE
);
432 while ((t
= getc(fbuf
)) != EOF
)
438 * Print out the current state of the
439 * message without altering anything.
442 printf("-------\nMessage contains:\n");
443 puthead(hp
, stdout
, GTO
|GSUBJECT
|GCC
|GBCC
|GNL
);
444 while ((t
= getc(collf
)) != EOF
)
449 * Pipe message through command.
450 * Collect output as new message.
453 mespipe(collf
, &linebuf
[2]);
458 * Edit the current message.
459 * 'e' means to use EDITOR
460 * 'v' means to use VISUAL
477 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
478 signal(SIGINT
, saveint
);
479 signal(SIGHUP
, savehup
);
480 signal(SIGTSTP
, savetstp
);
481 signal(SIGTTOU
, savettou
);
482 signal(SIGTTIN
, savettin
);
483 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
488 * Write a file, ex-like if f set.
491 exwrite(char *name
, FILE *fp
, int f
)
499 printf("\"%s\" ", name
);
502 if (stat(name
, &junk
) >= 0 && S_ISREG(junk
.st_mode
)) {
504 fprintf(stderr
, "%s: ", name
);
505 fprintf(stderr
, "File exists\n");
508 if ((of
= Fopen(name
, "w")) == NULL
) {
514 while ((c
= getc(fp
)) != EOF
) {
526 printf("%d/%ld\n", lc
, cc
);
532 * Edit the message being collected on fp.
533 * On return, make the edit file the new temp file.
536 mesedit(FILE *fp
, int c
)
538 sig_t sigint
= signal(SIGINT
, SIG_IGN
);
539 FILE *nf
= run_editor(fp
, (off_t
)-1, c
, 0);
542 fseeko(nf
, (off_t
)0, SEEK_END
);
546 signal(SIGINT
, sigint
);
550 * Pipe the message through the command.
551 * Old message is on stdin of command;
552 * New message collected from stdout.
553 * Sh -c must return 0 to accept the new message.
556 mespipe(FILE *fp
, char *cmd
)
560 sig_t sigint
= signal(SIGINT
, SIG_IGN
);
561 char *sh
, tempname
[PATHSIZE
];
563 snprintf(tempname
, sizeof(tempname
), "%s/mail.ReXXXXXXXXXX", tmpdir
);
564 if ((fd
= mkstemp(tempname
)) == -1 ||
565 (nf
= Fdopen(fd
, "w+")) == NULL
) {
566 warn("%s", tempname
);
571 * stdin = current message.
572 * stdout = new message.
574 if ((sh
= value("SHELL")) == NULL
)
576 if (run_command(sh
, 0, fileno(fp
), fileno(nf
), "-c", cmd
, NULL
) < 0) {
580 if (fsize(nf
) == 0) {
581 fprintf(stderr
, "No bytes from \"%s\" !?\n", cmd
);
588 fseeko(nf
, (off_t
)0, SEEK_END
);
592 signal(SIGINT
, sigint
);
596 * Interpolate the named messages into the current
597 * message, preceding each line with a tab.
598 * Return a count of the number of characters now in
599 * the message, or -1 if an error is encountered writing
600 * the message temporary. The flag argument is 'm' if we
601 * should shift over and 'f' if not.
604 forward(char *ms
, FILE *fp
, char *fn
, int f
)
607 struct ignoretab
*ig
;
610 msgvec
= (int *)salloc((msgCount
+1) * sizeof(*msgvec
));
613 if (getmsglist(ms
, msgvec
, 0) < 0)
616 *msgvec
= first(0, MMNORM
);
618 printf("No appropriate messages\n");
623 if (f
== 'f' || f
== 'F')
625 else if ((tabst
= value("indentprefix")) == NULL
)
627 ig
= isupper((unsigned char)f
) ? NULL
: ignore
;
628 printf("Interpolating:");
629 for (; *msgvec
!= 0; msgvec
++) {
630 struct message
*mp
= message
+ *msgvec
- 1;
633 printf(" %d", *msgvec
);
634 if (sendmessage(mp
, fp
, ig
, tabst
) < 0) {
644 * Print (continue) when continued after ^Z.
650 sig_t old_action
= signal(s
, SIG_DFL
);
655 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
657 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
658 signal(s
, old_action
);
667 * On interrupt, come here to save the partial message in ~/dead.letter.
668 * Then jump out of the collection loop.
675 * the control flow is subtle, because we can be called from ~q.
678 if (value("ignore") != NULL
) {
688 if (value("nosave") == NULL
)
689 savedeadletter(collf
);
690 longjmp(collabort
, 1);
698 savedeadletter(collf
);
700 * Let's pretend nobody else wants to clean up,
701 * a true statement at this time.
707 savedeadletter(FILE *fp
)
715 cp
= getdeadletter();
717 dbuf
= Fopen(cp
, "a");
721 while ((c
= getc(fp
)) != EOF
)