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