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