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