]> diplodocus.org Git - nmh/blob - uip/mhlist.c
6faef9b696b601416e432d1d1422ca6b74b9506e
[nmh] / uip / mhlist.c
1 /* mhlist.c -- list the contents of MIME messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/m_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_setcur.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/ambigsw.h"
23 #include "sbr/path.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
27 #include <fcntl.h>
28 #include "h/signals.h"
29 #include "h/mts.h"
30 #include "h/tws.h"
31 #include "h/mime.h"
32 #include "h/mhparse.h"
33 #include "h/done.h"
34 #include "h/utils.h"
35 #include "mhmisc.h"
36 #include "sbr/m_maildir.h"
37 #include "mhfree.h"
38
39 #define MHLIST_SWITCHES \
40 X("check", -5, CHECKSW) \
41 X("nocheck", -7, NCHECKSW) \
42 X("headers", 0, HEADSW) \
43 X("noheaders", 0, NHEADSW) \
44 X("realsize", 0, SIZESW) \
45 X("norealsize", 0, NSIZESW) \
46 X("verbose", 0, VERBSW) \
47 X("noverbose", 0, NVERBSW) \
48 X("disposition", 0, DISPOSW) \
49 X("nodisposition", 0, NDISPOSW) \
50 X("file file", 0, FILESW) \
51 X("part number", 0, PARTSW) \
52 X("type content", 0, TYPESW) \
53 X("prefer content", 0, PREFERSW) \
54 X("noprefer", 0, NPREFERSW) \
55 X("changecur", 0, CHGSW) \
56 X("nochangecur", 0, NCHGSW) \
57 X("version", 0, VERSIONSW) \
58 X("help", 0, HELPSW) \
59 X("debug", -5, DEBUGSW) \
60
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(MHLIST);
63 #undef X
64
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(MHLIST, switches);
67 #undef X
68
69 /*
70 * This is currently needed to keep mhparse happy.
71 * This needs to be changed.
72 */
73 int debugsw = 0;
74
75 #define quitser pipeser
76
77 /*
78 * static prototypes
79 */
80 static void pipeser (int);
81
82
83 int
84 main (int argc, char **argv)
85 {
86 bool sizesw = true;
87 bool headsw = true;
88 bool chgflag = true;
89 bool verbosw = false;
90 bool dispo = false;
91 int msgnum;
92 char *cp, *file = NULL, *folder = NULL;
93 char *maildir, buf[100], **argp;
94 char **arguments;
95 struct msgs_array msgs = { 0, 0, NULL };
96 struct msgs *mp = NULL;
97 CT ct, *ctp;
98
99 if (nmh_init(argv[0], true, true)) { return 1; }
100
101 set_done(freects_done);
102
103 arguments = getarguments (invo_name, argc, argv, 1);
104 argp = arguments;
105
106 /*
107 * Parse arguments
108 */
109 while ((cp = *argp++)) {
110 if (*cp == '-') {
111 switch (smatch (++cp, switches)) {
112 case AMBIGSW:
113 ambigsw (cp, switches);
114 done (1);
115 case UNKWNSW:
116 die("-%s unknown", cp);
117
118 case HELPSW:
119 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
120 invo_name);
121 print_help (buf, switches, 1);
122 done (0);
123 case VERSIONSW:
124 print_version(invo_name);
125 done (0);
126
127 case CHECKSW:
128 case NCHECKSW:
129 /* Currently a NOP */
130 continue;
131
132 case HEADSW:
133 headsw = true;
134 continue;
135 case NHEADSW:
136 headsw = false;
137 continue;
138
139 case SIZESW:
140 sizesw = true;
141 continue;
142 case NSIZESW:
143 sizesw = false;
144 continue;
145
146 case PARTSW:
147 if (!(cp = *argp++) || *cp == '-')
148 die("missing argument to %s", argp[-2]);
149 if (npart >= NPARTS)
150 die("too many parts (starting with %s), %d max",
151 cp, NPARTS);
152 parts[npart++] = cp;
153 continue;
154
155 case TYPESW:
156 if (!(cp = *argp++) || *cp == '-')
157 die("missing argument to %s", argp[-2]);
158 if (ntype >= NTYPES)
159 die("too many types (starting with %s), %d max",
160 cp, NTYPES);
161 types[ntype++] = cp;
162 continue;
163
164 case PREFERSW:
165 if (!(cp = *argp++) || *cp == '-')
166 die("missing argument to %s", argp[-2]);
167 if (npreferred >= NPREFS)
168 die("too many preferred types (starting with %s), %d max",
169 cp, NPREFS);
170 mime_preference[npreferred].type = cp;
171 cp = strchr(cp, '/');
172 if (cp) *cp++ = '\0';
173 mime_preference[npreferred++].subtype = cp;
174 continue;
175
176 case NPREFERSW:
177 npreferred = 0;
178 continue;
179
180 case FILESW:
181 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
182 die("missing argument to %s", argp[-2]);
183 file = *cp == '-' ? cp : path (cp, TFILE);
184 continue;
185
186 case CHGSW:
187 chgflag = true;
188 continue;
189 case NCHGSW:
190 chgflag = false;
191 continue;
192
193 case VERBSW:
194 verbosw = true;
195 continue;
196 case NVERBSW:
197 verbosw = false;
198 continue;
199 case DISPOSW:
200 dispo = true;
201 continue;
202 case NDISPOSW:
203 dispo = false;
204 continue;
205 case DEBUGSW:
206 debugsw = 1;
207 continue;
208 }
209 }
210 if (*cp == '+' || *cp == '@') {
211 if (folder)
212 die("only one folder at a time!");
213 folder = pluspath (cp);
214 } else
215 app_msgarg(&msgs, cp);
216 }
217
218 /* null terminate the list of acceptable parts/types */
219 parts[npart] = NULL;
220 types[ntype] = NULL;
221
222 if (!context_find ("path"))
223 free (path ("./", TFOLDER));
224
225 if (file && msgs.size)
226 die("cannot specify msg and file at same time!");
227
228 /*
229 * check if message is coming from file
230 */
231 if (file) {
232 cts = mh_xcalloc(2, sizeof *cts);
233 ctp = cts;
234
235 if ((ct = parse_mime (file)))
236 *ctp++ = ct;
237 } else {
238 /*
239 * message(s) are coming from a folder
240 */
241 if (!msgs.size)
242 app_msgarg(&msgs, "cur");
243 if (!folder)
244 folder = getfolder (1);
245 maildir = m_maildir (folder);
246
247 if (chdir (maildir) == NOTOK)
248 adios (maildir, "unable to change directory to");
249
250 /* read folder and create message structure */
251 if (!(mp = folder_read (folder, 0)))
252 die("unable to read folder %s", folder);
253
254 /* check for empty folder */
255 if (mp->nummsg == 0)
256 die("no messages in %s", folder);
257
258 /* parse all the message ranges/sequences and set SELECTED */
259 for (msgnum = 0; msgnum < msgs.size; msgnum++)
260 if (!m_convert (mp, msgs.msgs[msgnum]))
261 done (1);
262 seq_setprev (mp); /* set the previous-sequence */
263
264 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
265 ctp = cts;
266
267 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
268 if (is_selected(mp, msgnum)) {
269 char *msgnam;
270
271 msgnam = m_name (msgnum);
272 if ((ct = parse_mime (msgnam)))
273 *ctp++ = ct;
274 }
275 }
276 }
277
278 if (!*cts)
279 done (1);
280
281 userrs = true;
282 SIGNAL (SIGQUIT, quitser);
283 SIGNAL (SIGPIPE, pipeser);
284
285 /*
286 * Get the associated umask for the relevant contents.
287 */
288 for (ctp = cts; *ctp; ctp++) {
289 struct stat st;
290
291 ct = *ctp;
292 if (type_ok (ct, 1) && !ct->c_umask) {
293 if (stat (ct->c_file, &st) != NOTOK)
294 ct->c_umask = ~(st.st_mode & 0777);
295 else
296 ct->c_umask = ~m_gmprot();
297 }
298 }
299
300 /*
301 * List the message content
302 */
303 list_all_messages (cts, headsw, sizesw, verbosw, debugsw, dispo);
304
305 /* Now free all the structures for the content */
306 for (ctp = cts; *ctp; ctp++)
307 free_content (*ctp);
308
309 free(cts);
310 cts = NULL;
311
312 /* If reading from a folder, do some updating */
313 if (mp) {
314 context_replace (pfolder, folder);/* update current folder */
315 if (chgflag)
316 seq_setcur (mp, mp->hghsel); /* update current message */
317 seq_save (mp); /* synchronize sequences */
318 context_save (); /* save the context file */
319 }
320
321 done (0);
322 return 1;
323 }
324
325
326 static void
327 pipeser (int i)
328 {
329 if (i == SIGQUIT) {
330 fflush (stdout);
331 fprintf (stderr, "\n");
332 fflush (stderr);
333 }
334
335 done (1);
336 /* NOTREACHED */
337 }