10ec55d637b9a973663810be342b3d28bcc0fbff
[mmh] / uip / mhlistsbr.c
1
2 /*
3  * mhlistsbr.c -- routines to list information about the
4  *             -- contents of MIME messages
5  *
6  * $Id$
7  *
8  * This code is Copyright (c) 2002, by the authors of nmh.  See the
9  * COPYRIGHT file in the root directory of the nmh distribution for
10  * complete copyright information.
11  */
12
13 #include <h/mh.h>
14 #include <fcntl.h>
15 #include <h/signals.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include <h/mts.h>
19 #include <h/tws.h>
20 #include <h/mime.h>
21 #include <h/mhparse.h>
22 #include <h/utils.h>
23
24 /* mhmisc.c */
25 int part_ok (CT, int);
26 int type_ok (CT, int);
27 void flush_errors (void);
28
29 /*
30  * prototypes
31  */
32 void list_all_messages (CT *, int, int, int, int);
33 int list_switch (CT, int, int, int, int);
34 int list_content (CT, int, int, int, int);
35
36 /*
37  * static prototypes
38  */
39 static void list_single_message (CT, int, int, int);
40 static int list_debug (CT);
41 static int list_multi (CT, int, int, int, int);
42 static int list_partial (CT, int, int, int, int);
43 static int list_external (CT, int, int, int, int);
44 static int list_application (CT, int, int, int, int);
45 static int list_encoding (CT);
46
47
48 /*
49  * various formats for -list option
50  */
51 #define LSTFMT1         "%4s %-5s %-24s %5s %-36s\n"
52 #define LSTFMT2a        "%4d "
53 #define LSTFMT2b        "%-5s %-24.24s "
54 #define LSTFMT2c1       "%5lu"
55 #define LSTFMT2c2       "%4lu%c"
56 #define LSTFMT2c3       "huge "
57 #define LSTFMT2c4       "     "
58 #define LSTFMT2d1       " %-36.36s"
59 #define LSTFMT2d2       "\t     %-65.65s\n"
60
61
62 /*
63  * Top level entry point to list group of messages
64  */
65
66 void
67 list_all_messages (CT *cts, int headers, int realsize, int verbose, int debug)
68 {
69     CT ct, *ctp;
70
71     if (headers)
72         printf (LSTFMT1, "msg", "part", "type/subtype", "size", "description");
73
74     for (ctp = cts; *ctp; ctp++) {
75         ct = *ctp;
76         list_single_message (ct, realsize, verbose, debug);
77     }
78
79     flush_errors ();
80 }
81
82
83 /*
84  * Entry point to list a single message
85  */
86
87 static void
88 list_single_message (CT ct, int realsize, int verbose, int debug)
89 {
90     if (type_ok (ct, 1)) {
91         umask (ct->c_umask);
92         list_switch (ct, 1, realsize, verbose, debug);
93         if (ct->c_fp) {
94             fclose (ct->c_fp);
95             ct->c_fp = NULL;
96         }
97         if (ct->c_ceclosefnx)
98             (*ct->c_ceclosefnx) (ct);
99     }
100 }
101
102
103 /*
104  * Primary switching routine to list information about a content
105  */
106
107 int
108 list_switch (CT ct, int toplevel, int realsize, int verbose, int debug)
109 {
110     switch (ct->c_type) {
111         case CT_MULTIPART:
112             return list_multi (ct, toplevel, realsize, verbose, debug);
113             break;
114
115         case CT_MESSAGE:
116             switch (ct->c_subtype) {
117                 case MESSAGE_PARTIAL:
118                     return list_partial (ct, toplevel, realsize, verbose, debug);
119                     break;
120
121                 case MESSAGE_EXTERNAL:
122                     return list_external (ct, toplevel, realsize, verbose, debug);
123                     break;
124
125                 case MESSAGE_RFC822:
126                 default:
127                     return list_content (ct, toplevel, realsize, verbose, debug);
128                     break;
129             }
130             break;
131
132         case CT_TEXT:
133         case CT_AUDIO:
134         case CT_IMAGE:
135         case CT_VIDEO:
136             return list_content (ct, toplevel, realsize, verbose, debug);
137             break;
138
139         case CT_APPLICATION:
140             return list_application (ct, toplevel, realsize, verbose, debug);
141             break;
142
143         default:
144             /* list_debug (ct); */
145             adios (NULL, "unknown content type %d", ct->c_type);
146             break;
147     }
148
149     return 0;   /* NOT REACHED */
150 }
151
152
153 #define empty(s) ((s) ? (s) : "")
154
155 /*
156  * Method for listing information about a simple/generic content
157  */
158
159 int
160 list_content (CT ct, int toplevel, int realsize, int verbose, int debug)
161 {
162     unsigned long size;
163     char *cp, buffer[BUFSIZ];
164     CI ci = &ct->c_ctinfo;
165
166     printf (toplevel > 0 ? LSTFMT2a : toplevel < 0 ? "part " : "     ",
167             atoi (r1bindex (empty (ct->c_file), '/')));
168     snprintf (buffer, sizeof(buffer), "%s/%s", empty (ci->ci_type),
169                 empty (ci->ci_subtype));
170     printf (LSTFMT2b, empty (ct->c_partno), buffer);
171
172     if (ct->c_cesizefnx && realsize)
173         size = (*ct->c_cesizefnx) (ct);
174     else
175         size = ct->c_end - ct->c_begin;
176
177     /* find correct scale for size (Kilo/Mega/Giga/Tera) */
178     for (cp = " KMGT"; size > 9999; size >>= 10)
179         if (!*++cp)
180             break;
181
182     /* print size of this body part */
183     switch (*cp) {
184         case ' ':
185             if (size > 0 || ct->c_encoding != CE_EXTERNAL)
186                 printf (LSTFMT2c1, size);
187             else
188                 printf (LSTFMT2c4);
189             break;
190
191         default:
192             printf (LSTFMT2c2, size, *cp);
193             break;
194
195         case '\0':
196             printf (LSTFMT2c3);
197     }
198
199     /* print Content-Description */
200     if (ct->c_descr) {
201         char *dp;
202
203         dp = trimcpy (cp = add (ct->c_descr, NULL));
204         free (cp);
205         printf (LSTFMT2d1, dp);
206         free (dp);
207     }
208
209     printf ("\n");
210
211     /*
212      * If verbose, print any RFC-822 comments in the
213      * Content-Type line.
214      */
215     if (verbose && ci->ci_comment) {
216         char *dp;
217
218         dp = trimcpy (cp = add (ci->ci_comment, NULL));
219         free (cp);
220         snprintf (buffer, sizeof(buffer), "(%s)", dp);
221         free (dp);
222         printf (LSTFMT2d2, buffer);
223     }
224
225     if (debug)
226         list_debug (ct);
227
228     return OK;
229 }
230
231
232 /*
233  * Print debugging information about a content
234  */
235
236 static int
237 list_debug (CT ct)
238 {
239     char **ap, **ep;
240     CI ci = &ct->c_ctinfo;
241
242     fflush (stdout);
243     fprintf (stderr, "  partno \"%s\"\n", empty (ct->c_partno));
244
245     /* print MIME-Version line */
246     if (ct->c_vrsn)
247         fprintf (stderr, "  %s:%s\n", VRSN_FIELD, ct->c_vrsn);
248
249     /* print Content-Type line */
250     if (ct->c_ctline)
251         fprintf (stderr, "  %s:%s\n", TYPE_FIELD, ct->c_ctline);
252
253     /* print parsed elements of content type */
254     fprintf (stderr, "    type    \"%s\"\n", empty (ci->ci_type));
255     fprintf (stderr, "    subtype \"%s\"\n", empty (ci->ci_subtype));
256     fprintf (stderr, "    comment \"%s\"\n", empty (ci->ci_comment));
257     fprintf (stderr, "    magic   \"%s\"\n", empty (ci->ci_magic));
258
259     /* print parsed parameters attached to content type */
260     fprintf (stderr, "    parameters\n");
261     for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
262         fprintf (stderr, "      %s=\"%s\"\n", *ap, *ep);
263
264     /* print internal flags for type/subtype */
265     fprintf (stderr, "    type 0x%x subtype 0x%x params 0x%x\n",
266              ct->c_type, ct->c_subtype, (unsigned int) ct->c_ctparams);
267
268     fprintf (stderr, "    showproc  \"%s\"\n", empty (ct->c_showproc));
269     fprintf (stderr, "    termproc  \"%s\"\n", empty (ct->c_termproc));
270     fprintf (stderr, "    storeproc \"%s\"\n", empty (ct->c_storeproc));
271
272     /* print transfer encoding information */
273     if (ct->c_celine)
274         fprintf (stderr, "  %s:%s", ENCODING_FIELD, ct->c_celine);
275
276     /* print internal flags for transfer encoding */
277     fprintf (stderr, "    transfer encoding 0x%x params 0x%x\n",
278              ct->c_encoding, (unsigned int) ct->c_cefile);
279
280     /* print Content-ID */
281     if (ct->c_id)
282         fprintf (stderr, "  %s:%s", ID_FIELD, ct->c_id);
283
284     /* print Content-Description */
285     if (ct->c_descr)
286         fprintf (stderr, "  %s:%s", DESCR_FIELD, ct->c_descr);
287
288     fprintf (stderr, "    read fp 0x%x file \"%s\" begin %ld end %ld\n",
289              (unsigned int) ct->c_fp, empty (ct->c_file),
290              ct->c_begin, ct->c_end);
291
292     /* print more information about transfer encoding */
293     list_encoding (ct);
294
295     return OK;
296 }
297
298 #undef empty
299
300
301 /*
302  * list content information for type "multipart"
303  */
304
305 static int
306 list_multi (CT ct, int toplevel, int realsize, int verbose, int debug)
307 {
308     struct multipart *m = (struct multipart *) ct->c_ctparams;
309     struct part *part;
310
311     /* list the content for toplevel of this multipart */
312     list_content (ct, toplevel, realsize, verbose, debug);
313
314     /* now list for all the subparts */
315     for (part = m->mp_parts; part; part = part->mp_next) {
316         CT p = part->mp_part;
317
318         if (part_ok (p, 1) && type_ok (p, 1))
319             list_switch (p, 0, realsize, verbose, debug);
320     }
321
322     return OK;
323 }
324
325
326 /*
327  * list content information for type "message/partial"
328  */
329
330 static int
331 list_partial (CT ct, int toplevel, int realsize, int verbose, int debug)
332 {
333     struct partial *p = (struct partial *) ct->c_ctparams;
334
335     list_content (ct, toplevel, realsize, verbose, debug);
336     if (verbose) {
337         printf ("\t     [message %s, part %d", p->pm_partid, p->pm_partno);
338         if (p->pm_maxno)
339             printf (" of %d", p->pm_maxno);
340         printf ("]\n");
341     }
342
343     return OK;
344 }
345
346
347 /*
348  * list content information for type "message/external"
349  */
350
351 static int
352 list_external (CT ct, int toplevel, int realsize, int verbose, int debug)
353 {
354     struct exbody *e = (struct exbody *) ct->c_ctparams;
355
356     /*
357      * First list the information for the
358      * message/external content itself.
359      */
360     list_content (ct, toplevel, realsize, verbose, debug);
361
362     if (verbose) {
363         if (e->eb_name)
364             printf ("\t     name=\"%s\"\n", e->eb_name);
365         if (e->eb_dir)
366             printf ("\t     directory=\"%s\"\n", e->eb_dir);
367         if (e->eb_site)
368             printf ("\t     site=\"%s\"\n", e->eb_site);
369         if (e->eb_server)
370             printf ("\t     server=\"%s\"\n", e->eb_server);
371         if (e->eb_subject)
372             printf ("\t     subject=\"%s\"\n", e->eb_subject);
373
374         /* This must be defined */
375         printf     ("\t     access-type=\"%s\"\n", e->eb_access);
376
377         if (e->eb_mode)
378             printf ("\t     mode=\"%s\"\n", e->eb_mode);
379         if (e->eb_permission)
380             printf ("\t     permission=\"%s\"\n", e->eb_permission);
381
382         if (e->eb_flags == NOTOK)
383             printf ("\t     [service unavailable]\n");
384     }
385
386     /*
387      * Now list the information for the external content
388      * to which this content points.
389      */
390     list_content (e->eb_content, 0, realsize, verbose, debug);
391
392     return OK;
393 }
394
395
396 /*
397  * list content information for type "application"
398  */
399
400 static int
401 list_application (CT ct, int toplevel, int realsize, int verbose, int debug)
402 {
403     list_content (ct, toplevel, realsize, verbose, debug);
404     if (verbose) {
405         char **ap, **ep;
406         CI ci = &ct->c_ctinfo;
407
408         for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
409             printf ("\t     %s=\"%s\"\n", *ap, *ep);
410     }
411
412     return OK;
413 }
414
415
416 /*
417  * list information about the Content-Transfer-Encoding
418  * used by a content.
419  */
420
421 static int
422 list_encoding (CT ct)
423 {
424     CE ce;
425
426     if ((ce = ct->c_cefile))
427         fprintf (stderr, "    decoded fp 0x%x file \"%s\"\n",
428                  (unsigned int) ce->ce_fp, ce->ce_file ? ce->ce_file : "");
429
430     return OK;
431 }