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