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