]> diplodocus.org Git - nmh/blob - uip/attach.c
a7ae6a91f4291633ef54daf309176bdcbed3bb1b
[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(char *, size_t);
14 static int make_mime_composition_file_entry(char *, int, char *);
15
16 static FILE *draft_file; /* draft file pointer */
17 static FILE *composition_file; /* composition file pointer */
18
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; /* length of attachment header field name */
30 char *p; /* miscellaneous string pointer */
31 FILE *fp; /* pointer for mhn.defaults */
32 FILE *body_file = NULL; /* body file pointer */
33 int field_size; /* size of header field buffer */
34 char *field; /* header field buffer */
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(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(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(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(body_file_name, attachformat,
152 "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(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(p, attachformat, 0)) {
186 clean_up_temporary_files(body_file_name,
187 composition_file_name);
188 adios (NULL, "exiting");
189 }
190 } else {
191 adios (NULL, "unable to attach %s, not a plain file",
192 p);
193 }
194 } else {
195 adios (NULL, "unable to access file \"%s\"", p);
196 }
197 }
198 }
199 }
200
201 (void)fclose(composition_file);
202
203 /*
204 * We're ready to roll! Run mhbuild on the composition file.
205 * Note that mhbuild is in the context as buildmimeproc.
206 */
207
208 (void)sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
209
210 if (system(buf) != 0) {
211 clean_up_temporary_files(body_file_name, composition_file_name);
212 return NOTOK;
213 }
214
215 return OK;
216 }
217
218 void
219 clean_up_temporary_files(const char *body_file_name,
220 const char *composition_file_name)
221 {
222 (void)unlink(body_file_name);
223 (void)unlink(composition_file_name);
224
225 return;
226 }
227
228 static int
229 get_line(char *field, size_t field_size)
230 {
231 int c; /* current character */
232 size_t n; /* number of bytes in buffer */
233 char *p; /* buffer pointer */
234
235 /*
236 * Get a line from the input file, growing the field buffer as needed. We do this
237 * so that we can fit an entire line in the buffer making it easy to do a string
238 * comparison on both the field name and the field body which might be a long path
239 * name.
240 */
241
242 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
243 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
244 (void)ungetc(c, draft_file);
245 c = '\n';
246 break;
247 }
248
249 if (++n >= field_size - 1) {
250 field = (char *)mh_xrealloc((void *)field, field_size += 256);
251
252 p = field + n - 1;
253 }
254 }
255
256 /*
257 * NUL-terminate the field..
258 */
259
260 *p = '\0';
261
262 return (c);
263 }
264
265 static int
266 make_mime_composition_file_entry(char *file_name, int attachformat,
267 char *default_content_type)
268 {
269 int binary; /* binary character found flag */
270 int c; /* current character */
271 char cmd[PATH_MAX + 8]; /* file command buffer */
272 char *content_type; /* mime content type */
273 FILE *fp; /* content and pipe file pointer */
274 struct node *np; /* context scan node pointer */
275 char *p; /* miscellaneous string pointer */
276 struct stat st; /* file status buffer */
277
278 if ((content_type = mime_type (file_name)) == NULL) {
279 /*
280 * Check the file name for a suffix. Scan the context for
281 * that suffix on a mhshow-suffix- entry. We use these
282 * entries to be compatible with mhnshow, and there's no
283 * reason to make the user specify each suffix twice. Context
284 * entries of the form "mhshow-suffix-contenttype" in the name
285 * have the suffix in the field, including the dot.
286 */
287 if ((p = strrchr(file_name, '.')) != (char *)0) {
288 for (np = m_defs; np; np = np->n_next) {
289 if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0 &&
290 strcasecmp(p, np->n_field ? np->n_field : "") == 0) {
291 content_type = strdup (np->n_name + 14);
292 break;
293 }
294 }
295 }
296
297 if (content_type == NULL && default_content_type != NULL) {
298 content_type = strdup (default_content_type);
299 }
300 }
301
302 /*
303 * No content type was found, either because there was no matching entry in the
304 * context or because the file name has no suffix. Open the file and check for
305 * non-ASCII characters. Choose the content type based on this check.
306 */
307
308 if (content_type == (char *)0) {
309 if ((fp = fopen(file_name, "r")) == (FILE *)0) {
310 advise((char *)0, "unable to access file \"%s\"", file_name);
311 return NOTOK;
312 }
313
314 binary = 0;
315
316 while ((c = getc(fp)) != EOF) {
317 if (c > 127 || c < 0) {
318 binary = 1;
319 break;
320 }
321 }
322
323 (void)fclose(fp);
324
325 content_type =
326 strdup (binary ? "application/octet-stream" : "text/plain");
327 }
328
329 /*
330 * Make sure that the attachment file exists and is readable. Append a mhbuild
331 * directive to the draft file. This starts with the content type. Append a
332 * file name attribute and a private x-unix-mode attribute. Also append a
333 * description obtained (if possible) by running the "file" command on the file.
334 */
335
336 if (stat(file_name, &st) == -1 || access(file_name, R_OK) != 0) {
337 advise((char *)0, "unable to access file \"%s\"", file_name);
338 return NOTOK;
339 }
340
341 switch (attachformat) {
342 case 0:
343 /* Insert name, file mode, and Content-Id. */
344 (void)fprintf(composition_file, "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
345 content_type, ((p = strrchr(file_name, '/')) == (char *)0) ? file_name : p + 1, (unsigned short)(st.st_mode & 0777));
346
347 if (strlen(file_name) > PATH_MAX) {
348 advise((char *)0, "attachment file name `%s' too long.", file_name);
349 return NOTOK;
350 }
351
352 (void)sprintf(cmd, "file '%s'", file_name);
353
354 if ((fp = popen(cmd, "r")) != (FILE *)0 && fgets(cmd, sizeof (cmd), fp) != (char *)0) {
355 *strchr(cmd, '\n') = '\0';
356
357 /*
358 * The output of the "file" command is of the form
359 *
360 * file: description
361 *
362 * Strip off the "file:" and subsequent white space.
363 */
364
365 for (p = cmd; *p != '\0'; p++) {
366 if (*p == ':') {
367 for (p++; *p != '\0'; p++) {
368 if (*p != '\t')
369 break;
370 }
371 break;
372 }
373 }
374
375 if (*p != '\0')
376 /* Insert Content-Description. */
377 (void)fprintf(composition_file, " [ %s ]", p);
378
379 (void)pclose(fp);
380 }
381
382 break;
383 case 1:
384 if (stringdex (m_maildir(invo_name), file_name) == 0) {
385 /* Content had been placed by send into a temp file.
386 Don't generate Content-Disposition header, because
387 it confuses Microsoft Outlook, Build 10.0.6626, at
388 least. */
389 (void) fprintf (composition_file, "#%s <>", content_type);
390 } else {
391 /* Suppress Content-Id, insert simple Content-Disposition
392 and Content-Description with filename.
393 The Content-Disposition type needs to be "inline" for
394 MS Outlook and BlackBerry calendar programs to properly
395 handle a text/calendar attachment. */
396 p = strrchr(file_name, '/');
397 (void) fprintf (composition_file,
398 "#%s; name=\"%s\" <> [%s]{%s}",
399 content_type,
400 (p == (char *)0) ? file_name : p + 1,
401 (p == (char *)0) ? file_name : p + 1,
402 strcmp ("text/calendar", content_type)
403 ? "attachment" : "inline");
404 }
405
406 break;
407 case 2:
408 if (stringdex (m_maildir(invo_name), file_name) == 0) {
409 /* Content had been placed by send into a temp file.
410 Don't generate Content-Disposition header, because
411 it confuses Microsoft Outlook, Build 10.0.6626, at
412 least. */
413 (void) fprintf (composition_file, "#%s <>", content_type);
414 } else {
415 /* Suppress Content-Id, insert Content-Disposition with
416 modification date and Content-Description wtih filename.
417 The Content-Disposition type needs to be "inline" for
418 MS Outlook and BlackBerry calendar programs to properly
419 handle a text/calendar attachment. */
420 p = strrchr(file_name, '/');
421 (void) fprintf (composition_file,
422 "#%s; name=\"%s\" <>[%s]{%s; "
423 "modification-date=\"%s\"}",
424 content_type,
425 (p == (char *)0) ? file_name : p + 1,
426 (p == (char *)0) ? file_name : p + 1,
427 strcmp ("text/calendar", content_type)
428 ? "attachment" : "inline",
429 dtime (&st.st_mtime, 0));
430 }
431
432 break;
433 default:
434 adios ((char *)0, "unsupported attachformat %d", attachformat);
435 }
436
437 free (content_type);
438
439 /*
440 * Finish up with the file name.
441 */
442
443 (void)fprintf(composition_file, " %s\n", file_name);
444
445 return OK;
446 }
447
448 /*
449 * Try to use external command to determine mime type, and possibly
450 * encoding. Caller is responsible for free'ing returned memory.
451 */
452 char *
453 mime_type (const char *file_name) {
454 char *content_type = NULL; /* mime content type */
455
456 #ifdef MIMETYPEPROC
457 char cmd[2 * PATH_MAX + 2]; /* file command buffer */
458 char buf[BUFSIZ >= 2048 ? BUFSIZ : 2048];
459 FILE *fp; /* content and pipe file pointer */
460 char mimetypeproc[] = MIMETYPEPROC " '%s'";
461
462 if ((int) snprintf (cmd, sizeof cmd, mimetypeproc, file_name) <
463 (int) sizeof cmd) {
464 if ((fp = popen (cmd, "r")) != NULL) {
465 /* Make sure that buf has space for one additional
466 character, the semicolon that might be added below. */
467 if (fgets (buf, sizeof buf - 1, fp)) {
468 char *cp, *space;
469
470 /* Skip leading <filename>:<whitespace>, if present. */
471 if ((content_type = strchr (buf, ':')) != NULL) {
472 ++content_type;
473 while (*content_type && isblank (*content_type)) {
474 ++content_type;
475 }
476 } else {
477 content_type = buf;
478 }
479
480 /* Truncate at newline (LF or CR), if present. */
481 if ((cp = strpbrk (content_type, "\n\n")) != NULL) {
482 *cp = '\0';
483 }
484
485 /* If necessary, insert semicolon between content type
486 and charset. Assume that the first space is between
487 them. */
488 if ((space = strchr (content_type, ' ')) != NULL) {
489 ssize_t len = strlen (content_type);
490
491 if (space - content_type > 0 &&
492 len > space - content_type + 1) {
493 if (*(space - 1) != ';') {
494 /* The +1 is for the terminating NULL. */
495 memmove (space + 1, space,
496 len - (space - content_type) + 1);
497 *space = ';';
498 }
499 }
500 }
501 } else {
502 advise (NULL, "unable to read mime type");
503 }
504 } else {
505 advise (NULL, "unable to run %s", buf);
506 }
507 } else {
508 advise (NULL, "filename to large to deduce mime type");
509 }
510 #else
511 NMH_UNUSED (file_name);
512 #endif
513
514 return content_type ? strdup (content_type) : NULL;
515 }