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