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(void);
14 static int make_mime_composition_file_entry(char *, int, char *);
16 static int field_size
; /* size of header field buffer */
17 static char *field
; /* header field buffer */
18 static FILE *draft_file
; /* draft file pointer */
19 static FILE *composition_file
; /* composition file pointer */
22 attach(char *attachment_header_field_name
, char *draft_file_name
,
23 char *body_file_name
, size_t body_file_name_len
,
24 char *composition_file_name
, size_t composition_file_name_len
,
27 char buf
[PATH_MAX
+ 6]; /* miscellaneous buffer */
28 int c
; /* current character for body copy */
29 int has_attachment
; /* draft has at least one attachment */
30 int has_body
; /* draft has a message body */
31 int length
; /* length of attachment header field name */
32 char *p
; /* miscellaneous string pointer */
33 FILE *fp
; /* pointer for mhn.defaults */
34 FILE *body_file
= NULL
; /* body 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() != EOF
&& *field
!= '\0' && *field
!= '-') {
63 if (strncasecmp(field
, attachment_header_field_name
, length
) == 0 &&
64 field
[length
] == ':') {
65 for (p
= field
+ length
+ 1; *p
== ' ' || *p
== '\t'; p
++)
73 if (has_attachment
== 0)
77 * We have at least one attachment. Look for at least one non-blank line
78 * in the body of the message which indicates content in the body.
83 while (get_line() != EOF
) {
84 for (p
= field
; *p
!= '\0'; p
++) {
85 if (*p
!= ' ' && *p
!= '\t') {
96 * Make names for the temporary files.
99 (void)strncpy(body_file_name
,
100 m_mktemp(m_maildir(invo_name
), NULL
, NULL
),
102 (void)strncpy(composition_file_name
,
103 m_mktemp(m_maildir(invo_name
), NULL
, NULL
),
104 composition_file_name_len
);
107 body_file
= fopen(body_file_name
, "w");
109 composition_file
= fopen(composition_file_name
, "w");
111 if ((has_body
&& body_file
== (FILE *)0) || composition_file
== (FILE *)0) {
112 clean_up_temporary_files(body_file_name
, composition_file_name
);
113 adios((char *)0, "unable to open all of the temporary files.");
117 * Start at the beginning of the draft file. Copy all
118 * non-attachment header fields to the temporary composition
119 * file. Then add the dashed line separator.
124 while (get_line() != EOF
&& *field
!= '\0' && *field
!= '-')
125 if (strncasecmp(field
, attachment_header_field_name
, length
) != 0 ||
126 field
[length
] != ':')
127 (void)fprintf(composition_file
, "%s\n", field
);
129 (void)fputs("--------\n", composition_file
);
132 * Copy the message body to a temporary file.
136 while ((c
= getc(draft_file
)) != EOF
)
139 (void)fclose(body_file
);
143 * Add a mhbuild MIME composition file line for the body if there was one.
144 * Set the default content type to text/plain so that mhbuild takes care
145 * of any necessary encoding.
149 if (make_mime_composition_file_entry(body_file_name
, attachformat
,
151 clean_up_temporary_files(body_file_name
, composition_file_name
);
152 adios (NULL
, "exiting");
156 * Now, go back to the beginning of the draft file and look for
157 * header fields that specify attachments. Add a mhbuild MIME
158 * composition file for each.
161 if ((fp
= fopen (p
= etcpath ("mhn.defaults"), "r"))) {
162 readconfig ((struct node
**) NULL
, fp
, p
, 0);
168 while (get_line() != EOF
&& *field
!= '\0' && *field
!= '-') {
169 if (strncasecmp(field
, attachment_header_field_name
, length
) == 0 &&
170 field
[length
] == ':') {
171 for (p
= field
+ length
+ 1; *p
== ' ' || *p
== '\t'; p
++)
174 /* Skip empty attachment_header_field_name lines. */
175 if (strlen (p
) > 0) {
177 if (stat (p
, &st
) == OK
) {
178 if (S_ISREG (st
.st_mode
)) {
179 /* Don't set the default content type so take
180 make_mime_composition_file_entry() will try
181 to infer it from the file type. */
182 if (make_mime_composition_file_entry(p
, attachformat
, 0)) {
183 clean_up_temporary_files(body_file_name
,
184 composition_file_name
);
185 adios (NULL
, "exiting");
188 adios (NULL
, "unable to attach %s, not a plain file",
192 adios (NULL
, "unable to access file \"%s\"", p
);
198 (void)fclose(composition_file
);
201 * We're ready to roll! Run mhbuild on the composition file.
202 * Note that mhbuild is in the context as buildmimeproc.
205 (void)sprintf(buf
, "%s %s", buildmimeproc
, composition_file_name
);
207 if (system(buf
) != 0) {
208 clean_up_temporary_files(body_file_name
, composition_file_name
);
216 clean_up_temporary_files(const char *body_file_name
,
217 const char *composition_file_name
)
219 (void)unlink(body_file_name
);
220 (void)unlink(composition_file_name
);
228 int c
; /* current character */
229 int n
; /* number of bytes in buffer */
230 char *p
; /* buffer pointer */
233 * Get a line from the input file, growing the field buffer as needed. We do this
234 * so that we can fit an entire line in the buffer making it easy to do a string
235 * comparison on both the field name and the field body which might be a long path
239 for (n
= 0, p
= field
; (c
= getc(draft_file
)) != EOF
; *p
++ = c
) {
240 if (c
== '\n' && (c
= getc(draft_file
)) != ' ' && c
!= '\t') {
241 (void)ungetc(c
, draft_file
);
246 if (++n
>= field_size
- 1) {
247 field
= (char *)mh_xrealloc((void *)field
, field_size
+= 256);
254 * NUL-terminate the field..
263 make_mime_composition_file_entry(char *file_name
, int attachformat
,
264 char *default_content_type
)
266 int binary
; /* binary character found flag */
267 int c
; /* current character */
268 char cmd
[PATH_MAX
+ 8]; /* file command buffer */
269 char *content_type
; /* mime content type */
270 FILE *fp
; /* content and pipe file pointer */
271 struct node
*np
; /* context scan node pointer */
272 char *p
; /* miscellaneous string pointer */
273 struct stat st
; /* file status buffer */
275 if ((content_type
= mime_type (file_name
)) == NULL
) {
277 * Check the file name for a suffix. Scan the context for
278 * that suffix on a mhshow-suffix- entry. We use these
279 * entries to be compatible with mhnshow, and there's no
280 * reason to make the user specify each suffix twice. Context
281 * entries of the form "mhshow-suffix-contenttype" in the name
282 * have the suffix in the field, including the dot.
284 if ((p
= strrchr(file_name
, '.')) != (char *)0) {
285 for (np
= m_defs
; np
; np
= np
->n_next
) {
286 if (strncasecmp(np
->n_name
, "mhshow-suffix-", 14) == 0 &&
287 strcasecmp(p
, np
->n_field
? np
->n_field
: "") == 0) {
288 content_type
= strdup (np
->n_name
+ 14);
294 if (content_type
== NULL
&& default_content_type
!= NULL
) {
295 content_type
= strdup (default_content_type
);
300 * No content type was found, either because there was no matching entry in the
301 * context or because the file name has no suffix. Open the file and check for
302 * non-ASCII characters. Choose the content type based on this check.
305 if (content_type
== (char *)0) {
306 if ((fp
= fopen(file_name
, "r")) == (FILE *)0) {
307 advise((char *)0, "unable to access file \"%s\"", file_name
);
313 while ((c
= getc(fp
)) != EOF
) {
314 if (c
> 127 || c
< 0) {
323 strdup (binary
? "application/octet-stream" : "text/plain");
327 * Make sure that the attachment file exists and is readable. Append a mhbuild
328 * directive to the draft file. This starts with the content type. Append a
329 * file name attribute and a private x-unix-mode attribute. Also append a
330 * description obtained (if possible) by running the "file" command on the file.
333 if (stat(file_name
, &st
) == -1 || access(file_name
, R_OK
) != 0) {
334 advise((char *)0, "unable to access file \"%s\"", file_name
);
338 switch (attachformat
) {
340 /* Insert name, file mode, and Content-Id. */
341 (void)fprintf(composition_file
, "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
342 content_type
, ((p
= strrchr(file_name
, '/')) == (char *)0) ? file_name
: p
+ 1, (unsigned short)(st
.st_mode
& 0777));
344 if (strlen(file_name
) > PATH_MAX
) {
345 advise((char *)0, "attachment file name `%s' too long.", file_name
);
349 (void)sprintf(cmd
, "file '%s'", file_name
);
351 if ((fp
= popen(cmd
, "r")) != (FILE *)0 && fgets(cmd
, sizeof (cmd
), fp
) != (char *)0) {
352 *strchr(cmd
, '\n') = '\0';
355 * The output of the "file" command is of the form
359 * Strip off the "file:" and subsequent white space.
362 for (p
= cmd
; *p
!= '\0'; p
++) {
364 for (p
++; *p
!= '\0'; p
++) {
373 /* Insert Content-Description. */
374 (void)fprintf(composition_file
, " [ %s ]", p
);
381 if (stringdex (m_maildir(invo_name
), file_name
) == 0) {
382 /* Content had been placed by send into a temp file.
383 Don't generate Content-Disposition header, because
384 it confuses Microsoft Outlook, Build 10.0.6626, at
386 (void) fprintf (composition_file
, "#%s <>", content_type
);
388 /* Suppress Content-Id, insert simple Content-Disposition
389 and Content-Description with filename.
390 The Content-Disposition type needs to be "inline" for
391 MS Outlook and BlackBerry calendar programs to properly
392 handle a text/calendar attachment. */
393 p
= strrchr(file_name
, '/');
394 (void) fprintf (composition_file
,
395 "#%s; name=\"%s\" <> [%s]{%s}",
397 (p
== (char *)0) ? file_name
: p
+ 1,
398 (p
== (char *)0) ? file_name
: p
+ 1,
399 strcmp ("text/calendar", content_type
)
400 ? "attachment" : "inline");
405 if (stringdex (m_maildir(invo_name
), file_name
) == 0) {
406 /* Content had been placed by send into a temp file.
407 Don't generate Content-Disposition header, because
408 it confuses Microsoft Outlook, Build 10.0.6626, at
410 (void) fprintf (composition_file
, "#%s <>", content_type
);
412 /* Suppress Content-Id, insert Content-Disposition with
413 modification date and Content-Description wtih filename.
414 The Content-Disposition type needs to be "inline" for
415 MS Outlook and BlackBerry calendar programs to properly
416 handle a text/calendar attachment. */
417 p
= strrchr(file_name
, '/');
418 (void) fprintf (composition_file
,
419 "#%s; name=\"%s\" <>[%s]{%s; "
420 "modification-date=\"%s\"}",
422 (p
== (char *)0) ? file_name
: p
+ 1,
423 (p
== (char *)0) ? file_name
: p
+ 1,
424 strcmp ("text/calendar", content_type
)
425 ? "attachment" : "inline",
426 dtime (&st
.st_mtime
, 0));
431 adios ((char *)0, "unsupported attachformat %d", attachformat
);
437 * Finish up with the file name.
440 (void)fprintf(composition_file
, " %s\n", file_name
);
446 * Try to use external command to determine mime type, and possibly
447 * encoding. Caller is responsible for free'ing returned memory.
450 mime_type (const char *file_name
) {
451 char *content_type
= NULL
; /* mime content type */
454 char cmd
[2 * PATH_MAX
+ 2]; /* file command buffer */
455 char buf
[BUFSIZ
>= 2048 ? BUFSIZ
: 2048];
456 FILE *fp
; /* content and pipe file pointer */
457 char mimetypeproc
[] = MIMETYPEPROC
" '%s'";
459 if ((int) snprintf (cmd
, sizeof cmd
, mimetypeproc
, file_name
) <
461 if ((fp
= popen (cmd
, "r")) != NULL
) {
462 /* Make sure that buf has space for one additional
463 character, the semicolon that might be added below. */
464 if (fgets (buf
, sizeof buf
- 1, fp
)) {
467 /* Skip leading <filename>:<whitespace>, if present. */
468 if ((content_type
= strchr (buf
, ':')) != NULL
) {
470 while (*content_type
&& isblank (*content_type
)) {
477 /* Truncate at newline (LF or CR), if present. */
478 if ((cp
= strpbrk (content_type
, "\n\n")) != NULL
) {
482 /* If necessary, insert semicolon between content type
483 and charset. Assume that the first space is between
485 if ((space
= strchr (content_type
, ' ')) != NULL
) {
486 ssize_t len
= strlen (content_type
);
488 if (space
- content_type
> 0 &&
489 len
> space
- content_type
+ 1) {
490 if (*(space
- 1) != ';') {
491 /* The +1 is for the terminating NULL. */
492 memmove (space
+ 1, space
,
493 len
- (space
- content_type
) + 1);
499 advise (NULL
, "unable to read mime type");
502 advise (NULL
, "unable to run %s", buf
);
505 advise (NULL
, "filename to large to deduce mime type");
508 NMH_UNUSED (file_name
);
511 return content_type
? strdup (content_type
) : NULL
;