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