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