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