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