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