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