]> diplodocus.org Git - nmh/blob - uip/mhshow.c
1c6bdd8dad95858b91949b97fd88ecb8e84bb8fe
[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_gmprot.h"
10 #include "sbr/getarguments.h"
11 #include "sbr/seq_setunseen.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 "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 checksw++;
163 continue;
164 case NCHECKSW:
165 checksw = 0;
166 continue;
167
168 case CONCATSW:
169 concatsw = 1;
170 continue;
171 case NCONCATSW:
172 concatsw = 0;
173 continue;
174 case TEXTONLYSW:
175 textonly = 1;
176 continue;
177 case NTEXTONLYSW:
178 textonly = 0;
179 continue;
180 case INLINESW:
181 inlineonly = 1;
182 continue;
183 case NINLINESW:
184 inlineonly = 0;
185 continue;
186
187 case PARTSW:
188 if (!(cp = *argp++) || *cp == '-')
189 die("missing argument to %s", argp[-2]);
190 if (npart >= NPARTS)
191 die("too many parts (starting with %s), %d max",
192 cp, NPARTS);
193 parts[npart++] = cp;
194 continue;
195
196 case TYPESW:
197 if (!(cp = *argp++) || *cp == '-')
198 die("missing argument to %s", argp[-2]);
199 if (ntype >= NTYPES)
200 die("too many types (starting with %s), %d max",
201 cp, NTYPES);
202 types[ntype++] = cp;
203 continue;
204
205 case PREFERSW:
206 if (!(cp = *argp++) || *cp == '-')
207 die("missing argument to %s", argp[-2]);
208 if (npreferred >= NPREFS)
209 die("too many preferred types (starting with %s), %d max",
210 cp, NPREFS);
211 mime_preference[npreferred].type = cp;
212 cp = strchr(cp, '/');
213 if (cp) *cp++ = '\0';
214 mime_preference[npreferred++].subtype = cp;
215 continue;
216
217 case NPREFERSW:
218 npreferred = 0;
219 continue;
220
221 case FILESW:
222 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
223 die("missing argument to %s", argp[-2]);
224 file = *cp == '-' ? cp : path (cp, TFILE);
225 continue;
226
227 case FORMSW:
228 if (!(cp = *argp++) || *cp == '-')
229 die("missing argument to %s", argp[-2]);
230 free(formsw);
231 formsw = mh_xstrdup(etcpath(cp));
232 continue;
233
234 case HEADSW:
235 headersw = 1;
236 continue;
237 case NHEADSW:
238 headersw = 0;
239 continue;
240
241 case HEADFORMSW:
242 if (!(headerform = *argp++) || *headerform == '-')
243 die("missing argument to %s", argp[-2]);
244 continue;
245
246 case MARKFORMSW:
247 if (!(markerform = *argp++) || *markerform == '-')
248 die("missing argument to %s", argp[-2]);
249 continue;
250
251 /*
252 * Switches for moreproc/mhlproc
253 */
254 case PROGSW:
255 if (!(progsw = *argp++) || *progsw == '-')
256 die("missing argument to %s", argp[-2]);
257 continue;
258 case NPROGSW:
259 nomore++;
260 continue;
261
262 case LENSW:
263 case WIDTHSW:
264 if (!(cp = *argp++) || *cp == '-')
265 die("missing argument to %s", argp[-2]);
266 continue;
267
268 case VERBSW:
269 verbosw = 1;
270 continue;
271 case NVERBSW:
272 verbosw = 0;
273 continue;
274 case DEBUGSW:
275 debugsw = 1;
276 continue;
277 }
278 }
279 if (*cp == '+' || *cp == '@') {
280 if (folder)
281 die("only one folder at a time!");
282 folder = pluspath (cp);
283 } else
284 app_msgarg(&msgs, cp);
285 }
286
287 /* null terminate the list of acceptable parts/types */
288 parts[npart] = NULL;
289 types[ntype] = NULL;
290
291 /*
292 * If we had any specific parts or types specified, turn off text only
293 * content.
294 */
295
296 if (npart > 0 || ntype > 0) {
297 if (textonly == -1)
298 textonly = 0;
299 if (inlineonly == -1)
300 inlineonly = 0;
301 }
302
303 /*
304 * Check if we've specified an additional profile
305 */
306 if ((cp = getenv ("MHSHOW"))) {
307 if ((fp = fopen (cp, "r"))) {
308 readconfig(NULL, fp, cp, 0);
309 fclose (fp);
310 } else {
311 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
312 }
313 }
314
315 /*
316 * Read the standard profile setup
317 */
318 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
319 readconfig(NULL, fp, cp, 0);
320 fclose (fp);
321 }
322
323 /* Check for public cache location */
324 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
325 cache_public = NULL;
326
327 /* Check for private cache location */
328 if (!(cache_private = context_find (nmhprivcache)))
329 cache_private = ".cache";
330 cache_private = mh_xstrdup(m_maildir(cache_private));
331
332 if (!context_find ("path"))
333 free (path ("./", TFOLDER));
334
335 if (file && msgs.size)
336 die("cannot specify msg and file at same time!");
337
338 /*
339 * check if message is coming from file
340 */
341 if (file) {
342 cts = mh_xcalloc(2, sizeof *cts);
343 ctp = cts;
344
345 if ((ct = parse_mime (file)))
346 *ctp++ = ct;
347
348 headersw = 0;
349 } else {
350 /*
351 * message(s) are coming from a folder
352 */
353 if (!msgs.size)
354 app_msgarg(&msgs, "cur");
355 if (!folder)
356 folder = getfolder (1);
357 maildir = m_maildir (folder);
358
359 if (chdir (maildir) == NOTOK)
360 adios (maildir, "unable to change directory to");
361
362 /* read folder and create message structure */
363 if (!(mp = folder_read (folder, 1)))
364 die("unable to read folder %s", folder);
365
366 /* check for empty folder */
367 if (mp->nummsg == 0)
368 die("no messages in %s", folder);
369
370 /* parse all the message ranges/sequences and set SELECTED */
371 for (msgnum = 0; msgnum < msgs.size; msgnum++)
372 if (!m_convert (mp, msgs.msgs[msgnum]))
373 done (1);
374
375 /*
376 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
377 * since we will use that as a tag to know which messages
378 * to remove from the "unseen" sequence.
379 */
380 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
381 if (is_selected(mp, msgnum))
382 set_unseen (mp, msgnum);
383
384 seq_setprev (mp); /* set the Previous-Sequence */
385 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
386
387 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
388 ctp = cts;
389
390 /*
391 * Parse all the SELECTED messages.
392 */
393 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
394 if (is_selected(mp, msgnum)) {
395 char *msgnam;
396
397 msgnam = m_name (msgnum);
398 if ((ct = parse_mime (msgnam)))
399 *ctp++ = ct;
400 }
401 }
402 }
403
404 if (!*cts)
405 done (1);
406
407 userrs = true;
408 SIGNAL (SIGQUIT, quitser);
409 SIGNAL (SIGPIPE, pipeser);
410
411 /*
412 * Get the associated umask for the relevant contents.
413 */
414 for (ctp = cts; *ctp; ctp++) {
415 struct stat st;
416
417 ct = *ctp;
418 if (type_ok (ct, 1) && !ct->c_umask) {
419 if (stat (ct->c_file, &st) != NOTOK)
420 ct->c_umask = ~(st.st_mode & 0777);
421 else
422 ct->c_umask = ~m_gmprot();
423 }
424 }
425
426 /* If reading from a folder, do some updating */
427 if (mp) {
428 context_replace (pfolder, folder);/* update current folder */
429 seq_setcur (mp, mp->hghsel); /* update current message */
430 seq_save (mp); /* synchronize sequences */
431 context_save (); /* save the context file */
432 }
433
434 if (concatsw)
435 m_popen(moreproc, 0);
436
437 /*
438 * Show the message content
439 */
440 show_all_messages (cts, concatsw, textonly, inlineonly);
441
442 /* Now free all the structures for the content */
443 for (ctp = cts; *ctp; ctp++)
444 free_content (*ctp);
445
446 free(cts);
447 cts = NULL;
448
449 if (concatsw)
450 m_pclose();
451
452 done (0);
453 return 1;
454 }
455
456
457 static void
458 pipeser (int i)
459 {
460 if (i == SIGQUIT) {
461 fflush (stdout);
462 fprintf (stderr, "\n");
463 fflush (stderr);
464 }
465
466 done (1);
467 /* NOTREACHED */
468 }