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