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[] = {
30 #define FILESW 4 /* interface from show */
33 { "form formfile", 0 },
37 { "type content", 0 },
39 { "rcache policy", 0 },
41 { "wcache policy", 0 },
53 extern char *tmp; /* directory to place temp files */
58 extern char *cache_public;
59 extern char *cache_private;
68 extern char *parts[NPARTS + 1];
69 extern char *types[NTYPES + 1];
75 #define quitser pipeser
78 CT parse_mime(char *);
83 void set_endian(void);
84 void flush_errors(void);
87 void show_all_messages(CT *);
90 void free_content(CT);
92 void freects_done(int) NORETURN;
97 static void pipeser(int);
101 main(int argc, char **argv)
103 int msgnum, *icachesw;
104 char *cp, *file = NULL, *folder = NULL;
105 char *maildir, buf[100], **argp;
107 struct msgs_array msgs = { 0, 0, NULL };
108 struct msgs *mp = NULL;
115 setlocale(LC_ALL, "");
117 invo_name = mhbasename(argv[0]);
119 /* read user profile/context */
122 arguments = getarguments(invo_name, argc, argv, 1);
128 while ((cp = *argp++)) {
130 switch (smatch(++cp, switches)) {
132 ambigsw(cp, switches);
135 adios(NULL, "-%s unknown", cp);
138 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
139 print_help(buf, switches, 1);
142 print_version(invo_name);
146 icachesw = &rcachesw;
149 icachesw = &wcachesw;
151 if (!(cp = *argp++) || *cp == '-')
152 adios(NULL, "missing argument to %s",
154 switch (*icachesw = smatch(cp, caches)) {
159 adios(NULL, "%s unknown", cp);
173 if (!(cp = *argp++) || *cp == '-')
174 adios(NULL, "missing argument to %s",
177 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
182 if (!(cp = *argp++) || *cp == '-')
183 adios(NULL, "missing argument to %s",
186 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
191 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
192 adios(NULL, "missing argument to %s",
194 file = *cp == '-' ? cp : getcpy(expanddir(cp));
198 if (!(cp = *argp++) || *cp == '-')
199 adios(NULL, "missing argument to %s",
203 formsw = getcpy(etcpath(cp));
217 if (*cp == '+' || *cp == '@') {
219 adios(NULL, "only one folder at a time!");
221 folder = getcpy(expandfol(cp));
223 app_msgarg(&msgs, cp);
226 /* null terminate the list of acceptable parts/types */
232 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
237 ** Check if we've specified an additional profile
239 if ((cp = getenv("MHSHOW"))) {
240 if ((fp = fopen(cp, "r"))) {
241 readconfig((struct node **) 0, fp, cp, 0);
244 admonish("", "unable to read $MHSHOW profile (%s)",
250 ** Read the standard profile setup
252 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
253 readconfig((struct node **) 0, fp, cp, 0);
257 /* Check for public cache location */
258 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
261 /* Check for private cache location */
262 if (!(cache_private = context_find(nmhprivcache)))
263 cache_private = ".cache";
264 cache_private = getcpy(toabsdir(cache_private));
267 ** Check for storage directory. If specified,
268 ** then store temporary files there. Else we
269 ** store them in standard nmh directory.
271 if ((cp = context_find(nmhstorage)) && *cp)
272 tmp = concat(cp, "/", invo_name, NULL);
274 tmp = getcpy(toabsdir(invo_name));
276 if (file && msgs.size)
277 adios(NULL, "cannot specify msg and file at same time!");
280 ** check if message is coming from file
283 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
284 adios(NULL, "out of memory");
287 if ((ct = parse_mime(file)))
291 ** message(s) are coming from a folder
294 app_msgarg(&msgs, seq_cur);
296 folder = getcurfol();
297 maildir = toabsdir(folder);
299 if (chdir(maildir) == NOTOK)
300 adios(maildir, "unable to change directory to");
302 /* read folder and create message structure */
303 if (!(mp = folder_read(folder)))
304 adios(NULL, "unable to read folder %s", folder);
306 /* check for empty folder */
308 adios(NULL, "no messages in %s", folder);
310 /* parse all the message ranges/sequences and set SELECTED */
311 for (msgnum = 0; msgnum < msgs.size; msgnum++)
312 if (!m_convert(mp, msgs.msgs[msgnum]))
316 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
317 ** since we will use that as a tag to know which messages
318 ** to remove from the "unseen" sequence.
320 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
321 if (is_selected(mp, msgnum))
322 set_unseen(mp, msgnum);
324 seq_setprev(mp); /* set the Previous-Sequence */
325 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
327 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
329 adios(NULL, "out of memory");
333 ** Parse all the SELECTED messages.
335 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
336 if (is_selected(mp, msgnum)) {
339 msgnam = m_name(msgnum);
340 if ((ct = parse_mime(msgnam)))
350 SIGNAL(SIGQUIT, quitser);
351 SIGNAL(SIGPIPE, pipeser);
354 ** Get the associated umask for the relevant contents.
356 for (ctp = cts; *ctp; ctp++) {
360 if (type_ok(ct, 1) && !ct->c_umask) {
361 if (stat(ct->c_file, &st) != NOTOK)
362 ct->c_umask = ~(st.st_mode & 0777);
364 ct->c_umask = ~m_gmprot();
369 ** Show the message content
371 show_all_messages(cts);
373 /* Now free all the structures for the content */
374 for (ctp = cts; *ctp; ctp++)
380 /* If reading from a folder, do some updating */
382 context_replace(curfolder, folder); /* update current folder */
383 seq_setcur(mp, mp->hghsel); /* update current message */
384 seq_save(mp); /* synchronize sequences */
385 context_save(); /* save the context file */
399 fprintf(stderr, "\n");