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