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