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>
19 static struct swit switches[] = {
24 #define FILESW 2 /* interface from show */
27 { "form formfile", 0 },
31 { "type content", 0 },
43 extern char *tmp; /* directory to place temp files */
52 extern char *parts[NPARTS + 1];
53 extern char *types[NTYPES + 1];
59 #define quitser pipeser
62 CT parse_mime(char *);
67 void set_endian(void);
68 void flush_errors(void);
71 void show_all_messages(CT *);
74 void free_content(CT);
76 void freects_done(int) NORETURN;
81 static void pipeser(int);
85 main(int argc, char **argv)
88 char *cp, *file = NULL, *folder = NULL;
89 char *maildir, buf[100], **argp;
91 struct msgs_array msgs = { 0, 0, NULL };
92 struct msgs *mp = NULL;
99 setlocale(LC_ALL, "");
101 invo_name = mhbasename(argv[0]);
103 /* read user profile/context */
106 arguments = getarguments(invo_name, argc, argv, 1);
112 while ((cp = *argp++)) {
114 switch (smatch(++cp, switches)) {
116 ambigsw(cp, switches);
119 adios(NULL, "-%s unknown", cp);
122 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
123 print_help(buf, switches, 1);
126 print_version(invo_name);
130 if (!(cp = *argp++) || *cp == '-')
131 adios(NULL, "missing argument to %s",
134 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
139 if (!(cp = *argp++) || *cp == '-')
140 adios(NULL, "missing argument to %s",
143 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
148 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
149 adios(NULL, "missing argument to %s",
151 file = *cp == '-' ? cp : getcpy(expanddir(cp));
155 if (!(cp = *argp++) || *cp == '-')
156 adios(NULL, "missing argument to %s",
160 formsw = getcpy(etcpath(cp));
174 if (*cp == '+' || *cp == '@') {
176 adios(NULL, "only one folder at a time!");
178 folder = getcpy(expandfol(cp));
180 app_msgarg(&msgs, cp);
183 /* null terminate the list of acceptable parts/types */
189 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
194 ** Check if we've specified an additional profile
196 if ((cp = getenv("MHSHOW"))) {
197 if ((fp = fopen(cp, "r"))) {
198 readconfig((struct node **) 0, fp, cp, 0);
201 admonish("", "unable to read $MHSHOW profile (%s)",
207 ** Read the standard profile setup
209 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
210 readconfig((struct node **) 0, fp, cp, 0);
215 ** Check for storage directory. If specified,
216 ** then store temporary files there. Else we
217 ** store them in standard nmh directory.
219 if ((cp = context_find(nmhstorage)) && *cp)
220 tmp = concat(cp, "/", invo_name, NULL);
222 tmp = getcpy(toabsdir(invo_name));
224 if (file && msgs.size)
225 adios(NULL, "cannot specify msg and file at same time!");
228 ** check if message is coming from file
231 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
232 adios(NULL, "out of memory");
235 if ((ct = parse_mime(file)))
239 ** message(s) are coming from a folder
242 app_msgarg(&msgs, seq_cur);
244 folder = getcurfol();
245 maildir = toabsdir(folder);
247 if (chdir(maildir) == NOTOK)
248 adios(maildir, "unable to change directory to");
250 /* read folder and create message structure */
251 if (!(mp = folder_read(folder)))
252 adios(NULL, "unable to read folder %s", folder);
254 /* check for empty folder */
256 adios(NULL, "no messages in %s", folder);
258 /* parse all the message ranges/sequences and set SELECTED */
259 for (msgnum = 0; msgnum < msgs.size; msgnum++)
260 if (!m_convert(mp, msgs.msgs[msgnum]))
264 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
265 ** since we will use that as a tag to know which messages
266 ** to remove from the "unseen" sequence.
268 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
269 if (is_selected(mp, msgnum))
270 set_unseen(mp, msgnum);
272 seq_setprev(mp); /* set the Previous-Sequence */
273 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
275 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
277 adios(NULL, "out of memory");
281 ** Parse all the SELECTED messages.
283 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
284 if (is_selected(mp, msgnum)) {
287 msgnam = m_name(msgnum);
288 if ((ct = parse_mime(msgnam)))
298 SIGNAL(SIGQUIT, quitser);
299 SIGNAL(SIGPIPE, pipeser);
302 ** Get the associated umask for the relevant contents.
304 for (ctp = cts; *ctp; ctp++) {
308 if (type_ok(ct, 1) && !ct->c_umask) {
309 if (stat(ct->c_file, &st) != NOTOK)
310 ct->c_umask = ~(st.st_mode & 0777);
312 ct->c_umask = ~m_gmprot();
317 ** Show the message content
319 show_all_messages(cts);
321 /* Now free all the structures for the content */
322 for (ctp = cts; *ctp; ctp++)
328 /* If reading from a folder, do some updating */
330 context_replace(curfolder, folder); /* update current folder */
331 seq_setcur(mp, mp->hghsel); /* update current message */
332 seq_save(mp); /* synchronize sequences */
333 context_save(); /* save the context file */
347 fprintf(stderr, "\n");