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