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