]> diplodocus.org Git - nmh/blob - uip/mhn.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / mhn.c
1 /* mhn.c -- display, list, cache, or store the contents of MIME messages
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/signals.h>
11 #include <h/md5.h>
12 #include <h/mts.h>
13 #include <h/tws.h>
14 #include <h/fmt_scan.h>
15 #include <h/mime.h>
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
18 #include "h/done.h"
19 #include <h/utils.h>
20 #include "mhmisc.h"
21 #include "sbr/m_maildir.h"
22 #include "mhfree.h"
23 #include "mhshowsbr.h"
24
25 #define MHN_SWITCHES \
26 X("auto", 0, AUTOSW) \
27 X("noauto", 0, NAUTOSW) \
28 X("cache", 0, CACHESW) \
29 X("nocache", 0, NCACHESW) \
30 X("check", 0, CHECKSW) \
31 X("nocheck", 0, NCHECKSW) \
32 X("headers", 0, HEADSW) \
33 X("noheaders", 0, NHEADSW) \
34 X("list", 0, LISTSW) \
35 X("nolist", 0, NLISTSW) \
36 X("realsize", 0, SIZESW) \
37 X("norealsize", 0, NSIZESW) \
38 X("show", 0, SHOWSW) \
39 X("noshow", 0, NSHOWSW) \
40 X("store", 0, STORESW) \
41 X("nostore", 0, NSTORESW) \
42 X("verbose", 0, VERBSW) \
43 X("noverbose", 0, NVERBSW) \
44 X("file file", 0, FILESW) \
45 X("form formfile", 0, FORMSW) \
46 X("part number", 0, PARTSW) \
47 X("type content", 0, TYPESW) \
48 X("rcache policy", 0, RCACHESW) \
49 X("wcache policy", 0, WCACHESW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
52 /* \
53 * for debugging \
54 */ \
55 X("debug", -5, DEBUGSW) \
56 /* \
57 * switches for moreproc/mhlproc \
58 */ \
59 X("moreproc program", -4, PROGSW) \
60 X("nomoreproc", -3, NPROGSW) \
61 X("length lines", -4, LENSW) \
62 X("width columns", -4, WIDTHSW) \
63 /* \
64 * switches for mhbuild \
65 */ \
66 X("build", -5, BUILDSW) \
67 X("nobuild", -7, NBUILDSW) \
68 X("rfc934mode", 0, RFC934SW) \
69 X("norfc934mode", 0, NRFC934SW) \
70
71 #define X(sw, minchars, id) id,
72 DEFINE_SWITCH_ENUM(MHN);
73 #undef X
74
75 #define X(sw, minchars, id) { sw, minchars, id },
76 DEFINE_SWITCH_ARRAY(MHN, switches);
77 #undef X
78
79
80 int debugsw = 0;
81 int verbosw = 0;
82
83 /*
84 * variables for mhbuild (mhn -build)
85 */
86 static bool buildsw;
87 static int rfc934sw = 0;
88
89 /*
90 * what action to take?
91 */
92 static bool cachesw;
93 static bool listsw;
94 static bool showsw;
95 static bool storesw;
96
97 #define quitser pipeser
98
99 /*
100 * static prototypes
101 */
102 static void pipeser (int);
103
104
105 int
106 main (int argc, char **argv)
107 {
108 bool sizesw = true;
109 bool headsw = true;
110 bool autosw = false;
111 int msgnum, *icachesw;
112 char *cp, *file = NULL, *folder = NULL;
113 char *maildir, buf[100], **argp;
114 char **arguments;
115 char *cwd;
116 struct msgs_array msgs = { 0, 0, NULL };
117 struct msgs *mp = NULL;
118 CT ct, *ctp;
119 FILE *fp;
120 mhstoreinfo_t info;
121
122 if (nmh_init(argv[0], true, true)) { return 1; }
123
124 set_done(freects_done);
125
126 arguments = getarguments (invo_name, argc, argv, 1);
127 argp = arguments;
128
129 /*
130 * Parse arguments
131 */
132 while ((cp = *argp++)) {
133 if (*cp == '-') {
134 switch (smatch (++cp, switches)) {
135 case AMBIGSW:
136 ambigsw (cp, switches);
137 done (1);
138 case UNKWNSW:
139 die("-%s unknown", cp);
140
141 case HELPSW:
142 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
143 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 autosw = true;
152 continue;
153 case NAUTOSW:
154 autosw = false;
155 continue;
156
157 case CACHESW:
158 cachesw = true;
159 continue;
160 case NCACHESW:
161 cachesw = false;
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 LISTSW:
198 listsw = true;
199 continue;
200 case NLISTSW:
201 listsw = false;
202 continue;
203
204 case SHOWSW:
205 showsw = true;
206 continue;
207 case NSHOWSW:
208 showsw = false;
209 continue;
210
211 case SIZESW:
212 sizesw = true;
213 continue;
214 case NSIZESW:
215 sizesw = false;
216 continue;
217
218 case STORESW:
219 storesw = true;
220 continue;
221 case NSTORESW:
222 storesw = false;
223 continue;
224
225 case PARTSW:
226 if (!(cp = *argp++) || *cp == '-')
227 die("missing argument to %s", argp[-2]);
228 if (npart >= NPARTS)
229 die("too many parts (starting with %s), %d max",
230 cp, NPARTS);
231 parts[npart++] = cp;
232 continue;
233
234 case TYPESW:
235 if (!(cp = *argp++) || *cp == '-')
236 die("missing argument to %s", argp[-2]);
237 if (ntype >= NTYPES)
238 die("too many types (starting with %s), %d max",
239 cp, NTYPES);
240 types[ntype++] = cp;
241 continue;
242
243 case FILESW:
244 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
245 die("missing argument to %s", argp[-2]);
246 file = *cp == '-' ? cp : path (cp, TFILE);
247 continue;
248
249 case FORMSW:
250 if (!(cp = *argp++) || *cp == '-')
251 die("missing argument to %s", argp[-2]);
252 free(formsw);
253 formsw = mh_xstrdup(etcpath(cp));
254 continue;
255
256 /*
257 * Switches for moreproc/mhlproc
258 */
259 case PROGSW:
260 if (!(progsw = *argp++) || *progsw == '-')
261 die("missing argument to %s", argp[-2]);
262 continue;
263 case NPROGSW:
264 nomore++;
265 continue;
266
267 case LENSW:
268 case WIDTHSW:
269 if (!(cp = *argp++) || *cp == '-')
270 die("missing argument to %s", argp[-2]);
271 continue;
272
273 /*
274 * Switches for mhbuild
275 */
276 case BUILDSW:
277 buildsw = true;
278 continue;
279 case NBUILDSW:
280 buildsw = false;
281 continue;
282 case RFC934SW:
283 rfc934sw = 1;
284 continue;
285 case NRFC934SW:
286 rfc934sw = -1;
287 continue;
288
289 case VERBSW:
290 verbosw = 1;
291 continue;
292 case NVERBSW:
293 verbosw = 0;
294 continue;
295 case DEBUGSW:
296 debugsw = 1;
297 continue;
298 }
299 }
300 if (*cp == '+' || *cp == '@') {
301 if (folder)
302 die("only one folder at a time!");
303 folder = pluspath (cp);
304 } else
305 app_msgarg(&msgs, cp);
306 }
307
308 /* null terminate the list of acceptable parts/types */
309 parts[npart] = NULL;
310 types[ntype] = NULL;
311
312 /*
313 * Check if we've specified an additional profile
314 */
315 if ((cp = getenv ("MHN"))) {
316 if ((fp = fopen (cp, "r"))) {
317 readconfig(NULL, fp, cp, 0);
318 fclose (fp);
319 } else {
320 admonish ("", "unable to read $MHN profile (%s)", cp);
321 }
322 }
323
324 /*
325 * Read the standard profile setup
326 */
327 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
328 readconfig(NULL, fp, cp, 0);
329 fclose (fp);
330 }
331
332 /* Check for public cache location */
333 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
334 cache_public = NULL;
335
336 /* Check for private cache location */
337 if (!(cache_private = context_find (nmhprivcache)))
338 cache_private = ".cache";
339 cache_private = mh_xstrdup(m_maildir(cache_private));
340
341 /*
342 * Cache the current directory before we do any chdirs()'s.
343 */
344 cwd = mh_xstrdup(pwd());
345
346 if (!context_find ("path"))
347 free (path ("./", TFOLDER));
348
349 /*
350 * Process a mhn composition file (mhn -build)
351 */
352 if (buildsw) {
353 char *vec[MAXARGS];
354 int vecp;
355
356 if (showsw || storesw || cachesw)
357 die("cannot use -build with -show, -store, -cache");
358 if (msgs.size < 1)
359 die("need to specify a %s composition file", invo_name);
360 if (msgs.size > 1)
361 die("only one %s composition file at a time", invo_name);
362
363 vecp = 0;
364 vec[vecp++] = "mhbuild";
365
366 if (rfc934sw == 1)
367 vec[vecp++] = "-rfc934mode";
368 else if (rfc934sw == -1)
369 vec[vecp++] = "-norfc934mode";
370
371 vec[vecp++] = msgs.msgs[0];
372 vec[vecp] = NULL;
373
374 execvp ("mhbuild", vec);
375 fprintf (stderr, "unable to exec ");
376 _exit(1);
377 }
378
379 /*
380 * Process a mhn composition file (old MH style)
381 */
382 if (msgs.size == 1 && !folder && !npart && !cachesw
383 && !showsw && !storesw && !ntype && !file
384 && (cp = getenv ("mhdraft"))
385 && strcmp (cp, msgs.msgs[0]) == 0) {
386
387 char *vec[MAXARGS];
388 int vecp;
389
390 vecp = 0;
391 vec[vecp++] = "mhbuild";
392
393 if (rfc934sw == 1)
394 vec[vecp++] = "-rfc934mode";
395 else if (rfc934sw == -1)
396 vec[vecp++] = "-norfc934mode";
397
398 vec[vecp++] = cp;
399 vec[vecp] = NULL;
400
401 execvp ("mhbuild", vec);
402 fprintf (stderr, "unable to exec ");
403 _exit(1);
404 }
405
406 if (file && msgs.size)
407 die("cannot specify msg and file at same time!");
408
409 /*
410 * check if message is coming from file
411 */
412 if (file) {
413 cts = mh_xcalloc(2, sizeof *cts);
414 ctp = cts;
415
416 if ((ct = parse_mime (file)))
417 *ctp++ = ct;
418 } else {
419 /*
420 * message(s) are coming from a folder
421 */
422 if (!msgs.size)
423 app_msgarg(&msgs, "cur");
424 if (!folder)
425 folder = getfolder (1);
426 maildir = m_maildir (folder);
427
428 if (chdir (maildir) == NOTOK)
429 adios (maildir, "unable to change directory to");
430
431 /* read folder and create message structure */
432 if (!(mp = folder_read (folder, 1)))
433 die("unable to read folder %s", folder);
434
435 /* check for empty folder */
436 if (mp->nummsg == 0)
437 die("no messages in %s", folder);
438
439 /* parse all the message ranges/sequences and set SELECTED */
440 for (msgnum = 0; msgnum < msgs.size; msgnum++)
441 if (!m_convert (mp, msgs.msgs[msgnum]))
442 done (1);
443 seq_setprev (mp); /* set the previous-sequence */
444
445 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
446 ctp = cts;
447
448 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
449 if (is_selected(mp, msgnum)) {
450 char *msgnam;
451
452 msgnam = m_name (msgnum);
453 if ((ct = parse_mime (msgnam)))
454 *ctp++ = ct;
455 }
456 }
457 }
458
459 if (!*cts)
460 done (1);
461
462 /*
463 * You can't give more than one of these flags
464 * at a time.
465 */
466 if (showsw + listsw + storesw + cachesw > 1)
467 die("can only use one of -show, -list, -store, -cache at same time");
468
469 /* If no action is specified, assume -show */
470 if (!listsw && !showsw && !storesw && !cachesw)
471 showsw = true;
472
473 userrs = true;
474 SIGNAL (SIGQUIT, quitser);
475 SIGNAL (SIGPIPE, pipeser);
476
477 /*
478 * Get the associated umask for the relevant contents.
479 */
480 for (ctp = cts; *ctp; ctp++) {
481 struct stat st;
482
483 ct = *ctp;
484 if (type_ok (ct, 1) && !ct->c_umask) {
485 if (stat (ct->c_file, &st) != NOTOK)
486 ct->c_umask = ~(st.st_mode & 0777);
487 else
488 ct->c_umask = ~m_gmprot();
489 }
490 }
491
492 /*
493 * List the message content
494 */
495 if (listsw)
496 list_all_messages (cts, headsw, sizesw, verbosw, debugsw, 0);
497
498 /*
499 * Store the message content
500 */
501 if (storesw) {
502 info = mhstoreinfo_create (cts, cwd, "always", autosw, verbosw);;
503 store_all_messages (info);
504 mhstoreinfo_free (info);
505 }
506
507 /* If reading from a folder, do some updating */
508 if (mp) {
509 context_replace (pfolder, folder);/* update current folder */
510 seq_setcur (mp, mp->hghsel); /* update current message */
511 seq_save (mp); /* synchronize sequences */
512 context_save (); /* save the context file */
513 }
514
515 /*
516 * Cache the message content
517 */
518 if (cachesw)
519 cache_all_messages (cts);
520
521 /*
522 * Show the message content
523 */
524 if (showsw)
525 show_all_messages (cts, 0, 0, 0);
526
527 /* Now free all the structures for the content */
528 for (ctp = cts; *ctp; ctp++)
529 free_content (*ctp);
530
531 free (cts);
532 cts = NULL;
533
534 done (0);
535 return 1;
536 }
537
538
539 static void
540 pipeser (int i)
541 {
542 if (i == SIGQUIT) {
543 fflush (stdout);
544 fprintf (stderr, "\n");
545 fflush (stderr);
546 }
547
548 done (1);
549 /* NOTREACHED */
550 }