]> diplodocus.org Git - nmh/blob - uip/mhlist.c
f4aa532c8a35d9979c83afbc352305c820c1555b
[nmh] / uip / mhlist.c
1 /* mhlist.c -- list the contents of MIME messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/seq_setprev.h"
10 #include "sbr/seq_setcur.h"
11 #include "sbr/seq_save.h"
12 #include "sbr/smatch.h"
13 #include "sbr/m_convert.h"
14 #include "sbr/getfolder.h"
15 #include "sbr/folder_read.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/ambigsw.h"
20 #include "sbr/path.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/error.h"
24 #include <fcntl.h>
25 #include "h/signals.h"
26 #include "h/md5.h"
27 #include "h/mts.h"
28 #include "h/tws.h"
29 #include "h/mime.h"
30 #include "h/mhparse.h"
31 #include "h/mhcachesbr.h"
32 #include "h/done.h"
33 #include "h/utils.h"
34 #include "mhmisc.h"
35 #include "sbr/m_maildir.h"
36 #include "mhfree.h"
37
38 #define MHLIST_SWITCHES \
39 X("check", 0, CHECKSW) \
40 X("nocheck", 0, NCHECKSW) \
41 X("headers", 0, HEADSW) \
42 X("noheaders", 0, NHEADSW) \
43 X("realsize", 0, SIZESW) \
44 X("norealsize", 0, NSIZESW) \
45 X("verbose", 0, VERBSW) \
46 X("noverbose", 0, NVERBSW) \
47 X("disposition", 0, DISPOSW) \
48 X("nodisposition", 0, NDISPOSW) \
49 X("file file", 0, FILESW) \
50 X("part number", 0, PARTSW) \
51 X("type content", 0, TYPESW) \
52 X("prefer content", 0, PREFERSW) \
53 X("noprefer", 0, NPREFERSW) \
54 X("rcache policy", 0, RCACHESW) \
55 X("wcache policy", 0, WCACHESW) \
56 X("changecur", 0, CHGSW) \
57 X("nochangecur", 0, NCHGSW) \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
60 X("debug", -5, DEBUGSW) \
61
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(MHLIST);
64 #undef X
65
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(MHLIST, switches);
68 #undef X
69
70 /*
71 * This is currently needed to keep mhparse happy.
72 * This needs to be changed.
73 */
74 int debugsw = 0;
75
76 #define quitser pipeser
77
78 /*
79 * static prototypes
80 */
81 static void pipeser (int);
82
83
84 int
85 main (int argc, char **argv)
86 {
87 bool sizesw = true;
88 bool headsw = true;
89 bool chgflag = true;
90 bool verbosw = false;
91 bool dispo = false;
92 int msgnum, *icachesw;
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 if (nmh_init(argv[0], true, true)) { return 1; }
101
102 set_done(freects_done);
103
104 arguments = getarguments (invo_name, argc, argv, 1);
105 argp = arguments;
106
107 /*
108 * Parse arguments
109 */
110 while ((cp = *argp++)) {
111 if (*cp == '-') {
112 switch (smatch (++cp, switches)) {
113 case AMBIGSW:
114 ambigsw (cp, switches);
115 done (1);
116 case UNKWNSW:
117 die("-%s unknown", cp);
118
119 case HELPSW:
120 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
121 invo_name);
122 print_help (buf, switches, 1);
123 done (0);
124 case VERSIONSW:
125 print_version(invo_name);
126 done (0);
127
128 case RCACHESW:
129 icachesw = &rcachesw;
130 goto do_cache;
131 case WCACHESW:
132 icachesw = &wcachesw;
133 do_cache:
134 if (!(cp = *argp++) || *cp == '-')
135 die("missing argument to %s", argp[-2]);
136 switch (*icachesw = smatch (cp, cache_policy)) {
137 case AMBIGSW:
138 ambigsw (cp, cache_policy);
139 done (1);
140 case UNKWNSW:
141 die("%s unknown", cp);
142 default:
143 break;
144 }
145 continue;
146
147 case CHECKSW:
148 checksw++;
149 continue;
150 case NCHECKSW:
151 checksw = 0;
152 continue;
153
154 case HEADSW:
155 headsw = true;
156 continue;
157 case NHEADSW:
158 headsw = false;
159 continue;
160
161 case SIZESW:
162 sizesw = true;
163 continue;
164 case NSIZESW:
165 sizesw = false;
166 continue;
167
168 case PARTSW:
169 if (!(cp = *argp++) || *cp == '-')
170 die("missing argument to %s", argp[-2]);
171 if (npart >= NPARTS)
172 die("too many parts (starting with %s), %d max",
173 cp, NPARTS);
174 parts[npart++] = cp;
175 continue;
176
177 case TYPESW:
178 if (!(cp = *argp++) || *cp == '-')
179 die("missing argument to %s", argp[-2]);
180 if (ntype >= NTYPES)
181 die("too many types (starting with %s), %d max",
182 cp, NTYPES);
183 types[ntype++] = cp;
184 continue;
185
186 case PREFERSW:
187 if (!(cp = *argp++) || *cp == '-')
188 die("missing argument to %s", argp[-2]);
189 if (npreferred >= NPREFS)
190 die("too many preferred types (starting with %s), %d max",
191 cp, NPREFS);
192 mime_preference[npreferred].type = cp;
193 cp = strchr(cp, '/');
194 if (cp) *cp++ = '\0';
195 mime_preference[npreferred++].subtype = cp;
196 continue;
197
198 case NPREFERSW:
199 npreferred = 0;
200 continue;
201
202 case FILESW:
203 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
204 die("missing argument to %s", argp[-2]);
205 file = *cp == '-' ? cp : path (cp, TFILE);
206 continue;
207
208 case CHGSW:
209 chgflag = true;
210 continue;
211 case NCHGSW:
212 chgflag = false;
213 continue;
214
215 case VERBSW:
216 verbosw = true;
217 continue;
218 case NVERBSW:
219 verbosw = false;
220 continue;
221 case DISPOSW:
222 dispo = true;
223 continue;
224 case NDISPOSW:
225 dispo = false;
226 continue;
227 case DEBUGSW:
228 debugsw = 1;
229 continue;
230 }
231 }
232 if (*cp == '+' || *cp == '@') {
233 if (folder)
234 die("only one folder at a time!");
235 folder = pluspath (cp);
236 } else
237 app_msgarg(&msgs, cp);
238 }
239
240 /* null terminate the list of acceptable parts/types */
241 parts[npart] = NULL;
242 types[ntype] = NULL;
243
244 /* Check for public cache location */
245 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
246 cache_public = NULL;
247
248 /* Check for private cache location */
249 if (!(cache_private = context_find (nmhprivcache)))
250 cache_private = ".cache";
251 cache_private = mh_xstrdup(m_maildir(cache_private));
252
253 if (!context_find ("path"))
254 free (path ("./", TFOLDER));
255
256 if (file && msgs.size)
257 die("cannot specify msg and file at same time!");
258
259 /*
260 * check if message is coming from file
261 */
262 if (file) {
263 cts = mh_xcalloc(2, sizeof *cts);
264 ctp = cts;
265
266 if ((ct = parse_mime (file)))
267 *ctp++ = ct;
268 } else {
269 /*
270 * message(s) are coming from a folder
271 */
272 if (!msgs.size)
273 app_msgarg(&msgs, "cur");
274 if (!folder)
275 folder = getfolder (1);
276 maildir = m_maildir (folder);
277
278 if (chdir (maildir) == NOTOK)
279 adios (maildir, "unable to change directory to");
280
281 /* read folder and create message structure */
282 if (!(mp = folder_read (folder, 0)))
283 die("unable to read folder %s", folder);
284
285 /* check for empty folder */
286 if (mp->nummsg == 0)
287 die("no messages in %s", folder);
288
289 /* parse all the message ranges/sequences and set SELECTED */
290 for (msgnum = 0; msgnum < msgs.size; msgnum++)
291 if (!m_convert (mp, msgs.msgs[msgnum]))
292 done (1);
293 seq_setprev (mp); /* set the previous-sequence */
294
295 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
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 = true;
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, dispo);
335
336 /* Now free all the structures for the content */
337 for (ctp = cts; *ctp; ctp++)
338 free_content (*ctp);
339
340 free(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 if (chgflag)
347 seq_setcur (mp, mp->hghsel); /* update current message */
348 seq_save (mp); /* synchronize sequences */
349 context_save (); /* save the context file */
350 }
351
352 done (0);
353 return 1;
354 }
355
356
357 static void
358 pipeser (int i)
359 {
360 if (i == SIGQUIT) {
361 fflush (stdout);
362 fprintf (stderr, "\n");
363 fflush (stderr);
364 }
365
366 done (1);
367 /* NOTREACHED */
368 }