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