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