]> diplodocus.org Git - nmh/blob - uip/attach.c
6ded82eba00a247bd5981aa6ff3982d076026cc9
[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 static int make_mime_composition_file_entry(FILE *, char *, int, char *);
15
16 int
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,
20 int attachformat)
21 {
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 */
34
35
36 /*
37 * Open up the draft file.
38 */
39
40 if ((draft_file = fopen(draft_file_name, "r")) == (FILE *)0)
41 adios((char *)0, "can't open draft file `%s'.", draft_file_name);
42
43 /*
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.
46 */
47
48 field = (char *)mh_xmalloc(field_size = 256);
49
50 /*
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.
56 */
57
58 length = strlen(attachment_header_field_name);
59
60 has_attachment = 0;
61
62 while (get_line(draft_file, field, field_size) != EOF && *field != '\0' &&
63 *field != '-') {
64 if (strncasecmp(field, attachment_header_field_name, length) == 0 &&
65 field[length] == ':') {
66 for (p = field + length + 1; *p == ' ' || *p == '\t'; p++)
67 ;
68 if (strlen (p) > 0) {
69 has_attachment = 1;
70 }
71 }
72 }
73
74 if (has_attachment == 0)
75 return (DONE);
76
77 /*
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.
80 */
81
82 has_body = 0;
83
84 while (get_line(draft_file, field, field_size) != EOF) {
85 for (p = field; *p != '\0'; p++) {
86 if (*p != ' ' && *p != '\t') {
87 has_body = 1;
88 break;
89 }
90 }
91
92 if (has_body)
93 break;
94 }
95
96 /*
97 * Make names for the temporary files.
98 */
99
100 (void)strncpy(body_file_name,
101 m_mktemp(m_maildir(invo_name), NULL, NULL),
102 body_file_name_len);
103 (void)strncpy(composition_file_name,
104 m_mktemp(m_maildir(invo_name), NULL, NULL),
105 composition_file_name_len);
106
107 if (has_body)
108 body_file = fopen(body_file_name, "w");
109
110 composition_file = fopen(composition_file_name, "w");
111
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.");
115 }
116
117 /*
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.
121 */
122
123 rewind(draft_file);
124
125 while (get_line(draft_file, field, field_size) != EOF && *field != '\0' &&
126 *field != '-')
127 if (strncasecmp(field, attachment_header_field_name, length) != 0 ||
128 field[length] != ':')
129 (void)fprintf(composition_file, "%s\n", field);
130
131 (void)fputs("--------\n", composition_file);
132
133 /*
134 * Copy the message body to a temporary file.
135 */
136
137 if (has_body) {
138 while ((c = getc(draft_file)) != EOF)
139 putc(c, body_file);
140
141 (void)fclose(body_file);
142 }
143
144 /*
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.
148 */
149
150 if (has_body)
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");
155 }
156
157 /*
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.
161 */
162
163 if ((fp = fopen (p = etcpath ("mhn.defaults"), "r"))) {
164 readconfig ((struct node **) NULL, fp, p, 0);
165 fclose(fp);
166 }
167
168 rewind(draft_file);
169
170 while (get_line(draft_file, field, field_size) != EOF && *field != '\0' &&
171 *field != '-') {
172 if (strncasecmp(field, attachment_header_field_name, length) == 0 &&
173 field[length] == ':') {
174 for (p = field + length + 1; *p == ' ' || *p == '\t'; p++)
175 ;
176
177 /* Skip empty attachment_header_field_name lines. */
178 if (strlen (p) > 0) {
179 struct stat st;
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,
186 p, attachformat,
187 0)) {
188 clean_up_temporary_files(body_file_name,
189 composition_file_name);
190 adios (NULL, "exiting");
191 }
192 } else {
193 adios (NULL, "unable to attach %s, not a plain file",
194 p);
195 }
196 } else {
197 adios (NULL, "unable to access file \"%s\"", p);
198 }
199 }
200 }
201 }
202
203 (void)fclose(composition_file);
204
205 /*
206 * We're ready to roll! Run mhbuild on the composition file.
207 * Note that mhbuild is in the context as buildmimeproc.
208 */
209
210 (void)sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
211
212 if (system(buf) != 0) {
213 clean_up_temporary_files(body_file_name, composition_file_name);
214 return NOTOK;
215 }
216
217 return OK;
218 }
219
220 void
221 clean_up_temporary_files(const char *body_file_name,
222 const char *composition_file_name)
223 {
224 (void)unlink(body_file_name);
225 (void)unlink(composition_file_name);
226
227 return;
228 }
229
230 static int
231 get_line(FILE *draft_file, char *field, size_t field_size)
232 {
233 int c; /* current character */
234 size_t n; /* number of bytes in buffer */
235 char *p; /* buffer pointer */
236
237 /*
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
241 * name.
242 */
243
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);
247 c = '\n';
248 break;
249 }
250
251 if (++n >= field_size - 1) {
252 field = (char *)mh_xrealloc((void *)field, field_size += 256);
253
254 p = field + n - 1;
255 }
256 }
257
258 /*
259 * NUL-terminate the field..
260 */
261
262 *p = '\0';
263
264 return (c);
265 }
266
267 static int
268 make_mime_composition_file_entry(FILE *composition_file, char *file_name,
269 int attachformat, char *default_content_type)
270 {
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 */
279
280 if ((content_type = mime_type (file_name)) == NULL) {
281 /*
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.
288 */
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);
294 break;
295 }
296 }
297 }
298
299 if (content_type == NULL && default_content_type != NULL) {
300 content_type = strdup (default_content_type);
301 }
302 }
303
304 /*
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.
308 */
309
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);
313 return NOTOK;
314 }
315
316 binary = 0;
317
318 while ((c = getc(fp)) != EOF) {
319 if (c > 127 || c < 0) {
320 binary = 1;
321 break;
322 }
323 }
324
325 (void)fclose(fp);
326
327 content_type =
328 strdup (binary ? "application/octet-stream" : "text/plain");
329 }
330
331 /*
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.
336 */
337
338 if (stat(file_name, &st) == -1 || access(file_name, R_OK) != 0) {
339 advise((char *)0, "unable to access file \"%s\"", file_name);
340 return NOTOK;
341 }
342
343 switch (attachformat) {
344 case 0:
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));
348
349 if (strlen(file_name) > PATH_MAX) {
350 advise((char *)0, "attachment file name `%s' too long.", file_name);
351 return NOTOK;
352 }
353
354 (void)sprintf(cmd, "file '%s'", file_name);
355
356 if ((fp = popen(cmd, "r")) != (FILE *)0 && fgets(cmd, sizeof (cmd), fp) != (char *)0) {
357 *strchr(cmd, '\n') = '\0';
358
359 /*
360 * The output of the "file" command is of the form
361 *
362 * file: description
363 *
364 * Strip off the "file:" and subsequent white space.
365 */
366
367 for (p = cmd; *p != '\0'; p++) {
368 if (*p == ':') {
369 for (p++; *p != '\0'; p++) {
370 if (*p != '\t')
371 break;
372 }
373 break;
374 }
375 }
376
377 if (*p != '\0')
378 /* Insert Content-Description. */
379 (void)fprintf(composition_file, " [ %s ]", p);
380
381 (void)pclose(fp);
382 }
383
384 break;
385 case 1:
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
390 least. */
391 (void) fprintf (composition_file, "#%s <>", content_type);
392 } else {
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}",
401 content_type,
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");
406 }
407
408 break;
409 case 2:
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
414 least. */
415 (void) fprintf (composition_file, "#%s <>", content_type);
416 } else {
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\"}",
426 content_type,
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));
432 }
433
434 break;
435 default:
436 adios ((char *)0, "unsupported attachformat %d", attachformat);
437 }
438
439 free (content_type);
440
441 /*
442 * Finish up with the file name.
443 */
444
445 (void)fprintf(composition_file, " %s\n", file_name);
446
447 return OK;
448 }
449
450 /*
451 * Try to use external command to determine mime type, and possibly
452 * encoding. Caller is responsible for free'ing returned memory.
453 */
454 char *
455 mime_type (const char *file_name) {
456 char *content_type = NULL; /* mime content type */
457
458 #ifdef MIMETYPEPROC
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'";
463
464 if ((int) snprintf (cmd, sizeof cmd, mimetypeproc, file_name) <
465 (int) sizeof cmd) {
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)) {
470 char *cp, *space;
471
472 /* Skip leading <filename>:<whitespace>, if present. */
473 if ((content_type = strchr (buf, ':')) != NULL) {
474 ++content_type;
475 while (*content_type && isblank (*content_type)) {
476 ++content_type;
477 }
478 } else {
479 content_type = buf;
480 }
481
482 /* Truncate at newline (LF or CR), if present. */
483 if ((cp = strpbrk (content_type, "\n\n")) != NULL) {
484 *cp = '\0';
485 }
486
487 /* If necessary, insert semicolon between content type
488 and charset. Assume that the first space is between
489 them. */
490 if ((space = strchr (content_type, ' ')) != NULL) {
491 ssize_t len = strlen (content_type);
492
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);
499 *space = ';';
500 }
501 }
502 }
503 } else {
504 advise (NULL, "unable to read mime type");
505 }
506 } else {
507 advise (NULL, "unable to run %s", buf);
508 }
509 } else {
510 advise (NULL, "filename to large to deduce mime type");
511 }
512 #else
513 NMH_UNUSED (file_name);
514 #endif
515
516 return content_type ? strdup (content_type) : NULL;
517 }