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