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