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