]> diplodocus.org Git - nmh/blob - uip/mhbuild.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[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 <fcntl.h>
10 #include <h/md5.h>
11 #include <h/mts.h>
12 #include <h/tws.h>
13 #include <h/mime.h>
14 #include <h/mhparse.h>
15 #include <h/mhcachesbr.h>
16 #include <h/utils.h>
17 #include "sbr/m_maildir.h"
18 #include "sbr/m_mktemp.h"
19 #include "mhfree.h"
20 #include "mhoutsbr.h"
21
22 #define MHBUILD_SWITCHES \
23 X("auto", 0, AUTOSW) \
24 X("noauto", 0, NAUTOSW) \
25 X("check", 0, CHECKSW) \
26 X("nocheck", 0, NCHECKSW) \
27 X("directives", 0, DIRECTIVES) \
28 X("nodirectives", 0, NDIRECTIVES) \
29 X("headers", 0, HEADSW) \
30 X("noheaders", 0, NHEADSW) \
31 X("list", 0, LISTSW) \
32 X("nolist", 0, NLISTSW) \
33 X("realsize", 0, SIZESW) \
34 X("norealsize", 0, NSIZESW) \
35 X("rfc934mode", 0, RFC934SW) \
36 X("norfc934mode", 0, NRFC934SW) \
37 X("verbose", 0, VERBSW) \
38 X("noverbose", 0, NVERBSW) \
39 X("disposition", 0, DISPOSW) \
40 X("nodisposition", 0, NDISPOSW) \
41 X("rcache policy", 0, RCACHESW) \
42 X("wcache policy", 0, WCACHESW) \
43 X("contentid", 0, CONTENTIDSW) \
44 X("nocontentid", 0, NCONTENTIDSW) \
45 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
46 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
47 X("maxunencoded", 0, MAXUNENCSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
50 X("debug", -5, DEBUGSW) \
51 X("dist", 0, DISTSW) \
52
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(MHBUILD);
55 #undef X
56
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(MHBUILD, switches);
59 #undef X
60
61 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
62 #define MIMEENCODING_SWITCHES \
63 X("base64", 0, BASE64SW) \
64 X("quoted-printable", 0, QUOTEDPRINTSW) \
65 X("utf-8", 0, UTF8SW) \
66
67 #define X(sw, minchars, id) id,
68 DEFINE_SWITCH_ENUM(MIMEENCODING);
69 #undef X
70
71 #define X(sw, minchars, id) { sw, minchars, id },
72 DEFINE_SWITCH_ARRAY(MIMEENCODING, encodingswitches);
73 #undef X
74
75 int debugsw = 0;
76
77 int listsw = 0;
78 int rfc934sw = 0;
79 int contentidsw = 1;
80
81 /*
82 * Temporary files
83 */
84 static char infile[BUFSIZ];
85 static int unlink_infile = 0;
86
87 static char outfile[BUFSIZ];
88 static int unlink_outfile = 0;
89
90 static void unlink_done (int) NORETURN;
91
92
93 int
94 main (int argc, char **argv)
95 {
96 int sizesw = 1, headsw = 1, directives = 1, autobuild = 0, dist = 0;
97 int verbosw = 0, dispo = 0;
98 size_t maxunencoded = MAXTEXTPERLN;
99 int *icachesw;
100 char *cp, buf[BUFSIZ];
101 char buffer[BUFSIZ], *compfile = NULL;
102 char **argp, **arguments;
103 CT ct, cts[2];
104 FILE *fp = NULL;
105 FILE *fp_out = NULL;
106 int header_encoding = CE_UNKNOWN;
107 size_t n;
108
109 if (nmh_init(argv[0], 2)) { return 1; }
110
111 done=unlink_done;
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 adios (NULL, "cannot specify both standard input and a file");
120 compfile = cp;
121 listsw = 0; /* turn off -list if using standard in/out */
122 verbosw = 0; /* 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 adios (NULL, "-%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 = 1;
144 directives = 0;
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 = 0;
152 directives = 1;
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 adios (NULL, "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 adios (NULL, "%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++;
183 continue;
184 case NHEADSW:
185 headsw = 0;
186 continue;
187
188 case DIRECTIVES:
189 directives = 1;
190 continue;
191 case NDIRECTIVES:
192 directives = 0;
193 continue;
194
195 case LISTSW:
196 listsw++;
197 continue;
198 case NLISTSW:
199 listsw = 0;
200 continue;
201
202 case RFC934SW:
203 rfc934sw++;
204 continue;
205 case NRFC934SW:
206 rfc934sw = 0;
207 continue;
208
209 case SIZESW:
210 sizesw++;
211 continue;
212 case NSIZESW:
213 sizesw = 0;
214 continue;
215
216 case CONTENTIDSW:
217 contentidsw = 1;
218 continue;
219 case NCONTENTIDSW:
220 contentidsw = 0;
221 continue;
222
223 case HEADERENCSW: {
224 int encoding;
225
226 if (!(cp = *argp++) || *cp == '-')
227 adios (NULL, "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 adios (NULL, "%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 adios (NULL, "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 adios (NULL, "missing argument to %s", argp[-2]);
256 if ((maxunencoded = atoi(cp)) < 1)
257 adios (NULL, "Invalid argument for %s: %s", argp[-2], cp);
258 if (maxunencoded > 998)
259 adios (NULL, "limit of -maxunencoded is 998");
260 continue;
261
262 case VERBSW:
263 verbosw++;
264 continue;
265 case NVERBSW:
266 verbosw = 0;
267 continue;
268 case DISPOSW:
269 dispo = 1;
270 continue;
271 case NDISPOSW:
272 dispo = 0;
273 continue;
274 case DEBUGSW:
275 debugsw = 1;
276 continue;
277 case DISTSW:
278 dist = 1;
279 continue;
280 }
281 }
282 if (compfile)
283 adios (NULL, "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 = getcpy (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 adios (NULL, "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 adios(NULL, "unable to create temporary file in %s",
329 get_temp_dir());
330 }
331
332 /* save a copy of the name for later removal */
333 strncpy (infile, cp, sizeof(infile));
334 unlink_infile = 1;
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 adios (NULL, "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 adios(NULL, "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 adios(NULL, "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 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
393 }
394 strncpy(outfile, cp, sizeof(outfile));
395 unlink_outfile = 1;
396
397 /* output the message */
398 output_message_fp (ct, fp_out, outfile);
399 fclose(fp_out);
400
401 /*
402 * List the message info
403 */
404 if (listsw)
405 list_all_messages (cts, headsw, sizesw, verbosw, debugsw, dispo);
406
407 /* Rename composition draft */
408 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
409 if (rename (compfile, buffer) == NOTOK) {
410 adios (compfile, "unable to rename comp draft %s to", buffer);
411 }
412
413 /* Rename output file to take its place */
414 if (rename (outfile, compfile) == NOTOK) {
415 advise (outfile, "unable to rename output %s to", compfile);
416 rename (buffer, compfile);
417 done (1);
418 }
419 unlink_outfile = 0;
420
421 free_content (ct);
422 done (0);
423 return 1;
424 }
425
426
427 static void NORETURN
428 unlink_done (int status)
429 {
430 /*
431 * Check if we need to remove stray
432 * temporary files.
433 */
434 if (unlink_infile)
435 (void) m_unlink (infile);
436 if (unlink_outfile)
437 (void) m_unlink (outfile);
438
439 exit (status);
440 }