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