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