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
;
37 static char body_file_name
[PATH_MAX
+ 1]; /* name of temporary file for body content */
38 static char composition_file_name
[PATH_MAX
+ 1]; /* name of mhbuild composition temporary file */
39 static int field_size
; /* size of header field buffer */
40 static char *field
; /* header field buffer */
41 static FILE *draft_file
; /* draft file pointer */
42 static FILE *body_file
; /* body file pointer */
43 static FILE *composition_file
; /* composition file pointer */
48 int sendsbr (char **, int, char *, char *, struct stat
*, int, char *, int);
49 char *getusername (void);
54 static void armed_done (int) NORETURN
;
55 static void alert (char *, int);
56 static int tmp_fd (void);
57 static void anno (int, struct stat
*);
58 static void annoaux (int);
59 static int splitmsg (char **, int, char *, char *, struct stat
*, int);
60 static int sendaux (char **, int, char *, char *, struct stat
*);
62 static int attach(char *, char *, int);
63 static void clean_up_temporary_files(void);
64 static int get_line(void);
65 static void make_mime_composition_file_entry(char *, int, char *);
66 static char *mime_type (const 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
+ 8]; /* 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 if ((content_type
= mime_type (file_name
)) == NULL
) {
431 * Check the file name for a suffix. Scan the context for
432 * that suffix on a mhshow-suffix- entry. We use these
433 * entries to be compatible with mhnshow, and there's no
434 * reason to make the user specify each suffix twice. Context
435 * entries of the form "mhshow-suffix-contenttype" in the name
436 * have the suffix in the field, including the dot.
438 if ((p
= strrchr(file_name
, '.')) != (char *)0) {
439 for (np
= m_defs
; np
; np
= np
->n_next
) {
440 if (strncasecmp(np
->n_name
, "mhshow-suffix-", 14) == 0 &&
441 strcasecmp(p
, np
->n_field
? np
->n_field
: "") == 0) {
442 content_type
= strdup (np
->n_name
+ 14);
448 if (content_type
== NULL
&& default_content_type
!= NULL
) {
449 content_type
= strdup (default_content_type
);
454 * No content type was found, either because there was no matching entry in the
455 * context or because the file name has no suffix. Open the file and check for
456 * non-ASCII characters. Choose the content type based on this check.
459 if (content_type
== (char *)0) {
460 if ((fp
= fopen(file_name
, "r")) == (FILE *)0) {
461 clean_up_temporary_files();
462 adios((char *)0, "unable to access file \"%s\"", file_name
);
467 while ((c
= getc(fp
)) != EOF
) {
468 if (c
> 127 || c
< 0) {
477 strdup (binary
? "application/octet-stream" : "text/plain");
481 * Make sure that the attachment file exists and is readable. Append a mhbuild
482 * directive to the draft file. This starts with the content type. Append a
483 * file name attribute and a private x-unix-mode attribute. Also append a
484 * description obtained (if possible) by running the "file" command on the file.
487 if (stat(file_name
, &st
) == -1 || access(file_name
, R_OK
) != 0) {
488 clean_up_temporary_files();
489 adios((char *)0, "unable to access file \"%s\"", file_name
);
492 switch (attachformat
) {
494 /* Insert name, file mode, and Content-Id. */
495 (void)fprintf(composition_file
, "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
496 content_type
, ((p
= strrchr(file_name
, '/')) == (char *)0) ? file_name
: p
+ 1, (unsigned short)(st
.st_mode
& 0777));
498 if (strlen(file_name
) > PATH_MAX
) {
499 clean_up_temporary_files();
500 adios((char *)0, "attachment file name `%s' too long.", file_name
);
503 (void)sprintf(cmd
, "file '%s'", file_name
);
505 if ((fp
= popen(cmd
, "r")) != (FILE *)0 && fgets(cmd
, sizeof (cmd
), fp
) != (char *)0) {
506 *strchr(cmd
, '\n') = '\0';
509 * The output of the "file" command is of the form
513 * Strip off the "file:" and subsequent white space.
516 for (p
= cmd
; *p
!= '\0'; p
++) {
518 for (p
++; *p
!= '\0'; p
++) {
527 /* Insert Content-Description. */
528 (void)fprintf(composition_file
, " [ %s ]", p
);
535 if (stringdex (m_maildir(invo_name
), file_name
) == 0) {
536 /* Content had been placed by send into a temp file.
537 Don't generate Content-Disposition header, because
538 it confuses Microsoft Outlook, Build 10.0.6626, at
540 (void) fprintf (composition_file
, "#%s <>", content_type
);
542 /* Suppress Content-Id, insert simple Content-Disposition
543 and Content-Description with filename.
544 The Content-Disposition type needs to be "inline" for
545 MS Outlook and BlackBerry calendar programs to properly
546 handle a text/calendar attachment. */
547 p
= strrchr(file_name
, '/');
548 (void) fprintf (composition_file
,
549 "#%s; name=\"%s\" <> [%s]{%s}",
551 (p
== (char *)0) ? file_name
: p
+ 1,
552 (p
== (char *)0) ? file_name
: p
+ 1,
553 strcmp ("text/calendar", content_type
)
554 ? "attachment" : "inline");
559 if (stringdex (m_maildir(invo_name
), file_name
) == 0) {
560 /* Content had been placed by send into a temp file.
561 Don't generate Content-Disposition header, because
562 it confuses Microsoft Outlook, Build 10.0.6626, at
564 (void) fprintf (composition_file
, "#%s <>", content_type
);
566 /* Suppress Content-Id, insert Content-Disposition with
567 modification date and Content-Description wtih filename.
568 The Content-Disposition type needs to be "inline" for
569 MS Outlook and BlackBerry calendar programs to properly
570 handle a text/calendar attachment. */
571 p
= strrchr(file_name
, '/');
572 (void) fprintf (composition_file
,
573 "#%s; name=\"%s\" <>[%s]{%s; "
574 "modification-date=\"%s\"}",
576 (p
== (char *)0) ? file_name
: p
+ 1,
577 (p
== (char *)0) ? file_name
: p
+ 1,
578 strcmp ("text/calendar", content_type
)
579 ? "attachment" : "inline",
580 dtime (&st
.st_mtime
, 0));
585 adios ((char *)0, "unsupported attachformat %d", attachformat
);
591 * Finish up with the file name.
594 (void)fprintf(composition_file
, " %s\n", file_name
);
600 * Try to use external command to determine mime type, and possibly
601 * encoding. Caller is responsible for free'ing returned memory.
604 mime_type (const char *file_name
) {
605 char *content_type
= NULL
; /* mime content type */
608 char cmd
[2 * PATH_MAX
+ 2]; /* file command buffer */
609 char buf
[BUFSIZ
>= 2048 ? BUFSIZ
: 2048];
610 FILE *fp
; /* content and pipe file pointer */
611 char mimetypeproc
[] = MIMETYPEPROC
" '%s'";
613 if ((int) snprintf (cmd
, sizeof cmd
, mimetypeproc
, file_name
) <
615 if ((fp
= popen (cmd
, "r")) != NULL
) {
616 /* Make sure that buf has space for one additional
617 character, the semicolon that might be added below. */
618 if (fgets (buf
, sizeof buf
- 1, fp
)) {
621 /* Skip leading <filename>:<whitespace>, if present. */
622 if ((content_type
= strchr (buf
, ':')) != NULL
) {
624 while (*content_type
&& isblank (*content_type
)) {
631 /* Truncate at newline (LF or CR), if present. */
632 if ((cp
= strpbrk (content_type
, "\n\n")) != NULL
) {
636 /* If necessary, insert semicolon between content type
637 and charset. Assume that the first space is between
639 if ((space
= strchr (content_type
, ' ')) != NULL
) {
640 ssize_t len
= strlen (content_type
);
642 if (space
- content_type
> 0 &&
643 len
> space
- content_type
+ 1) {
644 if (*(space
- 1) != ';') {
645 /* The +1 is for the terminating NULL. */
646 memmove (space
+ 1, space
,
647 len
- (space
- content_type
) + 1);
653 advise (NULL
, "unable to read mime type");
656 advise (NULL
, "unable to run %s", buf
);
659 advise (NULL
, "filename to large to deduce mime type");
662 NMH_UNUSED (file_name
);
665 return content_type
? strdup (content_type
) : NULL
;
669 * Split large message into several messages of
670 * type "message/partial" and send them.
674 splitmsg (char **vec
, int vecp
, char *program
, char *drft
,
675 struct stat
*st
, int delay
)
677 int compnum
, nparts
, partno
, state
, status
;
680 char *cp
, *dp
, buffer
[BUFSIZ
], msgid
[BUFSIZ
];
681 char subject
[BUFSIZ
];
682 char name
[NAMESZ
], partnum
[BUFSIZ
];
684 m_getfld_state_t gstate
= 0;
686 if ((in
= fopen (drft
, "r")) == NULL
)
687 adios (drft
, "unable to open for reading");
693 * Scan through the message and examine the various header fields,
694 * as well as locate the beginning of the message body.
696 m_getfld_track_filepos (&gstate
, in
);
697 for (compnum
= 1;;) {
698 int bufsz
= sizeof buffer
;
699 switch (state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
)) {
705 * This header field is discarded.
707 if (!strcasecmp (name
, "Message-ID")) {
708 while (state
== FLDPLUS
) {
709 bufsz
= sizeof buffer
;
710 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
712 } else if (uprf (name
, XXX_FIELD_PRF
)
713 || !strcasecmp (name
, VRSN_FIELD
)
714 || !strcasecmp (name
, "Subject")
715 || !strcasecmp (name
, "Encrypted")) {
717 * These header fields are copied to the enclosed
718 * header of the first message in the collection
719 * of message/partials. For the "Subject" header
720 * field, we also record it, so that a modified
721 * version of it, can be copied to the header
722 * of each messsage/partial in the collection.
724 if (!strcasecmp (name
, "Subject")) {
727 strncpy (subject
, buffer
, BUFSIZ
);
728 sublen
= strlen (subject
);
729 if (sublen
> 0 && subject
[sublen
- 1] == '\n')
730 subject
[sublen
- 1] = '\0';
733 dp
= add (concat (name
, ":", buffer
, NULL
), dp
);
734 while (state
== FLDPLUS
) {
735 bufsz
= sizeof buffer
;
736 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
737 dp
= add (buffer
, dp
);
741 * These header fields are copied to the header of
742 * each message/partial in the collection.
744 cp
= add (concat (name
, ":", buffer
, NULL
), cp
);
745 while (state
== FLDPLUS
) {
746 bufsz
= sizeof buffer
;
747 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
748 cp
= add (buffer
, cp
);
752 start
= ftell (in
) + 1;
761 adios (NULL
, "message format error in component #%d", compnum
);
764 adios (NULL
, "getfld () returned %d", state
);
769 m_getfld_state_destroy (&gstate
);
771 adios (NULL
, "headers missing from draft");
775 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
778 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
784 /* Only one part, nothing to split */
791 return sendaux (vec
, vecp
, program
, drft
, st
);
795 printf ("Sending as %d Partial Messages\n", nparts
);
800 vec
[vecp
++] = "-partno";
801 vec
[vecp
++] = partnum
;
803 vec
[vecp
++] = "-queued";
806 snprintf (msgid
, sizeof(msgid
), "%s", message_id (clock
, 0));
808 fseek (in
, start
, SEEK_SET
);
809 for (partno
= 1; partno
<= nparts
; partno
++) {
813 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, &out
);
815 adios (drft
, "unable to create temporary file for");
817 strncpy(tmpdrf
, cp
, sizeof(tmpdrf
));
818 chmod (tmpdrf
, 0600);
821 * Output the header fields
824 fprintf (out
, "Subject: %s (part %d of %d)\n", subject
, partno
, nparts
);
825 fprintf (out
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
826 fprintf (out
, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD
, msgid
);
827 fprintf (out
, "\tnumber=%d; total=%d\n", partno
, nparts
);
828 fprintf (out
, "%s: part %d of %d\n\n", DESCR_FIELD
, partno
, nparts
);
831 * If this is the first in the collection, output the
832 * header fields we are encapsulating at the beginning
833 * of the body of the first message.
838 fprintf (out
, "Message-ID: %s\n", msgid
);
846 if (!fgets (buffer
, sizeof(buffer
) - 1, in
)) {
847 if (partno
== nparts
)
849 adios (NULL
, "premature eof");
852 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
853 fseek (in
, -len
, SEEK_CUR
);
861 adios (tmpdrf
, "error writing to");
865 if (!pushsw
&& verbsw
) {
870 /* Pause here, if a delay is specified */
871 if (delay
> 0 && 1 < partno
&& partno
<= nparts
) {
873 printf ("pausing %d seconds before sending part %d...\n",
877 sleep ((unsigned int) delay
);
880 snprintf (partnum
, sizeof(partnum
), "%d", partno
);
881 status
= sendaux (vec
, vecp
, program
, tmpdrf
, st
);
887 * This is so sendaux will only annotate
888 * the altmsg the first time it is called.
897 fclose (in
); /* close the draft */
903 * Annotate original message, and
904 * call `postproc' (which is passed down in "program") to send message.
908 sendaux (char **vec
, int vecp
, char *program
, char *drft
, struct stat
*st
)
911 int i
, status
, fd
, fd2
;
912 char backup
[BUFSIZ
], buf
[BUFSIZ
];
914 fd
= pushsw
? tmp_fd () : NOTOK
;
919 if ((fd2
= tmp_fd ()) != NOTOK
) {
920 vec
[vecp
++] = "-idanno";
921 snprintf (buf
, sizeof(buf
), "%d", fd2
);
924 admonish (NULL
, "unable to create file for annotation list");
927 if (distfile
&& distout (drft
, distfile
, backup
) == NOTOK
)
931 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
936 /* oops -- fork error */
937 adios ("fork", "unable to");
938 break; /* NOT REACHED */
942 * child process -- send it
944 * If fd is ok, then we are pushing and fd points to temp
945 * file, so capture anything on stdout and stderr there.
948 dup2 (fd
, fileno (stdout
));
949 dup2 (fd
, fileno (stderr
));
952 execvp (program
, vec
);
953 fprintf (stderr
, "unable to exec ");
959 * parent process -- wait for it
961 if ((status
= pidwait(child_id
, NOTOK
)) == OK
) {
962 if (annotext
&& fd2
!= NOTOK
)
966 * If postproc failed, and we have good fd (which means
967 * we pushed), then mail error message (and possibly the
968 * draft) back to the user.
974 advise (NULL
, "message not delivered to anyone");
976 if (annotext
&& fd2
!= NOTOK
)
980 if (rename (backup
, drft
) == NOTOK
)
981 advise (drft
, "unable to rename %s to", backup
);
992 * Mail error notification (and possibly a copy of the
993 * message) back to the user, using the mailproc
997 alert (char *file
, int out
)
1005 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1010 /* oops -- fork error */
1011 advise ("fork", "unable to");
1014 /* child process -- send it */
1015 SIGNAL (SIGHUP
, SIG_IGN
);
1016 SIGNAL (SIGINT
, SIG_IGN
);
1017 SIGNAL (SIGQUIT
, SIG_IGN
);
1018 SIGNAL (SIGTERM
, SIG_IGN
);
1020 if ((in
= open (file
, O_RDONLY
)) == NOTOK
) {
1021 admonish (file
, "unable to re-open");
1023 lseek (out
, (off_t
) 0, SEEK_END
);
1024 strncpy (buf
, "\nMessage not delivered to anyone.\n", sizeof(buf
));
1025 write (out
, buf
, strlen (buf
));
1026 strncpy (buf
, "\n------- Unsent Draft\n\n", sizeof(buf
));
1027 write (out
, buf
, strlen (buf
));
1028 cpydgst (in
, out
, file
, "temporary file");
1030 strncpy (buf
, "\n------- End of Unsent Draft\n", sizeof(buf
));
1031 write (out
, buf
, strlen (buf
));
1032 if (rename (file
, strncpy (buf
, m_backup (file
), sizeof(buf
))) == NOTOK
)
1033 admonish (buf
, "unable to rename %s to", file
);
1036 lseek (out
, (off_t
) 0, SEEK_SET
);
1037 dup2 (out
, fileno (stdin
));
1039 /* create subject for error notification */
1040 snprintf (buf
, sizeof(buf
), "send failed on %s",
1041 forwsw
? "enclosed draft" : file
);
1043 arglist
= argsplit(mailproc
, &program
, &argp
);
1045 arglist
[argp
++] = getusername();
1046 arglist
[argp
++] = "-subject";
1047 arglist
[argp
++] = buf
;
1048 arglist
[argp
] = NULL
;
1050 execvp (program
, arglist
);
1051 fprintf (stderr
, "unable to exec ");
1055 default: /* no waiting... */
1067 tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
);
1068 if (tfile
== NULL
) return NOTOK
;
1072 advise (NULL
, "temporary file %s selected", tfile
);
1074 if (unlink (tfile
) == NOTOK
)
1075 advise (tfile
, "unable to remove");
1082 anno (int fd
, struct stat
*st
)
1086 static char *cwd
= NULL
;
1090 (stat (altmsg
, &st2
) == NOTOK
1091 || st
->st_mtime
!= st2
.st_mtime
1092 || st
->st_dev
!= st2
.st_dev
1093 || st
->st_ino
!= st2
.st_ino
)) {
1095 admonish (NULL
, "$mhaltmsg mismatch");
1099 child_id
= debugsw
? NOTOK
: fork ();
1101 case NOTOK
: /* oops */
1104 "unable to fork, so doing annotations by hand...");
1106 cwd
= getcpy (pwd ());
1109 /* block a few signals */
1111 sigaddset (&set
, SIGHUP
);
1112 sigaddset (&set
, SIGINT
);
1113 sigaddset (&set
, SIGQUIT
);
1114 sigaddset (&set
, SIGTERM
);
1115 sigprocmask (SIG_BLOCK
, &set
, &oset
);
1121 /* reset the signal mask */
1122 sigprocmask (SIG_SETMASK
, &oset
, &set
);
1127 default: /* no waiting... */
1137 int fd2
, fd3
, msgnum
;
1138 char *cp
, *folder
, *maildir
;
1139 char buffer
[BUFSIZ
], **ap
;
1143 if ((folder
= getenv ("mhfolder")) == NULL
|| *folder
== 0) {
1145 admonish (NULL
, "$mhfolder not set");
1148 maildir
= m_maildir (folder
);
1149 if (chdir (maildir
) == NOTOK
) {
1151 admonish (maildir
, "unable to change directory to");
1154 if (!(mp
= folder_read (folder
, 0))) {
1156 admonish (NULL
, "unable to read folder %s", folder
);
1160 /* check for empty folder */
1161 if (mp
->nummsg
== 0) {
1163 admonish (NULL
, "no messages in %s", folder
);
1167 if ((cp
= getenv ("mhmessages")) == NULL
|| *cp
== 0) {
1169 admonish (NULL
, "$mhmessages not set");
1172 if (!debugsw
/* MOBY HACK... */
1174 && (fd3
= open ("/dev/null", O_RDWR
)) != NOTOK
1175 && (fd2
= dup (fileno (stderr
))) != NOTOK
) {
1176 dup2 (fd3
, fileno (stderr
));
1181 for (ap
= brkstring (cp
= getcpy (cp
), " ", NULL
); *ap
; ap
++)
1182 m_convert (mp
, *ap
);
1185 dup2 (fd2
, fileno (stderr
));
1186 if (mp
->numsel
== 0) {
1188 admonish (NULL
, "no messages to annotate");
1192 lseek (fd
, (off_t
) 0, SEEK_SET
);
1193 if ((fp
= fdopen (fd
, "r")) == NULL
) {
1195 admonish (NULL
, "unable to fdopen annotation list");
1199 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
1200 cp
= add (buffer
, cp
);
1204 advise (NULL
, "annotate%s with %s: \"%s\"",
1205 inplace
? " inplace" : "", annotext
, cp
);
1206 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
1207 if (is_selected(mp
, msgnum
)) {
1209 advise (NULL
, "annotate message %d", msgnum
);
1210 annotate (m_name (msgnum
), annotext
, cp
, inplace
, 1, -2, 0);
1217 folder_free (mp
); /* free folder/message structure */
1222 armed_done (int status
)
1224 longjmp (env
, status
? status
: NOTOK
);