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