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