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