2 ** mhshow.c -- display the contents of MIME messages
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
11 #include <h/signals.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 static struct swit switches[] = {
29 { "noserialonly", 0 },
34 #define FILESW 6 /* interface from show */
37 { "form formfile", 0 },
41 { "type content", 0 },
43 { "rcache policy", 0 },
45 { "wcache policy", 0 },
51 ** switches for debugging
60 extern char *tmp; /* directory to place temp files */
65 extern char *cache_public;
66 extern char *cache_private;
76 extern char *parts[NPARTS + 1];
77 extern char *types[NTYPES + 1];
83 #define quitser pipeser
86 CT parse_mime(char *);
91 void set_endian(void);
92 void flush_errors(void);
95 void show_all_messages(CT *);
98 void free_content(CT);
100 void freects_done(int) NORETURN;
105 static RETSIGTYPE pipeser(int);
109 main(int argc, char **argv)
111 int msgnum, *icachesw;
112 char *cp, *file = NULL, *folder = NULL;
113 char *maildir, buf[100], **argp;
115 struct msgs_array msgs = { 0, 0, NULL };
116 struct msgs *mp = NULL;
123 setlocale(LC_ALL, "");
125 invo_name = mhbasename(argv[0]);
127 /* read user profile/context */
130 arguments = getarguments(invo_name, argc, argv, 1);
136 while ((cp = *argp++)) {
138 switch (smatch(++cp, switches)) {
140 ambigsw(cp, switches);
143 adios(NULL, "-%s unknown", cp);
146 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
147 print_help(buf, switches, 1);
150 print_version(invo_name);
154 icachesw = &rcachesw;
157 icachesw = &wcachesw;
159 if (!(cp = *argp++) || *cp == '-')
160 adios(NULL, "missing argument to %s",
162 switch (*icachesw = smatch(cp, caches)) {
167 adios(NULL, "%s unknown", cp);
188 if (!(cp = *argp++) || *cp == '-')
189 adios(NULL, "missing argument to %s",
192 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
197 if (!(cp = *argp++) || *cp == '-')
198 adios(NULL, "missing argument to %s",
201 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
206 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
207 adios(NULL, "missing argument to %s",
209 file = *cp == '-' ? cp : getcpy(expanddir(cp));
213 if (!(cp = *argp++) || *cp == '-')
214 adios(NULL, "missing argument to %s",
218 formsw = getcpy(etcpath(cp));
232 if (*cp == '+' || *cp == '@') {
234 adios(NULL, "only one folder at a time!");
236 folder = getcpy(expandfol(cp));
238 app_msgarg(&msgs, cp);
241 /* null terminate the list of acceptable parts/types */
247 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
252 ** Check if we've specified an additional profile
254 if ((cp = getenv("MHSHOW"))) {
255 if ((fp = fopen(cp, "r"))) {
256 readconfig((struct node **) 0, fp, cp, 0);
259 admonish("", "unable to read $MHSHOW profile (%s)",
265 ** Read the standard profile setup
267 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
268 readconfig((struct node **) 0, fp, cp, 0);
272 /* Check for public cache location */
273 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
276 /* Check for private cache location */
277 if (!(cache_private = context_find(nmhprivcache)))
278 cache_private = ".cache";
279 cache_private = getcpy(toabsdir(cache_private));
282 ** Check for storage directory. If specified,
283 ** then store temporary files there. Else we
284 ** store them in standard nmh directory.
286 if ((cp = context_find(nmhstorage)) && *cp)
287 tmp = concat(cp, "/", invo_name, NULL);
289 tmp = getcpy(toabsdir(invo_name));
291 if (file && msgs.size)
292 adios(NULL, "cannot specify msg and file at same time!");
295 ** check if message is coming from file
298 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
299 adios(NULL, "out of memory");
302 if ((ct = parse_mime(file)))
306 ** message(s) are coming from a folder
309 app_msgarg(&msgs, seq_cur);
311 folder = getcurfol();
312 maildir = toabsdir(folder);
314 if (chdir(maildir) == NOTOK)
315 adios(maildir, "unable to change directory to");
317 /* read folder and create message structure */
318 if (!(mp = folder_read(folder)))
319 adios(NULL, "unable to read folder %s", folder);
321 /* check for empty folder */
323 adios(NULL, "no messages in %s", folder);
325 /* parse all the message ranges/sequences and set SELECTED */
326 for (msgnum = 0; msgnum < msgs.size; msgnum++)
327 if (!m_convert(mp, msgs.msgs[msgnum]))
331 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
332 ** since we will use that as a tag to know which messages
333 ** to remove from the "unseen" sequence.
335 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
336 if (is_selected(mp, msgnum))
337 set_unseen(mp, msgnum);
339 seq_setprev(mp); /* set the Previous-Sequence */
340 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
342 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
344 adios(NULL, "out of memory");
348 ** Parse all the SELECTED messages.
350 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
351 if (is_selected(mp, msgnum)) {
354 msgnam = m_name(msgnum);
355 if ((ct = parse_mime(msgnam)))
365 SIGNAL(SIGQUIT, quitser);
366 SIGNAL(SIGPIPE, pipeser);
369 ** Get the associated umask for the relevant contents.
371 for (ctp = cts; *ctp; ctp++) {
375 if (type_ok(ct, 1) && !ct->c_umask) {
376 if (stat(ct->c_file, &st) != NOTOK)
377 ct->c_umask = ~(st.st_mode & 0777);
379 ct->c_umask = ~m_gmprot();
384 ** Show the message content
386 show_all_messages(cts);
388 /* Now free all the structures for the content */
389 for (ctp = cts; *ctp; ctp++)
395 /* If reading from a folder, do some updating */
397 context_replace(curfolder, folder); /* update current folder */
398 seq_setcur(mp, mp->hghsel); /* update current message */
399 seq_save(mp); /* synchronize sequences */
400 context_save(); /* save the context file */
414 fprintf(stderr, "\n");