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