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