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