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