]> diplodocus.org Git - nmh/blob - uip/mhlist.c
c5b2fe8268d90851c9e9c02f1bc2da17ec022f3a
[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/m_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_setcur.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/ambigsw.h"
23 #include "sbr/path.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
27 #include <fcntl.h>
28 #include "h/signals.h"
29 #include "h/mts.h"
30 #include "h/tws.h"
31 #include "h/mime.h"
32 #include "h/mhparse.h"
33 #include "h/mhcachesbr.h"
34 #include "h/done.h"
35 #include "h/utils.h"
36 #include "mhmisc.h"
37 #include "sbr/m_maildir.h"
38 #include "mhfree.h"
39
40 #define MHLIST_SWITCHES \
41 X("check", -5, CHECKSW) \
42 X("nocheck", -7, NCHECKSW) \
43 X("headers", 0, HEADSW) \
44 X("noheaders", 0, NHEADSW) \
45 X("realsize", 0, SIZESW) \
46 X("norealsize", 0, NSIZESW) \
47 X("verbose", 0, VERBSW) \
48 X("noverbose", 0, NVERBSW) \
49 X("disposition", 0, DISPOSW) \
50 X("nodisposition", 0, NDISPOSW) \
51 X("file file", 0, FILESW) \
52 X("part number", 0, PARTSW) \
53 X("type content", 0, TYPESW) \
54 X("prefer content", 0, PREFERSW) \
55 X("noprefer", 0, NPREFERSW) \
56 X("rcache policy", 0, RCACHESW) \
57 X("wcache policy", 0, WCACHESW) \
58 X("changecur", 0, CHGSW) \
59 X("nochangecur", 0, NCHGSW) \
60 X("version", 0, VERSIONSW) \
61 X("help", 0, HELPSW) \
62 X("debug", -5, DEBUGSW) \
63
64 #define X(sw, minchars, id) id,
65 DEFINE_SWITCH_ENUM(MHLIST);
66 #undef X
67
68 #define X(sw, minchars, id) { sw, minchars, id },
69 DEFINE_SWITCH_ARRAY(MHLIST, switches);
70 #undef X
71
72 /*
73 * This is currently needed to keep mhparse happy.
74 * This needs to be changed.
75 */
76 int debugsw = 0;
77
78 #define quitser pipeser
79
80 /*
81 * static prototypes
82 */
83 static void pipeser (int);
84
85
86 int
87 main (int argc, char **argv)
88 {
89 bool sizesw = true;
90 bool headsw = true;
91 bool chgflag = true;
92 bool verbosw = false;
93 bool dispo = false;
94 int msgnum, *icachesw;
95 char *cp, *file = NULL, *folder = NULL;
96 char *maildir, buf[100], **argp;
97 char **arguments;
98 struct msgs_array msgs = { 0, 0, NULL };
99 struct msgs *mp = NULL;
100 CT ct, *ctp;
101
102 if (nmh_init(argv[0], true, true)) { return 1; }
103
104 set_done(freects_done);
105
106 arguments = getarguments (invo_name, argc, argv, 1);
107 argp = arguments;
108
109 /*
110 * Parse arguments
111 */
112 while ((cp = *argp++)) {
113 if (*cp == '-') {
114 switch (smatch (++cp, switches)) {
115 case AMBIGSW:
116 ambigsw (cp, switches);
117 done (1);
118 case UNKWNSW:
119 die("-%s unknown", cp);
120
121 case HELPSW:
122 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
123 invo_name);
124 print_help (buf, switches, 1);
125 done (0);
126 case VERSIONSW:
127 print_version(invo_name);
128 done (0);
129
130 case RCACHESW:
131 icachesw = &rcachesw;
132 goto do_cache;
133 case WCACHESW:
134 icachesw = &wcachesw;
135 do_cache:
136 if (!(cp = *argp++) || *cp == '-')
137 die("missing argument to %s", argp[-2]);
138 switch (*icachesw = smatch (cp, cache_policy)) {
139 case AMBIGSW:
140 ambigsw (cp, cache_policy);
141 done (1);
142 case UNKWNSW:
143 die("%s unknown", cp);
144 default:
145 break;
146 }
147 continue;
148
149 case CHECKSW:
150 case NCHECKSW:
151 /* Currently a NOP */
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 }