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