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