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