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