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