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 ("sendsbr", "unable to create temporary file");
116 if (rename (drft
, strncpy(file
, cp
, sizeof(file
))) == NOTOK
)
117 adios (file
, "unable to rename %s to", drft
);
122 * Check if we need to split the message into
123 * multiple messages of type "message/partial".
125 if (splitsw
>= 0 && !distfile
&& stat (drft
, &sts
) != NOTOK
126 && sts
.st_size
>= CPERMSG
) {
127 status
= splitmsg (vec
, vecp
, program
, drft
, st
, splitsw
) ? NOTOK
: OK
;
129 status
= sendaux (vec
, vecp
, program
, drft
, st
) ? NOTOK
: OK
;
132 /* rename the original draft */
133 if (rename_drft
&& status
== OK
&&
134 rename (original_draft
, strncpy (buffer
, m_backup (original_draft
), sizeof(buffer
))) == NOTOK
)
135 advise (buffer
, "unable to rename %s to", drft
);
148 * Get rid of any temporary files that we created for attachments. Also get rid of
149 * the renamed composition file that mhbuild leaves as a turd. It looks confusing,
150 * but we use the body file name to help build the renamed composition file name.
153 if (drft
== composition_file_name
) {
154 clean_up_temporary_files(body_file_name
, composition_file_name
);
156 if (strlen(composition_file_name
) >= sizeof (composition_file_name
) - 6)
157 advise((char *)0, "unable to remove original composition file.");
160 if ((p
= strrchr(composition_file_name
, '/')) == (char *)0)
161 p
= composition_file_name
;
165 (void)strcpy(body_file_name
, p
);
167 (void)strcpy(p
, body_file_name
);
168 (void)strcat(p
, ".orig");
170 (void)unlink(composition_file_name
);
178 * Split large message into several messages of
179 * type "message/partial" and send them.
183 splitmsg (char **vec
, int vecp
, char *program
, char *drft
,
184 struct stat
*st
, int delay
)
186 int compnum
, nparts
, partno
, state
, status
;
189 char *cp
, *dp
, buffer
[BUFSIZ
], msgid
[BUFSIZ
];
190 char subject
[BUFSIZ
];
191 char name
[NAMESZ
], partnum
[BUFSIZ
];
193 m_getfld_state_t gstate
= 0;
195 if ((in
= fopen (drft
, "r")) == NULL
)
196 adios (drft
, "unable to open for reading");
202 * Scan through the message and examine the various header fields,
203 * as well as locate the beginning of the message body.
205 m_getfld_track_filepos (&gstate
, in
);
206 for (compnum
= 1;;) {
207 int bufsz
= sizeof buffer
;
208 switch (state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
)) {
214 * This header field is discarded.
216 if (!strcasecmp (name
, "Message-ID")) {
217 while (state
== FLDPLUS
) {
218 bufsz
= sizeof buffer
;
219 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
221 } else if (uprf (name
, XXX_FIELD_PRF
)
222 || !strcasecmp (name
, VRSN_FIELD
)
223 || !strcasecmp (name
, "Subject")
224 || !strcasecmp (name
, "Encrypted")) {
226 * These header fields are copied to the enclosed
227 * header of the first message in the collection
228 * of message/partials. For the "Subject" header
229 * field, we also record it, so that a modified
230 * version of it, can be copied to the header
231 * of each messsage/partial in the collection.
233 if (!strcasecmp (name
, "Subject")) {
236 strncpy (subject
, buffer
, BUFSIZ
);
237 sublen
= strlen (subject
);
238 if (sublen
> 0 && subject
[sublen
- 1] == '\n')
239 subject
[sublen
- 1] = '\0';
242 dp
= add (concat (name
, ":", buffer
, NULL
), dp
);
243 while (state
== FLDPLUS
) {
244 bufsz
= sizeof buffer
;
245 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
246 dp
= add (buffer
, dp
);
250 * These header fields are copied to the header of
251 * each message/partial in the collection.
253 cp
= add (concat (name
, ":", buffer
, NULL
), cp
);
254 while (state
== FLDPLUS
) {
255 bufsz
= sizeof buffer
;
256 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
257 cp
= add (buffer
, cp
);
261 start
= ftell (in
) + 1;
270 adios (NULL
, "message format error in component #%d", compnum
);
273 adios (NULL
, "getfld () returned %d", state
);
278 m_getfld_state_destroy (&gstate
);
280 adios (NULL
, "headers missing from draft");
284 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
287 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
293 /* Only one part, nothing to split */
300 return sendaux (vec
, vecp
, program
, drft
, st
);
304 printf ("Sending as %d Partial Messages\n", nparts
);
309 vec
[vecp
++] = "-partno";
310 vec
[vecp
++] = partnum
;
312 vec
[vecp
++] = "-queued";
315 snprintf (msgid
, sizeof(msgid
), "%s", message_id (clock
, 0));
317 fseek (in
, start
, SEEK_SET
);
318 for (partno
= 1; partno
<= nparts
; partno
++) {
322 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, &out
);
324 adios (drft
, "unable to create temporary file for");
326 strncpy(tmpdrf
, cp
, sizeof(tmpdrf
));
327 chmod (tmpdrf
, 0600);
330 * Output the header fields
333 fprintf (out
, "Subject: %s (part %d of %d)\n", subject
, partno
, nparts
);
334 fprintf (out
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
335 fprintf (out
, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD
, msgid
);
336 fprintf (out
, "\tnumber=%d; total=%d\n", partno
, nparts
);
337 fprintf (out
, "%s: part %d of %d\n\n", DESCR_FIELD
, partno
, nparts
);
340 * If this is the first in the collection, output the
341 * header fields we are encapsulating at the beginning
342 * of the body of the first message.
347 fprintf (out
, "Message-ID: %s\n", msgid
);
355 if (!fgets (buffer
, sizeof(buffer
) - 1, in
)) {
356 if (partno
== nparts
)
358 adios (NULL
, "premature eof");
361 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
362 fseek (in
, -len
, SEEK_CUR
);
370 adios (tmpdrf
, "error writing to");
374 if (!pushsw
&& verbsw
) {
379 /* Pause here, if a delay is specified */
380 if (delay
> 0 && 1 < partno
&& partno
<= nparts
) {
382 printf ("pausing %d seconds before sending part %d...\n",
386 sleep ((unsigned int) delay
);
389 snprintf (partnum
, sizeof(partnum
), "%d", partno
);
390 status
= sendaux (vec
, vecp
, program
, tmpdrf
, st
);
396 * This is so sendaux will only annotate
397 * the altmsg the first time it is called.
406 fclose (in
); /* close the draft */
412 * Annotate original message, and
413 * call `postproc' (which is passed down in "program") to send message.
417 sendaux (char **vec
, int vecp
, char *program
, char *drft
, struct stat
*st
)
420 int i
, status
, fd
, fd2
;
421 char backup
[BUFSIZ
], buf
[BUFSIZ
];
423 fd
= pushsw
? tmp_fd () : NOTOK
;
428 if ((fd2
= tmp_fd ()) != NOTOK
) {
429 vec
[vecp
++] = "-idanno";
430 snprintf (buf
, sizeof(buf
), "%d", fd2
);
433 admonish (NULL
, "unable to create file for annotation list");
436 if (distfile
&& distout (drft
, distfile
, backup
) == NOTOK
)
440 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
445 /* oops -- fork error */
446 adios ("fork", "unable to");
447 break; /* NOT REACHED */
451 * child process -- send it
453 * If fd is ok, then we are pushing and fd points to temp
454 * file, so capture anything on stdout and stderr there.
457 dup2 (fd
, fileno (stdout
));
458 dup2 (fd
, fileno (stderr
));
461 execvp (program
, vec
);
462 fprintf (stderr
, "unable to exec ");
468 * parent process -- wait for it
470 if ((status
= pidwait(child_id
, NOTOK
)) == OK
) {
471 if (annotext
&& fd2
!= NOTOK
)
475 * If postproc failed, and we have good fd (which means
476 * we pushed), then mail error message (and possibly the
477 * draft) back to the user.
483 advise (NULL
, "message not delivered to anyone");
485 if (annotext
&& fd2
!= NOTOK
)
489 if (rename (backup
, drft
) == NOTOK
)
490 advise (drft
, "unable to rename %s to", backup
);
501 * Mail error notification (and possibly a copy of the
502 * message) back to the user, using the mailproc
506 alert (char *file
, int out
)
514 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
519 /* oops -- fork error */
520 advise ("fork", "unable to");
523 /* child process -- send it */
524 SIGNAL (SIGHUP
, SIG_IGN
);
525 SIGNAL (SIGINT
, SIG_IGN
);
526 SIGNAL (SIGQUIT
, SIG_IGN
);
527 SIGNAL (SIGTERM
, SIG_IGN
);
529 if ((in
= open (file
, O_RDONLY
)) == NOTOK
) {
530 admonish (file
, "unable to re-open");
532 lseek (out
, (off_t
) 0, SEEK_END
);
533 strncpy (buf
, "\nMessage not delivered to anyone.\n", sizeof(buf
));
534 write (out
, buf
, strlen (buf
));
535 strncpy (buf
, "\n------- Unsent Draft\n\n", sizeof(buf
));
536 write (out
, buf
, strlen (buf
));
537 cpydgst (in
, out
, file
, "temporary file");
539 strncpy (buf
, "\n------- End of Unsent Draft\n", sizeof(buf
));
540 write (out
, buf
, strlen (buf
));
541 if (rename (file
, strncpy (buf
, m_backup (file
), sizeof(buf
))) == NOTOK
)
542 admonish (buf
, "unable to rename %s to", file
);
545 lseek (out
, (off_t
) 0, SEEK_SET
);
546 dup2 (out
, fileno (stdin
));
548 /* create subject for error notification */
549 snprintf (buf
, sizeof(buf
), "send failed on %s",
550 forwsw
? "enclosed draft" : file
);
552 arglist
= argsplit(mailproc
, &program
, &argp
);
554 arglist
[argp
++] = getusername();
555 arglist
[argp
++] = "-subject";
556 arglist
[argp
++] = buf
;
557 arglist
[argp
] = NULL
;
559 execvp (program
, arglist
);
560 fprintf (stderr
, "unable to exec ");
564 default: /* no waiting... */
576 tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
);
577 if (tfile
== 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
);