]> diplodocus.org Git - nmh/blob - uip/attach.c
9ff7d0955598dbe73cc736173478f48c165fb8be
[nmh] / uip / attach.c
1 /*
2 * attach.c -- routines to help attach files via whatnow
3 *
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.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11 #include <h/tws.h>
12
13 static int get_line(FILE *, char *, size_t);
14 #ifdef MIMETYPEPROC
15 static char *get_file_info(const char *, const char *);
16 #endif /* MIMETYPEPROC */
17
18 int
19 attach(char *attachment_header_field_name, char *draft_file_name,
20 char *body_file_name, size_t body_file_name_len,
21 char *composition_file_name, size_t composition_file_name_len,
22 int attachformat)
23 {
24 char buf[PATH_MAX + 6]; /* miscellaneous buffer */
25 int c; /* current character for body copy */
26 int has_attachment; /* draft has at least one attachment */
27 int has_body; /* draft has a message body */
28 int length; /* of attachment header field name */
29 char *p; /* miscellaneous string pointer */
30 struct stat st; /* file status buffer */
31 FILE *body_file = NULL; /* body file pointer */
32 FILE *draft_file; /* draft file pointer */
33 int field_size; /* size of header field buffer */
34 char *field; /* header field buffer */
35 FILE *composition_file; /* composition file pointer */
36 char *build_directive; /* mhbuild directive */
37
38
39 /*
40 * Open up the draft file.
41 */
42
43 if ((draft_file = fopen(draft_file_name, "r")) == (FILE *)0)
44 adios(NULL, "can't open draft file `%s'.", draft_file_name);
45
46 /*
47 * Allocate a buffer to hold the header components as they're read in.
48 * This buffer might need to be quite large, so we grow it as needed.
49 */
50
51 field = (char *)mh_xmalloc(field_size = 256);
52
53 /*
54 * Scan the draft file for a header field name, with a non-empty
55 * body, that matches the -attach argument. The existence of one
56 * indicates that the draft has attachments. Bail out if there
57 * are no attachments because we're done. Read to the end of the
58 * headers even if we have no attachments.
59 */
60
61 length = strlen(attachment_header_field_name);
62
63 has_attachment = 0;
64
65 while (get_line(draft_file, field, field_size) != EOF && *field != '\0' &&
66 *field != '-') {
67 if (strncasecmp(field, attachment_header_field_name, length) == 0 &&
68 field[length] == ':') {
69 for (p = field + length + 1; *p == ' ' || *p == '\t'; p++)
70 ;
71 if (strlen (p) > 0) {
72 has_attachment = 1;
73 }
74 }
75 }
76
77 if (has_attachment == 0)
78 return (DONE);
79
80 /*
81 * We have at least one attachment. Look for at least one non-blank line
82 * in the body of the message which indicates content in the body.
83 */
84
85 has_body = 0;
86
87 while (get_line(draft_file, field, field_size) != EOF) {
88 for (p = field; *p != '\0'; p++) {
89 if (*p != ' ' && *p != '\t') {
90 has_body = 1;
91 break;
92 }
93 }
94
95 if (has_body)
96 break;
97 }
98
99 /*
100 * Make names for the temporary files.
101 */
102
103 if ((p = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
104 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
105 }
106 (void)strncpy(body_file_name, p, body_file_name_len);
107 if ((p = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
108 unlink(body_file_name);
109 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
110 }
111 (void)strncpy(composition_file_name, p, composition_file_name_len);
112
113 if (has_body)
114 body_file = fopen(body_file_name, "w");
115
116 composition_file = fopen(composition_file_name, "w");
117
118 if ((has_body && body_file == (FILE *)0) || composition_file == (FILE *)0) {
119 clean_up_temporary_files(body_file_name, composition_file_name);
120 adios(NULL, "unable to open all of the temporary files.");
121 }
122
123 /*
124 * Start at the beginning of the draft file. Copy all
125 * non-attachment header fields to the temporary composition
126 * file. Then add the dashed line separator.
127 */
128
129 rewind(draft_file);
130
131 while (get_line(draft_file, field, field_size) != EOF && *field != '\0' &&
132 *field != '-')
133 if (strncasecmp(field, attachment_header_field_name, length) != 0 ||
134 field[length] != ':')
135 (void)fprintf(composition_file, "%s\n", field);
136
137 (void)fputs("--------\n", composition_file);
138
139 /*
140 * Copy the message body to a temporary file.
141 */
142
143 if (has_body) {
144 while ((c = getc(draft_file)) != EOF)
145 putc(c, body_file);
146
147 (void)fclose(body_file);
148 }
149
150 /*
151 * Add a mhbuild MIME composition file line for the body if there was one.
152 * Set the default content type to text/plain so that mhbuild takes care
153 * of any necessary encoding.
154 */
155
156 if (has_body)
157 /*
158 * Make sure that the attachment file exists and is readable.
159 */
160 if (stat(body_file_name, &st) != OK ||
161 access(body_file_name, R_OK) != OK) {
162 advise(NULL, "unable to access file \"%s\"", body_file_name);
163 return NOTOK;
164 }
165
166 if ((build_directive = construct_build_directive (body_file_name,
167 "text/plain",
168 attachformat)) == NULL) {
169 clean_up_temporary_files(body_file_name, composition_file_name);
170 adios (NULL, "exiting due to failure in attach()");
171 } else {
172 (void) fputs(build_directive, composition_file);
173 free(build_directive);
174 }
175
176 /*
177 * Now, go back to the beginning of the draft file and look for
178 * header fields that specify attachments. Add a mhbuild MIME
179 * composition file for each.
180 */
181
182 rewind(draft_file);
183
184 while (get_line(draft_file, field, field_size) != EOF && *field != '\0' &&
185 *field != '-') {
186 if (strncasecmp(field, attachment_header_field_name, length) == 0 &&
187 field[length] == ':') {
188 for (p = field + length + 1; *p == ' ' || *p == '\t'; p++)
189 ;
190
191 /* Skip empty attachment_header_field_name lines. */
192 if (strlen (p) > 0) {
193 struct stat st;
194 if (stat(p, &st) == OK && access(p, R_OK) == OK) {
195 if (S_ISREG (st.st_mode)) {
196 /* Don't set the default content type so that
197 construct_build_directive() will try to infer
198 it from the file type. */
199 if ((build_directive = construct_build_directive (p, 0,
200 attachformat)) == NULL) {
201 clean_up_temporary_files(body_file_name,
202 composition_file_name);
203 adios (NULL, "exiting due to failure in attach()");
204 } else {
205 (void) fputs(build_directive, composition_file);
206 free(build_directive);
207 }
208 } else {
209 adios (NULL, "unable to attach %s, not a plain file",
210 p);
211 }
212 } else {
213 adios (NULL, "unable to access file \"%s\"", p);
214 }
215 }
216 }
217 }
218
219 (void)fclose(composition_file);
220
221 /*
222 * We're ready to roll! Run mhbuild on the composition file.
223 * Note that mhbuild is in the context as buildmimeproc.
224 */
225
226 (void)sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
227
228 if (system(buf) != 0) {
229 clean_up_temporary_files(body_file_name, composition_file_name);
230 return NOTOK;
231 }
232
233 return OK;
234 }
235
236 void
237 clean_up_temporary_files(const char *body_file_name,
238 const char *composition_file_name)
239 {
240 (void) unlink(body_file_name);
241 (void) unlink(composition_file_name);
242
243 return;
244 }
245
246 static int
247 get_line(FILE *draft_file, char *field, size_t field_size)
248 {
249 int c; /* current character */
250 size_t n; /* number of bytes in buffer */
251 char *p; /* buffer pointer */
252
253 /*
254 * Get a line from the input file, growing the field buffer as
255 * needed. We do this so that we can fit an entire line in the
256 * buffer making it easy to do a string comparison on both the
257 * field name and the field body which might be a long path name.
258 */
259
260 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
261 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
262 (void)ungetc(c, draft_file);
263 c = '\n';
264 break;
265 }
266
267 if (++n >= field_size - 1) {
268 field = (char *)mh_xrealloc((void *)field, field_size += 256);
269
270 p = field + n - 1;
271 }
272 }
273
274 /*
275 * NUL-terminate the field..
276 */
277
278 *p = '\0';
279
280 return (c);
281 }
282
283 /*
284 * Try to use external command to determine mime type, and possibly
285 * encoding. Caller is responsible for free'ing returned memory.
286 */
287 char *
288 mime_type(const char *file_name) {
289 char *content_type = NULL; /* mime content type */
290
291 #ifdef MIMETYPEPROC
292 char *mimetype;
293
294 if ((mimetype = get_file_info(MIMETYPEPROC, file_name))) {
295 #ifdef MIMEENCODINGPROC
296 /* Try to append charset for text content. */
297 char *mimeencoding;
298
299 if (strncasecmp(mimetype, "text", 4) == 0) {
300 if ((mimeencoding = get_file_info(MIMEENCODINGPROC, file_name))) {
301 content_type = concat(mimetype, "; charset=", mimeencoding,
302 NULL);
303 } else {
304 content_type = strdup(mimetype);
305 }
306 } else {
307 content_type = strdup(mimetype);
308 }
309 #else /* MIMEENCODINGPROC */
310 content_type = strdup(mimetype);
311 #endif /* MIMEENCODINGPROC */
312 }
313 #else /* MIMETYPEPROC */
314 NMH_UNUSED(file_name);
315 #endif /* MIMETYPEPROC */
316
317 return content_type;
318 }
319
320
321 #ifdef MIMETYPEPROC
322 /*
323 * Get information using proc about a file.
324 */
325 static char *
326 get_file_info(const char *proc, const char *file_name) {
327 char *cmd, *cp;
328 char *quotec = "'";
329
330 if ((cp = strchr(file_name, '\''))) {
331 /* file_name contains a single quote. */
332 if (strchr(file_name, '"')) {
333 advise(NULL, "filenames containing both single and double quotes "
334 "are unsupported for attachment");
335 return NULL;
336 } else {
337 quotec = "\"";
338 }
339 }
340
341 cmd = concat(proc, " ", quotec, file_name, quotec, NULL);
342 if ((cmd = concat(proc, " ", quotec, file_name, quotec, NULL))) {
343 FILE *fp;
344
345 if ((fp = popen(cmd, "r")) != NULL) {
346 char buf[BUFSIZ >= 2048 ? BUFSIZ : 2048];
347
348 buf[0] = '\0';
349 if (fgets(buf, sizeof buf, fp)) {
350 char *eol;
351
352 /* Skip leading <filename>:<whitespace>, if present. */
353 if ((cp = strchr(buf, ':')) != NULL) {
354 ++cp;
355 while (*cp && isblank((unsigned char) *cp)) {
356 ++cp;
357 }
358 } else {
359 cp = buf;
360 }
361
362 /* Truncate at newline (LF or CR), if present. */
363 if ((eol = strpbrk(cp, "\n\r")) != NULL) {
364 *eol = '\0';
365 }
366 } else if (buf[0] == '\0') {
367 /* This can happen on Cygwin if the popen()
368 mysteriously fails. Return NULL so that the caller
369 will use another method to determine the info. */
370 free (cp);
371 cp = NULL;
372 }
373
374 (void) pclose(fp);
375 } else {
376 advise(NULL, "no output from %s", cmd);
377 }
378
379 free(cmd);
380 } else {
381 advise(NULL, "concat with \"%s\" failed, out of memory?", proc);
382 }
383
384 return cp ? strdup(cp) : NULL;
385 }
386 #endif /* MIMETYPEPROC */
387
388
389 /*
390 * Construct an mhbuild directive for the draft file. This starts
391 * with the content type. Append a file name attribute, and depending
392 * on attachformat value a private x-unix-mode attribute and a
393 * description obtained (if possible) by running the "file" command on
394 * the file. Caller is responsible for free'ing returned memory.
395 */
396 char *
397 construct_build_directive (char *file_name, const char *default_content_type,
398 int attachformat) {
399 char *build_directive = NULL; /* Return value. */
400 char *content_type; /* mime content type */
401 char cmd[PATH_MAX + 8]; /* file command buffer */
402 struct stat st; /* file status buffer */
403 char *p; /* miscellaneous temporary variables */
404 int c; /* current character */
405
406 if ((content_type = mime_type (file_name)) == NULL) {
407 /*
408 * Check the file name for a suffix. Scan the context for
409 * that suffix on a mhshow-suffix- entry. We use these
410 * entries to be compatible with mhnshow, and there's no
411 * reason to make the user specify each suffix twice. Context
412 * entries of the form "mhshow-suffix-contenttype" in the name
413 * have the suffix in the field, including the dot.
414 */
415 struct node *np; /* context scan node pointer */
416 static FILE *fp = NULL; /* pointer for mhn.defaults */
417
418 if (fp == NULL && (fp = fopen (p = etcpath ("mhn.defaults"), "r"))) {
419 readconfig ((struct node **) NULL, fp, p, 0);
420 fclose(fp);
421 }
422
423 if ((p = strrchr(file_name, '.')) != NULL) {
424 for (np = m_defs; np; np = np->n_next) {
425 if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0 &&
426 strcasecmp(p, np->n_field ? np->n_field : "") == 0) {
427 content_type = strdup (np->n_name + 14);
428 break;
429 }
430 }
431 }
432
433 if (content_type == NULL && default_content_type != NULL) {
434 content_type = strdup (default_content_type);
435 }
436 }
437
438 /*
439 * No content type was found, either because there was no matching
440 * entry in the context or because the file name has no suffix.
441 * Open the file and check for non-ASCII characters. Choose the
442 * content type based on this check.
443 */
444 if (content_type == NULL) {
445 int binary; /* binary character found flag */
446 FILE *fp;
447
448 if ((fp = fopen(file_name, "r")) == (FILE *)0) {
449 advise(NULL, "unable to access file \"%s\"", file_name);
450 return NULL;
451 }
452
453 binary = 0;
454
455 while ((c = getc(fp)) != EOF) {
456 if (c > 127 || c < 0) {
457 binary = 1;
458 break;
459 }
460 }
461
462 (void) fclose(fp);
463
464 content_type =
465 strdup (binary ? "application/octet-stream" : "text/plain");
466 }
467
468 switch (attachformat) {
469 case 0: {
470 struct stat st;
471 FILE *fp;
472 char m[4];
473
474 /* Insert name, file mode, and Content-Id. */
475 if (stat(file_name, &st) != OK || access(file_name, R_OK) != OK) {
476 advise(NULL, "unable to access file \"%s\"", file_name);
477 return NULL;
478 }
479
480 snprintf (m, sizeof m, "%.3ho", (unsigned short)(st.st_mode & 0777));
481 build_directive = concat ("#", content_type, "; name=\"",
482 ((p = strrchr(file_name, '/')) == NULL)
483 ? file_name
484 : p + 1,
485 "\"; x-unix-mode=0", m, NULL);
486
487 if (strlen(file_name) > PATH_MAX) {
488 advise(NULL, "attachment file name `%s' too long.", file_name);
489 return NULL;
490 }
491
492 (void) sprintf(cmd, "file '%s'", file_name);
493
494 if ((fp = popen(cmd, "r")) != NULL &&
495 fgets(cmd, sizeof (cmd), fp) != NULL) {
496 *strchr(cmd, '\n') = '\0';
497
498 /*
499 * The output of the "file" command is of the form
500 *
501 * file: description
502 *
503 * Strip off the "file:" and subsequent white space.
504 */
505 for (p = cmd; *p != '\0'; p++) {
506 if (*p == ':') {
507 for (p++; *p != '\0'; p++) {
508 if (*p != '\t')
509 break;
510 }
511 break;
512 }
513 }
514
515 if (*p != '\0') {
516 /* Insert Content-Description. */
517 build_directive =
518 concat (build_directive, " [ ", p, " ]", NULL);
519 }
520
521 (void) pclose(fp);
522 }
523 break;
524 }
525 case 1:
526 if (stringdex (m_maildir(invo_name), file_name) == 0) {
527 /* Content had been placed by send into a temp file.
528 Don't generate Content-Disposition header, because
529 it confuses Microsoft Outlook, Build 10.0.6626, at
530 least. */
531 build_directive = concat ("#", content_type, " <>", NULL);
532 } else {
533 /* Suppress Content-Id, insert simple Content-Disposition
534 and Content-Description with filename.
535 The Content-Disposition type needs to be "inline" for
536 MS Outlook and BlackBerry calendar programs to properly
537 handle a text/calendar attachment. */
538 p = strrchr(file_name, '/');
539 build_directive = concat ("#", content_type, "; name=\"",
540 (p == NULL) ? file_name : p + 1,
541 "\" <> [",
542 (p == NULL) ? file_name : p + 1,
543 "]{",
544 strcmp ("text/calendar", content_type)
545 ? "attachment" : "inline",
546 "}", NULL);
547 }
548
549 break;
550 case 2:
551 if (stringdex (m_maildir(invo_name), file_name) == 0) {
552 /* Content had been placed by send into a temp file.
553 Don't generate Content-Disposition header, because
554 it confuses Microsoft Outlook, Build 10.0.6626, at
555 least. */
556 build_directive = concat ("#", content_type, " <>", NULL);
557 } else {
558 /* Suppress Content-Id, insert Content-Disposition with
559 modification date and Content-Description wtih filename.
560 The Content-Disposition type needs to be "inline" for
561 MS Outlook and BlackBerry calendar programs to properly
562 handle a text/calendar attachment. */
563
564 if (stat(file_name, &st) != OK || access(file_name, R_OK) != OK) {
565 advise(NULL, "unable to access file \"%s\"", file_name);
566 return NULL;
567 }
568
569 p = strrchr(file_name, '/');
570 build_directive = concat ("#", content_type, "; name=\"",
571 (p == NULL) ? file_name : p + 1,
572 "\" <> [",
573 (p == NULL) ? file_name : p + 1,
574 "]{",
575 strcmp ("text/calendar", content_type)
576 ? "attachment" : "inline",
577 "; modification-date=\"",
578 dtime (&st.st_mtime, 0),
579 "}", NULL);
580 }
581
582 break;
583 default:
584 advise (NULL, "unsupported attachformat %d", attachformat);
585 }
586
587 free(content_type);
588
589 /*
590 * Finish up with the file name.
591 */
592 build_directive = concat (build_directive, " ", file_name, "\n", NULL);
593
594 return build_directive;
595 }