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 },
57 extern char *tmp; /* directory to place temp files */
62 extern char *cache_public;
63 extern char *cache_private;
73 extern char *parts[NPARTS + 1];
74 extern char *types[NTYPES + 1];
80 #define quitser pipeser
83 CT parse_mime(char *);
88 void set_endian(void);
89 void flush_errors(void);
92 void show_all_messages(CT *);
95 void free_content(CT);
97 void freects_done(int) NORETURN;
102 static void pipeser(int);
106 main(int argc, char **argv)
108 int msgnum, *icachesw;
109 char *cp, *file = NULL, *folder = NULL;
110 char *maildir, buf[100], **argp;
112 struct msgs_array msgs = { 0, 0, NULL };
113 struct msgs *mp = NULL;
120 setlocale(LC_ALL, "");
122 invo_name = mhbasename(argv[0]);
124 /* read user profile/context */
127 arguments = getarguments(invo_name, argc, argv, 1);
133 while ((cp = *argp++)) {
135 switch (smatch(++cp, switches)) {
137 ambigsw(cp, switches);
140 adios(NULL, "-%s unknown", cp);
143 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
144 print_help(buf, switches, 1);
147 print_version(invo_name);
151 icachesw = &rcachesw;
154 icachesw = &wcachesw;
156 if (!(cp = *argp++) || *cp == '-')
157 adios(NULL, "missing argument to %s",
159 switch (*icachesw = smatch(cp, caches)) {
164 adios(NULL, "%s unknown", cp);
185 if (!(cp = *argp++) || *cp == '-')
186 adios(NULL, "missing argument to %s",
189 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
194 if (!(cp = *argp++) || *cp == '-')
195 adios(NULL, "missing argument to %s",
198 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
203 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
204 adios(NULL, "missing argument to %s",
206 file = *cp == '-' ? cp : getcpy(expanddir(cp));
210 if (!(cp = *argp++) || *cp == '-')
211 adios(NULL, "missing argument to %s",
215 formsw = getcpy(etcpath(cp));
229 if (*cp == '+' || *cp == '@') {
231 adios(NULL, "only one folder at a time!");
233 folder = getcpy(expandfol(cp));
235 app_msgarg(&msgs, cp);
238 /* null terminate the list of acceptable parts/types */
244 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
249 ** Check if we've specified an additional profile
251 if ((cp = getenv("MHSHOW"))) {
252 if ((fp = fopen(cp, "r"))) {
253 readconfig((struct node **) 0, fp, cp, 0);
256 admonish("", "unable to read $MHSHOW profile (%s)",
262 ** Read the standard profile setup
264 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
265 readconfig((struct node **) 0, fp, cp, 0);
269 /* Check for public cache location */
270 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
273 /* Check for private cache location */
274 if (!(cache_private = context_find(nmhprivcache)))
275 cache_private = ".cache";
276 cache_private = getcpy(toabsdir(cache_private));
279 ** Check for storage directory. If specified,
280 ** then store temporary files there. Else we
281 ** store them in standard nmh directory.
283 if ((cp = context_find(nmhstorage)) && *cp)
284 tmp = concat(cp, "/", invo_name, NULL);
286 tmp = getcpy(toabsdir(invo_name));
288 if (file && msgs.size)
289 adios(NULL, "cannot specify msg and file at same time!");
292 ** check if message is coming from file
295 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
296 adios(NULL, "out of memory");
299 if ((ct = parse_mime(file)))
303 ** message(s) are coming from a folder
306 app_msgarg(&msgs, seq_cur);
308 folder = getcurfol();
309 maildir = toabsdir(folder);
311 if (chdir(maildir) == NOTOK)
312 adios(maildir, "unable to change directory to");
314 /* read folder and create message structure */
315 if (!(mp = folder_read(folder)))
316 adios(NULL, "unable to read folder %s", folder);
318 /* check for empty folder */
320 adios(NULL, "no messages in %s", folder);
322 /* parse all the message ranges/sequences and set SELECTED */
323 for (msgnum = 0; msgnum < msgs.size; msgnum++)
324 if (!m_convert(mp, msgs.msgs[msgnum]))
328 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
329 ** since we will use that as a tag to know which messages
330 ** to remove from the "unseen" sequence.
332 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
333 if (is_selected(mp, msgnum))
334 set_unseen(mp, msgnum);
336 seq_setprev(mp); /* set the Previous-Sequence */
337 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
339 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
341 adios(NULL, "out of memory");
345 ** Parse all the SELECTED messages.
347 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
348 if (is_selected(mp, msgnum)) {
351 msgnam = m_name(msgnum);
352 if ((ct = parse_mime(msgnam)))
362 SIGNAL(SIGQUIT, quitser);
363 SIGNAL(SIGPIPE, pipeser);
366 ** Get the associated umask for the relevant contents.
368 for (ctp = cts; *ctp; ctp++) {
372 if (type_ok(ct, 1) && !ct->c_umask) {
373 if (stat(ct->c_file, &st) != NOTOK)
374 ct->c_umask = ~(st.st_mode & 0777);
376 ct->c_umask = ~m_gmprot();
381 ** Show the message content
383 show_all_messages(cts);
385 /* Now free all the structures for the content */
386 for (ctp = cts; *ctp; ctp++)
392 /* If reading from a folder, do some updating */
394 context_replace(curfolder, folder); /* update current folder */
395 seq_setcur(mp, mp->hghsel); /* update current message */
396 seq_save(mp); /* synchronize sequences */
397 context_save(); /* save the context file */
411 fprintf(stderr, "\n");