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(FILE *, char *, size_t);
14 static int make_mime_composition_file_entry(FILE *, char *, int, char *);
17 attach(char *attachment_header_field_name
, char *draft_file_name
,
18 char *body_file_name
, size_t body_file_name_len
,
19 char *composition_file_name
, size_t composition_file_name_len
,
22 char buf
[PATH_MAX
+ 6]; /* miscellaneous buffer */
23 int c
; /* current character for body copy */
24 int has_attachment
; /* draft has at least one attachment */
25 int has_body
; /* draft has a message body */
26 int length
; /* length of attachment header field name */
27 char *p
; /* miscellaneous string pointer */
28 FILE *fp
; /* pointer for mhn.defaults */
29 FILE *body_file
= NULL
; /* body file pointer */
30 FILE *draft_file
; /* draft file pointer */
31 int field_size
; /* size of header field buffer */
32 char *field
; /* header field buffer */
33 FILE *composition_file
; /* composition file pointer */
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(draft_file
, 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(draft_file
, 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(draft_file
, 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(composition_file
, body_file_name
,
152 attachformat
, "text/plain")) {
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(draft_file
, 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(composition_file
,
188 clean_up_temporary_files(body_file_name
,
189 composition_file_name
);
190 adios (NULL
, "exiting");
193 adios (NULL
, "unable to attach %s, not a plain file",
197 adios (NULL
, "unable to access file \"%s\"", p
);
203 (void)fclose(composition_file
);
206 * We're ready to roll! Run mhbuild on the composition file.
207 * Note that mhbuild is in the context as buildmimeproc.
210 (void)sprintf(buf
, "%s %s", buildmimeproc
, composition_file_name
);
212 if (system(buf
) != 0) {
213 clean_up_temporary_files(body_file_name
, composition_file_name
);
221 clean_up_temporary_files(const char *body_file_name
,
222 const char *composition_file_name
)
224 (void)unlink(body_file_name
);
225 (void)unlink(composition_file_name
);
231 get_line(FILE *draft_file
, char *field
, size_t field_size
)
233 int c
; /* current character */
234 size_t n
; /* number of bytes in buffer */
235 char *p
; /* buffer pointer */
238 * Get a line from the input file, growing the field buffer as needed. We do this
239 * so that we can fit an entire line in the buffer making it easy to do a string
240 * comparison on both the field name and the field body which might be a long path
244 for (n
= 0, p
= field
; (c
= getc(draft_file
)) != EOF
; *p
++ = c
) {
245 if (c
== '\n' && (c
= getc(draft_file
)) != ' ' && c
!= '\t') {
246 (void)ungetc(c
, draft_file
);
251 if (++n
>= field_size
- 1) {
252 field
= (char *)mh_xrealloc((void *)field
, field_size
+= 256);
259 * NUL-terminate the field..
268 make_mime_composition_file_entry(FILE *composition_file
, char *file_name
,
269 int attachformat
, char *default_content_type
)
271 int binary
; /* binary character found flag */
272 int c
; /* current character */
273 char cmd
[PATH_MAX
+ 8]; /* file command buffer */
274 char *content_type
; /* mime content type */
275 FILE *fp
; /* content and pipe file pointer */
276 struct node
*np
; /* context scan node pointer */
277 char *p
; /* miscellaneous string pointer */
278 struct stat st
; /* file status buffer */
280 if ((content_type
= mime_type (file_name
)) == NULL
) {
282 * Check the file name for a suffix. Scan the context for
283 * that suffix on a mhshow-suffix- entry. We use these
284 * entries to be compatible with mhnshow, and there's no
285 * reason to make the user specify each suffix twice. Context
286 * entries of the form "mhshow-suffix-contenttype" in the name
287 * have the suffix in the field, including the dot.
289 if ((p
= strrchr(file_name
, '.')) != (char *)0) {
290 for (np
= m_defs
; np
; np
= np
->n_next
) {
291 if (strncasecmp(np
->n_name
, "mhshow-suffix-", 14) == 0 &&
292 strcasecmp(p
, np
->n_field
? np
->n_field
: "") == 0) {
293 content_type
= strdup (np
->n_name
+ 14);
299 if (content_type
== NULL
&& default_content_type
!= NULL
) {
300 content_type
= strdup (default_content_type
);
305 * No content type was found, either because there was no matching entry in the
306 * context or because the file name has no suffix. Open the file and check for
307 * non-ASCII characters. Choose the content type based on this check.
310 if (content_type
== (char *)0) {
311 if ((fp
= fopen(file_name
, "r")) == (FILE *)0) {
312 advise((char *)0, "unable to access file \"%s\"", file_name
);
318 while ((c
= getc(fp
)) != EOF
) {
319 if (c
> 127 || c
< 0) {
328 strdup (binary
? "application/octet-stream" : "text/plain");
332 * Make sure that the attachment file exists and is readable. Append a mhbuild
333 * directive to the draft file. This starts with the content type. Append a
334 * file name attribute and a private x-unix-mode attribute. Also append a
335 * description obtained (if possible) by running the "file" command on the file.
338 if (stat(file_name
, &st
) == -1 || access(file_name
, R_OK
) != 0) {
339 advise((char *)0, "unable to access file \"%s\"", file_name
);
343 switch (attachformat
) {
345 /* Insert name, file mode, and Content-Id. */
346 (void)fprintf(composition_file
, "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
347 content_type
, ((p
= strrchr(file_name
, '/')) == (char *)0) ? file_name
: p
+ 1, (unsigned short)(st
.st_mode
& 0777));
349 if (strlen(file_name
) > PATH_MAX
) {
350 advise((char *)0, "attachment file name `%s' too long.", file_name
);
354 (void)sprintf(cmd
, "file '%s'", file_name
);
356 if ((fp
= popen(cmd
, "r")) != (FILE *)0 && fgets(cmd
, sizeof (cmd
), fp
) != (char *)0) {
357 *strchr(cmd
, '\n') = '\0';
360 * The output of the "file" command is of the form
364 * Strip off the "file:" and subsequent white space.
367 for (p
= cmd
; *p
!= '\0'; p
++) {
369 for (p
++; *p
!= '\0'; p
++) {
378 /* Insert Content-Description. */
379 (void)fprintf(composition_file
, " [ %s ]", p
);
386 if (stringdex (m_maildir(invo_name
), file_name
) == 0) {
387 /* Content had been placed by send into a temp file.
388 Don't generate Content-Disposition header, because
389 it confuses Microsoft Outlook, Build 10.0.6626, at
391 (void) fprintf (composition_file
, "#%s <>", content_type
);
393 /* Suppress Content-Id, insert simple Content-Disposition
394 and Content-Description with filename.
395 The Content-Disposition type needs to be "inline" for
396 MS Outlook and BlackBerry calendar programs to properly
397 handle a text/calendar attachment. */
398 p
= strrchr(file_name
, '/');
399 (void) fprintf (composition_file
,
400 "#%s; name=\"%s\" <> [%s]{%s}",
402 (p
== (char *)0) ? file_name
: p
+ 1,
403 (p
== (char *)0) ? file_name
: p
+ 1,
404 strcmp ("text/calendar", content_type
)
405 ? "attachment" : "inline");
410 if (stringdex (m_maildir(invo_name
), file_name
) == 0) {
411 /* Content had been placed by send into a temp file.
412 Don't generate Content-Disposition header, because
413 it confuses Microsoft Outlook, Build 10.0.6626, at
415 (void) fprintf (composition_file
, "#%s <>", content_type
);
417 /* Suppress Content-Id, insert Content-Disposition with
418 modification date and Content-Description wtih filename.
419 The Content-Disposition type needs to be "inline" for
420 MS Outlook and BlackBerry calendar programs to properly
421 handle a text/calendar attachment. */
422 p
= strrchr(file_name
, '/');
423 (void) fprintf (composition_file
,
424 "#%s; name=\"%s\" <>[%s]{%s; "
425 "modification-date=\"%s\"}",
427 (p
== (char *)0) ? file_name
: p
+ 1,
428 (p
== (char *)0) ? file_name
: p
+ 1,
429 strcmp ("text/calendar", content_type
)
430 ? "attachment" : "inline",
431 dtime (&st
.st_mtime
, 0));
436 adios ((char *)0, "unsupported attachformat %d", attachformat
);
442 * Finish up with the file name.
445 (void)fprintf(composition_file
, " %s\n", file_name
);
451 * Try to use external command to determine mime type, and possibly
452 * encoding. Caller is responsible for free'ing returned memory.
455 mime_type (const char *file_name
) {
456 char *content_type
= NULL
; /* mime content type */
459 char cmd
[2 * PATH_MAX
+ 2]; /* file command buffer */
460 char buf
[BUFSIZ
>= 2048 ? BUFSIZ
: 2048];
461 FILE *fp
; /* content and pipe file pointer */
462 char mimetypeproc
[] = MIMETYPEPROC
" '%s'";
464 if ((int) snprintf (cmd
, sizeof cmd
, mimetypeproc
, file_name
) <
466 if ((fp
= popen (cmd
, "r")) != NULL
) {
467 /* Make sure that buf has space for one additional
468 character, the semicolon that might be added below. */
469 if (fgets (buf
, sizeof buf
- 1, fp
)) {
472 /* Skip leading <filename>:<whitespace>, if present. */
473 if ((content_type
= strchr (buf
, ':')) != NULL
) {
475 while (*content_type
&& isblank (*content_type
)) {
482 /* Truncate at newline (LF or CR), if present. */
483 if ((cp
= strpbrk (content_type
, "\n\n")) != NULL
) {
487 /* If necessary, insert semicolon between content type
488 and charset. Assume that the first space is between
490 if ((space
= strchr (content_type
, ' ')) != NULL
) {
491 ssize_t len
= strlen (content_type
);
493 if (space
- content_type
> 0 &&
494 len
> space
- content_type
+ 1) {
495 if (*(space
- 1) != ';') {
496 /* The +1 is for the terminating NULL. */
497 memmove (space
+ 1, space
,
498 len
- (space
- content_type
) + 1);
504 advise (NULL
, "unable to read mime type");
507 advise (NULL
, "unable to run %s", buf
);
510 advise (NULL
, "filename to large to deduce mime type");
513 NMH_UNUSED (file_name
);
516 return content_type
? strdup (content_type
) : NULL
;