mhlist, mhbuild: Always evaluate realsizes of the MIME parts.
[mmh] / uip / mhlist.c
1 /*
2 ** mhlist.c -- list 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 HEADSW  0
21         { "headers", 0 },
22 #define NHEADSW  1
23         { "noheaders", 0 },
24 #define VERBSW  2
25         { "verbose", 0 },
26 #define NVERBSW  3
27         { "noverbose", 0 },
28 #define FILESW  4  /* interface from show */
29         { "file file", 0 },
30 #define PARTSW  5
31         { "part number", 0 },
32 #define TYPESW  6
33         { "type content", 0 },
34 #define VERSIONSW  7
35         { "version", 0 },
36 #define HELPSW  8
37         { "help", 0 },
38 #define DEBUGSW  9
39         { "debug", -5 },
40         { NULL, 0 }
41 };
42
43
44 /* mhparse.c */
45 extern char *tmp;  /* directory to place temp files */
46
47 /* mhmisc.c */
48 extern int npart;
49 extern int ntype;
50 extern char *parts[NPARTS + 1];
51 extern char *types[NTYPES + 1];
52 extern int userrs;
53
54 /*
55 ** This is currently needed to keep mhparse happy.
56 ** This needs to be changed.
57 */
58 pid_t xpid  = 0;
59
60 int debugsw = 0;
61 int verbosw = 0;
62
63 #define quitser pipeser
64
65 /* mhparse.c */
66 CT parse_mime(char *);
67
68 /* mhmisc.c */
69 int part_ok(CT, int);
70 int type_ok(CT, int);
71 void set_endian(void);
72 void flush_errors(void);
73
74 /* mhlistsbr.c */
75 void list_all_messages(CT *, int, int, int);
76
77 /* mhfree.c */
78 void free_content(CT);
79 extern CT *cts;
80 void freects_done(int) NORETURN;
81
82 /*
83 ** static prototypes
84 */
85 static void pipeser(int);
86
87
88 int
89 main(int argc, char **argv)
90 {
91         int headsw = 1;
92         int msgnum;
93         char *cp, *file = NULL, *folder = NULL;
94         char *maildir, buf[100], **argp;
95         char **arguments;
96         struct msgs_array msgs = { 0, 0, NULL };
97         struct msgs *mp = NULL;
98         CT ct, *ctp;
99
100         done=freects_done;
101
102 #ifdef LOCALE
103         setlocale(LC_ALL, "");
104 #endif
105         invo_name = mhbasename(argv[0]);
106
107         /* read user profile/context */
108         context_read();
109
110         arguments = getarguments(invo_name, argc, argv, 1);
111         argp = arguments;
112
113         /*
114         ** Parse arguments
115         */
116         while ((cp = *argp++)) {
117                 if (*cp == '-') {
118                         switch (smatch(++cp, switches)) {
119                         case AMBIGSW:
120                                 ambigsw(cp, switches);
121                                 done(1);
122                         case UNKWNSW:
123                                 adios(NULL, "-%s unknown", cp);
124
125                         case HELPSW:
126                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
127                                 print_help(buf, switches, 1);
128                                 done(1);
129                         case VERSIONSW:
130                                 print_version(invo_name);
131                                 done(1);
132
133                         case HEADSW:
134                                 headsw = 1;
135                                 continue;
136                         case NHEADSW:
137                                 headsw = 0;
138                                 continue;
139
140                         case PARTSW:
141                                 if (!(cp = *argp++) || *cp == '-')
142                                         adios(NULL, "missing argument to %s",
143                                                         argp[-2]);
144                                 if (npart >= NPARTS)
145                                         adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
146                                 parts[npart++] = cp;
147                                 continue;
148
149                         case TYPESW:
150                                 if (!(cp = *argp++) || *cp == '-')
151                                         adios(NULL, "missing argument to %s",
152                                                         argp[-2]);
153                                 if (ntype >= NTYPES)
154                                         adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
155                                 types[ntype++] = cp;
156                                 continue;
157
158                         case FILESW:
159                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
160                                         adios(NULL, "missing argument to %s",
161                                                         argp[-2]);
162                                 file = *cp == '-' ? cp : getcpy(expanddir(cp));
163                                 continue;
164
165                         case VERBSW:
166                                 verbosw = 1;
167                                 continue;
168                         case NVERBSW:
169                                 verbosw = 0;
170                                 continue;
171                         case DEBUGSW:
172                                 debugsw = 1;
173                                 continue;
174                         }
175                 }
176                 if (*cp == '+' || *cp == '@') {
177                         if (folder)
178                                 adios(NULL, "only one folder at a time!");
179                         else
180                                 folder = getcpy(expandfol(cp));
181                 } else
182                         app_msgarg(&msgs, cp);
183         }
184
185         /* null terminate the list of acceptable parts/types */
186         parts[npart] = NULL;
187         types[ntype] = NULL;
188
189         set_endian();
190
191         /*
192         ** Check for storage directory.  If specified,
193         ** then store temporary files there.  Else we
194         ** store them in standard nmh directory.
195         */
196         if ((cp = context_find(nmhstorage)) && *cp)
197                 tmp = concat(cp, "/", invo_name, NULL);
198         else
199                 tmp = getcpy(toabsdir(invo_name));
200
201         if (file && msgs.size)
202                 adios(NULL, "cannot specify msg and file at same time!");
203
204         /*
205         ** check if message is coming from file
206         */
207         if (file) {
208                 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
209                         adios(NULL, "out of memory");
210                 ctp = cts;
211
212                 if ((ct = parse_mime(file)))
213                         *ctp++ = ct;
214         } else {
215                 /*
216                 ** message(s) are coming from a folder
217                 */
218                 if (!msgs.size)
219                         app_msgarg(&msgs, seq_cur);
220                 if (!folder)
221                         folder = getcurfol();
222                 maildir = toabsdir(folder);
223
224                 if (chdir(maildir) == NOTOK)
225                         adios(maildir, "unable to change directory to");
226
227                 /* read folder and create message structure */
228                 if (!(mp = folder_read(folder)))
229                         adios(NULL, "unable to read folder %s", folder);
230
231                 /* check for empty folder */
232                 if (mp->nummsg == 0)
233                         adios(NULL, "no messages in %s", folder);
234
235                 /* parse all the message ranges/sequences and set SELECTED */
236                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
237                         if (!m_convert(mp, msgs.msgs[msgnum]))
238                                 done (1);
239                 seq_setprev(mp);  /* set the previous-sequence */
240
241                 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
242                                 sizeof(*cts))))
243                         adios(NULL, "out of memory");
244                 ctp = cts;
245
246                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
247                         if (is_selected(mp, msgnum)) {
248                                 char *msgnam;
249
250                                 msgnam = m_name(msgnum);
251                                 if ((ct = parse_mime(msgnam)))
252                                         *ctp++ = ct;
253                         }
254                 }
255         }
256
257         if (!*cts)
258                 done(1);
259
260         userrs = 1;
261         SIGNAL(SIGQUIT, quitser);
262         SIGNAL(SIGPIPE, pipeser);
263
264         /*
265         ** Get the associated umask for the relevant contents.
266         */
267         for (ctp = cts; *ctp; ctp++) {
268                 struct stat st;
269
270                 ct = *ctp;
271                 if (type_ok(ct, 1) && !ct->c_umask) {
272                         if (stat(ct->c_file, &st) != NOTOK)
273                                 ct->c_umask = ~(st.st_mode & 0777);
274                         else
275                                 ct->c_umask = ~m_gmprot();
276                 }
277         }
278
279         /*
280         ** List the message content
281         */
282         list_all_messages(cts, headsw, verbosw, debugsw);
283
284         /* Now free all the structures for the content */
285         for (ctp = cts; *ctp; ctp++)
286                 free_content(*ctp);
287
288         free((char *) cts);
289         cts = NULL;
290
291         /* If reading from a folder, do some updating */
292         if (mp) {
293                 context_replace(curfolder, folder); /* update current folder */
294                 seq_setcur(mp, mp->hghsel);  /* update current message */
295                 seq_save(mp);  /* synchronize sequences  */
296                 context_save();  /* save the context file  */
297         }
298
299         done(0);
300         return 1;
301 }
302
303
304 static void
305 pipeser(int i)
306 {
307         if (i == SIGQUIT) {
308                 unlink("core");
309                 fflush(stdout);
310                 fprintf(stderr, "\n");
311                 fflush(stderr);
312         }
313
314         done(1);
315         /* NOTREACHED */
316 }