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