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