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