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