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