56d9398f91395faae30318eb6bf70feba9efb64b
[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                 if (!(msgs = (char **) realloc (msgs,
268                         (size_t) (maxmsgs * sizeof(*msgs)))))
269                     adios (NULL, "unable to reallocate msgs storage");
270             }
271             msgs[nummsgs++] = cp;
272         }
273     }
274
275     /* null terminate the list of acceptable parts/types */
276     parts[npart] = NULL;
277     types[ntype] = NULL;
278
279     set_endian ();
280
281     /* Check for public cache location */
282     if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
283         cache_public = NULL;
284
285     /* Check for private cache location */
286     if (!(cache_private = context_find (nmhprivcache)))
287         cache_private = ".cache";
288     cache_private = getcpy (m_maildir (cache_private));
289
290     /*
291      * Check for storage directory.  If specified,
292      * then store temporary files there.  Else we
293      * store them in standard nmh directory.
294      */
295     if ((cp = context_find (nmhstorage)) && *cp)
296         tmp = concat (cp, "/", invo_name, NULL);
297     else
298         tmp = add (m_maildir (invo_name), NULL);
299
300     if (!context_find ("path"))
301         free (path ("./", TFOLDER));
302
303     if (file && nummsgs)
304         adios (NULL, "cannot specify msg and file at same time!");
305
306     /*
307      * check if message is coming from file
308      */
309     if (file) {
310         if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
311             adios (NULL, "out of memory");
312         ctp = cts;
313
314         if ((ct = parse_mime (file)));
315             *ctp++ = ct;
316     } else {
317         /*
318          * message(s) are coming from a folder
319          */
320         if (!nummsgs)
321             msgs[nummsgs++] = "cur";
322         if (!folder)
323             folder = getfolder (1);
324         maildir = m_maildir (folder);
325
326         if (chdir (maildir) == NOTOK)
327             adios (maildir, "unable to change directory to");
328
329         /* read folder and create message structure */
330         if (!(mp = folder_read (folder)))
331             adios (NULL, "unable to read folder %s", folder);
332
333         /* check for empty folder */
334         if (mp->nummsg == 0)
335             adios (NULL, "no messages in %s", folder);
336
337         /* parse all the message ranges/sequences and set SELECTED */
338         for (msgnum = 0; msgnum < nummsgs; msgnum++)
339             if (!m_convert (mp, msgs[msgnum]))
340                 done (1);
341         seq_setprev (mp);       /* set the previous-sequence */
342
343         if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
344             adios (NULL, "out of memory");
345         ctp = cts;
346
347         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
348             if (is_selected(mp, msgnum)) {
349                 char *msgnam;
350
351                 msgnam = m_name (msgnum);
352                 if ((ct = parse_mime (msgnam)))
353                     *ctp++ = ct;
354             }
355         }
356     }
357
358     if (!*cts)
359         done (1);
360
361     userrs = 1;
362     SIGNAL (SIGQUIT, quitser);
363     SIGNAL (SIGPIPE, pipeser);
364
365     /*
366      * Get the associated umask for the relevant contents.
367      */
368     for (ctp = cts; *ctp; ctp++) {
369         struct stat st;
370
371         ct = *ctp;
372         if (type_ok (ct, 1) && !ct->c_umask) {
373             if (stat (ct->c_file, &st) != NOTOK)
374                 ct->c_umask = ~(st.st_mode & 0777);
375             else
376                 ct->c_umask = ~m_gmprot();
377         }
378     }
379
380     /*
381      * List the message content
382      */
383     list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
384
385     /* Now free all the structures for the content */
386     for (ctp = cts; *ctp; ctp++)
387         free_content (*ctp);
388
389     free ((char *) cts);
390     cts = NULL;
391
392     /* If reading from a folder, do some updating */
393     if (mp) {
394         context_replace (pfolder, folder);/* update current folder  */
395         seq_setcur (mp, mp->hghsel);      /* update current message */
396         seq_save (mp);                    /* synchronize sequences  */
397         context_save ();                  /* save the context file  */
398     }
399
400     return done (0);
401 }
402
403
404 static RETSIGTYPE
405 pipeser (int i)
406 {
407     if (i == SIGQUIT) {
408         unlink ("core");
409         fflush (stdout);
410         fprintf (stderr, "\n");
411         fflush (stderr);
412     }
413
414     done (1);
415     /* NOTREACHED */
416 }
417
418
419 int
420 done (int status)
421 {
422     CT *ctp;
423
424     if ((ctp = cts))
425         for (; *ctp; ctp++)
426             free_content (*ctp);
427
428     exit (status);
429     return 1;  /* dead code to satisfy the compiler */
430 }