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