mime_attach.c revision 1.5 1 /* $NetBSD: mime_attach.c,v 1.5 2007/10/23 14:58:44 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Anon Ymous.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #ifdef MIME_SUPPORT
40
41 #include <sys/cdefs.h>
42 #ifndef __lint__
43 __RCSID("$NetBSD: mime_attach.c,v 1.5 2007/10/23 14:58:44 christos Exp $");
44 #endif /* not __lint__ */
45
46 #include <assert.h>
47 #include <err.h>
48 #include <fcntl.h>
49 #include <libgen.h>
50 #include <magic.h>
51 #include <setjmp.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <util.h>
58
59 #include "def.h"
60 #include "extern.h"
61 #ifdef USE_EDITLINE
62 #include "complete.h"
63 #endif
64 #ifdef MIME_SUPPORT
65 #include "mime.h"
66 #include "mime_codecs.h"
67 #include "mime_child.h"
68 #endif
69 #include "glob.h"
70
71
72 #if 0
73 #ifndef __lint__
74 /*
75 * XXX - This block for debugging only and eventually should go away.
76 */
77 static void
78 show_name(const char *prefix, struct name *np)
79 {
80 int i;
81
82 i = 0;
83 for (/*EMPTY*/; np; np = np->n_flink) {
84 (void)printf("%s[%d]: %s\n", prefix, i, np->n_name);
85 i++;
86 }
87 }
88
89 static void fput_mime_content(FILE *fp, struct Content *Cp);
90
91 PUBLIC void
92 show_attach(const char *prefix, struct attachment *ap)
93 {
94 int i;
95 i = 1;
96 for (/*EMPTY*/; ap; ap = ap->a_flink) {
97 (void)printf("%s[%d]:\n", prefix, i);
98 fput_mime_content(stdout, &ap->a_Content);
99 i++;
100 }
101 }
102
103 PUBLIC void
104 show_header(struct header *hp)
105 {
106 show_name("TO", hp->h_to);
107 (void)printf("SUBJECT: %s\n", hp->h_subject);
108 show_name("CC", hp->h_cc);
109 show_name("BCC", hp->h_bcc);
110 show_name("SMOPTS", hp->h_smopts);
111 show_attach("ATTACH", hp->h_attach);
112 }
113 #endif /* __lint__ */
114 #endif
115
116
117 /***************************
118 * boundary string routines
119 */
120 static char *
121 getrandstring(size_t length)
122 {
123 void *vbin;
124 uint32_t *bin;
125 size_t binlen;
126 size_t i;
127 char *b64;
128
129 /* XXX - check this stuff again!!! */
130
131 binlen = 3 * roundup(length, 4) / 4; /* bytes of binary to encode base64 */
132 bin = vbin = salloc(roundup(binlen, 4));
133 for (i = 0; i < roundup(binlen, 4) / 4; i++)
134 bin[i] = arc4random();
135
136 b64 = salloc(roundup(length, 4));
137 mime_bintob64(b64, vbin, binlen);
138 b64[length] = '\0';
139
140 return b64;
141 }
142
143 /*
144 * Generate a boundary for MIME multipart messages.
145 */
146 static char *
147 make_boundary(void)
148 {
149 #define BOUND_LEN 70 /* maximum length is 70 characters: RFC2046 sec 5.1.1 */
150
151 char *bound;
152 time_t now;
153
154 (void)time(&now);
155 bound = salloc(BOUND_LEN);
156 (void)snprintf(bound, BOUND_LEN, "=_%08lx.%s",
157 (long)now, getrandstring(BOUND_LEN - 12));
158 return bound;
159
160 #undef BOUND_LEN
161 }
162
163
164 /***************************
165 * Transfer coding routines
166 */
167 /*
168 * We determine the recommended transfer encoding type for a file as
169 * follows:
170 *
171 * 1) If there is a NULL byte or a stray CR (not in a CRLF
172 * combination) in the file, play it safe and use base64.
173 *
174 * 2) If any high bit is set, use quoted-printable if the content type
175 * is "text" and base64 otherwise.
176 *
177 * 3) Otherwise:
178 * a) use quoted-printable if there are any long lines, control
179 * chars (including CR), end-of-line blank space, or a missing
180 * terminating NL.
181 * b) use 7bit in all remaining case, including an empty file.
182 *
183 * NOTE: This means that CRLF text (MSDOS) files will be encoded
184 * quoted-printable.
185 */
186 /*
187 * RFC 821 imposes the following line length limit:
188 * The maximum total length of a text line including the
189 * <CRLF> is 1000 characters (but not counting the leading
190 * dot duplicated for transparency).
191 */
192 #define MIME_UNENCODED_LINE_MAX (1000 - 2)
193 static size_t
194 line_limit(void)
195 {
196 int limit;
197 const char *cp;
198 limit = -1;
199
200 if ((cp = value(ENAME_MIME_UNENC_LINE_MAX)) != NULL)
201 limit = atoi(cp);
202
203 if (limit < 0 || limit > MIME_UNENCODED_LINE_MAX)
204 limit = MIME_UNENCODED_LINE_MAX;
205
206 return (size_t)limit;
207 }
208
209 static inline int
210 is_text(const char *ctype)
211 {
212 return ctype &&
213 strncasecmp(ctype, "text/", sizeof("text/") - 1) == 0;
214 }
215
216 static const char *
217 content_encoding_core(void *fh, const char *ctype)
218 {
219 int c, lastc;
220 int ctrlchar, endwhite;
221 size_t curlen, maxlen;
222
223 curlen = 0;
224 maxlen = 0;
225 ctrlchar = 0;
226 endwhite = 0;
227 lastc = EOF;
228 while ((c = fgetc(fh)) != EOF) {
229 curlen++;
230
231 if (c == '\0' || (lastc == '\r' && c != '\n'))
232 return MIME_TRANSFER_BASE64;
233
234 if (c > 0x7f) {
235 if (is_text(ctype))
236 return MIME_TRANSFER_QUOTED;
237 else
238 return MIME_TRANSFER_BASE64;
239 }
240 if (c == '\n') {
241 if (is_WSP(lastc))
242 endwhite = 1;
243 if (curlen > maxlen)
244 maxlen = curlen;
245 curlen = 0;
246 }
247 else if ((c < 0x20 && c != '\t') || c == 0x7f)
248 ctrlchar = 1;
249
250 lastc = c;
251 }
252 if (lastc == EOF) /* no characters read */
253 return MIME_TRANSFER_7BIT;
254
255 if (lastc != '\n' || ctrlchar || endwhite || maxlen > line_limit())
256 return MIME_TRANSFER_QUOTED;
257
258 return MIME_TRANSFER_7BIT;
259 }
260
261 static const char *
262 content_encoding_by_name(const char *filename, const char *ctype)
263 {
264 FILE *fp;
265 const char *enc;
266 fp = fopen(filename, "r");
267 if (fp == NULL) {
268 warn("content_encoding_by_name: %s", filename);
269 return MIME_TRANSFER_BASE64; /* safe */
270 }
271 enc = content_encoding_core(fp, ctype);
272 (void)fclose(fp);
273 return enc;
274 }
275
276 static const char *
277 content_encoding_by_fileno(int fd, const char *ctype)
278 {
279 FILE *fp;
280 const char *encoding;
281 off_t cur_pos;
282
283 cur_pos = lseek(fd, (off_t)0, SEEK_CUR);
284 fp = fdopen(fd, "r");
285 if (fp == NULL) {
286 warn("content_encoding_by_fileno");
287 return MIME_TRANSFER_BASE64;
288 }
289 encoding = content_encoding_core(fp, ctype);
290 (void)lseek(fd, cur_pos, SEEK_SET);
291 return encoding;
292 }
293
294 static const char *
295 content_encoding(struct attachment *attach, const char *ctype)
296 {
297 switch (attach->a_type) {
298 case ATTACH_FNAME:
299 return content_encoding_by_name(attach->a_name, ctype);
300 case ATTACH_MSG:
301 errx(EXIT_FAILURE, "msgno not supported yet\n");
302 /* NOTREACHED */
303 case ATTACH_FILENO:
304 return content_encoding_by_fileno(attach->a_fileno, ctype);
305 default:
306 errx(EXIT_FAILURE, "invalid attach type: %d\n", attach->a_type);
307 }
308 /* NOTREACHED */
309 }
310
311
312
313 /************************
314 * Content type routines
315 */
316 /*
317 * We use libmagic(3) to get the content type, except in the case of a
318 * 0 or 1 byte file where libmagic gives rather useless results.
319 */
320 static const char *
321 content_type_by_name(char *filename)
322 {
323 const char *cp;
324 char *cp2;
325 magic_t magic;
326 struct stat sb;
327
328 /*
329 * libmagic produces annoying results on very short files.
330 * The common case is with mime-encode-message defined and an
331 * empty message body.
332 *
333 * Note: a 1-byte message body always consists of a newline,
334 * so size determines all there. However, 1-byte attachments
335 * (filename != NULL) could be anything, so check those.
336 */
337 if ((filename != NULL && stat(filename, &sb) == 0) ||
338 (filename == NULL && fstat(0, &sb) == 0)) {
339 if (sb.st_size < 2 && S_ISREG(sb.st_mode)) {
340 FILE *fp;
341 int ch;
342 if (sb.st_size == 0 || filename == NULL ||
343 (fp = fopen(filename, "r")) == NULL)
344 return "text/plain";
345
346 ch = fgetc(fp);
347 (void)fclose(fp);
348
349 return isprint(ch) || isspace(ch) ?
350 "text/plain" : "application/octet-stream";
351 }
352 }
353 magic = magic_open(MAGIC_MIME);
354 if (magic == NULL) {
355 warn("magic_open: %s", magic_error(magic));
356 return NULL;
357 }
358 if (magic_load(magic, NULL) != 0) {
359 warn("magic_load: %s", magic_error(magic));
360 return NULL;
361 }
362 cp = magic_file(magic, filename);
363 if (cp == NULL) {
364 warn("magic_load: %s", magic_error(magic));
365 return NULL;
366 }
367 if (filename &&
368 sasprintf(&cp2, "%s; name=\"%s\"", cp, basename(filename)) != -1)
369 cp = cp2;
370 else
371 cp = savestr(cp);
372 magic_close(magic);
373 return cp;
374 }
375
376 static const char *
377 content_type_by_fileno(int fd)
378 {
379 const char *cp;
380 off_t cur_pos;
381 int ofd;
382
383 cur_pos = lseek(fd, (off_t)0, SEEK_CUR);
384
385 ofd = dup(0); /* save stdin */
386 if (dup2(fd, 0) == -1) /* become stdin */
387 warn("dup2");
388
389 cp = content_type_by_name(NULL);
390
391 if (dup2(ofd, 0) == -1) /* restore stdin */
392 warn("dup2");
393 (void)close(ofd); /* close the copy */
394
395 (void)lseek(fd, cur_pos, SEEK_SET);
396 return cp;
397 }
398
399
400 static const char *
401 content_type(struct attachment *attach)
402 {
403 switch (attach->a_type) {
404 case ATTACH_FNAME:
405 return content_type_by_name(attach->a_name);
406 case ATTACH_MSG:
407 return "message/rfc822";
408 case ATTACH_FILENO:
409 return content_type_by_fileno(attach->a_fileno);
410 default:
411 /* This is a coding error! */
412 assert(/* CONSTCOND */ 0);
413 return NULL;
414 }
415 }
416
417
418 /*************************
419 * Other content routines
420 */
421
422 static const char *
423 content_disposition(struct attachment *ap)
424 {
425 switch (ap->a_type) {
426 case ATTACH_FNAME: {
427 char *disp;
428 (void)sasprintf(&disp, "attachment; filename=\"%s\"", basename(ap->a_name));
429 return disp;
430 }
431 case ATTACH_MSG:
432 case ATTACH_FILENO:
433 return "inline";
434
435 default:
436 return NULL;
437 }
438 }
439
440 /*ARGSUSED*/
441 static const char *
442 content_id(struct attachment *attach __unused)
443 {
444 /* XXX - to be written. */
445
446 return NULL;
447 }
448
449 static const char *
450 content_description(struct attachment *attach, int attach_num)
451 {
452 if (attach_num) {
453 char *description;
454 (void)sasprintf(&description, "attachment %d", attach_num);
455 return description;
456 }
457 else
458 return attach->a_Content.C_description;
459 }
460
461 /*******************************************
462 * Routines to get the MIME content strings.
463 */
464 static struct Content
465 get_mime_content(struct attachment *ap, int i)
466 {
467 struct Content Cp;
468
469 Cp.C_type = content_type(ap);
470 Cp.C_encoding = content_encoding(ap, Cp.C_type);
471 Cp.C_disposition = content_disposition(ap);
472 Cp.C_id = content_id(ap);
473 Cp.C_description = content_description(ap, i);
474
475 return Cp;
476 }
477
478 /******************
479 * Output routines
480 */
481 static void
482 fput_mime_content(FILE *fp, struct Content *Cp)
483 {
484 (void)fprintf(fp, MIME_HDR_TYPE ": %s\n", Cp->C_type);
485 (void)fprintf(fp, MIME_HDR_ENCODING ": %s\n", Cp->C_encoding);
486 if (Cp->C_disposition)
487 (void)fprintf(fp, MIME_HDR_DISPOSITION ": %s\n",
488 Cp->C_disposition);
489 if (Cp->C_id)
490 (void)fprintf(fp, MIME_HDR_ID ": %s\n", Cp->C_id);
491 if (Cp->C_description)
492 (void)fprintf(fp, MIME_HDR_DESCRIPTION ": %s\n",
493 Cp->C_description);
494 }
495
496 static void
497 fput_body(FILE *fi, FILE *fo, struct Content *Cp)
498 {
499 mime_codec_t enc;
500
501 enc = mime_fio_encoder(Cp->C_encoding);
502 if (enc == NULL)
503 warnx("unknown transfer encoding type: %s\n", Cp->C_encoding);
504 else
505 enc(fi, fo, 0);
506 }
507
508
509 static void
510 fput_attachment(FILE *fo, struct attachment *ap)
511 {
512 FILE *fi;
513 struct Content *Cp = &ap->a_Content;
514
515 fput_mime_content(fo, &ap->a_Content);
516 (void)putc('\n', fo);
517
518 switch (ap->a_type) {
519 case ATTACH_FNAME:
520 fi = fopen(ap->a_name, "r");
521 if (fi == NULL)
522 err(EXIT_FAILURE, "fopen: %s", ap->a_name);
523 break;
524
525 case ATTACH_FILENO:
526 fi = fdopen(ap->a_fileno, "r");
527 if (fi == NULL)
528 err(EXIT_FAILURE, "fdopen: %d", ap->a_fileno);
529 break;
530
531 case ATTACH_MSG:
532 default:
533 errx(EXIT_FAILURE, "unsupported attachment type");
534 }
535
536 fput_body(fi, fo, Cp);
537
538 if (ap->a_type == ATTACH_FNAME)
539 (void)fclose(fi);
540 }
541
542 /***********************************
543 * Higher level attachment routines.
544 */
545
546 static int
547 mktemp_file(FILE **nfo, FILE **nfi, const char *hint)
548 {
549 char tempname[PATHSIZE];
550 int fd, fd2;
551 (void)snprintf(tempname, sizeof(tempname), "%s/%sXXXXXXXXXX",
552 tmpdir, hint);
553 if ((fd = mkstemp(tempname)) == -1 ||
554 (*nfo = Fdopen(fd, "w")) == NULL) {
555 if (fd != -1)
556 (void)close(fd);
557 warn("%s", tempname);
558 return -1;
559 }
560 (void)rm(tempname);
561 if ((fd2 = dup(fd)) == -1 ||
562 (*nfi = Fdopen(fd2, "r")) == NULL) {
563 warn("%s", tempname);
564 (void)Fclose(*nfo);
565 return -1;
566 }
567 return 0;
568 }
569
570
571 /*
572 * Repackage the mail as a multipart MIME message. This should always
573 * be called whenever there are attachments, but might be called even
574 * if there are none if we want to wrap the message in a MIME package.
575 */
576 PUBLIC FILE *
577 mime_encode(FILE *fi, struct header *header)
578 {
579 struct attachment map; /* fake structure for the message body */
580 struct attachment *attach;
581 struct attachment *ap;
582 FILE *nfi, *nfo;
583
584 attach = header->h_attach;
585
586 /*
587 * Make new phantom temporary file with read and write file
588 * handles: nfi and nfo, resp.
589 */
590 if (mktemp_file(&nfo, &nfi, "mail.Rs") != 0)
591 return fi;
592
593 (void)memset(&map, 0, sizeof(map));
594 map.a_type = ATTACH_FILENO;
595 map.a_fileno = fileno(fi);
596
597 map.a_Content = get_mime_content(&map, 0);
598
599 if (attach) {
600 /* Multi-part message:
601 * Make an attachment structure for the body message
602 * and make that the first element in the attach list.
603 */
604 if (fsize(fi)) {
605 map.a_flink = attach;
606 attach->a_blink = ↦
607 attach = ↦
608 }
609
610 /* Construct our MIME boundary string - used by mime_putheader() */
611 header->h_mime_boundary = make_boundary();
612
613 (void)fprintf(nfo, "This is a multi-part message in MIME format.\n");
614
615 for (ap = attach; ap; ap = ap->a_flink) {
616 (void)fprintf(nfo, "\n--%s\n", header->h_mime_boundary);
617 fput_attachment(nfo, ap);
618 }
619
620 /* the final boundary with two attached dashes */
621 (void)fprintf(nfo, "\n--%s--\n", header->h_mime_boundary);
622 }
623 else {
624 /* Single-part message (no attachments):
625 * Update header->h_Content (used by mime_putheader()).
626 * Output the body contents.
627 */
628 char *encoding;
629
630 header->h_Content = map.a_Content;
631
632 /* check for an encoding override */
633 if ((encoding = value(ENAME_MIME_ENCODE_MSG)) && *encoding)
634 header->h_Content.C_encoding = encoding;
635
636 fput_body(fi, nfo, &header->h_Content);
637 }
638 (void)Fclose(fi);
639 (void)Fclose(nfo);
640 rewind(nfi);
641 return nfi;
642 }
643
644 static char*
645 check_filename(char *filename, char *canon_name)
646 {
647 int fd;
648 struct stat sb;
649 char *fname = filename;
650
651 /* We need to expand '~' if we got here from '~@'. The shell
652 * does this otherwise.
653 */
654 if (fname[0] == '~' && fname[1] == '/') {
655 if (homedir && homedir[0] != '~')
656 (void)easprintf(&fname, "%s/%s",
657 homedir, fname + 2);
658 }
659 if (realpath(fname, canon_name) == NULL) {
660 warn("realpath: %s", filename);
661 canon_name = NULL;
662 goto done;
663 }
664 fd = open(canon_name, O_RDONLY, 0);
665 if (fd == -1) {
666 warnx("open: cannot read %s", filename);
667 canon_name = NULL;
668 goto done;
669 }
670 if (fstat(fd, &sb) == -1) {
671 warn("stat: %s", canon_name);
672 canon_name = NULL;
673 goto do_close;
674 }
675 if (!S_ISREG(sb.st_mode)) {
676 warnx("stat: %s is not a file", filename);
677 canon_name = NULL;
678 /* goto do_close; */
679 }
680 do_close:
681 (void)close(fd);
682 done:
683 if (fname != filename)
684 free(fname);
685
686 return canon_name;
687 }
688
689 static struct attachment *
690 attach_one_file(struct attachment *attach, char *filename, int attach_num)
691 {
692 char canon_name[MAXPATHLEN];
693 struct attachment *ap, *nap;
694
695 /*
696 * 1) check that filename is really a readable file.
697 * 2) allocate an attachment structure.
698 * 3) save cananonical name for filename, so cd won't screw things later.
699 * 4) add the structure to the end of the chain.
700 */
701 if (check_filename(filename, canon_name) == NULL)
702 return NULL;
703
704 nap = csalloc(1, sizeof(*nap));
705 nap->a_type = ATTACH_FNAME;
706 nap->a_name = savestr(canon_name);
707
708 if (attach == NULL)
709 attach = nap;
710 else {
711 for (ap = attach; ap->a_flink != NULL; ap = ap->a_flink)
712 continue;
713 ap->a_flink = nap;
714 nap->a_blink = ap;
715 }
716
717 if (attach_num)
718 nap->a_Content = get_mime_content(nap, attach_num);
719
720 return attach;
721 }
722
723
724 static char *
725 get_line(el_mode_t *em, const char *pr, const char *str, int i)
726 {
727 char *prompt;
728 char *line;
729
730 /*
731 * Don't use a '\t' in the format string here as completion
732 * seems to handle it badly.
733 */
734 (void)easprintf(&prompt, "#%-8d%s: ", i, pr);
735 line = my_getline(em, prompt, __UNCONST(str));
736 /* LINTED */
737 line = line ? savestr(line) : __UNCONST("");
738 free(prompt);
739
740 return line;
741 }
742
743 static void
744 sget_line(el_mode_t *em, const char *pr, const char **str, int i)
745 {
746 char *line;
747 line = get_line(em, pr, *str, i);
748 if (strcmp(line, *str) != 0)
749 *str = savestr(line);
750 }
751
752 static void
753 sget_encoding(const char **str, const char *filename, const char *ctype, int num)
754 {
755 const char *ename;
756 const char *defename;
757
758 defename = NULL;
759 ename = *str;
760 for (;;) {
761 ename = get_line(&elm.mime_enc, "encoding", ename, num);
762
763 if (*ename == '\0') {
764 if (defename == NULL)
765 defename = content_encoding_by_name(filename, ctype);
766 ename = defename;
767 }
768 else if (mime_fio_encoder(ename) == NULL) {
769 const void *cookie;
770 (void)printf("Sorry: valid encoding modes are: ");
771 cookie = NULL;
772 ename = mime_next_encoding_name(&cookie);
773 for (;;) {
774 (void)printf("%s", ename);
775 ename = mime_next_encoding_name(&cookie);
776 if (ename == NULL)
777 break;
778 (void)fputc(',', stdout);
779 }
780 ename = *str;
781 }
782 else {
783 if (strcmp(ename, *str) != 0)
784 *str = savestr(ename);
785 break;
786 }
787 }
788 }
789
790 static struct attachment *
791 edit_attachments(struct attachment *attach)
792 {
793 char canon_name[MAXPATHLEN];
794 struct attachment *ap;
795 char *line;
796 int attach_num;
797
798 (void)printf("Attachments:\n");
799
800 attach_num = 1;
801 ap = attach;
802 while (ap) {
803 line = get_line(&elm.filec, "filename", ap->a_name, attach_num);
804 if (*line == '\0') { /* omit this attachment */
805 if (ap->a_blink)
806 ap->a_blink->a_flink = ap->a_flink;
807 else
808 attach = ap->a_flink;
809 }
810 else {
811 if (strcmp(line, ap->a_name) != 0) { /* new filename */
812 if (check_filename(line, canon_name) == NULL)
813 continue;
814 ap->a_name = savestr(canon_name);
815 ap->a_Content = get_mime_content(ap, 0);
816 }
817 sget_line(&elm.string, "description",
818 &ap->a_Content.C_description, attach_num);
819 sget_encoding(&ap->a_Content.C_encoding, ap->a_name,
820 ap->a_Content.C_type, attach_num);
821 }
822 attach_num++;
823 if (ap->a_flink == NULL)
824 break;
825
826 ap = ap->a_flink;
827 }
828
829 do {
830 struct attachment *nap;
831
832 line = get_line(&elm.filec, "filename", "", attach_num);
833 if (*line == '\0')
834 break;
835
836 nap = attach_one_file(ap, line, attach_num);
837 if (nap == NULL)
838 continue;
839
840 if (ap)
841 ap = ap->a_flink;
842 else
843 ap = attach = nap;
844
845 sget_line(&elm.string, "description",
846 &ap->a_Content.C_description, attach_num);
847 sget_encoding(&ap->a_Content.C_encoding, ap->a_name,
848 ap->a_Content.C_type, attach_num);
849 attach_num++;
850
851 } while (ap);
852
853 return attach;
854 }
855
856 /*
857 * Hook used by the '~@' escape to attach files.
858 */
859 PUBLIC struct attachment*
860 mime_attach_files(struct attachment *attach, char *linebuf)
861 {
862 struct attachment *ap;
863 char *argv[MAXARGC];
864 int argc;
865 int attach_num;
866
867 argc = getrawlist(linebuf, argv, sizeofarray(argv));
868 attach_num = 1;
869 for (ap = attach; ap && ap->a_flink; ap = ap->a_flink)
870 attach_num++;
871
872 if (argc) {
873 int i;
874 for (i = 0; i < argc; i++) {
875 struct attachment *ap2;
876 ap2 = attach_one_file(ap, argv[i], attach_num);
877 if (ap2 != NULL) {
878 ap = ap2;
879 if (attach == NULL)
880 attach = ap;
881 attach_num++;
882 }
883 }
884 }
885 else {
886 attach = edit_attachments(attach);
887 (void)printf("--- end attachments ---\n");
888 }
889
890 return attach;
891 }
892
893 /*
894 * Hook called in main() to attach files registered by the '-a' flag.
895 */
896 PUBLIC struct attachment *
897 mime_attach_optargs(struct name *optargs)
898 {
899 struct attachment *attach;
900 struct attachment *ap;
901 struct name *np;
902 char *expand_optargs;
903 int attach_num;
904
905 expand_optargs = value(ENAME_MIME_ATTACH_LIST);
906 attach_num = 1;
907 ap = NULL;
908 attach = NULL;
909 for (np = optargs; np; np = np->n_flink) {
910 char *argv[MAXARGC];
911 int argc;
912 int i;
913
914 if (expand_optargs != NULL)
915 argc = getrawlist(np->n_name, argv, sizeofarray(argv));
916 else {
917 if (np->n_name == '\0')
918 argc = 0;
919 else {
920 argc = 1;
921 argv[0] = np->n_name;
922 }
923 argv[argc] = NULL;/* be consistent with getrawlist() */
924 }
925 for (i = 0; i < argc; i++) {
926 struct attachment *ap2;
927 char *filename;
928
929 if (argv[i][0] == '/') /* an absolute path */
930 (void)easprintf(&filename, "%s", argv[i]);
931 else
932 (void)easprintf(&filename, "%s/%s",
933 origdir, argv[i]);
934
935 ap2 = attach_one_file(ap, filename, attach_num);
936 free(filename);
937 if (ap2 != NULL) {
938 ap = ap2;
939 if (attach == NULL)
940 attach = ap;
941 attach_num++;
942 }
943 }
944 }
945 return attach;
946 }
947
948 /*
949 * Output MIME header strings as specified in the header structure.
950 */
951 PUBLIC void
952 mime_putheader(FILE *fp, struct header *header)
953 {
954 (void)fprintf(fp, MIME_HDR_VERSION ": " MIME_VERSION "\n");
955 if (header->h_attach) {
956 (void)fprintf(fp, MIME_HDR_TYPE ": multipart/mixed;\n");
957 (void)fprintf(fp, "\tboundary=\"%s\"\n", header->h_mime_boundary);
958 }
959 else {
960 fput_mime_content(fp, &header->h_Content);
961 }
962 }
963
964 #endif /* MIME_SUPPORT */
965