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