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