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