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