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