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