]> diplodocus.org Git - nmh/blob - uip/mhshow.c
Removed temporary probes added in commit
[nmh] / uip / mhshow.c
1
2 /*
3 * mhshow.c -- display 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 MHSHOW_SWITCHES \
22 X("check", 0, CHECKSW) \
23 X("nocheck", 0, NCHECKSW) \
24 X("pause", 0, PAUSESW) \
25 X("nopause", 0, NPAUSESW) \
26 X("serialonly", 0, SERIALSW) \
27 X("noserialonly", 0, NSERIALSW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("file file", 0, FILESW) \
31 X("form formfile", 0, FORMSW) \
32 X("part number", 0, PARTSW) \
33 X("type content", 0, TYPESW) \
34 X("rcache policy", 0, RCACHESW) \
35 X("wcache policy", 0, WCACHESW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
38 /* \
39 * switches for moreproc/mhlproc \
40 */ \
41 X("moreproc program", -4, PROGSW) \
42 X("nomoreproc", -3, NPROGSW) \
43 X("length lines", -4, LENSW) \
44 X("width columns", -4, WIDTHSW) \
45 /* \
46 * switches for debugging \
47 */ \
48 X("debug", -5, DEBUGSW) \
49
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(MHSHOW);
52 #undef X
53
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(MHSHOW, switches);
56 #undef X
57
58
59 /* mhparse.c */
60 extern char *tmp; /* directory to place temp files */
61
62 /* mhcachesbr.c */
63 extern int rcachesw;
64 extern int wcachesw;
65 extern char *cache_public;
66 extern char *cache_private;
67
68 /* mhshowsbr.c */
69 extern int pausesw;
70 extern int serialsw;
71 extern char *progsw;
72 extern int nomore; /* flags for moreproc/header display */
73 extern char *formsw;
74
75 /* mhmisc.c */
76 extern int npart;
77 extern int ntype;
78 extern char *parts[NPARTS + 1];
79 extern char *types[NTYPES + 1];
80 extern int userrs;
81
82 int debugsw = 0;
83 int verbosw = 0;
84
85 #define quitser pipeser
86
87 /* mhparse.c */
88 CT parse_mime (char *);
89
90 /* mhmisc.c */
91 int part_ok (CT, int);
92 int type_ok (CT, int);
93 void flush_errors (void);
94
95 /* mhshowsbr.c */
96 void show_all_messages (CT *);
97
98 /* mhfree.c */
99 extern CT *cts;
100 void freects_done (int) NORETURN;
101
102 /*
103 * static prototypes
104 */
105 static void pipeser (int);
106
107
108 int
109 main (int argc, char **argv)
110 {
111 int msgnum, *icachesw;
112 char *cp, *file = NULL, *folder = NULL;
113 char *maildir, buf[100], **argp;
114 char **arguments;
115 struct msgs_array msgs = { 0, 0, NULL };
116 struct msgs *mp = NULL;
117 CT ct, *ctp;
118 FILE *fp;
119
120 done=freects_done;
121
122 #ifdef LOCALE
123 setlocale(LC_ALL, "");
124 #endif
125 invo_name = r1bindex (argv[0], '/');
126
127 /* read user profile/context */
128 context_read();
129
130 arguments = getarguments (invo_name, argc, argv, 1);
131 argp = arguments;
132
133 /*
134 * Parse arguments
135 */
136 while ((cp = *argp++)) {
137 if (*cp == '-') {
138 switch (smatch (++cp, switches)) {
139 case AMBIGSW:
140 ambigsw (cp, switches);
141 done (1);
142 case UNKWNSW:
143 adios (NULL, "-%s unknown", cp);
144
145 case HELPSW:
146 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
147 invo_name);
148 print_help (buf, switches, 1);
149 done (0);
150 case VERSIONSW:
151 print_version(invo_name);
152 done (0);
153
154 case RCACHESW:
155 icachesw = &rcachesw;
156 goto do_cache;
157 case WCACHESW:
158 icachesw = &wcachesw;
159 do_cache:
160 if (!(cp = *argp++) || *cp == '-')
161 adios (NULL, "missing argument to %s", argp[-2]);
162 switch (*icachesw = smatch (cp, caches)) {
163 case AMBIGSW:
164 ambigsw (cp, caches);
165 done (1);
166 case UNKWNSW:
167 adios (NULL, "%s unknown", cp);
168 default:
169 break;
170 }
171 continue;
172
173 case CHECKSW:
174 checksw++;
175 continue;
176 case NCHECKSW:
177 checksw = 0;
178 continue;
179
180 case PAUSESW:
181 pausesw = 1;
182 continue;
183 case NPAUSESW:
184 pausesw = 0;
185 continue;
186
187 case SERIALSW:
188 serialsw = 1;
189 continue;
190 case NSERIALSW:
191 serialsw = 0;
192 continue;
193
194 case PARTSW:
195 if (!(cp = *argp++) || *cp == '-')
196 adios (NULL, "missing argument to %s", argp[-2]);
197 if (npart >= NPARTS)
198 adios (NULL, "too many parts (starting with %s), %d max",
199 cp, NPARTS);
200 parts[npart++] = cp;
201 continue;
202
203 case TYPESW:
204 if (!(cp = *argp++) || *cp == '-')
205 adios (NULL, "missing argument to %s", argp[-2]);
206 if (ntype >= NTYPES)
207 adios (NULL, "too many types (starting with %s), %d max",
208 cp, NTYPES);
209 types[ntype++] = cp;
210 continue;
211
212 case FILESW:
213 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
214 adios (NULL, "missing argument to %s", argp[-2]);
215 file = *cp == '-' ? cp : path (cp, TFILE);
216 continue;
217
218 case FORMSW:
219 if (!(cp = *argp++) || *cp == '-')
220 adios (NULL, "missing argument to %s", argp[-2]);
221 if (formsw)
222 free (formsw);
223 formsw = getcpy (etcpath (cp));
224 continue;
225
226 /*
227 * Switches for moreproc/mhlproc
228 */
229 case PROGSW:
230 if (!(progsw = *argp++) || *progsw == '-')
231 adios (NULL, "missing argument to %s", argp[-2]);
232 continue;
233 case NPROGSW:
234 nomore++;
235 continue;
236
237 case LENSW:
238 case WIDTHSW:
239 if (!(cp = *argp++) || *cp == '-')
240 adios (NULL, "missing argument to %s", argp[-2]);
241 continue;
242
243 case VERBSW:
244 verbosw = 1;
245 continue;
246 case NVERBSW:
247 verbosw = 0;
248 continue;
249 case DEBUGSW:
250 debugsw = 1;
251 continue;
252 }
253 }
254 if (*cp == '+' || *cp == '@') {
255 if (folder)
256 adios (NULL, "only one folder at a time!");
257 else
258 folder = pluspath (cp);
259 } else
260 app_msgarg(&msgs, cp);
261 }
262
263 /* null terminate the list of acceptable parts/types */
264 parts[npart] = NULL;
265 types[ntype] = NULL;
266
267 /*
268 * Check if we've specified an additional profile
269 */
270 if ((cp = getenv ("MHSHOW"))) {
271 if ((fp = fopen (cp, "r"))) {
272 readconfig ((struct node **) 0, fp, cp, 0);
273 fclose (fp);
274 } else {
275 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
276 }
277 }
278
279 /*
280 * Read the standard profile setup
281 */
282 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
283 readconfig ((struct node **) 0, fp, cp, 0);
284 fclose (fp);
285 }
286
287 /* Check for public cache location */
288 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
289 cache_public = NULL;
290
291 /* Check for private cache location */
292 if (!(cache_private = context_find (nmhprivcache)))
293 cache_private = ".cache";
294 cache_private = getcpy (m_maildir (cache_private));
295
296 /*
297 * Check for storage directory. If specified,
298 * then store temporary files there. Else we
299 * store them in standard nmh directory.
300 */
301 if ((cp = context_find (nmhstorage)) && *cp)
302 tmp = concat (cp, "/", invo_name, NULL);
303 else
304 tmp = add (m_maildir (invo_name), NULL);
305
306 if (!context_find ("path"))
307 free (path ("./", TFOLDER));
308
309 if (file && msgs.size)
310 adios (NULL, "cannot specify msg and file at same time!");
311
312 /*
313 * check if message is coming from file
314 */
315 if (file) {
316 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
317 adios (NULL, "out of memory");
318 ctp = cts;
319
320 if ((ct = parse_mime (file)))
321 *ctp++ = ct;
322 } else {
323 /*
324 * message(s) are coming from a folder
325 */
326 if (!msgs.size)
327 app_msgarg(&msgs, "cur");
328 if (!folder)
329 folder = getfolder (1);
330 maildir = m_maildir (folder);
331
332 if (chdir (maildir) == NOTOK)
333 adios (maildir, "unable to change directory to");
334
335 /* read folder and create message structure */
336 if (!(mp = folder_read (folder, 1)))
337 adios (NULL, "unable to read folder %s", folder);
338
339 /* check for empty folder */
340 if (mp->nummsg == 0)
341 adios (NULL, "no messages in %s", folder);
342
343 /* parse all the message ranges/sequences and set SELECTED */
344 for (msgnum = 0; msgnum < msgs.size; msgnum++)
345 if (!m_convert (mp, msgs.msgs[msgnum]))
346 done (1);
347
348 /*
349 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
350 * since we will use that as a tag to know which messages
351 * to remove from the "unseen" sequence.
352 */
353 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
354 if (is_selected(mp, msgnum))
355 set_unseen (mp, msgnum);
356
357 seq_setprev (mp); /* set the Previous-Sequence */
358 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
359
360 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
361 adios (NULL, "out of memory");
362 ctp = cts;
363
364 /*
365 * Parse all the SELECTED messages.
366 */
367 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
368 if (is_selected(mp, msgnum)) {
369 char *msgnam;
370
371 msgnam = m_name (msgnum);
372 if ((ct = parse_mime (msgnam)))
373 *ctp++ = ct;
374 }
375 }
376 }
377
378 if (!*cts)
379 done (1);
380
381 userrs = 1;
382 SIGNAL (SIGQUIT, quitser);
383 SIGNAL (SIGPIPE, pipeser);
384
385 /*
386 * Get the associated umask for the relevant contents.
387 */
388 for (ctp = cts; *ctp; ctp++) {
389 struct stat st;
390
391 ct = *ctp;
392 if (type_ok (ct, 1) && !ct->c_umask) {
393 if (stat (ct->c_file, &st) != NOTOK)
394 ct->c_umask = ~(st.st_mode & 0777);
395 else
396 ct->c_umask = ~m_gmprot();
397 }
398 }
399
400 /* If reading from a folder, do some updating */
401 if (mp) {
402 context_replace (pfolder, folder);/* update current folder */
403 seq_setcur (mp, mp->hghsel); /* update current message */
404 seq_save (mp); /* synchronize sequences */
405 context_save (); /* save the context file */
406 }
407
408 /*
409 * Show the message content
410 */
411 show_all_messages (cts);
412
413 /* Now free all the structures for the content */
414 for (ctp = cts; *ctp; ctp++)
415 free_content (*ctp);
416
417 free ((char *) cts);
418 cts = NULL;
419
420 done (0);
421 return 1;
422 }
423
424
425 static void
426 pipeser (int i)
427 {
428 if (i == SIGQUIT) {
429 fflush (stdout);
430 fprintf (stderr, "\n");
431 fflush (stderr);
432 }
433
434 done (1);
435 /* NOTREACHED */
436 }