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