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