]> diplodocus.org Git - nmh/blob - uip/mhshow.c
6009fdc50f9388b445dad7e5546d048c21afbe87
[nmh] / uip / mhshow.c
1 /* mhshow.c -- display 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_setunseen.h"
13 #include "sbr/seq_setprev.h"
14 #include "sbr/seq_setcur.h"
15 #include "sbr/seq_save.h"
16 #include "sbr/smatch.h"
17 #include "sbr/m_convert.h"
18 #include "sbr/getfolder.h"
19 #include "sbr/folder_read.h"
20 #include "sbr/context_save.h"
21 #include "sbr/context_replace.h"
22 #include "sbr/context_find.h"
23 #include "sbr/readconfig.h"
24 #include "sbr/ambigsw.h"
25 #include "sbr/path.h"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/error.h"
29 #include <fcntl.h>
30 #include "h/signals.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 "sbr/m_popen.h"
42 #include "mhfree.h"
43 #include "mhshowsbr.h"
44
45 #define MHSHOW_SWITCHES \
46 X("check", 0, CHECKSW) \
47 X("nocheck", 0, NCHECKSW) \
48 X("verbose", 0, VERBSW) \
49 X("noverbose", 0, NVERBSW) \
50 X("concat", 0, CONCATSW) \
51 X("noconcat", 0, NCONCATSW) \
52 X("textonly", 0, TEXTONLYSW) \
53 X("notextonly", 0, NTEXTONLYSW) \
54 X("inlineonly", 0, INLINESW) \
55 X("noinlineonly", 0, NINLINESW) \
56 X("file file", 0, FILESW) \
57 X("form formfile", 0, FORMSW) \
58 X("header", 0, HEADSW) \
59 X("noheader", 0, NHEADSW) \
60 X("headerform formfile", 0, HEADFORMSW) \
61 X("markform formfile", 0, MARKFORMSW) \
62 X("part number", 0, PARTSW) \
63 X("type content", 0, TYPESW) \
64 X("prefer content", 0, PREFERSW) \
65 X("noprefer", 0, NPREFERSW) \
66 X("rcache policy", 0, RCACHESW) \
67 X("wcache policy", 0, WCACHESW) \
68 X("version", 0, VERSIONSW) \
69 X("help", 0, HELPSW) \
70 /* \
71 * switches for moreproc/mhlproc \
72 */ \
73 X("moreproc program", -4, PROGSW) \
74 X("nomoreproc", -3, NPROGSW) \
75 X("length lines", -4, LENSW) \
76 X("width columns", -4, WIDTHSW) \
77 /* \
78 * switches for debugging \
79 */ \
80 X("debug", -5, DEBUGSW) \
81
82 #define X(sw, minchars, id) id,
83 DEFINE_SWITCH_ENUM(MHSHOW);
84 #undef X
85
86 #define X(sw, minchars, id) { sw, minchars, id },
87 DEFINE_SWITCH_ARRAY(MHSHOW, switches);
88 #undef X
89
90
91 int debugsw = 0;
92 int verbosw = 0;
93
94 #define quitser pipeser
95
96 /*
97 * static prototypes
98 */
99 static void pipeser (int);
100
101
102 int
103 main (int argc, char **argv)
104 {
105 int msgnum, *icachesw, concatsw = -1, textonly = -1, inlineonly = -1;
106 char *cp, *file = NULL;
107 char *maildir, buf[100], **argp;
108 char **arguments;
109 struct msgs_array msgs = { 0, 0, NULL };
110 struct msgs *mp = NULL;
111 CT ct, *ctp;
112 FILE *fp;
113
114 if (nmh_init(argv[0], true, true)) { return 1; }
115
116 set_done(freects_done);
117
118 arguments = getarguments (invo_name, argc, argv, 1);
119 argp = arguments;
120
121 /*
122 * Parse arguments
123 */
124 while ((cp = *argp++)) {
125 if (*cp == '-') {
126 switch (smatch (++cp, switches)) {
127 case AMBIGSW:
128 ambigsw (cp, switches);
129 done (1);
130 case UNKWNSW:
131 die("-%s unknown", cp);
132
133 case HELPSW:
134 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
135 invo_name);
136 print_help (buf, switches, 1);
137 done (0);
138 case VERSIONSW:
139 print_version(invo_name);
140 done (0);
141
142 case RCACHESW:
143 icachesw = &rcachesw;
144 goto do_cache;
145 case WCACHESW:
146 icachesw = &wcachesw;
147 do_cache:
148 if (!(cp = *argp++) || *cp == '-')
149 die("missing argument to %s", argp[-2]);
150 switch (*icachesw = smatch (cp, cache_policy)) {
151 case AMBIGSW:
152 ambigsw (cp, cache_policy);
153 done (1);
154 case UNKWNSW:
155 die("%s unknown", cp);
156 default:
157 break;
158 }
159 continue;
160
161 case CHECKSW:
162 case NCHECKSW:
163 /* Currently a NOP */
164 continue;
165
166 case CONCATSW:
167 concatsw = 1;
168 continue;
169 case NCONCATSW:
170 concatsw = 0;
171 continue;
172 case TEXTONLYSW:
173 textonly = 1;
174 continue;
175 case NTEXTONLYSW:
176 textonly = 0;
177 continue;
178 case INLINESW:
179 inlineonly = 1;
180 continue;
181 case NINLINESW:
182 inlineonly = 0;
183 continue;
184
185 case PARTSW:
186 if (!(cp = *argp++) || *cp == '-')
187 die("missing argument to %s", argp[-2]);
188 if (npart >= NPARTS)
189 die("too many parts (starting with %s), %d max",
190 cp, NPARTS);
191 parts[npart++] = cp;
192 continue;
193
194 case TYPESW:
195 if (!(cp = *argp++) || *cp == '-')
196 die("missing argument to %s", argp[-2]);
197 if (ntype >= NTYPES)
198 die("too many types (starting with %s), %d max",
199 cp, NTYPES);
200 types[ntype++] = cp;
201 continue;
202
203 case PREFERSW:
204 if (!(cp = *argp++) || *cp == '-')
205 die("missing argument to %s", argp[-2]);
206 if (npreferred >= NPREFS)
207 die("too many preferred types (starting with %s), %d max",
208 cp, NPREFS);
209 mime_preference[npreferred].type = cp;
210 cp = strchr(cp, '/');
211 if (cp) *cp++ = '\0';
212 mime_preference[npreferred++].subtype = cp;
213 continue;
214
215 case NPREFERSW:
216 npreferred = 0;
217 continue;
218
219 case FILESW:
220 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
221 die("missing argument to %s", argp[-2]);
222 file = *cp == '-' ? cp : path (cp, TFILE);
223 continue;
224
225 case FORMSW:
226 if (!(cp = *argp++) || *cp == '-')
227 die("missing argument to %s", argp[-2]);
228 free(formsw);
229 formsw = mh_xstrdup(etcpath(cp));
230 continue;
231
232 case HEADSW:
233 headersw = 1;
234 continue;
235 case NHEADSW:
236 headersw = 0;
237 continue;
238
239 case HEADFORMSW:
240 if (!(headerform = *argp++) || *headerform == '-')
241 die("missing argument to %s", argp[-2]);
242 continue;
243
244 case MARKFORMSW:
245 if (!(markerform = *argp++) || *markerform == '-')
246 die("missing argument to %s", argp[-2]);
247 continue;
248
249 /*
250 * Switches for moreproc/mhlproc
251 */
252 case PROGSW:
253 if (!(progsw = *argp++) || *progsw == '-')
254 die("missing argument to %s", argp[-2]);
255 continue;
256 case NPROGSW:
257 nomore++;
258 continue;
259
260 case LENSW:
261 case WIDTHSW:
262 if (!(cp = *argp++) || *cp == '-')
263 die("missing argument to %s", argp[-2]);
264 continue;
265
266 case VERBSW:
267 verbosw = 1;
268 continue;
269 case NVERBSW:
270 verbosw = 0;
271 continue;
272 case DEBUGSW:
273 debugsw = 1;
274 continue;
275 }
276 }
277 if (*cp == '+' || *cp == '@') {
278 if (folder)
279 die("only one folder at a time!");
280 folder = pluspath (cp);
281 } else
282 app_msgarg(&msgs, cp);
283 }
284
285 /* null terminate the list of acceptable parts/types */
286 parts[npart] = NULL;
287 types[ntype] = NULL;
288
289 /*
290 * If we had any specific parts or types specified, turn off text only
291 * content.
292 */
293
294 if (npart > 0 || ntype > 0) {
295 if (textonly == -1)
296 textonly = 0;
297 if (inlineonly == -1)
298 inlineonly = 0;
299 }
300
301 /*
302 * Check if we've specified an additional profile
303 */
304 if ((cp = getenv ("MHSHOW"))) {
305 if ((fp = fopen (cp, "r"))) {
306 readconfig(NULL, fp, cp, 0);
307 fclose (fp);
308 } else {
309 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
310 }
311 }
312
313 /*
314 * Read the standard profile setup
315 */
316 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
317 readconfig(NULL, fp, cp, 0);
318 fclose (fp);
319 }
320
321 /* Check for public cache location */
322 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
323 cache_public = NULL;
324
325 /* Check for private cache location */
326 if (!(cache_private = context_find (nmhprivcache)))
327 cache_private = ".cache";
328 cache_private = mh_xstrdup(m_maildir(cache_private));
329
330 if (!context_find ("path"))
331 free (path ("./", TFOLDER));
332
333 if (file && msgs.size)
334 die("cannot specify msg and file at same time!");
335
336 /*
337 * check if message is coming from file
338 */
339 if (file) {
340 cts = mh_xcalloc(2, sizeof *cts);
341 ctp = cts;
342
343 if ((ct = parse_mime (file)))
344 *ctp++ = ct;
345
346 headersw = 0;
347 } else {
348 /*
349 * message(s) are coming from a folder
350 */
351 if (!msgs.size)
352 app_msgarg(&msgs, "cur");
353 if (!folder)
354 folder = getfolder (1);
355 maildir = m_maildir (folder);
356
357 if (chdir (maildir) == NOTOK)
358 adios (maildir, "unable to change directory to");
359
360 /* read folder and create message structure */
361 if (!(mp = folder_read (folder, 1)))
362 die("unable to read folder %s", folder);
363
364 /* check for empty folder */
365 if (mp->nummsg == 0)
366 die("no messages in %s", folder);
367
368 /* parse all the message ranges/sequences and set SELECTED */
369 for (msgnum = 0; msgnum < msgs.size; msgnum++)
370 if (!m_convert (mp, msgs.msgs[msgnum]))
371 done (1);
372
373 /*
374 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
375 * since we will use that as a tag to know which messages
376 * to remove from the "unseen" sequence.
377 */
378 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
379 if (is_selected(mp, msgnum))
380 set_unseen (mp, msgnum);
381
382 seq_setprev (mp); /* set the Previous-Sequence */
383 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
384
385 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
386 ctp = cts;
387
388 /*
389 * Parse all the SELECTED messages.
390 */
391 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
392 if (is_selected(mp, msgnum)) {
393 char *msgnam;
394
395 msgnam = m_name (msgnum);
396 if ((ct = parse_mime (msgnam)))
397 *ctp++ = ct;
398 }
399 }
400 }
401
402 if (!*cts)
403 done (1);
404
405 userrs = true;
406 SIGNAL (SIGQUIT, quitser);
407 SIGNAL (SIGPIPE, pipeser);
408
409 /*
410 * Get the associated umask for the relevant contents.
411 */
412 for (ctp = cts; *ctp; ctp++) {
413 struct stat st;
414
415 ct = *ctp;
416 if (type_ok (ct, 1) && !ct->c_umask) {
417 if (stat (ct->c_file, &st) != NOTOK)
418 ct->c_umask = ~(st.st_mode & 0777);
419 else
420 ct->c_umask = ~m_gmprot();
421 }
422 }
423
424 /* If reading from a folder, do some updating */
425 if (mp) {
426 context_replace (pfolder, folder);/* update current folder */
427 seq_setcur (mp, mp->hghsel); /* update current message */
428 seq_save (mp); /* synchronize sequences */
429 context_save (); /* save the context file */
430 }
431
432 if (concatsw)
433 m_popen(moreproc, 0);
434
435 /*
436 * Show the message content
437 */
438 show_all_messages (cts, concatsw, textonly, inlineonly);
439
440 /* Now free all the structures for the content */
441 for (ctp = cts; *ctp; ctp++)
442 free_content (*ctp);
443
444 free(cts);
445 cts = NULL;
446
447 if (concatsw)
448 m_pclose();
449
450 done (0);
451 return 1;
452 }
453
454
455 static void
456 pipeser (int i)
457 {
458 if (i == SIGQUIT) {
459 fflush (stdout);
460 fprintf (stderr, "\n");
461 fflush (stderr);
462 }
463
464 done (1);
465 /* NOTREACHED */
466 }