1 /* vi: set sw=4 ts=4: */
3 * reformime: parse MIME-encoded message
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
9 //config:config REFORMIME
10 //config: bool "reformime (7.5 kb)"
13 //config: Parse MIME-formatted messages.
15 //config:config FEATURE_REFORMIME_COMPAT
16 //config: bool "Accept and ignore options other than -x and -X"
18 //config: depends on REFORMIME
20 //config: Accept (for compatibility only) and ignore options
21 //config: other than -x and -X.
23 //applet:IF_REFORMIME(APPLET(reformime, BB_DIR_BIN, BB_SUID_DROP))
25 //kbuild:lib-$(CONFIG_REFORMIME) += reformime.o mail.o
31 # define dbg_error_msg(...) bb_error_msg(__VA_ARGS__)
33 # define dbg_error_msg(...) ((void)0)
36 static const char *find_token(const char *const string_array[], const char *key, const char *defvalue)
40 for (i = 0; string_array[i] != NULL; i++) {
41 if (strcasecmp(string_array[i], key) == 0) {
42 r = (char *)string_array[i+1];
46 return (r) ? r : defvalue;
49 static const char *xfind_token(const char *const string_array[], const char *key)
51 const char *r = find_token(string_array, key, NULL);
54 bb_error_msg_and_die("not found: '%s'", key);
60 #if ENABLE_FEATURE_REFORMIME_COMPAT
74 static int parse(const char *boundary, char **argv)
76 int boundary_len = strlen(boundary);
77 char uniq[sizeof("%%llu.%u") + sizeof(int)*3];
79 dbg_error_msg("BOUNDARY[%s]", boundary);
81 // prepare unique string pattern
82 sprintf(uniq, "%%llu.%u", (unsigned)getpid());
83 dbg_error_msg("UNIQ[%s]", uniq);
87 const char *tokens[32]; /* 32 is enough */
90 /* Read the header (everything up to two \n) */
92 unsigned header_idx = 0;
96 int ch = fgetc(stdin);
97 if (ch == '\r') /* Support both line endings */
101 if (ch == '\n' && last_ch == ch)
103 if (!(header_idx & 0xff))
104 header = xrealloc(header, header_idx + 0x101);
105 header[header_idx++] = last_ch = ch;
108 dbg_error_msg("EOF");
111 header[header_idx] = '\0';
112 dbg_error_msg("H:'%s'", p);
115 /* Split to tokens */
119 const char *delims = ";=\" \t\n";
121 /* Skip to last Content-Type: */
123 while ((p = strchr(p, '\n')) != NULL) {
125 if (strncasecmp(p, "Content-Type:", sizeof("Content-Type:")-1) == 0)
128 dbg_error_msg("L:'%s'", p);
130 s = strtok(s, delims);
133 if (ntokens < ARRAY_SIZE(tokens) - 1)
135 dbg_error_msg("L[%d]='%s'", ntokens, s);
136 s = strtok(NULL, delims);
138 tokens[ntokens] = NULL;
139 dbg_error_msg("EMPTYLINE, ntokens:%d", ntokens);
144 /* Is it multipart? */
145 type = find_token(tokens, "Content-Type:", "text/plain");
146 dbg_error_msg("TYPE:'%s'", type);
147 if (0 == strncasecmp(type, "multipart/", 10)) {
149 if (strcasecmp(type + 10, "mixed") != 0)
150 bb_error_msg_and_die("no support of content type '%s'", type);
151 parse(xfind_token(tokens, "boundary"), argv);
153 /* No, process one non-multipart section */
158 const char *charset = find_token(tokens, "charset", CONFIG_FEATURE_MIME_CHARSET);
159 const char *encoding = find_token(tokens, "Content-Transfer-Encoding:", "7bit");
161 /* Compose target filename */
162 char *filename = (char *)find_token(tokens, "filename", NULL);
164 filename = xasprintf(uniq, monotonic_us());
166 filename = bb_get_last_path_component_strip(xstrdup(filename));
171 /* start external helper */
175 /* child reads from fd[0] */
177 xmove_fd(fd[0], STDIN_FILENO);
178 xsetenv("CONTENT_TYPE", type);
179 xsetenv("CHARSET", charset);
180 xsetenv("ENCODING", encoding);
181 xsetenv("FILENAME", filename);
182 BB_EXECVP_or_die(argv);
184 /* parent will write to fd[1] */
186 fp = xfdopen_for_write(fd[1]);
187 signal(SIGPIPE, SIG_IGN);
190 char *fname = xasprintf("%s%s", *argv, filename);
191 fp = xfopen_for_write(fname);
198 if (0 == strcasecmp(encoding, "base64")) {
199 read_base64(stdin, fp, '-');
201 if (0 != strcasecmp(encoding, "7bit")
202 && 0 != strcasecmp(encoding, "8bit")
204 /* quoted-printable, binary, user-defined are unsupported so far */
205 bb_error_msg_and_die("encoding '%s' not supported", encoding);
207 /* plain 7bit or 8bit */
208 while ((end = xmalloc_fgets(stdin)) != NULL) {
211 && strncmp(end + 2, boundary, boundary_len) == 0
223 signal(SIGPIPE, SIG_DFL);
224 rc = (wait4pid(pid) & 0xff);
229 /* Multipart ended? */
230 if (end && '-' == end[2 + boundary_len] && '-' == end[2 + boundary_len + 1]) {
231 dbg_error_msg("FINISHED MPART:'%s'", end);
234 dbg_error_msg("FINISHED:'%s'", end);
236 } /* end of "handle one non-multipart block" */
241 dbg_error_msg("ENDPARSE[%s]", boundary);
246 //usage:#define reformime_trivial_usage
248 //usage:#define reformime_full_usage "\n\n"
249 //usage: "Parse MIME-encoded message on stdin\n"
250 //usage: "\n -x PREFIX Extract content of MIME sections to files"
251 //usage: "\n -X PROG ARGS Filter content of MIME sections through PROG"
252 //usage: "\n Must be the last option"
254 //usage: "\nOther options are silently ignored"
257 Usage: reformime [options]
258 -d - parse a delivery status notification.
259 -e - extract contents of MIME section.
260 -x - extract MIME section to a file.
261 -X - pipe MIME section to a program.
263 -s n.n.n.n - specify MIME section.
264 -r - rewrite message, filling in missing MIME headers.
265 -r7 - also convert 8bit/raw encoding to quoted-printable, if possible.
266 -r8 - also convert quoted-printable encoding to 8bit, if possible.
267 -c charset - default charset for rewriting, -o, and -O.
268 -m [file] [file]... - create a MIME message digest.
269 -h "header" - decode RFC 2047-encoded header.
270 -o "header" - encode unstructured header using RFC 2047.
271 -O "header" - encode address list header using RFC 2047.
274 int reformime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
275 int reformime_main(int argc UNUSED_PARAM, char **argv)
277 const char *opt_prefix = "";
282 // N.B. only -x and -X are supported so far
283 opts = getopt32(argv, "^"
284 "x:X" IF_FEATURE_REFORMIME_COMPAT("deis:r:c:m:*h:o:O:")
287 IF_FEATURE_REFORMIME_COMPAT(, NULL, NULL, &G.opt_charset, NULL, NULL, NULL, NULL)
291 return parse("", (opts & OPT_X) ? argv : (char **)&opt_prefix);