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