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