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