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