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