]> diplodocus.org Git - nmh/blob - uip/mhlist.c
Removed temporary probes added in commit
[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 #ifdef LOCALE
113 setlocale(LC_ALL, "");
114 #endif
115 invo_name = r1bindex (argv[0], '/');
116
117 /* read user profile/context */
118 context_read();
119
120 arguments = getarguments (invo_name, argc, argv, 1);
121 argp = arguments;
122
123 /*
124 * Parse arguments
125 */
126 while ((cp = *argp++)) {
127 if (*cp == '-') {
128 switch (smatch (++cp, switches)) {
129 case AMBIGSW:
130 ambigsw (cp, switches);
131 done (1);
132 case UNKWNSW:
133 adios (NULL, "-%s unknown", cp);
134
135 case HELPSW:
136 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
137 invo_name);
138 print_help (buf, switches, 1);
139 done (0);
140 case VERSIONSW:
141 print_version(invo_name);
142 done (0);
143
144 case RCACHESW:
145 icachesw = &rcachesw;
146 goto do_cache;
147 case WCACHESW:
148 icachesw = &wcachesw;
149 do_cache:
150 if (!(cp = *argp++) || *cp == '-')
151 adios (NULL, "missing argument to %s", argp[-2]);
152 switch (*icachesw = smatch (cp, caches)) {
153 case AMBIGSW:
154 ambigsw (cp, caches);
155 done (1);
156 case UNKWNSW:
157 adios (NULL, "%s unknown", cp);
158 default:
159 break;
160 }
161 continue;
162
163 case CHECKSW:
164 checksw++;
165 continue;
166 case NCHECKSW:
167 checksw = 0;
168 continue;
169
170 case HEADSW:
171 headsw = 1;
172 continue;
173 case NHEADSW:
174 headsw = 0;
175 continue;
176
177 case SIZESW:
178 sizesw = 1;
179 continue;
180 case NSIZESW:
181 sizesw = 0;
182 continue;
183
184 case PARTSW:
185 if (!(cp = *argp++) || *cp == '-')
186 adios (NULL, "missing argument to %s", argp[-2]);
187 if (npart >= NPARTS)
188 adios (NULL, "too many parts (starting with %s), %d max",
189 cp, NPARTS);
190 parts[npart++] = cp;
191 continue;
192
193 case TYPESW:
194 if (!(cp = *argp++) || *cp == '-')
195 adios (NULL, "missing argument to %s", argp[-2]);
196 if (ntype >= NTYPES)
197 adios (NULL, "too many types (starting with %s), %d max",
198 cp, NTYPES);
199 types[ntype++] = cp;
200 continue;
201
202 case FILESW:
203 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
204 adios (NULL, "missing argument to %s", argp[-2]);
205 file = *cp == '-' ? cp : path (cp, TFILE);
206 continue;
207
208 case CHGSW:
209 chgflag++;
210 continue;
211 case NCHGSW:
212 chgflag = 0;
213 continue;
214
215 case VERBSW:
216 verbosw = 1;
217 continue;
218 case NVERBSW:
219 verbosw = 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 /*
249 * Check for storage directory. If specified,
250 * then store temporary files there. Else we
251 * store them in standard nmh directory.
252 */
253 if ((cp = context_find (nmhstorage)) && *cp)
254 tmp = concat (cp, "/", invo_name, NULL);
255 else
256 tmp = add (m_maildir (invo_name), NULL);
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 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
269 adios (NULL, "out of memory");
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 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
302 adios (NULL, "out of memory");
303 ctp = cts;
304
305 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
306 if (is_selected(mp, msgnum)) {
307 char *msgnam;
308
309 msgnam = m_name (msgnum);
310 if ((ct = parse_mime (msgnam)))
311 *ctp++ = ct;
312 }
313 }
314 }
315
316 if (!*cts)
317 done (1);
318
319 userrs = 1;
320 SIGNAL (SIGQUIT, quitser);
321 SIGNAL (SIGPIPE, pipeser);
322
323 /*
324 * Get the associated umask for the relevant contents.
325 */
326 for (ctp = cts; *ctp; ctp++) {
327 struct stat st;
328
329 ct = *ctp;
330 if (type_ok (ct, 1) && !ct->c_umask) {
331 if (stat (ct->c_file, &st) != NOTOK)
332 ct->c_umask = ~(st.st_mode & 0777);
333 else
334 ct->c_umask = ~m_gmprot();
335 }
336 }
337
338 /*
339 * List the message content
340 */
341 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
342
343 /* Now free all the structures for the content */
344 for (ctp = cts; *ctp; ctp++)
345 free_content (*ctp);
346
347 free ((char *) cts);
348 cts = NULL;
349
350 /* If reading from a folder, do some updating */
351 if (mp) {
352 context_replace (pfolder, folder);/* update current folder */
353 if (chgflag)
354 seq_setcur (mp, mp->hghsel); /* update current message */
355 seq_save (mp); /* synchronize sequences */
356 context_save (); /* save the context file */
357 }
358
359 done (0);
360 return 1;
361 }
362
363
364 static void
365 pipeser (int i)
366 {
367 if (i == SIGQUIT) {
368 fflush (stdout);
369 fprintf (stderr, "\n");
370 fflush (stderr);
371 }
372
373 done (1);
374 /* NOTREACHED */
375 }