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