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