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