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