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