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