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