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