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