]> diplodocus.org Git - nmh/blob - uip/mhbuild.c
34a797e478bfdff625b90a3640edbf0ffa248efb
[nmh] / uip / mhbuild.c
1 /* mhbuild.c -- expand/translate MIME composition files
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/getarguments.h"
10 #include "sbr/smatch.h"
11 #include "sbr/m_backup.h"
12 #include "sbr/context_find.h"
13 #include "sbr/readconfig.h"
14 #include "sbr/ambigsw.h"
15 #include "sbr/path.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/error.h"
19 #include <fcntl.h>
20 #include "h/mts.h"
21 #include "h/tws.h"
22 #include "h/mime.h"
23 #include "h/mhparse.h"
24 #include "h/done.h"
25 #include "h/utils.h"
26 #include "sbr/m_maildir.h"
27 #include "sbr/m_mktemp.h"
28 #include "mhfree.h"
29 #include "mhoutsbr.h"
30
31 #define MHBUILD_SWITCHES \
32 X("auto", 0, AUTOSW) \
33 X("noauto", 0, NAUTOSW) \
34 X("check", -5, CHECKSW) \
35 X("nocheck", -7, NCHECKSW) \
36 X("directives", 0, DIRECTIVES) \
37 X("nodirectives", 0, NDIRECTIVES) \
38 X("headers", 0, HEADSW) \
39 X("noheaders", 0, NHEADSW) \
40 X("list", 0, LISTSW) \
41 X("nolist", 0, NLISTSW) \
42 X("realsize", 0, SIZESW) \
43 X("norealsize", 0, NSIZESW) \
44 X("rfc934mode", 0, RFC934SW) \
45 X("norfc934mode", 0, NRFC934SW) \
46 X("verbose", 0, VERBSW) \
47 X("noverbose", 0, NVERBSW) \
48 X("disposition", 0, DISPOSW) \
49 X("nodisposition", 0, NDISPOSW) \
50 X("contentid", 0, CONTENTIDSW) \
51 X("nocontentid", 0, NCONTENTIDSW) \
52 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
53 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
54 X("maxunencoded", 0, MAXUNENCSW) \
55 X("version", 0, VERSIONSW) \
56 X("help", 0, HELPSW) \
57 X("debug", -5, DEBUGSW) \
58 X("dist", 0, DISTSW) \
59
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(MHBUILD);
62 #undef X
63
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(MHBUILD, switches);
66 #undef X
67
68 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
69 #define MIMEENCODING_SWITCHES \
70 X("base64", 0, BASE64SW) \
71 X("quoted-printable", 0, QUOTEDPRINTSW) \
72 X("utf-8", 0, UTF8SW) \
73
74 #define X(sw, minchars, id) id,
75 DEFINE_SWITCH_ENUM(MIMEENCODING);
76 #undef X
77
78 #define X(sw, minchars, id) { sw, minchars, id },
79 DEFINE_SWITCH_ARRAY(MIMEENCODING, encodingswitches);
80 #undef X
81
82 int debugsw = 0;
83
84 bool listsw;
85 bool rfc934sw;
86 bool contentidsw = true;
87
88 /*
89 * Temporary files
90 */
91 static char infile[BUFSIZ];
92 static char outfile[BUFSIZ];
93
94
95 int
96 main (int argc, char **argv)
97 {
98 bool sizesw = true;
99 bool headsw = true;
100 bool directives = true;
101 bool autobuild = false;
102 bool dist = false;
103 bool verbosw = false;
104 bool dispo = false;
105 size_t maxunencoded = MAXTEXTPERLN;
106 char *cp, buf[BUFSIZ];
107 char buffer[BUFSIZ], *compfile = NULL;
108 char **argp, **arguments;
109 CT ct, cts[2];
110 FILE *fp = NULL;
111 FILE *fp_out = NULL;
112 int header_encoding = CE_UNKNOWN;
113 size_t n;
114
115 if (nmh_init(argv[0], true, false)) { return 1; }
116
117 arguments = getarguments (invo_name, argc, argv, 1);
118 argp = arguments;
119
120 while ((cp = *argp++)) {
121 if (cp[0] == '-' && cp[1] == '\0') {
122 if (compfile)
123 die("cannot specify both standard input and a file");
124 compfile = cp;
125 listsw = false; /* turn off -list if using standard in/out */
126 verbosw = false; /* turn off -verbose listings */
127 break;
128 }
129 if (*cp == '-') {
130 switch (smatch (++cp, switches)) {
131 case AMBIGSW:
132 ambigsw (cp, switches);
133 done (1);
134 case UNKWNSW:
135 die("-%s unknown", cp);
136
137 case HELPSW:
138 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
139 print_help (buf, switches, 1);
140 done (0);
141 case VERSIONSW:
142 print_version(invo_name);
143 done (0);
144
145 case AUTOSW:
146 /* -auto implies -nodirectives */
147 autobuild = true;
148 directives = false;
149 continue;
150 case NAUTOSW:
151 /*
152 * We're turning directives back on since this is likely here
153 * to override a profile entry
154 */
155 autobuild = false;
156 directives = true;
157 continue;
158
159 case CHECKSW:
160 case NCHECKSW:
161 /* Currently a NOP */
162 continue;
163
164 case HEADSW:
165 headsw = true;
166 continue;
167 case NHEADSW:
168 headsw = false;
169 continue;
170
171 case DIRECTIVES:
172 directives = true;
173 continue;
174 case NDIRECTIVES:
175 directives = false;
176 continue;
177
178 case LISTSW:
179 listsw = true;
180 continue;
181 case NLISTSW:
182 listsw = false;
183 continue;
184
185 case RFC934SW:
186 rfc934sw = true;
187 continue;
188 case NRFC934SW:
189 rfc934sw = false;
190 continue;
191
192 case SIZESW:
193 sizesw = true;
194 continue;
195 case NSIZESW:
196 sizesw = false;
197 continue;
198
199 case CONTENTIDSW:
200 contentidsw = true;
201 continue;
202 case NCONTENTIDSW:
203 contentidsw = false;
204 continue;
205
206 case HEADERENCSW: {
207 int encoding;
208
209 if (!(cp = *argp++) || *cp == '-')
210 die("missing argument to %s", argp[-2]);
211 switch (encoding = smatch (cp, encodingswitches)) {
212 case AMBIGSW:
213 ambigsw (cp, encodingswitches);
214 done (1);
215 case UNKWNSW:
216 die("%s unknown encoding algorithm", cp);
217 case BASE64SW:
218 header_encoding = CE_BASE64;
219 break;
220 case QUOTEDPRINTSW:
221 header_encoding = CE_QUOTED;
222 break;
223 case UTF8SW:
224 header_encoding = CE_8BIT;
225 break;
226 default:
227 die("Internal error: algorithm %s", cp);
228 }
229 continue;
230 }
231
232 case AUTOHEADERENCSW:
233 header_encoding = CE_UNKNOWN;
234 continue;
235
236 case MAXUNENCSW:
237 if (!(cp = *argp++) || *cp == '-')
238 die("missing argument to %s", argp[-2]);
239 if ((maxunencoded = atoi(cp)) < 1)
240 die("Invalid argument for %s: %s", argp[-2], cp);
241 if (maxunencoded > 998)
242 die("limit of -maxunencoded is 998");
243 continue;
244
245 case VERBSW:
246 verbosw = true;
247 continue;
248 case NVERBSW:
249 verbosw = false;
250 continue;
251 case DISPOSW:
252 dispo = true;
253 continue;
254 case NDISPOSW:
255 dispo = false;
256 continue;
257 case DEBUGSW:
258 debugsw = 1;
259 continue;
260 case DISTSW:
261 dist = true;
262 continue;
263 }
264 }
265 if (compfile)
266 die("only one composition file allowed");
267 compfile = cp;
268 }
269
270 /*
271 * Check if we've specified an additional profile
272 */
273 if ((cp = getenv ("MHBUILD"))) {
274 if ((fp = fopen (cp, "r"))) {
275 readconfig(NULL, fp, cp, 0);
276 fclose (fp);
277 } else {
278 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
279 }
280 }
281
282 /*
283 * Read the standard profile setup
284 */
285 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
286 readconfig(NULL, fp, cp, 0);
287 fclose (fp);
288 }
289
290 if (!context_find ("path"))
291 free (path ("./", TFOLDER));
292
293 /* Check if we have a file to process */
294 if (!compfile)
295 die("need to specify a %s composition file", invo_name);
296
297 /*
298 * Process the composition file from standard input.
299 */
300 if (compfile[0] == '-' && compfile[1] == '\0') {
301 if ((cp = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
302 die("unable to create temporary file in %s",
303 get_temp_dir());
304 }
305 strncpy (infile, cp, sizeof(infile));
306
307 /* copy standard input to temporary file */
308 while ((n = fread(buffer, 1, sizeof(buffer), stdin)) > 0) {
309 if (fwrite(buffer, 1, n, fp) != n) {
310 die("error copying to temporary file");
311 }
312 }
313 fclose (fp);
314
315 /* build the content structures for MIME message */
316 ct = build_mime (infile, autobuild, dist, directives, header_encoding,
317 maxunencoded, verbosw);
318
319 /*
320 * If ct == NULL, that means that -auto was set and a MIME version
321 * header was already seen. Just use the input file as the output
322 */
323
324 if (!ct) {
325 if (! (fp = fopen(infile, "r"))) {
326 die("Unable to open %s for reading", infile);
327 }
328 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
329 if (fwrite(buffer, 1, n, stdout) != n) {
330 die("error copying %s to stdout", infile);
331 }
332 }
333 } else {
334 /* output the message */
335 output_message_fp (ct, stdout, NULL);
336 free_content (ct);
337 }
338
339 done (0);
340 }
341
342 /*
343 * Process the composition file from a file.
344 */
345
346 /* build the content structures for MIME message */
347 ct = build_mime (compfile, autobuild, dist, directives, header_encoding,
348 maxunencoded, verbosw);
349
350 /*
351 * If ct == NULL, that means -auto was set and we found a MIME version
352 * header. Simply exit and do nothing.
353 */
354
355 if (! ct)
356 done(0);
357
358 cts[0] = ct;
359 cts[1] = NULL;
360
361 /* output MIME message to this temporary file */
362 if ((cp = m_mktemp2(compfile, invo_name, NULL, &fp_out)) == NULL) {
363 die("unable to create temporary file");
364 }
365 strncpy(outfile, cp, sizeof(outfile));
366
367 /* output the message */
368 output_message_fp (ct, fp_out, outfile);
369 fclose(fp_out);
370
371 /*
372 * List the message info
373 */
374 if (listsw)
375 list_all_messages (cts, headsw, sizesw, verbosw, debugsw, dispo);
376
377 /* Rename composition draft */
378 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
379 if (rename (compfile, buffer) == NOTOK) {
380 adios (compfile, "unable to rename comp draft %s to", buffer);
381 }
382
383 /* Rename output file to take its place */
384 if (rename (outfile, compfile) == NOTOK) {
385 advise (outfile, "unable to rename output %s to", compfile);
386 rename (buffer, compfile);
387 done (1);
388 }
389 /* Remove from atexit(3) list of files to unlink. */
390 if (!(m_unlink(outfile) == -1 && errno == ENOENT)) {
391 adios(outfile, "file exists after rename:");
392 }
393
394 free_content (ct);
395 done (0);
396 return 1;
397 }