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