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