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