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