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 &&
442 strcasecmp(p
, np
->n_field
? np
->n_field
: "") == 0) {
443 content_type
= np
->n_name
+ 14;
450 * No content type was found, either because there was no matching entry in the
451 * context or because the file name has no suffix. Open the file and check for
452 * non-ASCII characters. Choose the content type based on this check.
455 if (content_type
== (char *)0) {
456 if ((fp
= fopen(file_name
, "r")) == (FILE *)0) {
457 clean_up_temporary_files();
458 adios((char *)0, "unable to access file \"%s\"", file_name
);
463 while ((c
= getc(fp
)) != EOF
) {
464 if (c
> 127 || c
< 0) {
472 content_type
= binary
? "application/octet-stream" : "text/plain";
476 * Make sure that the attachment file exists and is readable. Append a mhbuild
477 * directive to the draft file. This starts with the content type. Append a
478 * file name attribute and a private x-unix-mode attribute. Also append a
479 * description obtained (if possible) by running the "file" command on the file.
482 if (stat(file_name
, &st
) == -1 || access(file_name
, R_OK
) != 0) {
483 clean_up_temporary_files();
484 adios((char *)0, "unable to access file \"%s\"", file_name
);
487 switch (attachformat
) {
489 /* Insert name, file mode, and Content-Id. */
490 (void)fprintf(composition_file
, "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
491 content_type
, ((p
= strrchr(file_name
, '/')) == (char *)0) ? file_name
: p
+ 1, (unsigned short)(st
.st_mode
& 0777));
493 if (strlen(file_name
) > PATH_MAX
) {
494 clean_up_temporary_files();
495 adios((char *)0, "attachment file name `%s' too long.", file_name
);
498 (void)sprintf(cmd
, "file '%s'", file_name
);
500 if ((fp
= popen(cmd
, "r")) != (FILE *)0 && fgets(cmd
, sizeof (cmd
), fp
) != (char *)0) {
501 *strchr(cmd
, '\n') = '\0';
504 * The output of the "file" command is of the form
508 * Strip off the "file:" and subsequent white space.
511 for (p
= cmd
; *p
!= '\0'; p
++) {
513 for (p
++; *p
!= '\0'; p
++) {
522 /* Insert Content-Description. */
523 (void)fprintf(composition_file
, " [ %s ]", p
);
530 if (stringdex (m_maildir(invo_name
), file_name
) == 0) {
531 /* Content had been placed by send into a temp file.
532 Don't generate Content-Disposition header, because
533 it confuses Microsoft Outlook, Build 10.0.6626, at
535 (void) fprintf (composition_file
, "#%s <>", content_type
);
537 /* Suppress Content-Id, insert simple Content-Disposition
538 and Content-Description with filename. */
539 p
= strrchr(file_name
, '/');
540 (void) fprintf (composition_file
,
541 "#%s; name=\"%s\" <> [%s]{attachment}",
543 (p
== (char *)0) ? file_name
: p
+ 1,
544 (p
== (char *)0) ? file_name
: p
+ 1);
549 if (stringdex (m_maildir(invo_name
), file_name
) == 0) {
550 /* Content had been placed by send into a temp file.
551 Don't generate Content-Disposition header, because
552 it confuses Microsoft Outlook, Build 10.0.6626, at
554 (void) fprintf (composition_file
, "#%s <>", content_type
);
556 /* Suppress Content-Id, insert Content-Disposition with
557 modification date and Content-Description wtih filename. */
558 p
= strrchr(file_name
, '/');
559 (void) fprintf (composition_file
,
560 "#%s; name=\"%s\" <>[%s]{attachment; modification-date=\"%s\"}",
562 (p
== (char *)0) ? file_name
: p
+ 1,
563 (p
== (char *)0) ? file_name
: p
+ 1,
564 dtime (&st
.st_mtime
, 0));
569 adios ((char *)0, "unsupported attachformat %d", attachformat
);
573 * Finish up with the file name.
576 (void)fprintf(composition_file
, " %s\n", file_name
);
582 * Split large message into several messages of
583 * type "message/partial" and send them.
587 splitmsg (char **vec
, int vecp
, char *program
, char *drft
,
588 struct stat
*st
, int delay
)
590 int compnum
, nparts
, partno
, state
, status
;
593 char *cp
, *dp
, buffer
[BUFSIZ
], msgid
[BUFSIZ
];
594 char subject
[BUFSIZ
];
595 char name
[NAMESZ
], partnum
[BUFSIZ
];
597 m_getfld_state_t gstate
= 0;
599 if ((in
= fopen (drft
, "r")) == NULL
)
600 adios (drft
, "unable to open for reading");
606 * Scan through the message and examine the various header fields,
607 * as well as locate the beginning of the message body.
609 m_getfld_track_filepos (&gstate
, in
);
610 for (compnum
= 1;;) {
611 int bufsz
= sizeof buffer
;
612 switch (state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
)) {
618 * This header field is discarded.
620 if (!strcasecmp (name
, "Message-ID")) {
621 while (state
== FLDPLUS
) {
622 bufsz
= sizeof buffer
;
623 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
625 } else if (uprf (name
, XXX_FIELD_PRF
)
626 || !strcasecmp (name
, VRSN_FIELD
)
627 || !strcasecmp (name
, "Subject")
628 || !strcasecmp (name
, "Encrypted")) {
630 * These header fields are copied to the enclosed
631 * header of the first message in the collection
632 * of message/partials. For the "Subject" header
633 * field, we also record it, so that a modified
634 * version of it, can be copied to the header
635 * of each messsage/partial in the collection.
637 if (!strcasecmp (name
, "Subject")) {
640 strncpy (subject
, buffer
, BUFSIZ
);
641 sublen
= strlen (subject
);
642 if (sublen
> 0 && subject
[sublen
- 1] == '\n')
643 subject
[sublen
- 1] = '\0';
646 dp
= add (concat (name
, ":", buffer
, NULL
), dp
);
647 while (state
== FLDPLUS
) {
648 bufsz
= sizeof buffer
;
649 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
650 dp
= add (buffer
, dp
);
654 * These header fields are copied to the header of
655 * each message/partial in the collection.
657 cp
= add (concat (name
, ":", buffer
, NULL
), cp
);
658 while (state
== FLDPLUS
) {
659 bufsz
= sizeof buffer
;
660 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
661 cp
= add (buffer
, cp
);
665 start
= ftell (in
) + 1;
674 adios (NULL
, "message format error in component #%d", compnum
);
677 adios (NULL
, "getfld () returned %d", state
);
682 m_getfld_state_destroy (&gstate
);
684 adios (NULL
, "headers missing from draft");
688 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
691 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
697 /* Only one part, nothing to split */
704 return sendaux (vec
, vecp
, program
, drft
, st
);
708 printf ("Sending as %d Partial Messages\n", nparts
);
713 vec
[vecp
++] = "-partno";
714 vec
[vecp
++] = partnum
;
716 vec
[vecp
++] = "-queued";
719 snprintf (msgid
, sizeof(msgid
), "%s", message_id (clock
, 0));
721 fseek (in
, start
, SEEK_SET
);
722 for (partno
= 1; partno
<= nparts
; partno
++) {
726 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, &out
);
728 adios (drft
, "unable to create temporary file for");
730 strncpy(tmpdrf
, cp
, sizeof(tmpdrf
));
731 chmod (tmpdrf
, 0600);
734 * Output the header fields
737 fprintf (out
, "Subject: %s (part %d of %d)\n", subject
, partno
, nparts
);
738 fprintf (out
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
739 fprintf (out
, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD
, msgid
);
740 fprintf (out
, "\tnumber=%d; total=%d\n", partno
, nparts
);
741 fprintf (out
, "%s: part %d of %d\n\n", DESCR_FIELD
, partno
, nparts
);
744 * If this is the first in the collection, output the
745 * header fields we are encapsulating at the beginning
746 * of the body of the first message.
751 fprintf (out
, "Message-ID: %s\n", msgid
);
759 if (!fgets (buffer
, sizeof(buffer
) - 1, in
)) {
760 if (partno
== nparts
)
762 adios (NULL
, "premature eof");
765 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
766 fseek (in
, -len
, SEEK_CUR
);
774 adios (tmpdrf
, "error writing to");
778 if (!pushsw
&& verbsw
) {
783 /* Pause here, if a delay is specified */
784 if (delay
> 0 && 1 < partno
&& partno
<= nparts
) {
786 printf ("pausing %d seconds before sending part %d...\n",
790 sleep ((unsigned int) delay
);
793 snprintf (partnum
, sizeof(partnum
), "%d", partno
);
794 status
= sendaux (vec
, vecp
, program
, tmpdrf
, st
);
800 * This is so sendaux will only annotate
801 * the altmsg the first time it is called.
810 fclose (in
); /* close the draft */
816 * Annotate original message, and
817 * call `postproc' (which is passed down in "program") to send message.
821 sendaux (char **vec
, int vecp
, char *program
, char *drft
, struct stat
*st
)
824 int i
, status
, fd
, fd2
;
825 char backup
[BUFSIZ
], buf
[BUFSIZ
];
827 fd
= pushsw
? tmp_fd () : NOTOK
;
832 if ((fd2
= tmp_fd ()) != NOTOK
) {
833 vec
[vecp
++] = "-idanno";
834 snprintf (buf
, sizeof(buf
), "%d", fd2
);
837 admonish (NULL
, "unable to create file for annotation list");
840 if (distfile
&& distout (drft
, distfile
, backup
) == NOTOK
)
844 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
849 /* oops -- fork error */
850 adios ("fork", "unable to");
851 break; /* NOT REACHED */
855 * child process -- send it
857 * If fd is ok, then we are pushing and fd points to temp
858 * file, so capture anything on stdout and stderr there.
861 dup2 (fd
, fileno (stdout
));
862 dup2 (fd
, fileno (stderr
));
865 execvp (program
, vec
);
866 fprintf (stderr
, "unable to exec ");
869 break; /* NOT REACHED */
873 * parent process -- wait for it
875 if ((status
= pidwait(child_id
, NOTOK
)) == OK
) {
876 if (annotext
&& fd2
!= NOTOK
)
880 * If postproc failed, and we have good fd (which means
881 * we pushed), then mail error message (and possibly the
882 * draft) back to the user.
888 advise (NULL
, "message not delivered to anyone");
890 if (annotext
&& fd2
!= NOTOK
)
894 if (rename (backup
, drft
) == NOTOK
)
895 advise (drft
, "unable to rename %s to", backup
);
906 * Mail error notification (and possibly a copy of the
907 * message) back to the user, using the mailproc
911 alert (char *file
, int out
)
919 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
924 /* oops -- fork error */
925 advise ("fork", "unable to");
928 /* child process -- send it */
929 SIGNAL (SIGHUP
, SIG_IGN
);
930 SIGNAL (SIGINT
, SIG_IGN
);
931 SIGNAL (SIGQUIT
, SIG_IGN
);
932 SIGNAL (SIGTERM
, SIG_IGN
);
934 if ((in
= open (file
, O_RDONLY
)) == NOTOK
) {
935 admonish (file
, "unable to re-open");
937 lseek (out
, (off_t
) 0, SEEK_END
);
938 strncpy (buf
, "\nMessage not delivered to anyone.\n", sizeof(buf
));
939 write (out
, buf
, strlen (buf
));
940 strncpy (buf
, "\n------- Unsent Draft\n\n", sizeof(buf
));
941 write (out
, buf
, strlen (buf
));
942 cpydgst (in
, out
, file
, "temporary file");
944 strncpy (buf
, "\n------- End of Unsent Draft\n", sizeof(buf
));
945 write (out
, buf
, strlen (buf
));
946 if (rename (file
, strncpy (buf
, m_backup (file
), sizeof(buf
))) == NOTOK
)
947 admonish (buf
, "unable to rename %s to", file
);
950 lseek (out
, (off_t
) 0, SEEK_SET
);
951 dup2 (out
, fileno (stdin
));
953 /* create subject for error notification */
954 snprintf (buf
, sizeof(buf
), "send failed on %s",
955 forwsw
? "enclosed draft" : file
);
957 arglist
= argsplit(mailproc
, &program
, &argp
);
959 arglist
[argp
++] = getusername();
960 arglist
[argp
++] = "-subject";
961 arglist
[argp
++] = buf
;
962 arglist
[argp
] = NULL
;
964 execvp (program
, arglist
);
965 fprintf (stderr
, "unable to exec ");
969 default: /* no waiting... */
981 tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
);
982 if (tfile
== NULL
) return NOTOK
;
986 advise (NULL
, "temporary file %s selected", tfile
);
988 if (unlink (tfile
) == NOTOK
)
989 advise (tfile
, "unable to remove");
996 anno (int fd
, struct stat
*st
)
1000 static char *cwd
= NULL
;
1004 (stat (altmsg
, &st2
) == NOTOK
1005 || st
->st_mtime
!= st2
.st_mtime
1006 || st
->st_dev
!= st2
.st_dev
1007 || st
->st_ino
!= st2
.st_ino
)) {
1009 admonish (NULL
, "$mhaltmsg mismatch");
1013 child_id
= debugsw
? NOTOK
: fork ();
1015 case NOTOK
: /* oops */
1018 "unable to fork, so doing annotations by hand...");
1020 cwd
= getcpy (pwd ());
1023 /* block a few signals */
1025 sigaddset (&set
, SIGHUP
);
1026 sigaddset (&set
, SIGINT
);
1027 sigaddset (&set
, SIGQUIT
);
1028 sigaddset (&set
, SIGTERM
);
1029 sigprocmask (SIG_BLOCK
, &set
, &oset
);
1035 /* reset the signal mask */
1036 sigprocmask (SIG_SETMASK
, &oset
, &set
);
1041 default: /* no waiting... */
1051 int fd2
, fd3
, msgnum
;
1052 char *cp
, *folder
, *maildir
;
1053 char buffer
[BUFSIZ
], **ap
;
1057 if ((folder
= getenv ("mhfolder")) == NULL
|| *folder
== 0) {
1059 admonish (NULL
, "$mhfolder not set");
1062 maildir
= m_maildir (folder
);
1063 if (chdir (maildir
) == NOTOK
) {
1065 admonish (maildir
, "unable to change directory to");
1068 if (!(mp
= folder_read (folder
, 0))) {
1070 admonish (NULL
, "unable to read folder %s", folder
);
1074 /* check for empty folder */
1075 if (mp
->nummsg
== 0) {
1077 admonish (NULL
, "no messages in %s", folder
);
1081 if ((cp
= getenv ("mhmessages")) == NULL
|| *cp
== 0) {
1083 admonish (NULL
, "$mhmessages not set");
1086 if (!debugsw
/* MOBY HACK... */
1088 && (fd3
= open ("/dev/null", O_RDWR
)) != NOTOK
1089 && (fd2
= dup (fileno (stderr
))) != NOTOK
) {
1090 dup2 (fd3
, fileno (stderr
));
1095 for (ap
= brkstring (cp
= getcpy (cp
), " ", NULL
); *ap
; ap
++)
1096 m_convert (mp
, *ap
);
1099 dup2 (fd2
, fileno (stderr
));
1100 if (mp
->numsel
== 0) {
1102 admonish (NULL
, "no messages to annotate");
1106 lseek (fd
, (off_t
) 0, SEEK_SET
);
1107 if ((fp
= fdopen (fd
, "r")) == NULL
) {
1109 admonish (NULL
, "unable to fdopen annotation list");
1113 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
1114 cp
= add (buffer
, cp
);
1118 advise (NULL
, "annotate%s with %s: \"%s\"",
1119 inplace
? " inplace" : "", annotext
, cp
);
1120 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
1121 if (is_selected(mp
, msgnum
)) {
1123 advise (NULL
, "annotate message %d", msgnum
);
1124 annotate (m_name (msgnum
), annotext
, cp
, inplace
, 1, -2, 0);
1131 folder_free (mp
); /* free folder/message structure */
1136 armed_done (int status
)
1138 longjmp (env
, status
? status
: NOTOK
);