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>
19 #ifdef HAVE_SYS_TIME_H
20 # include <sys/time.h>
24 int debugsw
= 0; /* global */
32 char *altmsg
= NULL
; /* .. */
33 char *annotext
= NULL
;
34 char *distfile
= NULL
;
38 static char body_file_name
[PATH_MAX
+ 1]; /* name of temporary file for body content */
39 static char composition_file_name
[PATH_MAX
+ 1]; /* name of mhbuild composition temporary file */
40 static int field_size
; /* size of header field buffer */
41 static char *field
; /* header field buffer */
42 static FILE *draft_file
; /* draft file pointer */
43 static FILE *body_file
; /* body file pointer */
44 static FILE *composition_file
; /* composition file pointer */
49 int sendsbr (char **, int, char *, char *, struct stat
*, int, char *, int);
50 char *getusername (void);
55 static void armed_done (int) NORETURN
;
56 static void alert (char *, int);
57 static int tmp_fd (void);
58 static void anno (int, struct stat
*);
59 static void annoaux (int);
60 static int splitmsg (char **, int, char *, char *, struct stat
*, int);
61 static int sendaux (char **, int, char *, char *, struct stat
*);
63 static int attach(char *, char *, int);
64 static void clean_up_temporary_files(void);
65 static int get_line(void);
66 static void make_mime_composition_file_entry(char *, int, char *);
70 * Entry point into (back-end) routines to send message.
74 sendsbr (char **vec
, int vecp
, char *program
, char *drft
, struct stat
*st
,
75 int rename_drft
, char *attachment_header_field_name
, int attachformat
)
78 char buffer
[BUFSIZ
], file
[BUFSIZ
];
80 char *original_draft
; /* name of original draft file */
81 char *p
; /* string pointer for building file name */
84 * Save the original name of the draft file. The name of the draft file is changed
85 * to a temporary file containing the built MIME message if there are attachments.
86 * We need the original name so that it can be renamed after the message is sent.
89 original_draft
= drft
;
92 * There might be attachments if a header field name for attachments is supplied.
93 * Convert the draft to a MIME message. Use the mhbuild composition file for the
94 * draft if there was a successful conversion because that now contains the MIME
95 * message. A nice side effect of this is that it leaves the original draft file
96 * untouched so that it can be retrieved and modified if desired.
99 if (attachment_header_field_name
!= (char *)0) {
100 switch (attach(attachment_header_field_name
, drft
, attachformat
)) {
102 drft
= composition_file_name
;
114 switch (setjmp (env
)) {
117 * If given -push and -unique (which is undocumented), then
118 * rename the draft file. I'm not quite sure why.
120 if (pushsw
&& unique
) {
121 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, NULL
);
123 adios ("sendsbr", "unable to create temporary file");
125 if (rename (drft
, strncpy(file
, cp
, sizeof(file
))) == NOTOK
)
126 adios (file
, "unable to rename %s to", drft
);
131 * Check if we need to split the message into
132 * multiple messages of type "message/partial".
134 if (splitsw
>= 0 && !distfile
&& stat (drft
, &sts
) != NOTOK
135 && sts
.st_size
>= CPERMSG
) {
136 status
= splitmsg (vec
, vecp
, program
, drft
, st
, splitsw
) ? NOTOK
: OK
;
138 status
= sendaux (vec
, vecp
, program
, drft
, st
) ? NOTOK
: OK
;
141 /* rename the original draft */
142 if (rename_drft
&& status
== OK
&&
143 rename (original_draft
, strncpy (buffer
, m_backup (original_draft
), sizeof(buffer
))) == NOTOK
)
144 advise (buffer
, "unable to rename %s to", drft
);
157 * Get rid of any temporary files that we created for attachments. Also get rid of
158 * the renamed composition file that mhbuild leaves as a turd. It looks confusing,
159 * but we use the body file name to help build the renamed composition file name.
162 if (drft
== composition_file_name
) {
163 clean_up_temporary_files();
165 if (strlen(composition_file_name
) >= sizeof (composition_file_name
) - 6)
166 advise((char *)0, "unable to remove original composition file.");
169 if ((p
= strrchr(composition_file_name
, '/')) == (char *)0)
170 p
= composition_file_name
;
174 (void)strcpy(body_file_name
, p
);
176 (void)strcpy(p
, body_file_name
);
177 (void)strcat(p
, ".orig");
179 (void)unlink(composition_file_name
);
187 attach(char *attachment_header_field_name
, char *draft_file_name
,
190 char buf
[PATH_MAX
+ 6]; /* miscellaneous buffer */
191 int c
; /* current character for body copy */
192 int has_attachment
; /* draft has at least one attachment */
193 int has_body
; /* draft has a message body */
194 int length
; /* length of attachment header field name */
195 char *p
; /* miscellaneous string pointer */
196 FILE *fp
; /* pointer for mhn.defaults */
199 * Open up the draft file.
202 if ((draft_file
= fopen(draft_file_name
, "r")) == (FILE *)0)
203 adios((char *)0, "can't open draft file `%s'.", draft_file_name
);
206 * Allocate a buffer to hold the header components as they're read in.
207 * This buffer might need to be quite large, so we grow it as needed.
210 field
= (char *)mh_xmalloc(field_size
= 256);
213 * Scan the draft file for a header field name, with a non-empty
214 * body, that matches the -attach argument. The existence of one
215 * indicates that the draft has attachments. Bail out if there
216 * are no attachments because we're done. Read to the end of the
217 * headers even if we have no attachments.
220 length
= strlen(attachment_header_field_name
);
224 while (get_line() != EOF
&& *field
!= '\0' && *field
!= '-') {
225 if (strncasecmp(field
, attachment_header_field_name
, length
) == 0 &&
226 field
[length
] == ':') {
227 for (p
= field
+ length
+ 1; *p
== ' ' || *p
== '\t'; p
++)
229 if (strlen (p
) > 0) {
235 if (has_attachment
== 0)
239 * We have at least one attachment. Look for at least one non-blank line
240 * in the body of the message which indicates content in the body.
245 while (get_line() != EOF
) {
246 for (p
= field
; *p
!= '\0'; p
++) {
247 if (*p
!= ' ' && *p
!= '\t') {
258 * Make names for the temporary files.
261 (void)strncpy(body_file_name
,
262 m_mktemp(m_maildir(invo_name
), NULL
, NULL
),
263 sizeof (body_file_name
));
264 (void)strncpy(composition_file_name
,
265 m_mktemp(m_maildir(invo_name
), NULL
, NULL
),
266 sizeof (composition_file_name
));
269 body_file
= fopen(body_file_name
, "w");
271 composition_file
= fopen(composition_file_name
, "w");
273 if ((has_body
&& body_file
== (FILE *)0) || composition_file
== (FILE *)0) {
274 clean_up_temporary_files();
275 adios((char *)0, "unable to open all of the temporary files.");
279 * Start at the beginning of the draft file. Copy all
280 * non-attachment header fields to the temporary composition
281 * file. Then add the dashed line separator.
286 while (get_line() != EOF
&& *field
!= '\0' && *field
!= '-')
287 if (strncasecmp(field
, attachment_header_field_name
, length
) != 0 ||
288 field
[length
] != ':')
289 (void)fprintf(composition_file
, "%s\n", field
);
291 (void)fputs("--------\n", composition_file
);
294 * Copy the message body to a temporary file.
298 while ((c
= getc(draft_file
)) != EOF
)
301 (void)fclose(body_file
);
305 * Add a mhbuild MIME composition file line for the body if there was one.
306 * Set the default content type to text/plain so that mhbuild takes care
307 * of any necessary encoding.
311 make_mime_composition_file_entry(body_file_name
, attachformat
,
315 * Now, go back to the beginning of the draft file and look for
316 * header fields that specify attachments. Add a mhbuild MIME
317 * composition file for each.
320 if ((fp
= fopen (p
= etcpath ("mhn.defaults"), "r"))) {
321 readconfig ((struct node
**) NULL
, fp
, p
, 0);
327 while (get_line() != EOF
&& *field
!= '\0' && *field
!= '-') {
328 if (strncasecmp(field
, attachment_header_field_name
, length
) == 0 &&
329 field
[length
] == ':') {
330 for (p
= field
+ length
+ 1; *p
== ' ' || *p
== '\t'; p
++)
333 /* Skip empty attachment_header_field_name lines. */
334 if (strlen (p
) > 0) {
336 if (stat (p
, &st
) == OK
) {
337 if (S_ISREG (st
.st_mode
)) {
338 /* Don't set the default content type so take
339 make_mime_composition_file_entry() will try
340 to infer it from the file type. */
341 make_mime_composition_file_entry(p
, attachformat
, 0);
343 adios (NULL
, "unable to attach %s, not a plain file",
347 adios (NULL
, "unable to access file \"%s\"", p
);
353 (void)fclose(composition_file
);
356 * We're ready to roll! Run mhbuild on the composition file.
357 * Note that mhbuild is in the context as buildmimeproc.
360 (void)sprintf(buf
, "%s %s", buildmimeproc
, composition_file_name
);
362 if (system(buf
) != 0) {
363 clean_up_temporary_files();
371 clean_up_temporary_files(void)
373 (void)unlink(body_file_name
);
374 (void)unlink(composition_file_name
);
382 int c
; /* current character */
383 int n
; /* number of bytes in buffer */
384 char *p
; /* buffer pointer */
387 * Get a line from the input file, growing the field buffer as needed. We do this
388 * so that we can fit an entire line in the buffer making it easy to do a string
389 * comparison on both the field name and the field body which might be a long path
393 for (n
= 0, p
= field
; (c
= getc(draft_file
)) != EOF
; *p
++ = c
) {
394 if (c
== '\n' && (c
= getc(draft_file
)) != ' ' && c
!= '\t') {
395 (void)ungetc(c
, draft_file
);
400 if (++n
>= field_size
- 1) {
401 field
= (char *)mh_xrealloc((void *)field
, field_size
+= 256);
408 * NUL-terminate the field..
417 make_mime_composition_file_entry(char *file_name
, int attachformat
,
418 char *default_content_type
)
420 int binary
; /* binary character found flag */
421 int c
; /* current character */
422 char cmd
[PATH_MAX
+ 6]; /* file command buffer */
423 char *content_type
; /* mime content type */
424 FILE *fp
; /* content and pipe file pointer */
425 struct node
*np
; /* context scan node pointer */
426 char *p
; /* miscellaneous string pointer */
427 struct stat st
; /* file status buffer */
429 content_type
= default_content_type
;
432 * Check the file name for a suffix. Scan the context for that suffix on a
433 * mhshow-suffix- entry. We use these entries to be compatible with mhnshow,
434 * and there's no reason to make the user specify each suffix twice. Context
435 * entries of the form "mhshow-suffix-contenttype" in the name have the suffix
436 * in the field, including the dot.
439 if ((p
= strrchr(file_name
, '.')) != (char *)0) {
440 for (np
= m_defs
; np
; np
= np
->n_next
) {
441 if (strncasecmp(np
->n_name
, "mhshow-suffix-", 14) == 0 && mh_strcasecmp(p
, np
->n_field
) == 0) {
442 content_type
= np
->n_name
+ 14;
449 * No content type was found, either because there was no matching entry in the
450 * context or because the file name has no suffix. Open the file and check for
451 * non-ASCII characters. Choose the content type based on this check.
454 if (content_type
== (char *)0) {
455 if ((fp
= fopen(file_name
, "r")) == (FILE *)0) {
456 clean_up_temporary_files();
457 adios((char *)0, "unable to access file \"%s\"", file_name
);
462 while ((c
= getc(fp
)) != EOF
) {
463 if (c
> 127 || c
< 0) {
471 content_type
= binary
? "application/octet-stream" : "text/plain";
475 * Make sure that the attachment file exists and is readable. Append a mhbuild
476 * directive to the draft file. This starts with the content type. Append a
477 * file name attribute and a private x-unix-mode attribute. Also append a
478 * description obtained (if possible) by running the "file" command on the file.
481 if (stat(file_name
, &st
) == -1 || access(file_name
, R_OK
) != 0) {
482 clean_up_temporary_files();
483 adios((char *)0, "unable to access file \"%s\"", file_name
);
486 switch (attachformat
) {
488 /* Insert name, file mode, and Content-Id. */
489 (void)fprintf(composition_file
, "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
490 content_type
, ((p
= strrchr(file_name
, '/')) == (char *)0) ? file_name
: p
+ 1, (unsigned short)(st
.st_mode
& 0777));
492 if (strlen(file_name
) > PATH_MAX
) {
493 clean_up_temporary_files();
494 adios((char *)0, "attachment file name `%s' too long.", file_name
);
497 (void)sprintf(cmd
, "file '%s'", file_name
);
499 if ((fp
= popen(cmd
, "r")) != (FILE *)0 && fgets(cmd
, sizeof (cmd
), fp
) != (char *)0) {
500 *strchr(cmd
, '\n') = '\0';
503 * The output of the "file" command is of the form
507 * Strip off the "file:" and subsequent white space.
510 for (p
= cmd
; *p
!= '\0'; p
++) {
512 for (p
++; *p
!= '\0'; p
++) {
521 /* Insert Content-Description. */
522 (void)fprintf(composition_file
, " [ %s ]", p
);
529 if (stringdex (m_maildir(invo_name
), file_name
) == 0) {
530 /* Content had been placed by send into a temp file.
531 Don't generate Content-Disposition header, because
532 it confuses Microsoft Outlook, Build 10.0.6626, at
534 (void) fprintf (composition_file
, "#%s <>", content_type
);
536 /* Suppress Content-Id, insert simple Content-Disposition
537 and Content-Description with filename. */
538 p
= strrchr(file_name
, '/');
539 (void) fprintf (composition_file
,
540 "#%s; name=\"%s\" <> [%s]{attachment}",
542 (p
== (char *)0) ? file_name
: p
+ 1,
543 (p
== (char *)0) ? file_name
: p
+ 1);
548 if (stringdex (m_maildir(invo_name
), file_name
) == 0) {
549 /* Content had been placed by send into a temp file.
550 Don't generate Content-Disposition header, because
551 it confuses Microsoft Outlook, Build 10.0.6626, at
553 (void) fprintf (composition_file
, "#%s <>", content_type
);
555 /* Suppress Content-Id, insert Content-Disposition with
556 modification date and Content-Description wtih filename. */
557 p
= strrchr(file_name
, '/');
558 (void) fprintf (composition_file
,
559 "#%s; name=\"%s\" <>[%s]{attachment; modification-date=\"%s\"}",
561 (p
== (char *)0) ? file_name
: p
+ 1,
562 (p
== (char *)0) ? file_name
: p
+ 1,
563 dtime (&st
.st_mtime
, 0));
568 adios ((char *)0, "unsupported attachformat %d", attachformat
);
572 * Finish up with the file name.
575 (void)fprintf(composition_file
, " %s\n", file_name
);
581 * Split large message into several messages of
582 * type "message/partial" and send them.
586 splitmsg (char **vec
, int vecp
, char *program
, char *drft
,
587 struct stat
*st
, int delay
)
589 int compnum
, nparts
, partno
, state
, status
;
592 char *cp
, *dp
, buffer
[BUFSIZ
], msgid
[BUFSIZ
];
593 char subject
[BUFSIZ
];
594 char name
[NAMESZ
], partnum
[BUFSIZ
];
596 m_getfld_state_t gstate
= 0;
598 if ((in
= fopen (drft
, "r")) == NULL
)
599 adios (drft
, "unable to open for reading");
605 * Scan through the message and examine the various header fields,
606 * as well as locate the beginning of the message body.
608 m_getfld_track_filepos (&gstate
, in
);
609 for (compnum
= 1;;) {
610 int bufsz
= sizeof buffer
;
611 switch (state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
)) {
617 * This header field is discarded.
619 if (!mh_strcasecmp (name
, "Message-ID")) {
620 while (state
== FLDPLUS
) {
621 bufsz
= sizeof buffer
;
622 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
624 } else if (uprf (name
, XXX_FIELD_PRF
)
625 || !mh_strcasecmp (name
, VRSN_FIELD
)
626 || !mh_strcasecmp (name
, "Subject")
627 || !mh_strcasecmp (name
, "Encrypted")) {
629 * These header fields are copied to the enclosed
630 * header of the first message in the collection
631 * of message/partials. For the "Subject" header
632 * field, we also record it, so that a modified
633 * version of it, can be copied to the header
634 * of each messsage/partial in the collection.
636 if (!mh_strcasecmp (name
, "Subject")) {
639 strncpy (subject
, buffer
, BUFSIZ
);
640 sublen
= strlen (subject
);
641 if (sublen
> 0 && subject
[sublen
- 1] == '\n')
642 subject
[sublen
- 1] = '\0';
645 dp
= add (concat (name
, ":", buffer
, NULL
), dp
);
646 while (state
== FLDPLUS
) {
647 bufsz
= sizeof buffer
;
648 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
649 dp
= add (buffer
, dp
);
653 * These header fields are copied to the header of
654 * each message/partial in the collection.
656 cp
= add (concat (name
, ":", buffer
, NULL
), cp
);
657 while (state
== FLDPLUS
) {
658 bufsz
= sizeof buffer
;
659 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
660 cp
= add (buffer
, cp
);
664 start
= ftell (in
) + 1;
673 adios (NULL
, "message format error in component #%d", compnum
);
676 adios (NULL
, "getfld () returned %d", state
);
681 m_getfld_state_destroy (&gstate
);
683 adios (NULL
, "headers missing from draft");
687 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
690 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
696 /* Only one part, nothing to split */
703 return sendaux (vec
, vecp
, program
, drft
, st
);
707 printf ("Sending as %d Partial Messages\n", nparts
);
712 vec
[vecp
++] = "-partno";
713 vec
[vecp
++] = partnum
;
715 vec
[vecp
++] = "-queued";
718 snprintf (msgid
, sizeof(msgid
), "%s", message_id (clock
, 0));
720 fseek (in
, start
, SEEK_SET
);
721 for (partno
= 1; partno
<= nparts
; partno
++) {
725 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, &out
);
727 adios (drft
, "unable to create temporary file for");
729 strncpy(tmpdrf
, cp
, sizeof(tmpdrf
));
730 chmod (tmpdrf
, 0600);
733 * Output the header fields
736 fprintf (out
, "Subject: %s (part %d of %d)\n", subject
, partno
, nparts
);
737 fprintf (out
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
738 fprintf (out
, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD
, msgid
);
739 fprintf (out
, "\tnumber=%d; total=%d\n", partno
, nparts
);
740 fprintf (out
, "%s: part %d of %d\n\n", DESCR_FIELD
, partno
, nparts
);
743 * If this is the first in the collection, output the
744 * header fields we are encapsulating at the beginning
745 * of the body of the first message.
750 fprintf (out
, "Message-ID: %s\n", msgid
);
758 if (!fgets (buffer
, sizeof(buffer
) - 1, in
)) {
759 if (partno
== nparts
)
761 adios (NULL
, "premature eof");
764 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
765 fseek (in
, -len
, SEEK_CUR
);
773 adios (tmpdrf
, "error writing to");
777 if (!pushsw
&& verbsw
) {
782 /* Pause here, if a delay is specified */
783 if (delay
> 0 && 1 < partno
&& partno
<= nparts
) {
785 printf ("pausing %d seconds before sending part %d...\n",
789 sleep ((unsigned int) delay
);
792 snprintf (partnum
, sizeof(partnum
), "%d", partno
);
793 status
= sendaux (vec
, vecp
, program
, tmpdrf
, st
);
799 * This is so sendaux will only annotate
800 * the altmsg the first time it is called.
809 fclose (in
); /* close the draft */
815 * Annotate original message, and
816 * call `postproc' (which is passed down in "program") to send message.
820 sendaux (char **vec
, int vecp
, char *program
, char *drft
, struct stat
*st
)
823 int i
, status
, fd
, fd2
;
824 char backup
[BUFSIZ
], buf
[BUFSIZ
];
826 fd
= pushsw
? tmp_fd () : NOTOK
;
831 if ((fd2
= tmp_fd ()) != NOTOK
) {
832 vec
[vecp
++] = "-idanno";
833 snprintf (buf
, sizeof(buf
), "%d", fd2
);
836 admonish (NULL
, "unable to create file for annotation list");
839 if (distfile
&& distout (drft
, distfile
, backup
) == NOTOK
)
843 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
848 /* oops -- fork error */
849 adios ("fork", "unable to");
850 break; /* NOT REACHED */
854 * child process -- send it
856 * If fd is ok, then we are pushing and fd points to temp
857 * file, so capture anything on stdout and stderr there.
860 dup2 (fd
, fileno (stdout
));
861 dup2 (fd
, fileno (stderr
));
864 execvp (program
, vec
);
865 fprintf (stderr
, "unable to exec ");
868 break; /* NOT REACHED */
872 * parent process -- wait for it
874 if ((status
= pidwait(child_id
, NOTOK
)) == OK
) {
875 if (annotext
&& fd2
!= NOTOK
)
879 * If postproc failed, and we have good fd (which means
880 * we pushed), then mail error message (and possibly the
881 * draft) back to the user.
887 advise (NULL
, "message not delivered to anyone");
889 if (annotext
&& fd2
!= NOTOK
)
893 if (rename (backup
, drft
) == NOTOK
)
894 advise (drft
, "unable to rename %s to", backup
);
905 * Mail error notification (and possibly a copy of the
906 * message) back to the user, using the mailproc
910 alert (char *file
, int out
)
918 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
923 /* oops -- fork error */
924 advise ("fork", "unable to");
927 /* child process -- send it */
928 SIGNAL (SIGHUP
, SIG_IGN
);
929 SIGNAL (SIGINT
, SIG_IGN
);
930 SIGNAL (SIGQUIT
, SIG_IGN
);
931 SIGNAL (SIGTERM
, SIG_IGN
);
933 if ((in
= open (file
, O_RDONLY
)) == NOTOK
) {
934 admonish (file
, "unable to re-open");
936 lseek (out
, (off_t
) 0, SEEK_END
);
937 strncpy (buf
, "\nMessage not delivered to anyone.\n", sizeof(buf
));
938 write (out
, buf
, strlen (buf
));
939 strncpy (buf
, "\n------- Unsent Draft\n\n", sizeof(buf
));
940 write (out
, buf
, strlen (buf
));
941 cpydgst (in
, out
, file
, "temporary file");
943 strncpy (buf
, "\n------- End of Unsent Draft\n", sizeof(buf
));
944 write (out
, buf
, strlen (buf
));
945 if (rename (file
, strncpy (buf
, m_backup (file
), sizeof(buf
))) == NOTOK
)
946 admonish (buf
, "unable to rename %s to", file
);
949 lseek (out
, (off_t
) 0, SEEK_SET
);
950 dup2 (out
, fileno (stdin
));
952 /* create subject for error notification */
953 snprintf (buf
, sizeof(buf
), "send failed on %s",
954 forwsw
? "enclosed draft" : file
);
956 arglist
= argsplit(mailproc
, &program
, &argp
);
958 arglist
[argp
++] = getusername();
959 arglist
[argp
++] = "-subject";
960 arglist
[argp
++] = buf
;
961 arglist
[argp
] = NULL
;
963 execvp (program
, arglist
);
964 fprintf (stderr
, "unable to exec ");
968 default: /* no waiting... */
980 tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
);
981 if (tfile
== NULL
) return NOTOK
;
985 advise (NULL
, "temporary file %s selected", tfile
);
987 if (unlink (tfile
) == NOTOK
)
988 advise (tfile
, "unable to remove");
995 anno (int fd
, struct stat
*st
)
999 static char *cwd
= NULL
;
1003 (stat (altmsg
, &st2
) == NOTOK
1004 || st
->st_mtime
!= st2
.st_mtime
1005 || st
->st_dev
!= st2
.st_dev
1006 || st
->st_ino
!= st2
.st_ino
)) {
1008 admonish (NULL
, "$mhaltmsg mismatch");
1012 child_id
= debugsw
? NOTOK
: fork ();
1014 case NOTOK
: /* oops */
1017 "unable to fork, so doing annotations by hand...");
1019 cwd
= getcpy (pwd ());
1022 /* block a few signals */
1024 sigaddset (&set
, SIGHUP
);
1025 sigaddset (&set
, SIGINT
);
1026 sigaddset (&set
, SIGQUIT
);
1027 sigaddset (&set
, SIGTERM
);
1028 sigprocmask (SIG_BLOCK
, &set
, &oset
);
1034 /* reset the signal mask */
1035 sigprocmask (SIG_SETMASK
, &oset
, &set
);
1040 default: /* no waiting... */
1050 int fd2
, fd3
, msgnum
;
1051 char *cp
, *folder
, *maildir
;
1052 char buffer
[BUFSIZ
], **ap
;
1056 if ((folder
= getenv ("mhfolder")) == NULL
|| *folder
== 0) {
1058 admonish (NULL
, "$mhfolder not set");
1061 maildir
= m_maildir (folder
);
1062 if (chdir (maildir
) == NOTOK
) {
1064 admonish (maildir
, "unable to change directory to");
1067 if (!(mp
= folder_read (folder
))) {
1069 admonish (NULL
, "unable to read folder %s", folder
);
1073 /* check for empty folder */
1074 if (mp
->nummsg
== 0) {
1076 admonish (NULL
, "no messages in %s", folder
);
1080 if ((cp
= getenv ("mhmessages")) == NULL
|| *cp
== 0) {
1082 admonish (NULL
, "$mhmessages not set");
1085 if (!debugsw
/* MOBY HACK... */
1087 && (fd3
= open ("/dev/null", O_RDWR
)) != NOTOK
1088 && (fd2
= dup (fileno (stderr
))) != NOTOK
) {
1089 dup2 (fd3
, fileno (stderr
));
1094 for (ap
= brkstring (cp
= getcpy (cp
), " ", NULL
); *ap
; ap
++)
1095 m_convert (mp
, *ap
);
1098 dup2 (fd2
, fileno (stderr
));
1099 if (mp
->numsel
== 0) {
1101 admonish (NULL
, "no messages to annotate");
1105 lseek (fd
, (off_t
) 0, SEEK_SET
);
1106 if ((fp
= fdopen (fd
, "r")) == NULL
) {
1108 admonish (NULL
, "unable to fdopen annotation list");
1112 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
1113 cp
= add (buffer
, cp
);
1117 advise (NULL
, "annotate%s with %s: \"%s\"",
1118 inplace
? " inplace" : "", annotext
, cp
);
1119 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
1120 if (is_selected(mp
, msgnum
)) {
1122 advise (NULL
, "annotate message %d", msgnum
);
1123 annotate (m_name (msgnum
), annotext
, cp
, inplace
, 1, -2, 0);
1130 folder_free (mp
); /* free folder/message structure */
1135 armed_done (int status
)
1137 longjmp (env
, status
? status
: NOTOK
);