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