]> diplodocus.org Git - nmh/blob - uip/mhlist.c
* test/tests/bad-input/test-header: Add test for it.
[nmh] / uip / mhlist.c
1
2 /*
3 * mhlist.c -- list the contents of MIME messages
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <fcntl.h>
14 #include <h/signals.h>
15 #include <h/md5.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include <h/mts.h>
19 #include <h/tws.h>
20 #include <h/mime.h>
21 #include <h/mhparse.h>
22 #include <h/mhcachesbr.h>
23 #include <h/utils.h>
24
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
27 #endif
28
29 static struct swit switches[] = {
30 #define CHECKSW 0
31 { "check", 0 },
32 #define NCHECKSW 1
33 { "nocheck", 0 },
34 #define HEADSW 2
35 { "headers", 0 },
36 #define NHEADSW 3
37 { "noheaders", 0 },
38 #define SIZESW 4
39 { "realsize", 0 },
40 #define NSIZESW 5
41 { "norealsize", 0 },
42 #define VERBSW 6
43 { "verbose", 0 },
44 #define NVERBSW 7
45 { "noverbose", 0 },
46 #define FILESW 8 /* interface from show */
47 { "file file", 0 },
48 #define PARTSW 9
49 { "part number", 0 },
50 #define TYPESW 10
51 { "type content", 0 },
52 #define RCACHESW 11
53 { "rcache policy", 0 },
54 #define WCACHESW 12
55 { "wcache policy", 0 },
56 #define VERSIONSW 13
57 { "version", 0 },
58 #define HELPSW 14
59 { "help", 0 },
60
61 /*
62 * switches for debugging
63 */
64 #define DEBUGSW 15
65 { "debug", -5 },
66 { NULL, 0 }
67 };
68
69
70 /* mhparse.c */
71 extern int checksw;
72 extern char *tmp; /* directory to place temp files */
73
74 /* mhcachesbr.c */
75 extern int rcachesw;
76 extern int wcachesw;
77 extern char *cache_public;
78 extern char *cache_private;
79
80 /* mhmisc.c */
81 extern int npart;
82 extern int ntype;
83 extern char *parts[NPARTS + 1];
84 extern char *types[NTYPES + 1];
85 extern int userrs;
86
87 /*
88 * This is currently needed to keep mhparse happy.
89 * This needs to be changed.
90 */
91 pid_t xpid = 0;
92
93 int debugsw = 0;
94 int verbosw = 0;
95
96 #define quitser pipeser
97
98 /* mhparse.c */
99 CT parse_mime (char *);
100
101 /* mhmisc.c */
102 int part_ok (CT, int);
103 int type_ok (CT, int);
104 void set_endian (void);
105 void flush_errors (void);
106
107 /* mhlistsbr.c */
108 void list_all_messages (CT *, int, int, int, int);
109
110 /* mhfree.c */
111 void free_content (CT);
112 extern CT *cts;
113 void freects_done (int) NORETURN;
114
115 /*
116 * static prototypes
117 */
118 static RETSIGTYPE pipeser (int);
119
120
121 int
122 main (int argc, char **argv)
123 {
124 int sizesw = 1, headsw = 1;
125 int msgnum, *icachesw;
126 char *cp, *file = NULL, *folder = NULL;
127 char *maildir, buf[100], **argp;
128 char **arguments;
129 struct msgs_array msgs = { 0, 0, NULL };
130 struct msgs *mp = NULL;
131 CT ct, *ctp;
132
133 done=freects_done;
134
135 #ifdef LOCALE
136 setlocale(LC_ALL, "");
137 #endif
138 invo_name = r1bindex (argv[0], '/');
139
140 /* read user profile/context */
141 context_read();
142
143 arguments = getarguments (invo_name, argc, argv, 1);
144 argp = arguments;
145
146 /*
147 * Parse arguments
148 */
149 while ((cp = *argp++)) {
150 if (*cp == '-') {
151 switch (smatch (++cp, switches)) {
152 case AMBIGSW:
153 ambigsw (cp, switches);
154 done (1);
155 case UNKWNSW:
156 adios (NULL, "-%s unknown", cp);
157
158 case HELPSW:
159 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
160 invo_name);
161 print_help (buf, switches, 1);
162 done (1);
163 case VERSIONSW:
164 print_version(invo_name);
165 done (1);
166
167 case RCACHESW:
168 icachesw = &rcachesw;
169 goto do_cache;
170 case WCACHESW:
171 icachesw = &wcachesw;
172 do_cache:
173 if (!(cp = *argp++) || *cp == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 switch (*icachesw = smatch (cp, caches)) {
176 case AMBIGSW:
177 ambigsw (cp, caches);
178 done (1);
179 case UNKWNSW:
180 adios (NULL, "%s unknown", cp);
181 default:
182 break;
183 }
184 continue;
185
186 case CHECKSW:
187 checksw++;
188 continue;
189 case NCHECKSW:
190 checksw = 0;
191 continue;
192
193 case HEADSW:
194 headsw = 1;
195 continue;
196 case NHEADSW:
197 headsw = 0;
198 continue;
199
200 case SIZESW:
201 sizesw = 1;
202 continue;
203 case NSIZESW:
204 sizesw = 0;
205 continue;
206
207 case PARTSW:
208 if (!(cp = *argp++) || *cp == '-')
209 adios (NULL, "missing argument to %s", argp[-2]);
210 if (npart >= NPARTS)
211 adios (NULL, "too many parts (starting with %s), %d max",
212 cp, NPARTS);
213 parts[npart++] = cp;
214 continue;
215
216 case TYPESW:
217 if (!(cp = *argp++) || *cp == '-')
218 adios (NULL, "missing argument to %s", argp[-2]);
219 if (ntype >= NTYPES)
220 adios (NULL, "too many types (starting with %s), %d max",
221 cp, NTYPES);
222 types[ntype++] = cp;
223 continue;
224
225 case FILESW:
226 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
227 adios (NULL, "missing argument to %s", argp[-2]);
228 file = *cp == '-' ? cp : path (cp, TFILE);
229 continue;
230
231 case VERBSW:
232 verbosw = 1;
233 continue;
234 case NVERBSW:
235 verbosw = 0;
236 continue;
237 case DEBUGSW:
238 debugsw = 1;
239 continue;
240 }
241 }
242 if (*cp == '+' || *cp == '@') {
243 if (folder)
244 adios (NULL, "only one folder at a time!");
245 else
246 folder = pluspath (cp);
247 } else
248 app_msgarg(&msgs, cp);
249 }
250
251 /* null terminate the list of acceptable parts/types */
252 parts[npart] = NULL;
253 types[ntype] = NULL;
254
255 set_endian ();
256
257 /* Check for public cache location */
258 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
259 cache_public = NULL;
260
261 /* Check for private cache location */
262 if (!(cache_private = context_find (nmhprivcache)))
263 cache_private = ".cache";
264 cache_private = getcpy (m_maildir (cache_private));
265
266 /*
267 * Check for storage directory. If specified,
268 * then store temporary files there. Else we
269 * store them in standard nmh directory.
270 */
271 if ((cp = context_find (nmhstorage)) && *cp)
272 tmp = concat (cp, "/", invo_name, NULL);
273 else
274 tmp = add (m_maildir (invo_name), NULL);
275
276 if (!context_find ("path"))
277 free (path ("./", TFOLDER));
278
279 if (file && msgs.size)
280 adios (NULL, "cannot specify msg and file at same time!");
281
282 /*
283 * check if message is coming from file
284 */
285 if (file) {
286 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
287 adios (NULL, "out of memory");
288 ctp = cts;
289
290 if ((ct = parse_mime (file)));
291 *ctp++ = ct;
292 } else {
293 /*
294 * message(s) are coming from a folder
295 */
296 if (!msgs.size)
297 app_msgarg(&msgs, "cur");
298 if (!folder)
299 folder = getfolder (1);
300 maildir = m_maildir (folder);
301
302 if (chdir (maildir) == NOTOK)
303 adios (maildir, "unable to change directory to");
304
305 /* read folder and create message structure */
306 if (!(mp = folder_read (folder)))
307 adios (NULL, "unable to read folder %s", folder);
308
309 /* check for empty folder */
310 if (mp->nummsg == 0)
311 adios (NULL, "no messages in %s", folder);
312
313 /* parse all the message ranges/sequences and set SELECTED */
314 for (msgnum = 0; msgnum < msgs.size; msgnum++)
315 if (!m_convert (mp, msgs.msgs[msgnum]))
316 done (1);
317 seq_setprev (mp); /* set the previous-sequence */
318
319 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
320 adios (NULL, "out of memory");
321 ctp = cts;
322
323 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
324 if (is_selected(mp, msgnum)) {
325 char *msgnam;
326
327 msgnam = m_name (msgnum);
328 if ((ct = parse_mime (msgnam)))
329 *ctp++ = ct;
330 }
331 }
332 }
333
334 if (!*cts)
335 done (1);
336
337 userrs = 1;
338 SIGNAL (SIGQUIT, quitser);
339 SIGNAL (SIGPIPE, pipeser);
340
341 /*
342 * Get the associated umask for the relevant contents.
343 */
344 for (ctp = cts; *ctp; ctp++) {
345 struct stat st;
346
347 ct = *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);
351 else
352 ct->c_umask = ~m_gmprot();
353 }
354 }
355
356 /*
357 * List the message content
358 */
359 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
360
361 /* Now free all the structures for the content */
362 for (ctp = cts; *ctp; ctp++)
363 free_content (*ctp);
364
365 free ((char *) cts);
366 cts = NULL;
367
368 /* If reading from a folder, do some updating */
369 if (mp) {
370 context_replace (pfolder, 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 */
374 }
375
376 done (0);
377 return 1;
378 }
379
380
381 static RETSIGTYPE
382 pipeser (int i)
383 {
384 if (i == SIGQUIT) {
385 unlink ("core");
386 fflush (stdout);
387 fprintf (stderr, "\n");
388 fflush (stderr);
389 }
390
391 done (1);
392 /* NOTREACHED */
393 }