makemime: content-type should default to application/octet-stream
[platform/upstream/busybox.git] / mailutils / makemime.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * makemime: create MIME-encoded message
4  * reformime: parse MIME-encoded message
5  *
6  * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7  *
8  * Licensed under GPLv2, see file LICENSE in this source tree.
9  */
10
11 //kbuild:lib-$(CONFIG_MAKEMIME) += makemime.o mail.o
12
13 #include "libbb.h"
14 #include "mail.h"
15
16 #if 0
17 # define dbg_error_msg(...) bb_error_msg(__VA_ARGS__)
18 #else
19 # define dbg_error_msg(...) ((void)0)
20 #endif
21
22 /*
23   makemime -c type [-o file] [-e encoding] [-C charset] [-N name] \
24                    [-a "Header: Contents"] file
25            -m [ type ] [-o file] [-e encoding] [-a "Header: Contents"] file
26            -j [-o file] file1 file2
27            @file
28
29    file:  filename    - read or write from filename
30           -           - read or write from stdin or stdout
31           &n          - read or write from file descriptor n
32           \( opts \)  - read from child process, that generates [ opts ]
33
34 Options:
35   -c type         - create a new MIME section from "file" with this
36                     Content-Type: (default is application/octet-stream).
37   -C charset      - MIME charset of a new text/plain section.
38   -N name         - MIME content name of the new mime section.
39   -m [ type ]     - create a multipart mime section from "file" of this
40                     Content-Type: (default is multipart/mixed).
41   -e encoding     - use the given encoding (7bit, 8bit, quoted-printable,
42                     or base64), instead of guessing.  Omit "-e" and use
43                     -c auto to set Content-Type: to text/plain or
44                     application/octet-stream based on picked encoding.
45   -j file1 file2  - join mime section file2 to multipart section file1.
46   -o file         - write the result to file, instead of stdout (not
47                     allowed in child processes).
48   -a header       - prepend an additional header to the output.
49
50   @file - read all of the above options from file, one option or
51           value on each line.
52   {which version of makemime is this? What do we support?}
53 */
54 /* man makemime:
55
56  * -c TYPE: create a (non-multipart) MIME section with Content-Type: TYPE
57  * makemime -c TYPE [-e ENCODING] [-o OUTFILE] [-C CHARSET] [-N NAME] [-a HEADER...] FILE
58  * The -C option sets the MIME charset attribute for text/plain content.
59  * The -N option sets the name attribute for Content-Type:
60  * Encoding must be one of the following: 7bit, 8bit, quoted-printable, or base64.
61
62  * -m multipart/TYPE: create a multipart MIME collection with Content-Type: multipart/TYPE
63  * makemime -m multipart/TYPE [-e ENCODING] [-o OUTFILE] [-a HEADER...] FILE
64  * Type must be either "multipart/mixed", "multipart/alternative", or some other MIME multipart content type.
65  * Additionally, encoding can only be "7bit" or "8bit", and will default to "8bit" if not specified.
66  * Finally, filename must be a MIME-formatted section, NOT a regular file.
67  * The -m option creates an initial multipart MIME collection, that contains only one MIME section, taken from filename.
68  * The collection is written to standard output, or the pipe or to outputfile.
69
70  * -j FILE1: add a section to a multipart MIME collection
71  * makemime -j FILE1 [-o OUTFILE] FILE2
72  * FILE1 must be a MIME collection that was previously created by the -m option.
73  * FILE2 must be a MIME section that was previously created by the -c option.
74  * The -j options adds the MIME section in FILE2 to the MIME collection in FILE1.
75  */
76
77
78 /* In busybox 1.15.0.svn, makemime generates output like this
79  * (empty lines are shown exactly!):
80 {headers added with -a HDR}
81 Mime-Version: 1.0
82 Content-Type: multipart/mixed; boundary="24269534-2145583448-1655890676"
83
84 --24269534-2145583448-1655890676
85 Content-Type: {set by -c, e.g. text/plain}; charset={set by -C, e.g. us-ascii}
86 Content-Disposition: inline; filename="A"
87 Content-Transfer-Encoding: base64
88
89 ...file A contents...
90 --24269534-2145583448-1655890676
91 Content-Type: {set by -c, e.g. text/plain}; charset={set by -C, e.g. us-ascii}
92 Content-Disposition: inline; filename="B"
93 Content-Transfer-Encoding: base64
94
95 ...file B contents...
96 --24269534-2145583448-1655890676--
97
98  *
99  * For reference: here is an example email to LKML which has
100  * 1st unnamed part (so it serves as an email body)
101  * and one attached file:
102 ...other headers...
103 Content-Type: multipart/mixed; boundary="=-tOfTf3byOS0vZgxEWcX+"
104 ...other headers...
105 Mime-Version: 1.0
106 ...other headers...
107
108
109 --=-tOfTf3byOS0vZgxEWcX+
110 Content-Type: text/plain
111 Content-Transfer-Encoding: 7bit
112
113 ...email text...
114 ...email text...
115
116
117 --=-tOfTf3byOS0vZgxEWcX+
118 Content-Disposition: attachment; filename="xyz"
119 Content-Type: text/plain; name="xyz"; charset="UTF-8"
120 Content-Transfer-Encoding: 7bit
121
122 ...file contents...
123 ...file contents...
124
125 --=-tOfTf3byOS0vZgxEWcX+--
126
127 ...random junk added by mailing list robots and such...
128 */
129
130 //usage:#define makemime_trivial_usage
131 //usage:       "[OPTIONS] [FILE]..."
132 //usage:#define makemime_full_usage "\n\n"
133 //usage:       "Create multipart MIME-encoded message from FILEs\n"
134 /* //usage:    "Transfer encoding is base64, disposition is inline (not attachment)\n" */
135 //usage:     "\n        -o FILE Output. Default: stdout"
136 //usage:     "\n        -a HDR  Add header. Examples:"
137 //usage:     "\n                \"From: user@host.org\", \"Date: `date -R`\""
138 //usage:     "\n        -c CT   Content type. Default: text/plain"
139 //usage:     "\n        -C CS   Charset. Default: " CONFIG_FEATURE_MIME_CHARSET
140 /* //usage:  "\n        -e ENC  Transfer encoding. Ignored. base64 is assumed" */
141 //usage:     "\n"
142 //usage:     "\nOther options are silently ignored"
143
144 int makemime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
145 int makemime_main(int argc UNUSED_PARAM, char **argv)
146 {
147         llist_t *opt_headers = NULL, *l;
148         const char *opt_output;
149         const char *content_type = "application/octet-stream";
150 #define boundary opt_output
151
152         enum {
153                 OPT_c = 1 << 0,         // create (non-multipart) section
154                 OPT_e = 1 << 1,         // Content-Transfer-Encoding. Ignored. Assumed base64
155                 OPT_o = 1 << 2,         // output to
156                 OPT_C = 1 << 3,         // charset
157                 OPT_N = 1 << 4,         // COMPAT
158                 OPT_a = 1 << 5,         // additional headers
159                 //OPT_m = 1 << 6,         // create mutipart section
160                 //OPT_j = 1 << 7,         // join section to multipart section
161         };
162
163         INIT_G();
164
165         // parse options
166         opt_complementary = "a::";
167         opts = getopt32(argv,
168                 "c:e:o:C:N:a:", //:m:j:",
169                 &content_type, NULL, &opt_output, &G.opt_charset, NULL, &opt_headers //, NULL, NULL
170         );
171         //argc -= optind;
172         argv += optind;
173
174         // respect -o output
175         if (opts & OPT_o)
176                 freopen(opt_output, "w", stdout);
177
178         // no files given on command line? -> use stdin
179         if (!*argv)
180                 *--argv = (char *)"-";
181
182         // put additional headers
183         for (l = opt_headers; l; l = l->link)
184                 puts(l->data);
185
186         // make a random string -- it will delimit message parts
187         srand(monotonic_us());
188         boundary = xasprintf("%u-%u-%u",
189                         (unsigned)rand(), (unsigned)rand(), (unsigned)rand());
190
191         // put multipart header
192         printf(
193                 "Mime-Version: 1.0\n"
194                 "Content-Type: multipart/mixed; boundary=\"%s\"\n"
195                 , boundary
196         );
197
198         // put attachments
199         while (*argv) {
200                 printf(
201                         "\n--%s\n"
202                         "Content-Type: %s; charset=%s\n"
203                         "Content-Disposition: inline; filename=\"%s\"\n"
204                         "Content-Transfer-Encoding: base64\n"
205                         , boundary
206                         , content_type
207                         , G.opt_charset
208                         , bb_get_last_path_component_strip(*argv)
209                 );
210                 encode_base64(*argv++, (const char *)stdin, "");
211         }
212
213         // put multipart footer
214         printf("\n--%s--\n" "\n", boundary);
215
216         return EXIT_SUCCESS;
217 #undef boundary
218 }