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