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