Reformated comments and long lines
[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 = r1bindex (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]",
156                                                 invo_name);
157                                 print_help (buf, switches, 1);
158                                 done (1);
159                         case VERSIONSW:
160                                 print_version(invo_name);
161                                 done (1);
162
163                         case RCACHESW:
164                                 icachesw = &rcachesw;
165                                 goto do_cache;
166                         case WCACHESW:
167                                 icachesw = &wcachesw;
168 do_cache:
169                                 if (!(cp = *argp++) || *cp == '-')
170                                         adios (NULL, "missing argument to %s", argp[-2]);
171                                 switch (*icachesw = smatch (cp, caches)) {
172                                 case AMBIGSW:
173                                         ambigsw (cp, caches);
174                                         done (1);
175                                 case UNKWNSW:
176                                         adios (NULL, "%s unknown", cp);
177                                 default:
178                                         break;
179                                 }
180                                 continue;
181
182                         case CHECKSW:
183                                 checksw++;
184                                 continue;
185                         case NCHECKSW:
186                                 checksw = 0;
187                                 continue;
188
189                         case HEADSW:
190                                 headsw = 1;
191                                 continue;
192                         case NHEADSW:
193                                 headsw = 0;
194                                 continue;
195
196                         case SIZESW:
197                                 sizesw = 1;
198                                 continue;
199                         case NSIZESW:
200                                 sizesw = 0;
201                                 continue;
202
203                         case PARTSW:
204                                 if (!(cp = *argp++) || *cp == '-')
205                                         adios (NULL, "missing argument to %s", argp[-2]);
206                                 if (npart >= NPARTS)
207                                         adios (NULL, "too many parts (starting with %s), %d max",
208                                                    cp, NPARTS);
209                                 parts[npart++] = cp;
210                                 continue;
211
212                         case TYPESW:
213                                 if (!(cp = *argp++) || *cp == '-')
214                                         adios (NULL, "missing argument to %s", argp[-2]);
215                                 if (ntype >= NTYPES)
216                                         adios (NULL, "too many types (starting with %s), %d max",
217                                                    cp, NTYPES);
218                                 types[ntype++] = cp;
219                                 continue;
220
221                         case FILESW:
222                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
223                                         adios (NULL, "missing argument to %s", argp[-2]);
224                                 file = *cp == '-' ? cp : path (cp, TFILE);
225                                 continue;
226
227                         case VERBSW:
228                                 verbosw = 1;
229                                 continue;
230                         case NVERBSW:
231                                 verbosw = 0;
232                                 continue;
233                         case DEBUGSW:
234                                 debugsw = 1;
235                                 continue;
236                         }
237                 }
238                 if (*cp == '+' || *cp == '@') {
239                         if (folder)
240                                 adios (NULL, "only one folder at a time!");
241                         else
242                                 folder = pluspath (cp);
243                 } else
244                                 app_msgarg(&msgs, cp);
245         }
246
247         /* null terminate the list of acceptable parts/types */
248         parts[npart] = NULL;
249         types[ntype] = NULL;
250
251         set_endian ();
252
253         /* Check for public cache location */
254         if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
255                 cache_public = NULL;
256
257         /* Check for private cache location */
258         if (!(cache_private = context_find (nmhprivcache)))
259                 cache_private = ".cache";
260         cache_private = getcpy (m_maildir (cache_private));
261
262         /*
263         ** Check for storage directory.  If specified,
264         ** then store temporary files there.  Else we
265         ** store them in standard nmh directory.
266         */
267         if ((cp = context_find (nmhstorage)) && *cp)
268                 tmp = concat (cp, "/", invo_name, NULL);
269         else
270                 tmp = add (m_maildir (invo_name), NULL);
271
272         if (!context_find ("path"))
273                 free (path ("./", TFOLDER));
274
275         if (file && msgs.size)
276                 adios (NULL, "cannot specify msg and file at same time!");
277
278         /*
279         ** check if message is coming from file
280         */
281         if (file) {
282                 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
283                         adios (NULL, "out of memory");
284                 ctp = cts;
285
286                 if ((ct = parse_mime (file)));
287                         *ctp++ = ct;
288         } else {
289                 /*
290                 ** message(s) are coming from a folder
291                 */
292                 if (!msgs.size)
293                         app_msgarg(&msgs, "cur");
294                 if (!folder)
295                         folder = getfolder (1);
296                 maildir = m_maildir (folder);
297
298                 if (chdir (maildir) == NOTOK)
299                         adios (maildir, "unable to change directory to");
300
301                 /* read folder and create message structure */
302                 if (!(mp = folder_read (folder)))
303                         adios (NULL, "unable to read folder %s", folder);
304
305                 /* check for empty folder */
306                 if (mp->nummsg == 0)
307                         adios (NULL, "no messages in %s", folder);
308
309                 /* parse all the message ranges/sequences and set SELECTED */
310                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
311                         if (!m_convert (mp, msgs.msgs[msgnum]))
312                                 done (1);
313                 seq_setprev (mp);  /* set the previous-sequence */
314
315                 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1),
316                                 sizeof(*cts))))
317                         adios (NULL, "out of memory");
318                 ctp = cts;
319
320                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
321                         if (is_selected(mp, msgnum)) {
322                                 char *msgnam;
323
324                                 msgnam = m_name (msgnum);
325                                 if ((ct = parse_mime (msgnam)))
326                                         *ctp++ = ct;
327                         }
328                 }
329         }
330
331         if (!*cts)
332                 done (1);
333
334         userrs = 1;
335         SIGNAL (SIGQUIT, quitser);
336         SIGNAL (SIGPIPE, pipeser);
337
338         /*
339         ** Get the associated umask for the relevant contents.
340         */
341         for (ctp = cts; *ctp; ctp++) {
342                 struct stat st;
343
344                 ct = *ctp;
345                 if (type_ok (ct, 1) && !ct->c_umask) {
346                         if (stat (ct->c_file, &st) != NOTOK)
347                                 ct->c_umask = ~(st.st_mode & 0777);
348                         else
349                                 ct->c_umask = ~m_gmprot();
350                 }
351         }
352
353         /*
354         ** List the message content
355         */
356         list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
357
358         /* Now free all the structures for the content */
359         for (ctp = cts; *ctp; ctp++)
360                 free_content (*ctp);
361
362         free ((char *) cts);
363         cts = NULL;
364
365         /* If reading from a folder, do some updating */
366         if (mp) {
367                 context_replace (pfolder, folder);  /* update current folder */
368                 seq_setcur (mp, mp->hghsel);  /* update current message */
369                 seq_save (mp);  /* synchronize sequences  */
370                 context_save ();  /* save the context file  */
371         }
372
373         done (0);
374         return 1;
375 }
376
377
378 static RETSIGTYPE
379 pipeser (int i)
380 {
381         if (i == SIGQUIT) {
382                 unlink ("core");
383                 fflush (stdout);
384                 fprintf (stderr, "\n");
385                 fflush (stderr);
386         }
387
388         done (1);
389         /* NOTREACHED */
390 }