2 * attach.c -- routines to help attach files via whatnow
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
13 static int get_line(char *, size_t);
14 static int make_mime_composition_file_entry(char *, int, char *);
16 static FILE *draft_file
; /* draft file pointer */
17 static FILE *composition_file
; /* composition file pointer */
20 attach(char *attachment_header_field_name
, char *draft_file_name
,
21 char *body_file_name
, size_t body_file_name_len
,
22 char *composition_file_name
, size_t composition_file_name_len
,
25 char buf
[PATH_MAX
+ 6]; /* miscellaneous buffer */
26 int c
; /* current character for body copy */
27 int has_attachment
; /* draft has at least one attachment */
28 int has_body
; /* draft has a message body */
29 int length
; /* length of attachment header field name */
30 char *p
; /* miscellaneous string pointer */
31 FILE *fp
; /* pointer for mhn.defaults */
32 FILE *body_file
= NULL
; /* body file pointer */
33 int field_size
; /* size of header field buffer */
34 char *field
; /* header field buffer */
37 * Open up the draft file.
40 if ((draft_file
= fopen(draft_file_name
, "r")) == (FILE *)0)
41 adios((char *)0, "can't open draft file `%s'.", draft_file_name
);
44 * Allocate a buffer to hold the header components as they're read in.
45 * This buffer might need to be quite large, so we grow it as needed.
48 field
= (char *)mh_xmalloc(field_size
= 256);
51 * Scan the draft file for a header field name, with a non-empty
52 * body, that matches the -attach argument. The existence of one
53 * indicates that the draft has attachments. Bail out if there
54 * are no attachments because we're done. Read to the end of the
55 * headers even if we have no attachments.
58 length
= strlen(attachment_header_field_name
);
62 while (get_line(field
, field_size
) != EOF
&& *field
!= '\0' &&
64 if (strncasecmp(field
, attachment_header_field_name
, length
) == 0 &&
65 field
[length
] == ':') {
66 for (p
= field
+ length
+ 1; *p
== ' ' || *p
== '\t'; p
++)
74 if (has_attachment
== 0)
78 * We have at least one attachment. Look for at least one non-blank line
79 * in the body of the message which indicates content in the body.
84 while (get_line(field
, field_size
) != EOF
) {
85 for (p
= field
; *p
!= '\0'; p
++) {
86 if (*p
!= ' ' && *p
!= '\t') {
97 * Make names for the temporary files.
100 (void)strncpy(body_file_name
,
101 m_mktemp(m_maildir(invo_name
), NULL
, NULL
),
103 (void)strncpy(composition_file_name
,
104 m_mktemp(m_maildir(invo_name
), NULL
, NULL
),
105 composition_file_name_len
);
108 body_file
= fopen(body_file_name
, "w");
110 composition_file
= fopen(composition_file_name
, "w");
112 if ((has_body
&& body_file
== (FILE *)0) || composition_file
== (FILE *)0) {
113 clean_up_temporary_files(body_file_name
, composition_file_name
);
114 adios((char *)0, "unable to open all of the temporary files.");
118 * Start at the beginning of the draft file. Copy all
119 * non-attachment header fields to the temporary composition
120 * file. Then add the dashed line separator.
125 while (get_line(field
, field_size
) != EOF
&& *field
!= '\0' &&
127 if (strncasecmp(field
, attachment_header_field_name
, length
) != 0 ||
128 field
[length
] != ':')
129 (void)fprintf(composition_file
, "%s\n", field
);
131 (void)fputs("--------\n", composition_file
);
134 * Copy the message body to a temporary file.
138 while ((c
= getc(draft_file
)) != EOF
)
141 (void)fclose(body_file
);
145 * Add a mhbuild MIME composition file line for the body if there was one.
146 * Set the default content type to text/plain so that mhbuild takes care
147 * of any necessary encoding.
151 if (make_mime_composition_file_entry(body_file_name
, attachformat
,
153 clean_up_temporary_files(body_file_name
, composition_file_name
);
154 adios (NULL
, "exiting");
158 * Now, go back to the beginning of the draft file and look for
159 * header fields that specify attachments. Add a mhbuild MIME
160 * composition file for each.
163 if ((fp
= fopen (p
= etcpath ("mhn.defaults"), "r"))) {
164 readconfig ((struct node
**) NULL
, fp
, p
, 0);
170 while (get_line(field
, field_size
) != EOF
&& *field
!= '\0' &&
172 if (strncasecmp(field
, attachment_header_field_name
, length
) == 0 &&
173 field
[length
] == ':') {
174 for (p
= field
+ length
+ 1; *p
== ' ' || *p
== '\t'; p
++)
177 /* Skip empty attachment_header_field_name lines. */
178 if (strlen (p
) > 0) {
180 if (stat (p
, &st
) == OK
) {
181 if (S_ISREG (st
.st_mode
)) {
182 /* Don't set the default content type so take
183 make_mime_composition_file_entry() will try
184 to infer it from the file type. */
185 if (make_mime_composition_file_entry(p
, attachformat
, 0)) {
186 clean_up_temporary_files(body_file_name
,
187 composition_file_name
);
188 adios (NULL
, "exiting");
191 adios (NULL
, "unable to attach %s, not a plain file",
195 adios (NULL
, "unable to access file \"%s\"", p
);
201 (void)fclose(composition_file
);
204 * We're ready to roll! Run mhbuild on the composition file.
205 * Note that mhbuild is in the context as buildmimeproc.
208 (void)sprintf(buf
, "%s %s", buildmimeproc
, composition_file_name
);
210 if (system(buf
) != 0) {
211 clean_up_temporary_files(body_file_name
, composition_file_name
);
219 clean_up_temporary_files(const char *body_file_name
,
220 const char *composition_file_name
)
222 (void)unlink(body_file_name
);
223 (void)unlink(composition_file_name
);
229 get_line(char *field
, size_t field_size
)
231 int c
; /* current character */
232 size_t n
; /* number of bytes in buffer */
233 char *p
; /* buffer pointer */
236 * Get a line from the input file, growing the field buffer as needed. We do this
237 * so that we can fit an entire line in the buffer making it easy to do a string
238 * comparison on both the field name and the field body which might be a long path
242 for (n
= 0, p
= field
; (c
= getc(draft_file
)) != EOF
; *p
++ = c
) {
243 if (c
== '\n' && (c
= getc(draft_file
)) != ' ' && c
!= '\t') {
244 (void)ungetc(c
, draft_file
);
249 if (++n
>= field_size
- 1) {
250 field
= (char *)mh_xrealloc((void *)field
, field_size
+= 256);
257 * NUL-terminate the field..
266 make_mime_composition_file_entry(char *file_name
, int attachformat
,
267 char *default_content_type
)
269 int binary
; /* binary character found flag */
270 int c
; /* current character */
271 char cmd
[PATH_MAX
+ 8]; /* file command buffer */
272 char *content_type
; /* mime content type */
273 FILE *fp
; /* content and pipe file pointer */
274 struct node
*np
; /* context scan node pointer */
275 char *p
; /* miscellaneous string pointer */
276 struct stat st
; /* file status buffer */
278 if ((content_type
= mime_type (file_name
)) == NULL
) {
280 * Check the file name for a suffix. Scan the context for
281 * that suffix on a mhshow-suffix- entry. We use these
282 * entries to be compatible with mhnshow, and there's no
283 * reason to make the user specify each suffix twice. Context
284 * entries of the form "mhshow-suffix-contenttype" in the name
285 * have the suffix in the field, including the dot.
287 if ((p
= strrchr(file_name
, '.')) != (char *)0) {
288 for (np
= m_defs
; np
; np
= np
->n_next
) {
289 if (strncasecmp(np
->n_name
, "mhshow-suffix-", 14) == 0 &&
290 strcasecmp(p
, np
->n_field
? np
->n_field
: "") == 0) {
291 content_type
= strdup (np
->n_name
+ 14);
297 if (content_type
== NULL
&& default_content_type
!= NULL
) {
298 content_type
= strdup (default_content_type
);
303 * No content type was found, either because there was no matching entry in the
304 * context or because the file name has no suffix. Open the file and check for
305 * non-ASCII characters. Choose the content type based on this check.
308 if (content_type
== (char *)0) {
309 if ((fp
= fopen(file_name
, "r")) == (FILE *)0) {
310 advise((char *)0, "unable to access file \"%s\"", file_name
);
316 while ((c
= getc(fp
)) != EOF
) {
317 if (c
> 127 || c
< 0) {
326 strdup (binary
? "application/octet-stream" : "text/plain");
330 * Make sure that the attachment file exists and is readable. Append a mhbuild
331 * directive to the draft file. This starts with the content type. Append a
332 * file name attribute and a private x-unix-mode attribute. Also append a
333 * description obtained (if possible) by running the "file" command on the file.
336 if (stat(file_name
, &st
) == -1 || access(file_name
, R_OK
) != 0) {
337 advise((char *)0, "unable to access file \"%s\"", file_name
);
341 switch (attachformat
) {
343 /* Insert name, file mode, and Content-Id. */
344 (void)fprintf(composition_file
, "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
345 content_type
, ((p
= strrchr(file_name
, '/')) == (char *)0) ? file_name
: p
+ 1, (unsigned short)(st
.st_mode
& 0777));
347 if (strlen(file_name
) > PATH_MAX
) {
348 advise((char *)0, "attachment file name `%s' too long.", file_name
);
352 (void)sprintf(cmd
, "file '%s'", file_name
);
354 if ((fp
= popen(cmd
, "r")) != (FILE *)0 && fgets(cmd
, sizeof (cmd
), fp
) != (char *)0) {
355 *strchr(cmd
, '\n') = '\0';
358 * The output of the "file" command is of the form
362 * Strip off the "file:" and subsequent white space.
365 for (p
= cmd
; *p
!= '\0'; p
++) {
367 for (p
++; *p
!= '\0'; p
++) {
376 /* Insert Content-Description. */
377 (void)fprintf(composition_file
, " [ %s ]", p
);
384 if (stringdex (m_maildir(invo_name
), file_name
) == 0) {
385 /* Content had been placed by send into a temp file.
386 Don't generate Content-Disposition header, because
387 it confuses Microsoft Outlook, Build 10.0.6626, at
389 (void) fprintf (composition_file
, "#%s <>", content_type
);
391 /* Suppress Content-Id, insert simple Content-Disposition
392 and Content-Description with filename.
393 The Content-Disposition type needs to be "inline" for
394 MS Outlook and BlackBerry calendar programs to properly
395 handle a text/calendar attachment. */
396 p
= strrchr(file_name
, '/');
397 (void) fprintf (composition_file
,
398 "#%s; name=\"%s\" <> [%s]{%s}",
400 (p
== (char *)0) ? file_name
: p
+ 1,
401 (p
== (char *)0) ? file_name
: p
+ 1,
402 strcmp ("text/calendar", content_type
)
403 ? "attachment" : "inline");
408 if (stringdex (m_maildir(invo_name
), file_name
) == 0) {
409 /* Content had been placed by send into a temp file.
410 Don't generate Content-Disposition header, because
411 it confuses Microsoft Outlook, Build 10.0.6626, at
413 (void) fprintf (composition_file
, "#%s <>", content_type
);
415 /* Suppress Content-Id, insert Content-Disposition with
416 modification date and Content-Description wtih filename.
417 The Content-Disposition type needs to be "inline" for
418 MS Outlook and BlackBerry calendar programs to properly
419 handle a text/calendar attachment. */
420 p
= strrchr(file_name
, '/');
421 (void) fprintf (composition_file
,
422 "#%s; name=\"%s\" <>[%s]{%s; "
423 "modification-date=\"%s\"}",
425 (p
== (char *)0) ? file_name
: p
+ 1,
426 (p
== (char *)0) ? file_name
: p
+ 1,
427 strcmp ("text/calendar", content_type
)
428 ? "attachment" : "inline",
429 dtime (&st
.st_mtime
, 0));
434 adios ((char *)0, "unsupported attachformat %d", attachformat
);
440 * Finish up with the file name.
443 (void)fprintf(composition_file
, " %s\n", file_name
);
449 * Try to use external command to determine mime type, and possibly
450 * encoding. Caller is responsible for free'ing returned memory.
453 mime_type (const char *file_name
) {
454 char *content_type
= NULL
; /* mime content type */
457 char cmd
[2 * PATH_MAX
+ 2]; /* file command buffer */
458 char buf
[BUFSIZ
>= 2048 ? BUFSIZ
: 2048];
459 FILE *fp
; /* content and pipe file pointer */
460 char mimetypeproc
[] = MIMETYPEPROC
" '%s'";
462 if ((int) snprintf (cmd
, sizeof cmd
, mimetypeproc
, file_name
) <
464 if ((fp
= popen (cmd
, "r")) != NULL
) {
465 /* Make sure that buf has space for one additional
466 character, the semicolon that might be added below. */
467 if (fgets (buf
, sizeof buf
- 1, fp
)) {
470 /* Skip leading <filename>:<whitespace>, if present. */
471 if ((content_type
= strchr (buf
, ':')) != NULL
) {
473 while (*content_type
&& isblank (*content_type
)) {
480 /* Truncate at newline (LF or CR), if present. */
481 if ((cp
= strpbrk (content_type
, "\n\n")) != NULL
) {
485 /* If necessary, insert semicolon between content type
486 and charset. Assume that the first space is between
488 if ((space
= strchr (content_type
, ' ')) != NULL
) {
489 ssize_t len
= strlen (content_type
);
491 if (space
- content_type
> 0 &&
492 len
> space
- content_type
+ 1) {
493 if (*(space
- 1) != ';') {
494 /* The +1 is for the terminating NULL. */
495 memmove (space
+ 1, space
,
496 len
- (space
- content_type
) + 1);
502 advise (NULL
, "unable to read mime type");
505 advise (NULL
, "unable to run %s", buf
);
508 advise (NULL
, "filename to large to deduce mime type");
511 NMH_UNUSED (file_name
);
514 return content_type
? strdup (content_type
) : NULL
;