]> diplodocus.org Git - nmh/blob - uip/mhshow.c
16079dcda4ebb591076b588919a001ede75d4481
[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 "mhfree.h"
20 #include "mhshowsbr.h"
21
22 #define MHSHOW_SWITCHES \
23 X("check", 0, CHECKSW) \
24 X("nocheck", 0, NCHECKSW) \
25 X("verbose", 0, VERBSW) \
26 X("noverbose", 0, NVERBSW) \
27 X("concat", 0, CONCATSW) \
28 X("noconcat", 0, NCONCATSW) \
29 X("textonly", 0, TEXTONLYSW) \
30 X("notextonly", 0, NTEXTONLYSW) \
31 X("inlineonly", 0, INLINESW) \
32 X("noinlineonly", 0, NINLINESW) \
33 X("file file", 0, FILESW) \
34 X("form formfile", 0, FORMSW) \
35 X("header", 0, HEADSW) \
36 X("noheader", 0, NHEADSW) \
37 X("headerform formfile", 0, HEADFORMSW) \
38 X("markform formfile", 0, MARKFORMSW) \
39 X("part number", 0, PARTSW) \
40 X("type content", 0, TYPESW) \
41 X("prefer content", 0, PREFERSW) \
42 X("rcache policy", 0, RCACHESW) \
43 X("wcache policy", 0, WCACHESW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
46 /* \
47 * switches for moreproc/mhlproc \
48 */ \
49 X("moreproc program", -4, PROGSW) \
50 X("nomoreproc", -3, NPROGSW) \
51 X("length lines", -4, LENSW) \
52 X("width columns", -4, WIDTHSW) \
53 /* \
54 * switches for debugging \
55 */ \
56 X("debug", -5, DEBUGSW) \
57
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(MHSHOW);
60 #undef X
61
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(MHSHOW, switches);
64 #undef X
65
66
67 /* mhmisc.c */
68 extern int npart;
69 extern int ntype;
70 extern char *parts[NPARTS + 1];
71 extern char *types[NTYPES + 1];
72 extern int userrs;
73
74 /* mhparse.c */
75 extern char *preferred_types[];
76 extern char *preferred_subtypes[];
77 extern int npreferred;
78
79 int debugsw = 0;
80 int verbosw = 0;
81
82 #define quitser pipeser
83
84 /* mhparse.c */
85 CT parse_mime (char *);
86
87 /* mhmisc.c */
88 int part_ok (CT);
89 int type_ok (CT, int);
90 void flush_errors (void);
91
92 /*
93 * static prototypes
94 */
95 static void pipeser (int);
96
97
98 int
99 main (int argc, char **argv)
100 {
101 int msgnum, *icachesw, concatsw = -1, textonly = -1, inlineonly = -1;
102 char *cp, *file = NULL;
103 char *maildir, buf[100], **argp;
104 char **arguments;
105 struct msgs_array msgs = { 0, 0, NULL };
106 struct msgs *mp = NULL;
107 CT ct, *ctp;
108 FILE *fp;
109
110 if (nmh_init(argv[0], 1)) { return 1; }
111
112 done=freects_done;
113
114 arguments = getarguments (invo_name, argc, argv, 1);
115 argp = arguments;
116
117 /*
118 * Parse arguments
119 */
120 while ((cp = *argp++)) {
121 if (*cp == '-') {
122 switch (smatch (++cp, switches)) {
123 case AMBIGSW:
124 ambigsw (cp, switches);
125 done (1);
126 case UNKWNSW:
127 adios (NULL, "-%s unknown", cp);
128
129 case HELPSW:
130 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
131 invo_name);
132 print_help (buf, switches, 1);
133 done (0);
134 case VERSIONSW:
135 print_version(invo_name);
136 done (0);
137
138 case RCACHESW:
139 icachesw = &rcachesw;
140 goto do_cache;
141 case WCACHESW:
142 icachesw = &wcachesw;
143 do_cache:
144 if (!(cp = *argp++) || *cp == '-')
145 adios (NULL, "missing argument to %s", argp[-2]);
146 switch (*icachesw = smatch (cp, cache_policy)) {
147 case AMBIGSW:
148 ambigsw (cp, cache_policy);
149 done (1);
150 case UNKWNSW:
151 adios (NULL, "%s unknown", cp);
152 default:
153 break;
154 }
155 continue;
156
157 case CHECKSW:
158 checksw++;
159 continue;
160 case NCHECKSW:
161 checksw = 0;
162 continue;
163
164 case CONCATSW:
165 concatsw = 1;
166 continue;
167 case NCONCATSW:
168 concatsw = 0;
169 continue;
170 case TEXTONLYSW:
171 textonly = 1;
172 continue;
173 case NTEXTONLYSW:
174 textonly = 0;
175 continue;
176 case INLINESW:
177 inlineonly = 1;
178 continue;
179 case NINLINESW:
180 inlineonly = 0;
181 continue;
182
183 case PARTSW:
184 if (!(cp = *argp++) || *cp == '-')
185 adios (NULL, "missing argument to %s", argp[-2]);
186 if (npart >= NPARTS)
187 adios (NULL, "too many parts (starting with %s), %d max",
188 cp, NPARTS);
189 parts[npart++] = cp;
190 continue;
191
192 case TYPESW:
193 if (!(cp = *argp++) || *cp == '-')
194 adios (NULL, "missing argument to %s", argp[-2]);
195 if (ntype >= NTYPES)
196 adios (NULL, "too many types (starting with %s), %d max",
197 cp, NTYPES);
198 types[ntype++] = cp;
199 continue;
200
201 case PREFERSW:
202 if (!(cp = *argp++) || *cp == '-')
203 adios (NULL, "missing argument to %s", argp[-2]);
204 if (npreferred >= NPREFS)
205 adios (NULL, "too many preferred types (starting with %s), %d max",
206 cp, NPREFS);
207 preferred_types[npreferred] = cp;
208 cp = strchr(cp, '/');
209 if (cp) *cp++ = '\0';
210 preferred_subtypes[npreferred++] = cp;
211 continue;
212
213 case FILESW:
214 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
215 adios (NULL, "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 adios (NULL, "missing argument to %s", argp[-2]);
222 mh_xfree(formsw);
223 formsw = getcpy (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 adios (NULL, "missing argument to %s", argp[-2]);
236 continue;
237
238 case MARKFORMSW:
239 if (!(markerform = *argp++) || *markerform == '-')
240 adios (NULL, "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 adios (NULL, "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 adios (NULL, "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 adios (NULL, "only one folder at a time!");
274 else
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 ((struct node **) 0, 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 ((struct node **) 0, 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 = getcpy (m_maildir (cache_private));
324
325 if (!context_find ("path"))
326 free (path ("./", TFOLDER));
327
328 if (file && msgs.size)
329 adios (NULL, "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 adios (NULL, "unable to read folder %s", folder);
358
359 /* check for empty folder */
360 if (mp->nummsg == 0)
361 adios (NULL, "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 = 1;
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 }