]> diplodocus.org Git - nmh/blob - uip/mhlist.c
7092434895a57f34375cd137495c427dbdbc5440
[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("file file", 0, FILESW) \
31 X("part number", 0, PARTSW) \
32 X("type content", 0, TYPESW) \
33 X("rcache policy", 0, RCACHESW) \
34 X("wcache policy", 0, WCACHESW) \
35 X("changecur", 0, CHGSW) \
36 X("nochangecur", 0, NCHGSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
39 X("debug", -5, DEBUGSW) \
40
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(MHLIST);
43 #undef X
44
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(MHLIST, switches);
47 #undef X
48
49
50 /* mhparse.c */
51 extern char *tmp; /* directory to place temp files */
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 /*
67 * This is currently needed to keep mhparse happy.
68 * This needs to be changed.
69 */
70 pid_t xpid = 0;
71
72 int debugsw = 0;
73 int verbosw = 0;
74
75 #define quitser pipeser
76
77 /* mhparse.c */
78 CT parse_mime (char *);
79
80 /* mhmisc.c */
81 int part_ok (CT, int);
82 int type_ok (CT, int);
83 void flush_errors (void);
84
85 /* mhlistsbr.c */
86 void list_all_messages (CT *, int, int, int, int);
87
88 /* mhfree.c */
89 extern CT *cts;
90 void freects_done (int) NORETURN;
91
92 /*
93 * static prototypes
94 */
95 static void pipeser (int);
96
97
98 int
99 main (int argc, char **argv)
100 {
101 int sizesw = 1, headsw = 1, chgflag = 1;
102 int msgnum, *icachesw;
103 char *cp, *file = NULL, *folder = NULL;
104 char *maildir, buf[100], **argp;
105 char **arguments;
106 struct msgs_array msgs = { 0, 0, NULL };
107 struct msgs *mp = NULL;
108 CT ct, *ctp;
109
110 done=freects_done;
111
112 setlocale(LC_ALL, "");
113 invo_name = r1bindex (argv[0], '/');
114
115 /* read user profile/context */
116 context_read();
117
118 arguments = getarguments (invo_name, argc, argv, 1);
119 argp = arguments;
120
121 /*
122 * Parse arguments
123 */
124 while ((cp = *argp++)) {
125 if (*cp == '-') {
126 switch (smatch (++cp, switches)) {
127 case AMBIGSW:
128 ambigsw (cp, switches);
129 done (1);
130 case UNKWNSW:
131 adios (NULL, "-%s unknown", cp);
132
133 case HELPSW:
134 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
135 invo_name);
136 print_help (buf, switches, 1);
137 done (0);
138 case VERSIONSW:
139 print_version(invo_name);
140 done (0);
141
142 case RCACHESW:
143 icachesw = &rcachesw;
144 goto do_cache;
145 case WCACHESW:
146 icachesw = &wcachesw;
147 do_cache:
148 if (!(cp = *argp++) || *cp == '-')
149 adios (NULL, "missing argument to %s", argp[-2]);
150 switch (*icachesw = smatch (cp, caches)) {
151 case AMBIGSW:
152 ambigsw (cp, caches);
153 done (1);
154 case UNKWNSW:
155 adios (NULL, "%s unknown", cp);
156 default:
157 break;
158 }
159 continue;
160
161 case CHECKSW:
162 checksw++;
163 continue;
164 case NCHECKSW:
165 checksw = 0;
166 continue;
167
168 case HEADSW:
169 headsw = 1;
170 continue;
171 case NHEADSW:
172 headsw = 0;
173 continue;
174
175 case SIZESW:
176 sizesw = 1;
177 continue;
178 case NSIZESW:
179 sizesw = 0;
180 continue;
181
182 case PARTSW:
183 if (!(cp = *argp++) || *cp == '-')
184 adios (NULL, "missing argument to %s", argp[-2]);
185 if (npart >= NPARTS)
186 adios (NULL, "too many parts (starting with %s), %d max",
187 cp, NPARTS);
188 parts[npart++] = cp;
189 continue;
190
191 case TYPESW:
192 if (!(cp = *argp++) || *cp == '-')
193 adios (NULL, "missing argument to %s", argp[-2]);
194 if (ntype >= NTYPES)
195 adios (NULL, "too many types (starting with %s), %d max",
196 cp, NTYPES);
197 types[ntype++] = cp;
198 continue;
199
200 case FILESW:
201 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
202 adios (NULL, "missing argument to %s", argp[-2]);
203 file = *cp == '-' ? cp : path (cp, TFILE);
204 continue;
205
206 case CHGSW:
207 chgflag++;
208 continue;
209 case NCHGSW:
210 chgflag = 0;
211 continue;
212
213 case VERBSW:
214 verbosw = 1;
215 continue;
216 case NVERBSW:
217 verbosw = 0;
218 continue;
219 case DEBUGSW:
220 debugsw = 1;
221 continue;
222 }
223 }
224 if (*cp == '+' || *cp == '@') {
225 if (folder)
226 adios (NULL, "only one folder at a time!");
227 else
228 folder = pluspath (cp);
229 } else
230 app_msgarg(&msgs, cp);
231 }
232
233 /* null terminate the list of acceptable parts/types */
234 parts[npart] = NULL;
235 types[ntype] = NULL;
236
237 /* Check for public cache location */
238 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
239 cache_public = NULL;
240
241 /* Check for private cache location */
242 if (!(cache_private = context_find (nmhprivcache)))
243 cache_private = ".cache";
244 cache_private = getcpy (m_maildir (cache_private));
245
246 /*
247 * Check for storage directory. If specified,
248 * then store temporary files there. Else we
249 * store them in standard nmh directory.
250 */
251 if ((cp = context_find (nmhstorage)) && *cp)
252 tmp = concat (cp, "/", invo_name, NULL);
253 else
254 tmp = add (m_maildir (invo_name), NULL);
255
256 if (!context_find ("path"))
257 free (path ("./", TFOLDER));
258
259 if (file && msgs.size)
260 adios (NULL, "cannot specify msg and file at same time!");
261
262 /*
263 * check if message is coming from file
264 */
265 if (file) {
266 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
267 adios (NULL, "out of memory");
268 ctp = cts;
269
270 if ((ct = parse_mime (file)))
271 *ctp++ = ct;
272 } else {
273 /*
274 * message(s) are coming from a folder
275 */
276 if (!msgs.size)
277 app_msgarg(&msgs, "cur");
278 if (!folder)
279 folder = getfolder (1);
280 maildir = m_maildir (folder);
281
282 if (chdir (maildir) == NOTOK)
283 adios (maildir, "unable to change directory to");
284
285 /* read folder and create message structure */
286 if (!(mp = folder_read (folder, 0)))
287 adios (NULL, "unable to read folder %s", folder);
288
289 /* check for empty folder */
290 if (mp->nummsg == 0)
291 adios (NULL, "no messages in %s", folder);
292
293 /* parse all the message ranges/sequences and set SELECTED */
294 for (msgnum = 0; msgnum < msgs.size; msgnum++)
295 if (!m_convert (mp, msgs.msgs[msgnum]))
296 done (1);
297 seq_setprev (mp); /* set the previous-sequence */
298
299 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
300 adios (NULL, "out of memory");
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);
340
341 /* Now free all the structures for the content */
342 for (ctp = cts; *ctp; ctp++)
343 free_content (*ctp);
344
345 free ((char *) 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 }