3 * sendsbr.c -- routines to help WhatNow/Send along
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/signals.h>
18 #ifdef HAVE_SYS_TIME_H
19 # include <sys/time.h>
23 int debugsw
= 0; /* global */
31 char *altmsg
= NULL
; /* .. */
32 char *annotext
= NULL
;
33 char *distfile
= NULL
;
40 int sendsbr (char **, int, char *, char *, struct stat
*, int, char *, int);
41 char *getusername (void);
46 static void armed_done (int) NORETURN
;
47 static void alert (char *, int);
48 static int tmp_fd (void);
49 static void anno (int, struct stat
*);
50 static void annoaux (int);
51 static int splitmsg (char **, int, char *, char *, struct stat
*, int);
52 static int sendaux (char **, int, char *, char *, struct stat
*);
56 * Entry point into (back-end) routines to send message.
60 sendsbr (char **vec
, int vecp
, char *program
, char *drft
, struct stat
*st
,
61 int rename_drft
, char *attachment_header_field_name
, int attachformat
)
64 char buffer
[BUFSIZ
], file
[BUFSIZ
];
66 char *original_draft
; /* name of original draft file */
67 char *p
; /* string pointer for building file name */
68 char composition_file_name
[PATH_MAX
+ 1]; /* name of mhbuild composition temporary file */
69 char body_file_name
[PATH_MAX
+ 1]; /* name of temporary file for body content */
72 * Save the original name of the draft file. The name of the draft file is changed
73 * to a temporary file containing the built MIME message if there are attachments.
74 * We need the original name so that it can be renamed after the message is sent.
77 original_draft
= drft
;
80 * There might be attachments if a header field name for attachments is supplied.
81 * Convert the draft to a MIME message. Use the mhbuild composition file for the
82 * draft if there was a successful conversion because that now contains the MIME
83 * message. A nice side effect of this is that it leaves the original draft file
84 * untouched so that it can be retrieved and modified if desired.
87 if (attachment_header_field_name
!= (char *)0) {
88 switch (attach(attachment_header_field_name
, drft
,
89 body_file_name
, sizeof body_file_name
,
90 composition_file_name
, sizeof composition_file_name
,
93 drft
= composition_file_name
;
105 switch (setjmp (env
)) {
108 * If given -push and -unique (which is undocumented), then
109 * rename the draft file. I'm not quite sure why.
111 if (pushsw
&& unique
) {
112 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, NULL
);
114 adios(NULL
, "unable to create temporary file in %s",
117 if (rename (drft
, strncpy(file
, cp
, sizeof(file
))) == NOTOK
)
118 adios (file
, "unable to rename %s to", drft
);
123 * Check if we need to split the message into
124 * multiple messages of type "message/partial".
126 if (splitsw
>= 0 && !distfile
&& stat (drft
, &sts
) != NOTOK
127 && sts
.st_size
>= CPERMSG
) {
128 status
= splitmsg (vec
, vecp
, program
, drft
, st
, splitsw
) ? NOTOK
: OK
;
130 status
= sendaux (vec
, vecp
, program
, drft
, st
) ? NOTOK
: OK
;
133 /* rename the original draft */
134 if (rename_drft
&& status
== OK
&&
135 rename (original_draft
, strncpy (buffer
, m_backup (original_draft
), sizeof(buffer
))) == NOTOK
)
136 advise (buffer
, "unable to rename %s to", drft
);
149 * Get rid of any temporary files that we created for attachments. Also get rid of
150 * the renamed composition file that mhbuild leaves as a turd. It looks confusing,
151 * but we use the body file name to help build the renamed composition file name.
154 if (drft
== composition_file_name
) {
155 clean_up_temporary_files(body_file_name
, composition_file_name
);
157 if (strlen(composition_file_name
) >= sizeof (composition_file_name
) - 6)
158 advise((char *)0, "unable to remove original composition file.");
161 if ((p
= strrchr(composition_file_name
, '/')) == (char *)0)
162 p
= composition_file_name
;
166 (void)strcpy(body_file_name
, p
);
168 (void)strcpy(p
, body_file_name
);
169 (void)strcat(p
, ".orig");
171 (void)unlink(composition_file_name
);
179 * Split large message into several messages of
180 * type "message/partial" and send them.
184 splitmsg (char **vec
, int vecp
, char *program
, char *drft
,
185 struct stat
*st
, int delay
)
187 int compnum
, nparts
, partno
, state
, status
;
190 char *cp
, *dp
, buffer
[BUFSIZ
], msgid
[BUFSIZ
];
191 char subject
[BUFSIZ
];
192 char name
[NAMESZ
], partnum
[BUFSIZ
];
194 m_getfld_state_t gstate
= 0;
196 if ((in
= fopen (drft
, "r")) == NULL
)
197 adios (drft
, "unable to open for reading");
203 * Scan through the message and examine the various header fields,
204 * as well as locate the beginning of the message body.
206 m_getfld_track_filepos (&gstate
, in
);
207 for (compnum
= 1;;) {
208 int bufsz
= sizeof buffer
;
209 switch (state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
)) {
215 * This header field is discarded.
217 if (!strcasecmp (name
, "Message-ID")) {
218 while (state
== FLDPLUS
) {
219 bufsz
= sizeof buffer
;
220 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
222 } else if (uprf (name
, XXX_FIELD_PRF
)
223 || !strcasecmp (name
, VRSN_FIELD
)
224 || !strcasecmp (name
, "Subject")
225 || !strcasecmp (name
, "Encrypted")) {
227 * These header fields are copied to the enclosed
228 * header of the first message in the collection
229 * of message/partials. For the "Subject" header
230 * field, we also record it, so that a modified
231 * version of it, can be copied to the header
232 * of each messsage/partial in the collection.
234 if (!strcasecmp (name
, "Subject")) {
237 strncpy (subject
, buffer
, BUFSIZ
);
238 sublen
= strlen (subject
);
239 if (sublen
> 0 && subject
[sublen
- 1] == '\n')
240 subject
[sublen
- 1] = '\0';
243 dp
= add (concat (name
, ":", buffer
, NULL
), dp
);
244 while (state
== FLDPLUS
) {
245 bufsz
= sizeof buffer
;
246 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
247 dp
= add (buffer
, dp
);
251 * These header fields are copied to the header of
252 * each message/partial in the collection.
254 cp
= add (concat (name
, ":", buffer
, NULL
), cp
);
255 while (state
== FLDPLUS
) {
256 bufsz
= sizeof buffer
;
257 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
258 cp
= add (buffer
, cp
);
262 start
= ftell (in
) + 1;
271 adios (NULL
, "message format error in component #%d", compnum
);
274 adios (NULL
, "getfld () returned %d", state
);
279 m_getfld_state_destroy (&gstate
);
281 adios (NULL
, "headers missing from draft");
285 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
288 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
294 /* Only one part, nothing to split */
301 return sendaux (vec
, vecp
, program
, drft
, st
);
305 printf ("Sending as %d Partial Messages\n", nparts
);
310 vec
[vecp
++] = "-partno";
311 vec
[vecp
++] = partnum
;
313 vec
[vecp
++] = "-queued";
316 snprintf (msgid
, sizeof(msgid
), "%s", message_id (clock
, 0));
318 fseek (in
, start
, SEEK_SET
);
319 for (partno
= 1; partno
<= nparts
; partno
++) {
323 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, &out
);
325 adios(NULL
, "unable to create temporary file in %s",
328 strncpy(tmpdrf
, cp
, sizeof(tmpdrf
));
331 * Output the header fields
334 fprintf (out
, "Subject: %s (part %d of %d)\n", subject
, partno
, nparts
);
335 fprintf (out
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
336 fprintf (out
, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD
, msgid
);
337 fprintf (out
, "\tnumber=%d; total=%d\n", partno
, nparts
);
338 fprintf (out
, "%s: part %d of %d\n\n", DESCR_FIELD
, partno
, nparts
);
341 * If this is the first in the collection, output the
342 * header fields we are encapsulating at the beginning
343 * of the body of the first message.
348 fprintf (out
, "Message-ID: %s\n", msgid
);
356 if (!fgets (buffer
, sizeof(buffer
) - 1, in
)) {
357 if (partno
== nparts
)
359 adios (NULL
, "premature eof");
362 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
363 fseek (in
, -len
, SEEK_CUR
);
371 adios (tmpdrf
, "error writing to");
375 if (!pushsw
&& verbsw
) {
380 /* Pause here, if a delay is specified */
381 if (delay
> 0 && 1 < partno
&& partno
<= nparts
) {
383 printf ("pausing %d seconds before sending part %d...\n",
387 sleep ((unsigned int) delay
);
390 snprintf (partnum
, sizeof(partnum
), "%d", partno
);
391 status
= sendaux (vec
, vecp
, program
, tmpdrf
, st
);
397 * This is so sendaux will only annotate
398 * the altmsg the first time it is called.
407 fclose (in
); /* close the draft */
413 * Annotate original message, and
414 * call `postproc' (which is passed down in "program") to send message.
418 sendaux (char **vec
, int vecp
, char *program
, char *drft
, struct stat
*st
)
421 int i
, status
, fd
, fd2
;
422 char backup
[BUFSIZ
], buf
[BUFSIZ
];
424 fd
= pushsw
? tmp_fd () : NOTOK
;
429 if ((fd2
= tmp_fd ()) != NOTOK
) {
430 vec
[vecp
++] = "-idanno";
431 snprintf (buf
, sizeof(buf
), "%d", fd2
);
434 admonish (NULL
, "unable to create temporary file in %s "
435 "for annotation list", get_temp_dir());
438 if (distfile
&& distout (drft
, distfile
, backup
) == NOTOK
)
442 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
447 /* oops -- fork error */
448 adios ("fork", "unable to");
449 break; /* NOT REACHED */
453 * child process -- send it
455 * If fd is ok, then we are pushing and fd points to temp
456 * file, so capture anything on stdout and stderr there.
459 dup2 (fd
, fileno (stdout
));
460 dup2 (fd
, fileno (stderr
));
463 execvp (program
, vec
);
464 fprintf (stderr
, "unable to exec ");
470 * parent process -- wait for it
472 if ((status
= pidwait(child_id
, NOTOK
)) == OK
) {
473 if (annotext
&& fd2
!= NOTOK
)
477 * If postproc failed, and we have good fd (which means
478 * we pushed), then mail error message (and possibly the
479 * draft) back to the user.
485 advise (NULL
, "message not delivered to anyone");
487 if (annotext
&& fd2
!= NOTOK
)
491 if (rename (backup
, drft
) == NOTOK
)
492 advise (drft
, "unable to rename %s to", backup
);
503 * Mail error notification (and possibly a copy of the
504 * message) back to the user, using the mailproc
508 alert (char *file
, int out
)
516 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
521 /* oops -- fork error */
522 advise ("fork", "unable to");
525 /* child process -- send it */
526 SIGNAL (SIGHUP
, SIG_IGN
);
527 SIGNAL (SIGINT
, SIG_IGN
);
528 SIGNAL (SIGQUIT
, SIG_IGN
);
529 SIGNAL (SIGTERM
, SIG_IGN
);
531 if ((in
= open (file
, O_RDONLY
)) == NOTOK
) {
532 admonish (file
, "unable to re-open");
534 lseek (out
, (off_t
) 0, SEEK_END
);
535 strncpy (buf
, "\nMessage not delivered to anyone.\n", sizeof(buf
));
536 write (out
, buf
, strlen (buf
));
537 strncpy (buf
, "\n------- Unsent Draft\n\n", sizeof(buf
));
538 write (out
, buf
, strlen (buf
));
539 cpydgst (in
, out
, file
, "temporary file");
541 strncpy (buf
, "\n------- End of Unsent Draft\n", sizeof(buf
));
542 write (out
, buf
, strlen (buf
));
543 if (rename (file
, strncpy (buf
, m_backup (file
), sizeof(buf
))) == NOTOK
)
544 admonish (buf
, "unable to rename %s to", file
);
547 lseek (out
, (off_t
) 0, SEEK_SET
);
548 dup2 (out
, fileno (stdin
));
550 /* create subject for error notification */
551 snprintf (buf
, sizeof(buf
), "send failed on %s",
552 forwsw
? "enclosed draft" : file
);
554 arglist
= argsplit(mailproc
, &program
, &argp
);
556 arglist
[argp
++] = getusername();
557 arglist
[argp
++] = "-subject";
558 arglist
[argp
++] = buf
;
559 arglist
[argp
] = NULL
;
561 execvp (program
, arglist
);
562 fprintf (stderr
, "unable to exec ");
566 default: /* no waiting... */
578 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) return NOTOK
;
581 advise (NULL
, "temporary file %s selected", tfile
);
583 if (unlink (tfile
) == NOTOK
)
584 advise (tfile
, "unable to remove");
591 anno (int fd
, struct stat
*st
)
595 static char *cwd
= NULL
;
599 (stat (altmsg
, &st2
) == NOTOK
600 || st
->st_mtime
!= st2
.st_mtime
601 || st
->st_dev
!= st2
.st_dev
602 || st
->st_ino
!= st2
.st_ino
)) {
604 admonish (NULL
, "$mhaltmsg mismatch");
608 child_id
= debugsw
? NOTOK
: fork ();
610 case NOTOK
: /* oops */
613 "unable to fork, so doing annotations by hand...");
615 cwd
= getcpy (pwd ());
618 /* block a few signals */
620 sigaddset (&set
, SIGHUP
);
621 sigaddset (&set
, SIGINT
);
622 sigaddset (&set
, SIGQUIT
);
623 sigaddset (&set
, SIGTERM
);
624 sigprocmask (SIG_BLOCK
, &set
, &oset
);
630 /* reset the signal mask */
631 sigprocmask (SIG_SETMASK
, &oset
, &set
);
636 default: /* no waiting... */
646 int fd2
, fd3
, msgnum
;
647 char *cp
, *folder
, *maildir
;
648 char buffer
[BUFSIZ
], **ap
;
652 if ((folder
= getenv ("mhfolder")) == NULL
|| *folder
== 0) {
654 admonish (NULL
, "$mhfolder not set");
657 maildir
= m_maildir (folder
);
658 if (chdir (maildir
) == NOTOK
) {
660 admonish (maildir
, "unable to change directory to");
663 if (!(mp
= folder_read (folder
, 0))) {
665 admonish (NULL
, "unable to read folder %s", folder
);
669 /* check for empty folder */
670 if (mp
->nummsg
== 0) {
672 admonish (NULL
, "no messages in %s", folder
);
676 if ((cp
= getenv ("mhmessages")) == NULL
|| *cp
== 0) {
678 admonish (NULL
, "$mhmessages not set");
681 if (!debugsw
/* MOBY HACK... */
683 && (fd3
= open ("/dev/null", O_RDWR
)) != NOTOK
684 && (fd2
= dup (fileno (stderr
))) != NOTOK
) {
685 dup2 (fd3
, fileno (stderr
));
690 for (ap
= brkstring (cp
= getcpy (cp
), " ", NULL
); *ap
; ap
++)
694 dup2 (fd2
, fileno (stderr
));
695 if (mp
->numsel
== 0) {
697 admonish (NULL
, "no messages to annotate");
701 lseek (fd
, (off_t
) 0, SEEK_SET
);
702 if ((fp
= fdopen (fd
, "r")) == NULL
) {
704 admonish (NULL
, "unable to fdopen annotation list");
708 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
709 cp
= add (buffer
, cp
);
713 advise (NULL
, "annotate%s with %s: \"%s\"",
714 inplace
? " inplace" : "", annotext
, cp
);
715 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
716 if (is_selected(mp
, msgnum
)) {
718 advise (NULL
, "annotate message %d", msgnum
);
719 annotate (m_name (msgnum
), annotext
, cp
, inplace
, 1, -2, 0);
726 folder_free (mp
); /* free folder/message structure */
731 armed_done (int status
)
733 longjmp (env
, status
? status
: NOTOK
);