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