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