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