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