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