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