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