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