]> diplodocus.org Git - nmh/blob - uip/mhbuild.c
d57da81afd87d9c343b1147faa3d4bd83ca24289
[nmh] / uip / mhbuild.c
1 /*
2 * mhbuild.c -- expand/translate MIME composition files
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <h/md5.h>
12 #include <h/mts.h>
13 #include <h/tws.h>
14 #include <h/mime.h>
15 #include <h/mhparse.h>
16 #include <h/mhcachesbr.h>
17 #include <h/utils.h>
18
19 #define MHBUILD_SWITCHES \
20 X("auto", 0, AUTOSW) \
21 X("noauto", 0, NAUTOSW) \
22 X("check", 0, CHECKSW) \
23 X("nocheck", 0, NCHECKSW) \
24 X("directives", 0, DIRECTIVES) \
25 X("nodirectives", 0, NDIRECTIVES) \
26 X("headers", 0, HEADSW) \
27 X("noheaders", 0, NHEADSW) \
28 X("list", 0, LISTSW) \
29 X("nolist", 0, NLISTSW) \
30 X("realsize", 0, SIZESW) \
31 X("norealsize", 0, NSIZESW) \
32 X("rfc934mode", 0, RFC934SW) \
33 X("norfc934mode", 0, NRFC934SW) \
34 X("verbose", 0, VERBSW) \
35 X("noverbose", 0, NVERBSW) \
36 X("disposition", 0, DISPOSW) \
37 X("nodisposition", 0, NDISPOSW) \
38 X("rcache policy", 0, RCACHESW) \
39 X("wcache policy", 0, WCACHESW) \
40 X("contentid", 0, CONTENTIDSW) \
41 X("nocontentid", 0, NCONTENTIDSW) \
42 X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
43 X("autoheaderencoding", 0, AUTOHEADERENCSW) \
44 X("maxunencoded", 0, MAXUNENCSW) \
45 X("version", 0, VERSIONSW) \
46 X("help", 0, HELPSW) \
47 X("debug", -5, DEBUGSW) \
48 X("dist", 0, DISTSW) \
49
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(MHBUILD);
52 #undef X
53
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(MHBUILD, switches);
56 #undef X
57
58 /* utf-8 is for Email Address Internationalization, using SMTPUTF8. */
59 #define MIMEENCODING_SWITCHES \
60 X("base64", 0, BASE64SW) \
61 X("quoted-printable", 0, QUOTEDPRINTSW) \
62 X("utf-8", 0, UTF8SW) \
63
64 #define X(sw, minchars, id) id,
65 DEFINE_SWITCH_ENUM(MIMEENCODING);
66 #undef X
67
68 #define X(sw, minchars, id) { sw, minchars, id },
69 DEFINE_SWITCH_ARRAY(MIMEENCODING, encodingswitches);
70 #undef X
71
72 /* mhcachesbr.c */
73 extern int rcachesw;
74 extern int wcachesw;
75 extern char *cache_public;
76 extern char *cache_private;
77
78 int debugsw = 0;
79
80 int listsw = 0;
81 int rfc934sw = 0;
82 int contentidsw = 1;
83
84 /*
85 * Temporary files
86 */
87 static char infile[BUFSIZ];
88 static int unlink_infile = 0;
89
90 static char outfile[BUFSIZ];
91 static int unlink_outfile = 0;
92
93 static void unlink_done (int) NORETURN;
94
95 /* mhbuildsbr.c */
96 int output_message_fp (CT, FILE *, char*);
97
98
99 int
100 main (int argc, char **argv)
101 {
102 int sizesw = 1, headsw = 1, directives = 1, autobuild = 0, dist = 0;
103 int verbosw = 0, dispo = 0;
104 size_t maxunencoded = MAXTEXTPERLN;
105 int *icachesw;
106 char *cp, buf[BUFSIZ];
107 char buffer[BUFSIZ], *compfile = NULL;
108 char **argp, **arguments;
109 CT ct, cts[2];
110 FILE *fp = NULL;
111 FILE *fp_out = NULL;
112 int header_encoding = CE_UNKNOWN;
113 size_t n;
114
115 if (nmh_init(argv[0], 2)) { return 1; }
116
117 done=unlink_done;
118
119 arguments = getarguments (invo_name, argc, argv, 1);
120 argp = arguments;
121
122 while ((cp = *argp++)) {
123 if (cp[0] == '-' && cp[1] == '\0') {
124 if (compfile)
125 adios (NULL, "cannot specify both standard input and a file");
126 else
127 compfile = cp;
128 listsw = 0; /* turn off -list if using standard in/out */
129 verbosw = 0; /* 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 adios (NULL, "-%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 = 1;
151 directives = 0;
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 = 0;
159 directives = 1;
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 adios (NULL, "missing argument to %s", argp[-2]);
170 switch (*icachesw = smatch (cp, caches)) {
171 case AMBIGSW:
172 ambigsw (cp, caches);
173 done (1);
174 case UNKWNSW:
175 adios (NULL, "%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++;
190 continue;
191 case NHEADSW:
192 headsw = 0;
193 continue;
194
195 case DIRECTIVES:
196 directives = 1;
197 continue;
198 case NDIRECTIVES:
199 directives = 0;
200 continue;
201
202 case LISTSW:
203 listsw++;
204 continue;
205 case NLISTSW:
206 listsw = 0;
207 continue;
208
209 case RFC934SW:
210 rfc934sw++;
211 continue;
212 case NRFC934SW:
213 rfc934sw = 0;
214 continue;
215
216 case SIZESW:
217 sizesw++;
218 continue;
219 case NSIZESW:
220 sizesw = 0;
221 continue;
222
223 case CONTENTIDSW:
224 contentidsw = 1;
225 continue;
226 case NCONTENTIDSW:
227 contentidsw = 0;
228 continue;
229
230 case HEADERENCSW: {
231 int encoding;
232
233 if (!(cp = *argp++) || *cp == '-')
234 adios (NULL, "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 adios (NULL, "%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 adios (NULL, "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 adios (NULL, "missing argument to %s", argp[-2]);
263 if ((maxunencoded = atoi(cp)) < 1)
264 adios (NULL, "Invalid argument for %s: %s", argp[-2], cp);
265 if (maxunencoded > 998)
266 adios (NULL, "limit of -maxunencoded is 998");
267 continue;
268
269 case VERBSW:
270 verbosw++;
271 continue;
272 case NVERBSW:
273 verbosw = 0;
274 continue;
275 case DISPOSW:
276 dispo = 1;
277 continue;
278 case NDISPOSW:
279 dispo = 0;
280 continue;
281 case DEBUGSW:
282 debugsw = 1;
283 continue;
284 case DISTSW:
285 dist = 1;
286 continue;
287 }
288 }
289 if (compfile)
290 adios (NULL, "only one composition file allowed");
291 else
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 ((struct node **) 0, 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 ((struct node **) 0, 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 = getcpy (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 adios (NULL, "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 adios(NULL, "unable to create temporary file in %s",
337 get_temp_dir());
338 }
339
340 /* save a copy of the name for later removal */
341 strncpy (infile, cp, sizeof(infile));
342 unlink_infile = 1;
343
344 /* copy standard input to temporary file */
345 while ((n = fread(buffer, 1, sizeof(buffer), stdin)) > 0) {
346 if (fwrite(buffer, 1, n, fp) != n) {
347 adios (NULL, "error copying to temporary file");
348 }
349 }
350 fclose (fp);
351
352 /* build the content structures for MIME message */
353 ct = build_mime (infile, autobuild, dist, directives, header_encoding,
354 maxunencoded, verbosw);
355
356 /*
357 * If ct == NULL, that means that -auto was set and a MIME version
358 * header was already seen. Just use the input file as the output
359 */
360
361 if (!ct) {
362 if (! (fp = fopen(infile, "r"))) {
363 adios(NULL, "Unable to open %s for reading", infile);
364 }
365 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
366 if (fwrite(buffer, 1, n, stdout) != n) {
367 adios(NULL, "error copying %s to stdout", infile);
368 }
369 }
370 } else {
371 /* output the message */
372 output_message_fp (ct, stdout, NULL);
373 free_content (ct);
374 }
375
376 done (0);
377 }
378
379 /*
380 * Process the composition file from a file.
381 */
382
383 /* build the content structures for MIME message */
384 ct = build_mime (compfile, autobuild, dist, directives, header_encoding,
385 maxunencoded, verbosw);
386
387 /*
388 * If ct == NULL, that means -auto was set and we found a MIME version
389 * header. Simply exit and do nothing.
390 */
391
392 if (! ct)
393 done(0);
394
395 cts[0] = ct;
396 cts[1] = NULL;
397
398 /* output MIME message to this temporary file */
399 if ((cp = m_mktemp2(compfile, invo_name, NULL, &fp_out)) == NULL) {
400 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
401 }
402 strncpy(outfile, cp, sizeof(outfile));
403 unlink_outfile = 1;
404
405 /* output the message */
406 output_message_fp (ct, fp_out, outfile);
407 fclose(fp_out);
408
409 /*
410 * List the message info
411 */
412 if (listsw)
413 list_all_messages (cts, headsw, sizesw, verbosw, debugsw, dispo);
414
415 /* Rename composition draft */
416 snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
417 if (rename (compfile, buffer) == NOTOK) {
418 adios (compfile, "unable to rename comp draft %s to", buffer);
419 }
420
421 /* Rename output file to take its place */
422 if (rename (outfile, compfile) == NOTOK) {
423 advise (outfile, "unable to rename output %s to", compfile);
424 rename (buffer, compfile);
425 done (1);
426 }
427 unlink_outfile = 0;
428
429 free_content (ct);
430 done (0);
431 return 1;
432 }
433
434
435 static void
436 unlink_done (int status)
437 {
438 /*
439 * Check if we need to remove stray
440 * temporary files.
441 */
442 if (unlink_infile)
443 (void) m_unlink (infile);
444 if (unlink_outfile)
445 (void) m_unlink (outfile);
446
447 exit (status);
448 }