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