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