]> diplodocus.org Git - nmh/blob - uip/attach.c
3492d957eef78eba6faf5d260d2f65e95d056f35
[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(void);
14 static int make_mime_composition_file_entry(char *, int, char *);
15
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 */
20
21 int
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,
25 int attachformat)
26 {
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 */
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() != 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++)
66 ;
67 if (strlen (p) > 0) {
68 has_attachment = 1;
69 }
70 }
71 }
72
73 if (has_attachment == 0)
74 return (DONE);
75
76 /*
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.
79 */
80
81 has_body = 0;
82
83 while (get_line() != EOF) {
84 for (p = field; *p != '\0'; p++) {
85 if (*p != ' ' && *p != '\t') {
86 has_body = 1;
87 break;
88 }
89 }
90
91 if (has_body)
92 break;
93 }
94
95 /*
96 * Make names for the temporary files.
97 */
98
99 (void)strncpy(body_file_name,
100 m_mktemp(m_maildir(invo_name), NULL, NULL),
101 body_file_name_len);
102 (void)strncpy(composition_file_name,
103 m_mktemp(m_maildir(invo_name), NULL, NULL),
104 composition_file_name_len);
105
106 if (has_body)
107 body_file = fopen(body_file_name, "w");
108
109 composition_file = fopen(composition_file_name, "w");
110
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.");
114 }
115
116 /*
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.
120 */
121
122 rewind(draft_file);
123
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);
128
129 (void)fputs("--------\n", composition_file);
130
131 /*
132 * Copy the message body to a temporary file.
133 */
134
135 if (has_body) {
136 while ((c = getc(draft_file)) != EOF)
137 putc(c, body_file);
138
139 (void)fclose(body_file);
140 }
141
142 /*
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.
146 */
147
148 if (has_body)
149 if (make_mime_composition_file_entry(body_file_name, attachformat,
150 "text/plain")) {
151 clean_up_temporary_files(body_file_name, composition_file_name);
152 adios (NULL, "exiting");
153 }
154
155 /*
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.
159 */
160
161 if ((fp = fopen (p = etcpath ("mhn.defaults"), "r"))) {
162 readconfig ((struct node **) NULL, fp, p, 0);
163 fclose(fp);
164 }
165
166 rewind(draft_file);
167
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++)
172 ;
173
174 /* Skip empty attachment_header_field_name lines. */
175 if (strlen (p) > 0) {
176 struct stat st;
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");
186 }
187 } else {
188 adios (NULL, "unable to attach %s, not a plain file",
189 p);
190 }
191 } else {
192 adios (NULL, "unable to access file \"%s\"", p);
193 }
194 }
195 }
196 }
197
198 (void)fclose(composition_file);
199
200 /*
201 * We're ready to roll! Run mhbuild on the composition file.
202 * Note that mhbuild is in the context as buildmimeproc.
203 */
204
205 (void)sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
206
207 if (system(buf) != 0) {
208 clean_up_temporary_files(body_file_name, composition_file_name);
209 return NOTOK;
210 }
211
212 return OK;
213 }
214
215 void
216 clean_up_temporary_files(const char *body_file_name,
217 const char *composition_file_name)
218 {
219 (void)unlink(body_file_name);
220 (void)unlink(composition_file_name);
221
222 return;
223 }
224
225 static int
226 get_line(void)
227 {
228 int c; /* current character */
229 int n; /* number of bytes in buffer */
230 char *p; /* buffer pointer */
231
232 /*
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
236 * name.
237 */
238
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);
242 c = '\n';
243 break;
244 }
245
246 if (++n >= field_size - 1) {
247 field = (char *)mh_xrealloc((void *)field, field_size += 256);
248
249 p = field + n - 1;
250 }
251 }
252
253 /*
254 * NUL-terminate the field..
255 */
256
257 *p = '\0';
258
259 return (c);
260 }
261
262 static int
263 make_mime_composition_file_entry(char *file_name, int attachformat,
264 char *default_content_type)
265 {
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 */
274
275 if ((content_type = mime_type (file_name)) == NULL) {
276 /*
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.
283 */
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);
289 break;
290 }
291 }
292 }
293
294 if (content_type == NULL && default_content_type != NULL) {
295 content_type = strdup (default_content_type);
296 }
297 }
298
299 /*
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.
303 */
304
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);
308 return NOTOK;
309 }
310
311 binary = 0;
312
313 while ((c = getc(fp)) != EOF) {
314 if (c > 127 || c < 0) {
315 binary = 1;
316 break;
317 }
318 }
319
320 (void)fclose(fp);
321
322 content_type =
323 strdup (binary ? "application/octet-stream" : "text/plain");
324 }
325
326 /*
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.
331 */
332
333 if (stat(file_name, &st) == -1 || access(file_name, R_OK) != 0) {
334 advise((char *)0, "unable to access file \"%s\"", file_name);
335 return NOTOK;
336 }
337
338 switch (attachformat) {
339 case 0:
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));
343
344 if (strlen(file_name) > PATH_MAX) {
345 advise((char *)0, "attachment file name `%s' too long.", file_name);
346 return NOTOK;
347 }
348
349 (void)sprintf(cmd, "file '%s'", file_name);
350
351 if ((fp = popen(cmd, "r")) != (FILE *)0 && fgets(cmd, sizeof (cmd), fp) != (char *)0) {
352 *strchr(cmd, '\n') = '\0';
353
354 /*
355 * The output of the "file" command is of the form
356 *
357 * file: description
358 *
359 * Strip off the "file:" and subsequent white space.
360 */
361
362 for (p = cmd; *p != '\0'; p++) {
363 if (*p == ':') {
364 for (p++; *p != '\0'; p++) {
365 if (*p != '\t')
366 break;
367 }
368 break;
369 }
370 }
371
372 if (*p != '\0')
373 /* Insert Content-Description. */
374 (void)fprintf(composition_file, " [ %s ]", p);
375
376 (void)pclose(fp);
377 }
378
379 break;
380 case 1:
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
385 least. */
386 (void) fprintf (composition_file, "#%s <>", content_type);
387 } else {
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}",
396 content_type,
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");
401 }
402
403 break;
404 case 2:
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
409 least. */
410 (void) fprintf (composition_file, "#%s <>", content_type);
411 } else {
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\"}",
421 content_type,
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));
427 }
428
429 break;
430 default:
431 adios ((char *)0, "unsupported attachformat %d", attachformat);
432 }
433
434 free (content_type);
435
436 /*
437 * Finish up with the file name.
438 */
439
440 (void)fprintf(composition_file, " %s\n", file_name);
441
442 return OK;
443 }
444
445 /*
446 * Try to use external command to determine mime type, and possibly
447 * encoding. Caller is responsible for free'ing returned memory.
448 */
449 char *
450 mime_type (const char *file_name) {
451 char *content_type = NULL; /* mime content type */
452
453 #ifdef MIMETYPEPROC
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'";
458
459 if ((int) snprintf (cmd, sizeof cmd, mimetypeproc, file_name) <
460 (int) sizeof cmd) {
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)) {
465 char *cp, *space;
466
467 /* Skip leading <filename>:<whitespace>, if present. */
468 if ((content_type = strchr (buf, ':')) != NULL) {
469 ++content_type;
470 while (*content_type && isblank (*content_type)) {
471 ++content_type;
472 }
473 } else {
474 content_type = buf;
475 }
476
477 /* Truncate at newline (LF or CR), if present. */
478 if ((cp = strpbrk (content_type, "\n\n")) != NULL) {
479 *cp = '\0';
480 }
481
482 /* If necessary, insert semicolon between content type
483 and charset. Assume that the first space is between
484 them. */
485 if ((space = strchr (content_type, ' ')) != NULL) {
486 ssize_t len = strlen (content_type);
487
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);
494 *space = ';';
495 }
496 }
497 }
498 } else {
499 advise (NULL, "unable to read mime type");
500 }
501 } else {
502 advise (NULL, "unable to run %s", buf);
503 }
504 } else {
505 advise (NULL, "filename to large to deduce mime type");
506 }
507 #else
508 NMH_UNUSED (file_name);
509 #endif
510
511 return content_type ? strdup (content_type) : NULL;
512 }