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