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