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