]> diplodocus.org Git - nmh/blob - uip/mhbuild.c
inc/test-eom-align: Use octal, not hex, in printf(1) format string.
[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 int listsw = 0;
79 int rfc934sw = 0;
80 int contentidsw = 1;
81
82 /*
83 * Temporary files
84 */
85 static char infile[BUFSIZ];
86 static int unlink_infile = 0;
87
88 static char outfile[BUFSIZ];
89 static int unlink_outfile = 0;
90
91 static void unlink_done (int) NORETURN;
92
93
94 int
95 main (int argc, char **argv)
96 {
97 int sizesw = 1, headsw = 1, directives = 1, autobuild = 0, dist = 0;
98 int verbosw = 0, dispo = 0;
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], 2)) { return 1; }
111
112 set_done(unlink_done);
113
114 arguments = getarguments (invo_name, argc, argv, 1);
115 argp = arguments;
116
117 while ((cp = *argp++)) {
118 if (cp[0] == '-' && cp[1] == '\0') {
119 if (compfile)
120 adios (NULL, "cannot specify both standard input and a file");
121 compfile = cp;
122 listsw = 0; /* turn off -list if using standard in/out */
123 verbosw = 0; /* turn off -verbose listings */
124 break;
125 }
126 if (*cp == '-') {
127 switch (smatch (++cp, switches)) {
128 case AMBIGSW:
129 ambigsw (cp, switches);
130 done (1);
131 case UNKWNSW:
132 adios (NULL, "-%s unknown", cp);
133
134 case HELPSW:
135 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
136 print_help (buf, switches, 1);
137 done (0);
138 case VERSIONSW:
139 print_version(invo_name);
140 done (0);
141
142 case AUTOSW:
143 /* -auto implies -nodirectives */
144 autobuild = 1;
145 directives = 0;
146 continue;
147 case NAUTOSW:
148 /*
149 * We're turning directives back on since this is likely here
150 * to override a profile entry
151 */
152 autobuild = 0;
153 directives = 1;
154 continue;
155
156 case RCACHESW:
157 icachesw = &rcachesw;
158 goto do_cache;
159 case WCACHESW:
160 icachesw = &wcachesw;
161 do_cache: ;
162 if (!(cp = *argp++) || *cp == '-')
163 adios (NULL, "missing argument to %s", argp[-2]);
164 switch (*icachesw = smatch (cp, cache_policy)) {
165 case AMBIGSW:
166 ambigsw (cp, cache_policy);
167 done (1);
168 case UNKWNSW:
169 adios (NULL, "%s unknown", cp);
170 default:
171 break;
172 }
173 continue;
174
175 case CHECKSW:
176 checksw++;
177 continue;
178 case NCHECKSW:
179 checksw = 0;
180 continue;
181
182 case HEADSW:
183 headsw++;
184 continue;
185 case NHEADSW:
186 headsw = 0;
187 continue;
188
189 case DIRECTIVES:
190 directives = 1;
191 continue;
192 case NDIRECTIVES:
193 directives = 0;
194 continue;
195
196 case LISTSW:
197 listsw++;
198 continue;
199 case NLISTSW:
200 listsw = 0;
201 continue;
202
203 case RFC934SW:
204 rfc934sw++;
205 continue;
206 case NRFC934SW:
207 rfc934sw = 0;
208 continue;
209
210 case SIZESW:
211 sizesw++;
212 continue;
213 case NSIZESW:
214 sizesw = 0;
215 continue;
216
217 case CONTENTIDSW:
218 contentidsw = 1;
219 continue;
220 case NCONTENTIDSW:
221 contentidsw = 0;
222 continue;
223
224 case HEADERENCSW: {
225 int encoding;
226
227 if (!(cp = *argp++) || *cp == '-')
228 adios (NULL, "missing argument to %s", argp[-2]);
229 switch (encoding = smatch (cp, encodingswitches)) {
230 case AMBIGSW:
231 ambigsw (cp, encodingswitches);
232 done (1);
233 case UNKWNSW:
234 adios (NULL, "%s unknown encoding algorithm", cp);
235 case BASE64SW:
236 header_encoding = CE_BASE64;
237 break;
238 case QUOTEDPRINTSW:
239 header_encoding = CE_QUOTED;
240 break;
241 case UTF8SW:
242 header_encoding = CE_8BIT;
243 break;
244 default:
245 adios (NULL, "Internal error: algorithm %s", cp);
246 }
247 continue;
248 }
249
250 case AUTOHEADERENCSW:
251 header_encoding = CE_UNKNOWN;
252 continue;
253
254 case MAXUNENCSW:
255 if (!(cp = *argp++) || *cp == '-')
256 adios (NULL, "missing argument to %s", argp[-2]);
257 if ((maxunencoded = atoi(cp)) < 1)
258 adios (NULL, "Invalid argument for %s: %s", argp[-2], cp);
259 if (maxunencoded > 998)
260 adios (NULL, "limit of -maxunencoded is 998");
261 continue;
262
263 case VERBSW:
264 verbosw++;
265 continue;
266 case NVERBSW:
267 verbosw = 0;
268 continue;
269 case DISPOSW:
270 dispo = 1;
271 continue;
272 case NDISPOSW:
273 dispo = 0;
274 continue;
275 case DEBUGSW:
276 debugsw = 1;
277 continue;
278 case DISTSW:
279 dist = 1;
280 continue;
281 }
282 }
283 if (compfile)
284 adios (NULL, "only one composition file allowed");
285 compfile = cp;
286 }
287
288 /*
289 * Check if we've specified an additional profile
290 */
291 if ((cp = getenv ("MHBUILD"))) {
292 if ((fp = fopen (cp, "r"))) {
293 readconfig(NULL, fp, cp, 0);
294 fclose (fp);
295 } else {
296 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
297 }
298 }
299
300 /*
301 * Read the standard profile setup
302 */
303 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
304 readconfig(NULL, fp, cp, 0);
305 fclose (fp);
306 }
307
308 /* Check for public cache location */
309 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
310 cache_public = NULL;
311
312 /* Check for private cache location */
313 if (!(cache_private = context_find (nmhprivcache)))
314 cache_private = ".cache";
315 cache_private = getcpy (m_maildir (cache_private));
316
317 if (!context_find ("path"))
318 free (path ("./", TFOLDER));
319
320 /* Check if we have a file to process */
321 if (!compfile)
322 adios (NULL, "need to specify a %s composition file", invo_name);
323
324 /*
325 * Process the composition file from standard input.
326 */
327 if (compfile[0] == '-' && compfile[1] == '\0') {
328 if ((cp = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
329 adios(NULL, "unable to create temporary file in %s",
330 get_temp_dir());
331 }
332
333 /* save a copy of the name for later removal */
334 strncpy (infile, cp, sizeof(infile));
335 unlink_infile = 1;
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 adios (NULL, "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 adios(NULL, "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 adios(NULL, "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 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
394 }
395 strncpy(outfile, cp, sizeof(outfile));
396 unlink_outfile = 1;
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 unlink_outfile = 0;
421
422 free_content (ct);
423 done (0);
424 return 1;
425 }
426
427
428 static void NORETURN
429 unlink_done (int status)
430 {
431 /*
432 * Check if we need to remove stray
433 * temporary files.
434 */
435 if (unlink_infile)
436 (void) m_unlink (infile);
437 if (unlink_outfile)
438 (void) m_unlink (outfile);
439
440 exit (status);
441 }