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