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