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