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