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