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