]> diplodocus.org Git - nmh/blob - uip/mhbuild.c
Simplified m_strn() per Ralph's suggestions.
[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 else
121 compfile = cp;
122 listsw = 0; /* turn off -list if using standard in/out */
123 verbosw = 0; /* turn off -verbose listings */
124 break;
125 }
126 if (*cp == '-') {
127 switch (smatch (++cp, switches)) {
128 case AMBIGSW:
129 ambigsw (cp, switches);
130 done (1);
131 case UNKWNSW:
132 adios (NULL, "-%s unknown", cp);
133
134 case HELPSW:
135 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
136 print_help (buf, switches, 1);
137 done (0);
138 case VERSIONSW:
139 print_version(invo_name);
140 done (0);
141
142 case AUTOSW:
143 /* -auto implies -nodirectives */
144 autobuild = 1;
145 directives = 0;
146 continue;
147 case NAUTOSW:
148 /*
149 * We're turning directives back on since this is likely here
150 * to override a profile entry
151 */
152 autobuild = 0;
153 directives = 1;
154 continue;
155
156 case RCACHESW:
157 icachesw = &rcachesw;
158 goto do_cache;
159 case WCACHESW:
160 icachesw = &wcachesw;
161 do_cache: ;
162 if (!(cp = *argp++) || *cp == '-')
163 adios (NULL, "missing argument to %s", argp[-2]);
164 switch (*icachesw = smatch (cp, cache_policy)) {
165 case AMBIGSW:
166 ambigsw (cp, cache_policy);
167 done (1);
168 case UNKWNSW:
169 adios (NULL, "%s unknown", cp);
170 default:
171 break;
172 }
173 continue;
174
175 case CHECKSW:
176 checksw++;
177 continue;
178 case NCHECKSW:
179 checksw = 0;
180 continue;
181
182 case HEADSW:
183 headsw++;
184 continue;
185 case NHEADSW:
186 headsw = 0;
187 continue;
188
189 case DIRECTIVES:
190 directives = 1;
191 continue;
192 case NDIRECTIVES:
193 directives = 0;
194 continue;
195
196 case LISTSW:
197 listsw++;
198 continue;
199 case NLISTSW:
200 listsw = 0;
201 continue;
202
203 case RFC934SW:
204 rfc934sw++;
205 continue;
206 case NRFC934SW:
207 rfc934sw = 0;
208 continue;
209
210 case SIZESW:
211 sizesw++;
212 continue;
213 case NSIZESW:
214 sizesw = 0;
215 continue;
216
217 case CONTENTIDSW:
218 contentidsw = 1;
219 continue;
220 case NCONTENTIDSW:
221 contentidsw = 0;
222 continue;
223
224 case HEADERENCSW: {
225 int encoding;
226
227 if (!(cp = *argp++) || *cp == '-')
228 adios (NULL, "missing argument to %s", argp[-2]);
229 switch (encoding = smatch (cp, encodingswitches)) {
230 case AMBIGSW:
231 ambigsw (cp, encodingswitches);
232 done (1);
233 case UNKWNSW:
234 adios (NULL, "%s unknown encoding algorithm", cp);
235 case BASE64SW:
236 header_encoding = CE_BASE64;
237 break;
238 case QUOTEDPRINTSW:
239 header_encoding = CE_QUOTED;
240 break;
241 case UTF8SW:
242 header_encoding = CE_8BIT;
243 break;
244 default:
245 adios (NULL, "Internal error: algorithm %s", cp);
246 }
247 continue;
248 }
249
250 case AUTOHEADERENCSW:
251 header_encoding = CE_UNKNOWN;
252 continue;
253
254 case MAXUNENCSW:
255 if (!(cp = *argp++) || *cp == '-')
256 adios (NULL, "missing argument to %s", argp[-2]);
257 if ((maxunencoded = atoi(cp)) < 1)
258 adios (NULL, "Invalid argument for %s: %s", argp[-2], cp);
259 if (maxunencoded > 998)
260 adios (NULL, "limit of -maxunencoded is 998");
261 continue;
262
263 case VERBSW:
264 verbosw++;
265 continue;
266 case NVERBSW:
267 verbosw = 0;
268 continue;
269 case DISPOSW:
270 dispo = 1;
271 continue;
272 case NDISPOSW:
273 dispo = 0;
274 continue;
275 case DEBUGSW:
276 debugsw = 1;
277 continue;
278 case DISTSW:
279 dist = 1;
280 continue;
281 }
282 }
283 if (compfile)
284 adios (NULL, "only one composition file allowed");
285 else
286 compfile = cp;
287 }
288
289 /*
290 * Check if we've specified an additional profile
291 */
292 if ((cp = getenv ("MHBUILD"))) {
293 if ((fp = fopen (cp, "r"))) {
294 readconfig ((struct node **) 0, fp, cp, 0);
295 fclose (fp);
296 } else {
297 admonish ("", "unable to read $MHBUILD profile (%s)", cp);
298 }
299 }
300
301 /*
302 * Read the standard profile setup
303 */
304 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
305 readconfig ((struct node **) 0, fp, cp, 0);
306 fclose (fp);
307 }
308
309 /* Check for public cache location */
310 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
311 cache_public = NULL;
312
313 /* Check for private cache location */
314 if (!(cache_private = context_find (nmhprivcache)))
315 cache_private = ".cache";
316 cache_private = getcpy (m_maildir (cache_private));
317
318 if (!context_find ("path"))
319 free (path ("./", TFOLDER));
320
321 /* Check if we have a file to process */
322 if (!compfile)
323 adios (NULL, "need to specify a %s composition file", invo_name);
324
325 /*
326 * Process the composition file from standard input.
327 */
328 if (compfile[0] == '-' && compfile[1] == '\0') {
329 if ((cp = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
330 adios(NULL, "unable to create temporary file in %s",
331 get_temp_dir());
332 }
333
334 /* save a copy of the name for later removal */
335 strncpy (infile, cp, sizeof(infile));
336 unlink_infile = 1;
337
338 /* copy standard input to temporary file */
339 while ((n = fread(buffer, 1, sizeof(buffer), stdin)) > 0) {
340 if (fwrite(buffer, 1, n, fp) != n) {
341 adios (NULL, "error copying to temporary file");
342 }
343 }
344 fclose (fp);
345
346 /* build the content structures for MIME message */
347 ct = build_mime (infile, autobuild, dist, directives, header_encoding,
348 maxunencoded, verbosw);
349
350 /*
351 * If ct == NULL, that means that -auto was set and a MIME version
352 * header was already seen. Just use the input file as the output
353 */
354
355 if (!ct) {
356 if (! (fp = fopen(infile, "r"))) {
357 adios(NULL, "Unable to open %s for reading", infile);
358 }
359 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
360 if (fwrite(buffer, 1, n, stdout) != n) {
361 adios(NULL, "error copying %s to stdout", infile);
362 }
363 }
364 } else {
365 /* output the message */
366 output_message_fp (ct, stdout, NULL);
367 free_content (ct);
368 }
369
370 done (0);
371 }
372
373 /*
374 * Process the composition file from a file.
375 */
376
377 /* build the content structures for MIME message */
378 ct = build_mime (compfile, autobuild, dist, directives, header_encoding,
379 maxunencoded, verbosw);
380
381 /*
382 * If ct == NULL, that means -auto was set and we found a MIME version
383 * header. Simply exit and do nothing.
384 */
385
386 if (! ct)
387 done(0);
388
389 cts[0] = ct;
390 cts[1] = NULL;
391
392 /* output MIME message to this temporary file */
393 if ((cp = m_mktemp2(compfile, invo_name, NULL, &fp_out)) == NULL) {
394 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
395 }
396 strncpy(outfile, cp, sizeof(outfile));
397 unlink_outfile = 1;
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 unlink_outfile = 0;
422
423 free_content (ct);
424 done (0);
425 return 1;
426 }
427
428
429 static void NORETURN
430 unlink_done (int status)
431 {
432 /*
433 * Check if we need to remove stray
434 * temporary files.
435 */
436 if (unlink_infile)
437 (void) m_unlink (infile);
438 if (unlink_outfile)
439 (void) m_unlink (outfile);
440
441 exit (status);
442 }