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