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