mime_attach.c revision 1.1 1 /* $NetBSD: mime_attach.c,v 1.1 2006/10/21 21:37:21 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.1 2006/10/21 21:37:21 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 PUBLIC void
90 show_attach(const char *prefix, struct attachment *ap)
91 {
92 int i;
93 i = 1;
94 for (/* EMPTY */; ap; ap = ap->a_flink) {
95 (void)printf("%s[%d]:\n", prefix, i);
96 fput_mime_content(stdout, &ap->a_Content);
97 i++;
98 }
99 }
100
101 PUBLIC void
102 show_header(struct header *hp)
103 {
104 show_name("TO", hp->h_to);
105 (void)printf("SUBJECT: %s\n", hp->h_subject);
106 show_name("CC", hp->h_cc);
107 show_name("BCC", hp->h_bcc);
108 show_name("SMOPTS", hp->h_smopts);
109 show_attach("ATTACH", hp->h_attach);
110 }
111 #endif /* __lint__ */
112 #endif
113
114
115 /***************************
116 * boundary string routines
117 */
118 static char *
119 getrandstring(size_t length)
120 {
121 void *vbin;
122 uint32_t *bin;
123 size_t binlen;
124 size_t i;
125 char *b64;
126
127 /* XXX - check this stuff again!!! */
128
129 binlen = 3 * roundup(length, 4) / 4; /* bytes of binary to encode base64 */
130 bin = vbin = salloc(roundup(binlen, 4));
131 for (i = 0; i < roundup(binlen, 4) / 4; i++)
132 bin[i] = arc4random();
133
134 b64 = salloc(roundup(length, 4));
135 mime_bintob64(b64, vbin, binlen);
136 b64[length] = '\0';
137
138 return b64;
139 }
140
141 /*
142 * Generate a boundary for MIME multipart messages.
143 */
144 static char *
145 make_boundary(void)
146 {
147 #define BOUND_LEN 70 /* maximum length is 70 characters: RFC2046 sec 5.1.1 */
148
149 char *bound;
150 time_t now;
151
152 (void)time(&now);
153 bound = salloc(BOUND_LEN);
154 (void)snprintf(bound, BOUND_LEN, "=_%08lx.%s",
155 (long)now, getrandstring(BOUND_LEN - 12));
156 return bound;
157
158 #undef BOUND_LEN
159 }
160
161
162 /***************************
163 * Transfer coding routines
164 */
165 /*
166 * We determine the recommended transfer encoding type for a file as
167 * follows:
168 *
169 * 1) If there is a NULL byte or a stray CR (not in a CRLF
170 * combination) in the file, play it safe and use base64.
171 *
172 * 2) If any high bit is set, use quoted-printable if the content type
173 * is "text" and base64 otherwise.
174 *
175 * 3) Otherwise:
176 * a) use quoted-printable if there are any long lines, control
177 * chars (including CR), end-of-line blank space, or a missing
178 * terminating NL.
179 * b) use 7bit in all remaining case, including an empty file.
180 *
181 * NOTE: This means that CRLF text (MSDOS) files will be encoded
182 * quoted-printable.
183 */
184 /*
185 * RFC 821 imposes the following line length limit:
186 * The maximum total length of a text line including the
187 * <CRLF> is 1000 characters (but not counting the leading
188 * dot duplicated for transparency).
189 */
190 #define MIME_UNENCODED_LINE_MAX (1000 - 2)
191 static size_t
192 line_limit(void)
193 {
194 int limit;
195 const char *cp;
196 limit = -1;
197
198 if ((cp = value(ENAME_MIME_UNENC_LINE_MAX)) != NULL)
199 limit = atoi(cp);
200
201 if (limit < 0 || limit > MIME_UNENCODED_LINE_MAX)
202 limit = MIME_UNENCODED_LINE_MAX;
203
204 return (size_t)limit;
205 }
206
207 static inline
208 int is_text(const char *ctype)
209 {
210 return ctype &&
211 strncasecmp(ctype, "text/", sizeof("text/") - 1) == 0;
212 }
213
214 static const char *
215 content_encoding_core(void *fh, const char *ctype)
216 {
217 int c, lastc;
218 int ctrlchar, endwhite;
219 size_t curlen, maxlen;
220
221 curlen = 0;
222 maxlen = 0;
223 ctrlchar = 0;
224 endwhite = 0;
225 lastc = EOF;
226 while ((c = fgetc(fh)) != EOF) {
227 curlen++;
228
229 if (c == '\0' || (lastc == '\r' && c != '\n'))
230 return MIME_TRANSFER_BASE64;
231
232 if (c > 0x7f) {
233 if (is_text(ctype))
234 return MIME_TRANSFER_QUOTED;
235 else
236 return MIME_TRANSFER_BASE64;
237 }
238 if (c == '\n') {
239 if (isblank((unsigned char)lastc))
240 endwhite = 1;
241 if (curlen > maxlen)
242 maxlen = curlen;
243 curlen = 0;
244 }
245 else if ((c < 0x20 && c != '\t') || c == 0x7f)
246 ctrlchar = 1;
247
248 lastc = c;
249 }
250 if (lastc == EOF) /* no characters read */
251 return MIME_TRANSFER_7BIT;
252
253 if (lastc != '\n' || ctrlchar || endwhite || maxlen > line_limit())
254 return MIME_TRANSFER_QUOTED;
255
256 return MIME_TRANSFER_7BIT;
257 }
258
259 static const char *
260 content_encoding_by_name(const char *filename, const char *ctype)
261 {
262 FILE *fp;
263 const char *enc;
264 fp = fopen(filename, "r");
265 if (fp == NULL) {
266 warn("content_encoding_by_name: %s", filename);
267 return MIME_TRANSFER_BASE64; /* safe */
268 }
269 enc = content_encoding_core(fp, ctype);
270 (void)fclose(fp);
271 return enc;
272 }
273
274 static const char *
275 content_encoding_by_fileno(int fd, const char *ctype)
276 {
277 FILE *fp;
278 const char *encoding;
279 off_t cur_pos;
280
281 cur_pos = lseek(fd, (off_t)0, SEEK_CUR);
282 fp = fdopen(fd, "r");
283 if (fp == NULL) {
284 warn("content_encoding_by_fileno");
285 return MIME_TRANSFER_BASE64;
286 }
287 encoding = content_encoding_core(fp, ctype);
288 (void)lseek(fd, cur_pos, SEEK_SET);
289 return encoding;
290 }
291
292 static const char *
293 content_encoding(struct attachment *attach, const char *ctype)
294 {
295 switch (attach->a_type) {
296 case ATTACH_FNAME:
297 return content_encoding_by_name(attach->a_name, ctype);
298 case ATTACH_MSG:
299 errx(EXIT_FAILURE, "msgno not supported yet\n");
300 /* NOTREACHED */
301 case ATTACH_FILENO:
302 return content_encoding_by_fileno(attach->a_fileno, ctype);
303 default:
304 errx(EXIT_FAILURE, "invalid attach type: %d\n", attach->a_type);
305 }
306 /* NOTREACHED */
307 }
308
309
310
311 /************************
312 * Content type routines
313 */
314 /*
315 * We use libmagic(3) to get the content type, except in the case of a
316 * 0 or 1 byte file, which we declare "text/plain".
317 */
318 static const char *
319 content_type_by_name(const char *filename)
320 {
321 const char *cp;
322 magic_t magic;
323 struct stat sb;
324
325 /*
326 * libmagic produces annoying results on very short files.
327 * Note: a 1-byte file always consists of a newline, so size
328 * determines all here.
329 */
330 if ((filename != NULL && stat(filename, &sb) == 0) ||
331 (filename == NULL && fstat(0, &sb) == 0))
332 if (sb.st_size < 2 && S_ISREG(sb.st_mode))
333 return "text/plain";
334
335 magic = magic_open(MAGIC_MIME);
336 if (magic == NULL) {
337 warn("magic_open: %s", magic_error(magic));
338 return NULL;
339 }
340 if (magic_load(magic, NULL) != 0) {
341 warn("magic_load: %s", magic_error(magic));
342 return NULL;
343 }
344 cp = magic_file(magic, filename);
345 if (cp == NULL) {
346 warn("magic_load: %s", magic_error(magic));
347 return NULL;
348 }
349 cp = savestr(cp);
350 magic_close(magic);
351 return cp;
352 }
353
354 static const char *
355 content_type_by_fileno(int fd)
356 {
357 const char *cp;
358 off_t cur_pos;
359 int ofd;
360
361 cur_pos = lseek(fd, (off_t)0, SEEK_CUR);
362
363 ofd = dup(0); /* save stdin */
364 if (dup2(fd, 0) == -1) /* become stdin */
365 warn("dup2");
366
367 cp = content_type_by_name(NULL);
368
369 if (dup2(ofd, 0) == -1) /* restore stdin */
370 warn("dup2");
371 (void)close(ofd); /* close the copy */
372
373 (void)lseek(fd, cur_pos, SEEK_SET);
374 return cp;
375 }
376
377
378 static const char *
379 content_type(struct attachment *attach)
380 {
381 switch (attach->a_type) {
382 case ATTACH_FNAME:
383 return content_type_by_name(attach->a_name);
384 case ATTACH_MSG:
385 return "message/rfc822";
386 case ATTACH_FILENO:
387 return content_type_by_fileno(attach->a_fileno);
388 default:
389 /* This is a coding error! */
390 assert(/* CONSTCOND */ 0);
391 return NULL;
392 }
393 }
394
395
396 /*************************
397 * Other content routines
398 */
399
400 static const char *
401 content_disposition(struct attachment *ap)
402 {
403 switch (ap->a_type) {
404 case ATTACH_FNAME: {
405 char *buf;
406 char *disp;
407 (void)easprintf(&buf, "attachment; filename=\"%s\"", basename(ap->a_name));
408 disp = savestr(buf);
409 free(buf);
410 return disp;
411 }
412 case ATTACH_MSG:
413 case ATTACH_FILENO:
414 return "inline";
415
416 default:
417 return NULL;
418 }
419 }
420
421 /*ARGSUSED*/
422 static const char *
423 content_id(struct attachment *attach __unused)
424 {
425 /* XXX - to be written. */
426
427 return NULL;
428 }
429
430 static const char *
431 content_description(struct attachment *attach, int attach_num)
432 {
433 if (attach_num) {
434 char *description;
435 char *cp;
436 (void)easprintf(&cp, "attachment %d", attach_num);
437 description = savestr(cp);
438 free(cp);
439 return description;
440 }
441 else
442 return attach->a_Content.C_description;
443 }
444
445 /*******************************************
446 * Routines to get the MIME content strings.
447 */
448 static struct Content
449 get_mime_content(struct attachment *ap, int i)
450 {
451 struct Content Cp;
452
453 Cp.C_type = content_type(ap);
454 Cp.C_encoding = content_encoding(ap, Cp.C_type);
455 Cp.C_disposition = content_disposition(ap);
456 Cp.C_id = content_id(ap);
457 Cp.C_description = content_description(ap, i);
458
459 return Cp;
460 }
461
462 /*
463 * A hook to complete the content part of the attachment struct. This
464 * is used when a file is attached by the '-a' to complete the process
465 * after the flags have been processed.
466 */
467 PUBLIC void
468 mime_attach_content(struct attachment *ap)
469 {
470 int i;
471 i = 1;
472
473 for (/* EMPTY */; ap; ap = ap->a_flink)
474 ap->a_Content = get_mime_content(ap, i++);
475 }
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 * 1) check that filename is really a file.
652 * 2) check that filename is readable.
653 * 3) allocate an attachment structure.
654 * 4) save cananonical name for filename, so cd won't screw things later.
655 * 5) add the structure to the end of the chain.
656 */
657
658 if (fname[0] == '~' && fname[1] == '/') {
659 char *home = getenv("HOME");
660 /*
661 * XXX - we could use the global 'homedir' here if we
662 * move tinit() before getopt() in main.c
663 */
664 if (home && home[0] != '~')
665 (void)easprintf(&fname, "%s/%s", home, fname + 2);
666 }
667 if (realpath(fname, canon_name) == NULL) {
668 warn("realpath: %s", filename);
669 canon_name = NULL;
670 goto done;
671 }
672 fd = open(canon_name, O_RDONLY, 0);
673 if (fd == -1) {
674 warnx("open: cannot read %s", filename);
675 canon_name = NULL;
676 goto done;
677 }
678 if (fstat(fd, &sb) == -1) {
679 warn("stat: %s", canon_name);
680 canon_name = NULL;
681 goto do_close;
682 }
683 if (!S_ISREG(sb.st_mode)) {
684 warnx("stat: %s is not a file", filename);
685 canon_name = NULL;
686 /* goto do_close; */
687 }
688 do_close:
689 (void)close(fd);
690 done:
691 if (fname != filename)
692 free(fname);
693
694 return canon_name;
695 }
696
697 static struct attachment *
698 attach_one_file(struct attachment *attach, char *filename, int attach_num)
699 {
700 char canon_name[MAXPATHLEN];
701 struct attachment *ap, *nap;
702
703 if (check_filename(filename, canon_name) == NULL)
704 return NULL;
705
706 nap = csalloc(1, sizeof(*nap));
707 nap->a_type = ATTACH_FNAME;
708 nap->a_name = savestr(canon_name);
709
710 if (attach == NULL)
711 attach = nap;
712 else {
713 for (ap = attach; ap->a_flink != NULL; ap = ap->a_flink)
714 continue;
715 ap->a_flink = nap;
716 nap->a_blink = ap;
717 }
718
719 if (attach_num)
720 nap->a_Content = get_mime_content(nap, attach_num);
721
722 return attach;
723 }
724
725
726 static jmp_buf intjmp;
727 /*ARGSUSED*/
728 static void
729 sigint(int signum __unused)
730 {
731 siglongjmp(intjmp, 1);
732 }
733
734 static char *
735 get_line(el_mode_t *em, const char *pr, const char *str, int i)
736 {
737 sig_t saveint;
738 char *cp;
739 char *line;
740 char *prompt;
741
742 saveint = signal(SIGINT, sigint);
743 if (sigsetjmp(intjmp, 1)) {
744 (void)signal(SIGINT, saveint);
745 (void)putc('\n', stdout);
746 return __UNCONST("");
747 }
748
749 /* Don't use a '\t' in the format string here as completion
750 * seems to handle it badly. */
751 (void)easprintf(&prompt, "#%-8d%s: ", i, pr);
752 line = my_gets(em, prompt, __UNCONST(str));
753 /* LINTED */
754 line = line ? savestr(line) : __UNCONST("");
755 free(prompt);
756
757 (void)signal(SIGINT, saveint);
758
759 /* strip trailing white space */
760 for (cp = line + strlen(line) - 1;
761 cp >= line && isblank((unsigned char)*cp); cp--)
762 *cp = '\0';
763
764 /* skip leading white space */
765 cp = skip_white(line);
766
767 return cp;
768 }
769
770 static void
771 sget_line(el_mode_t *em, const char *pr, const char **str, int i)
772 {
773 char *line;
774 line = get_line(em, pr, *str, i);
775 if (strcmp(line, *str) != 0)
776 *str = savestr(line);
777 }
778
779 static void
780 sget_encoding(const char **str, const char *filename, const char *ctype, int num)
781 {
782 const char *ename;
783 const char *defename;
784
785 defename = NULL;
786 ename = *str;
787 for (;;) {
788 ename = get_line(&elm.mime_enc, "encoding", ename, num);
789
790 if (*ename == '\0') {
791 if (defename == NULL)
792 defename = content_encoding_by_name(filename, ctype);
793 ename = defename;
794 }
795 else if (mime_fio_encoder(ename) == NULL) {
796 const void *cookie;
797 (void)printf("Sorry: valid encoding modes are: ");
798 cookie = NULL;
799 ename = mime_next_encoding_name(&cookie);
800 for (;;) {
801 (void)printf("%s", ename);
802 ename = mime_next_encoding_name(&cookie);
803 if (ename == NULL)
804 break;
805 (void)fputc(',', stdout);
806 }
807 ename = *str;
808 }
809 else {
810 if (strcmp(ename, *str) != 0)
811 *str = savestr(ename);
812 break;
813 }
814 }
815 }
816
817 static struct attachment *
818 edit_attachments(struct attachment *attach)
819 {
820 char canon_name[MAXPATHLEN];
821 struct attachment *ap;
822 char *line;
823 int i;
824
825 (void)printf("Attachments:\n");
826
827 i = 1;
828 ap = attach;
829 while (ap) {
830 line = get_line(&elm.filec, "filename", ap->a_name, i);
831 if (*line == '\0') { /* omit this attachment */
832 if (ap->a_blink)
833 ap->a_blink->a_flink = ap->a_flink;
834 else
835 attach = ap->a_flink;
836 }
837 else {
838 if (strcmp(line, ap->a_name) != 0) { /* new filename */
839 if (check_filename(line, canon_name) == NULL)
840 continue;
841 ap->a_name = savestr(canon_name);
842 ap->a_Content = get_mime_content(ap, 0);
843 }
844 sget_line(&elm.string, "description", &ap->a_Content.C_description, i);
845 sget_encoding(&ap->a_Content.C_encoding, ap->a_name, ap->a_Content.C_type, i);
846 }
847 i++;
848 if (ap->a_flink == NULL)
849 break;
850
851 ap = ap->a_flink;
852 }
853
854 do {
855 struct attachment *nap;
856
857 line = get_line(&elm.filec, "filename", "", i);
858 if (*line == '\0')
859 break;
860
861 nap = attach_one_file(ap, line, i);
862 if (nap == NULL)
863 continue;
864
865 if (ap)
866 ap = ap->a_flink;
867 else
868 ap = attach = nap;
869
870 sget_line(&elm.string, "description", &ap->a_Content.C_description, i);
871 sget_encoding(&ap->a_Content.C_encoding, ap->a_name, ap->a_Content.C_type, i);
872 i++;
873
874 } while (ap);
875
876 return attach;
877 }
878
879 /*
880 * Hook used by the '-a' flag and '~@' escape to attach files.
881 */
882 PUBLIC struct attachment*
883 mime_attach_files(struct attachment *attach, char *linebuf, int with_content)
884 {
885 struct attachment *ap;
886 char *argv[MAXARGC];
887 int argc;
888 int attach_num;
889
890 argc = getrawlist(linebuf, argv, sizeof(argv)/sizeof(*argv));
891
892 attach_num = attach ? 1 : 0;
893 for (ap = attach; ap && ap->a_flink; ap = ap->a_flink)
894 attach_num++;
895
896 if (argc) {
897 int i;
898 for (i = 0; i < argc; i++) {
899 attach_num++;
900 ap = attach_one_file(ap, argv[i], with_content ? attach_num : 0);
901 if (attach == NULL)
902 attach = ap;
903 }
904 }
905 else {
906 attach = edit_attachments(attach);
907 (void)printf("--- end attachments ---\n");
908 }
909
910 return attach;
911 }
912
913 /*
914 * Output MIME header strings as specified in the header structure.
915 */
916 PUBLIC void
917 mime_putheader(FILE *fp, struct header *header)
918 {
919 (void)fprintf(fp, MIME_HDR_VERSION ": " MIME_VERSION "\n");
920 if (header->h_attach) {
921 (void)fprintf(fp, MIME_HDR_TYPE ": multipart/mixed;\n");
922 (void)fprintf(fp, "\tboundary=\"%s\"\n", header->h_mime_boundary);
923 }
924 else {
925 fput_mime_content(fp, &header->h_Content);
926 }
927 }
928
929 #endif /* MIME_SUPPORT */
930