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>
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
20 static struct swit switches[] = {
25 #define FILESW 2 /* interface from show */
28 { "form formfile", 0 },
32 { "type content", 0 },
34 { "rcache policy", 0 },
36 { "wcache policy", 0 },
48 extern char *tmp; /* directory to place temp files */
53 extern char *cache_public;
54 extern char *cache_private;
63 extern char *parts[NPARTS + 1];
64 extern char *types[NTYPES + 1];
70 #define quitser pipeser
73 CT parse_mime(char *);
78 void set_endian(void);
79 void flush_errors(void);
82 void show_all_messages(CT *);
85 void free_content(CT);
87 void freects_done(int) NORETURN;
92 static void pipeser(int);
96 main(int argc, char **argv)
98 int msgnum, *icachesw;
99 char *cp, *file = NULL, *folder = NULL;
100 char *maildir, buf[100], **argp;
102 struct msgs_array msgs = { 0, 0, NULL };
103 struct msgs *mp = NULL;
110 setlocale(LC_ALL, "");
112 invo_name = mhbasename(argv[0]);
114 /* read user profile/context */
117 arguments = getarguments(invo_name, argc, argv, 1);
123 while ((cp = *argp++)) {
125 switch (smatch(++cp, switches)) {
127 ambigsw(cp, switches);
130 adios(NULL, "-%s unknown", cp);
133 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
134 print_help(buf, switches, 1);
137 print_version(invo_name);
141 icachesw = &rcachesw;
144 icachesw = &wcachesw;
146 if (!(cp = *argp++) || *cp == '-')
147 adios(NULL, "missing argument to %s",
149 switch (*icachesw = smatch(cp, caches)) {
154 adios(NULL, "%s unknown", cp);
161 if (!(cp = *argp++) || *cp == '-')
162 adios(NULL, "missing argument to %s",
165 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
170 if (!(cp = *argp++) || *cp == '-')
171 adios(NULL, "missing argument to %s",
174 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
179 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
180 adios(NULL, "missing argument to %s",
182 file = *cp == '-' ? cp : getcpy(expanddir(cp));
186 if (!(cp = *argp++) || *cp == '-')
187 adios(NULL, "missing argument to %s",
191 formsw = getcpy(etcpath(cp));
205 if (*cp == '+' || *cp == '@') {
207 adios(NULL, "only one folder at a time!");
209 folder = getcpy(expandfol(cp));
211 app_msgarg(&msgs, cp);
214 /* null terminate the list of acceptable parts/types */
220 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
225 ** Check if we've specified an additional profile
227 if ((cp = getenv("MHSHOW"))) {
228 if ((fp = fopen(cp, "r"))) {
229 readconfig((struct node **) 0, fp, cp, 0);
232 admonish("", "unable to read $MHSHOW profile (%s)",
238 ** Read the standard profile setup
240 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
241 readconfig((struct node **) 0, fp, cp, 0);
245 /* Check for public cache location */
246 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
249 /* Check for private cache location */
250 if (!(cache_private = context_find(nmhprivcache)))
251 cache_private = ".cache";
252 cache_private = getcpy(toabsdir(cache_private));
255 ** Check for storage directory. If specified,
256 ** then store temporary files there. Else we
257 ** store them in standard nmh directory.
259 if ((cp = context_find(nmhstorage)) && *cp)
260 tmp = concat(cp, "/", invo_name, NULL);
262 tmp = getcpy(toabsdir(invo_name));
264 if (file && msgs.size)
265 adios(NULL, "cannot specify msg and file at same time!");
268 ** check if message is coming from file
271 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
272 adios(NULL, "out of memory");
275 if ((ct = parse_mime(file)))
279 ** message(s) are coming from a folder
282 app_msgarg(&msgs, seq_cur);
284 folder = getcurfol();
285 maildir = toabsdir(folder);
287 if (chdir(maildir) == NOTOK)
288 adios(maildir, "unable to change directory to");
290 /* read folder and create message structure */
291 if (!(mp = folder_read(folder)))
292 adios(NULL, "unable to read folder %s", folder);
294 /* check for empty folder */
296 adios(NULL, "no messages in %s", folder);
298 /* parse all the message ranges/sequences and set SELECTED */
299 for (msgnum = 0; msgnum < msgs.size; msgnum++)
300 if (!m_convert(mp, msgs.msgs[msgnum]))
304 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
305 ** since we will use that as a tag to know which messages
306 ** to remove from the "unseen" sequence.
308 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
309 if (is_selected(mp, msgnum))
310 set_unseen(mp, msgnum);
312 seq_setprev(mp); /* set the Previous-Sequence */
313 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
315 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
317 adios(NULL, "out of memory");
321 ** Parse all the SELECTED messages.
323 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
324 if (is_selected(mp, msgnum)) {
327 msgnam = m_name(msgnum);
328 if ((ct = parse_mime(msgnam)))
338 SIGNAL(SIGQUIT, quitser);
339 SIGNAL(SIGPIPE, pipeser);
342 ** Get the associated umask for the relevant contents.
344 for (ctp = cts; *ctp; ctp++) {
348 if (type_ok(ct, 1) && !ct->c_umask) {
349 if (stat(ct->c_file, &st) != NOTOK)
350 ct->c_umask = ~(st.st_mode & 0777);
352 ct->c_umask = ~m_gmprot();
357 ** Show the message content
359 show_all_messages(cts);
361 /* Now free all the structures for the content */
362 for (ctp = cts; *ctp; ctp++)
368 /* If reading from a folder, do some updating */
370 context_replace(curfolder, folder); /* update current folder */
371 seq_setcur(mp, mp->hghsel); /* update current message */
372 seq_save(mp); /* synchronize sequences */
373 context_save(); /* save the context file */
387 fprintf(stderr, "\n");