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