Remove caching of external MIME parts.
[mmh] / uip / mhshow.c
1 /*
2 ** mhshow.c -- display the contents of MIME messages
3 **
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.
7 */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <h/signals.h>
12 #include <errno.h>
13 #include <signal.h>
14 #include <h/tws.h>
15 #include <h/mime.h>
16 #include <h/mhparse.h>
17 #include <h/utils.h>
18
19 static struct swit switches[] = {
20 #define VERBSW  0
21         { "verbose", 0 },
22 #define NVERBSW  1
23         { "noverbose", 0 },
24 #define FILESW  2  /* interface from show */
25         { "file file", 0 },
26 #define FORMSW  3
27         { "form formfile", 0 },
28 #define PARTSW  4
29         { "part number", 0 },
30 #define TYPESW  5
31         { "type content", 0 },
32 #define VERSIONSW  6
33         { "version", 0 },
34 #define HELPSW  7
35         { "help", 0 },
36 #define DEBUGSW  8
37         { "debug", -5 },
38         { NULL, 0 }
39 };
40
41
42 /* mhparse.c */
43 extern char *tmp;  /* directory to place temp files */
44
45 /* mhshowsbr.c */
46 extern int nolist;
47 extern char *formsw;
48
49 /* mhmisc.c */
50 extern int npart;
51 extern int ntype;
52 extern char *parts[NPARTS + 1];
53 extern char *types[NTYPES + 1];
54 extern int userrs;
55
56 int debugsw = 0;
57 int verbosw = 0;
58
59 #define quitser pipeser
60
61 /* mhparse.c */
62 CT parse_mime(char *);
63
64 /* mhmisc.c */
65 int part_ok(CT, int);
66 int type_ok(CT, int);
67 void set_endian(void);
68 void flush_errors(void);
69
70 /* mhshowsbr.c */
71 void show_all_messages(CT *);
72
73 /* mhfree.c */
74 void free_content(CT);
75 extern CT *cts;
76 void freects_done(int) NORETURN;
77
78 /*
79 ** static prototypes
80 */
81 static void pipeser(int);
82
83
84 int
85 main(int argc, char **argv)
86 {
87         int msgnum;
88         char *cp, *file = NULL, *folder = NULL;
89         char *maildir, buf[100], **argp;
90         char **arguments;
91         struct msgs_array msgs = { 0, 0, NULL };
92         struct msgs *mp = NULL;
93         CT ct, *ctp;
94         FILE *fp;
95
96         done=freects_done;
97
98 #ifdef LOCALE
99         setlocale(LC_ALL, "");
100 #endif
101         invo_name = mhbasename(argv[0]);
102
103         /* read user profile/context */
104         context_read();
105
106         arguments = getarguments(invo_name, argc, argv, 1);
107         argp = arguments;
108
109         /*
110         ** Parse arguments
111         */
112         while ((cp = *argp++)) {
113                 if (*cp == '-') {
114                         switch (smatch(++cp, switches)) {
115                         case AMBIGSW:
116                                 ambigsw(cp, switches);
117                                 done(1);
118                         case UNKWNSW:
119                                 adios(NULL, "-%s unknown", cp);
120
121                         case HELPSW:
122                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
123                                 print_help(buf, switches, 1);
124                                 done(1);
125                         case VERSIONSW:
126                                 print_version(invo_name);
127                                 done(1);
128
129                         case PARTSW:
130                                 if (!(cp = *argp++) || *cp == '-')
131                                         adios(NULL, "missing argument to %s",
132                                                         argp[-2]);
133                                 if (npart >= NPARTS)
134                                         adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
135                                 parts[npart++] = cp;
136                                 continue;
137
138                         case TYPESW:
139                                 if (!(cp = *argp++) || *cp == '-')
140                                         adios(NULL, "missing argument to %s",
141                                                         argp[-2]);
142                                 if (ntype >= NTYPES)
143                                         adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
144                                 types[ntype++] = cp;
145                                 continue;
146
147                         case FILESW:
148                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
149                                         adios(NULL, "missing argument to %s",
150                                                         argp[-2]);
151                                 file = *cp == '-' ? cp : getcpy(expanddir(cp));
152                                 continue;
153
154                         case FORMSW:
155                                 if (!(cp = *argp++) || *cp == '-')
156                                         adios(NULL, "missing argument to %s",
157                                                         argp[-2]);
158                                 if (formsw)
159                                         free(formsw);
160                                 formsw = getcpy(etcpath(cp));
161                                 continue;
162
163                         case VERBSW:
164                                 verbosw = 1;
165                                 continue;
166                         case NVERBSW:
167                                 verbosw = 0;
168                                 continue;
169                         case DEBUGSW:
170                                 debugsw = 1;
171                                 continue;
172                         }
173                 }
174                 if (*cp == '+' || *cp == '@') {
175                         if (folder)
176                                 adios(NULL, "only one folder at a time!");
177                         else
178                                 folder = getcpy(expandfol(cp));
179                 } else
180                         app_msgarg(&msgs, cp);
181         }
182
183         /* null terminate the list of acceptable parts/types */
184         parts[npart] = NULL;
185         types[ntype] = NULL;
186
187         set_endian();
188
189         if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
190                 nolist  = 1;
191         }
192
193         /*
194         ** Check if we've specified an additional profile
195         */
196         if ((cp = getenv("MHSHOW"))) {
197                 if ((fp = fopen(cp, "r"))) {
198                         readconfig((struct node **) 0, fp, cp, 0);
199                         fclose(fp);
200                 } else {
201                         admonish("", "unable to read $MHSHOW profile (%s)",
202                                         cp);
203                 }
204         }
205
206         /*
207         ** Read the standard profile setup
208         */
209         if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
210                 readconfig((struct node **) 0, fp, cp, 0);
211                 fclose(fp);
212         }
213
214         /*
215         ** Check for storage directory.  If specified,
216         ** then store temporary files there.  Else we
217         ** store them in standard nmh directory.
218         */
219         if ((cp = context_find(nmhstorage)) && *cp)
220                 tmp = concat(cp, "/", invo_name, NULL);
221         else
222                 tmp = getcpy(toabsdir(invo_name));
223
224         if (file && msgs.size)
225                 adios(NULL, "cannot specify msg and file at same time!");
226
227         /*
228         ** check if message is coming from file
229         */
230         if (file) {
231                 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
232                         adios(NULL, "out of memory");
233                 ctp = cts;
234
235                 if ((ct = parse_mime(file)))
236                         *ctp++ = ct;
237         } else {
238                 /*
239                 ** message(s) are coming from a folder
240                 */
241                 if (!msgs.size)
242                         app_msgarg(&msgs, seq_cur);
243                 if (!folder)
244                         folder = getcurfol();
245                 maildir = toabsdir(folder);
246
247                 if (chdir(maildir) == NOTOK)
248                         adios(maildir, "unable to change directory to");
249
250                 /* read folder and create message structure */
251                 if (!(mp = folder_read(folder)))
252                         adios(NULL, "unable to read folder %s", folder);
253
254                 /* check for empty folder */
255                 if (mp->nummsg == 0)
256                         adios(NULL, "no messages in %s", folder);
257
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]))
261                                 done(1);
262
263                 /*
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.
267                 */
268                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
269                         if (is_selected(mp, msgnum))
270                                 set_unseen(mp, msgnum);
271
272                 seq_setprev(mp);  /* set the Previous-Sequence */
273                 seq_setunseen(mp, 0);  /* unset unseen seqs for shown msgs */
274
275                 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
276                                 sizeof(*cts))))
277                         adios(NULL, "out of memory");
278                 ctp = cts;
279
280                 /*
281                 ** Parse all the SELECTED messages.
282                 */
283                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
284                         if (is_selected(mp, msgnum)) {
285                                 char *msgnam;
286
287                                 msgnam = m_name(msgnum);
288                                 if ((ct = parse_mime(msgnam)))
289                                         *ctp++ = ct;
290                         }
291                 }
292         }
293
294         if (!*cts)
295                 done(1);
296
297         userrs = 1;
298         SIGNAL(SIGQUIT, quitser);
299         SIGNAL(SIGPIPE, pipeser);
300
301         /*
302         ** Get the associated umask for the relevant contents.
303         */
304         for (ctp = cts; *ctp; ctp++) {
305                 struct stat st;
306
307                 ct = *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);
311                         else
312                                 ct->c_umask = ~m_gmprot();
313                 }
314         }
315
316         /*
317         ** Show the message content
318         */
319         show_all_messages(cts);
320
321         /* Now free all the structures for the content */
322         for (ctp = cts; *ctp; ctp++)
323                 free_content(*ctp);
324
325         free((char *) cts);
326         cts = NULL;
327
328         /* If reading from a folder, do some updating */
329         if (mp) {
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 */
334         }
335
336         done(0);
337         return 1;
338 }
339
340
341 static void
342 pipeser(int i)
343 {
344         if (i == SIGQUIT) {
345                 unlink("core");
346                 fflush(stdout);
347                 fprintf(stderr, "\n");
348                 fflush(stderr);
349         }
350
351         done(1);
352         /* NOTREACHED */
353 }