a59e0e88e5860655e220cda558aae4be705a9858
[mmh] / uip / mhlistsbr.c
1 /*
2 ** mhlistsbr.c -- routines to list information about the
3 **             -- 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 <errno.h>
13 #include <h/tws.h>
14 #include <h/mime.h>
15 #include <h/mhparse.h>
16 #include <h/utils.h>
17 #include <sys/stat.h>
18
19 /* mhmisc.c */
20 int part_ok(CT, int);
21 int type_ok(CT, int);
22 void flush_errors(void);
23
24 /*
25 ** prototypes
26 */
27 void list_all_messages(CT *, int, int);
28 int list_switch(CT, int, int, int);
29 int list_content(CT, int, int, int);
30
31 /*
32 ** static prototypes
33 */
34 static void list_single_message(CT, int, int);
35 static int list_debug(CT);
36 static int list_multi(CT, int, int, int);
37 static int list_partial(CT, int, int, int);
38 static int list_encoding(CT);
39
40
41 /*
42 ** various formats for -list option
43 */
44 #define LSTFMT1    "%4s %-5s %-24s %5s %-36s\n"
45 #define LSTFMT2a   "%4d "
46 #define LSTFMT2b   "%-5s %-24.24s "
47 #define LSTFMT2c1  "%5lu"
48 #define LSTFMT2c2  "%4lu%c"
49 #define LSTFMT2c3  "huge "
50 #define LSTFMT2c4  "     "
51 #define LSTFMT2d1  " %-36.36s"
52 #define LSTFMT2d2  "\t     %-65.65s\n"
53
54
55 /*
56 ** Top level entry point to list group of messages
57 */
58 void
59 list_all_messages(CT *cts, int verbose, int debug)
60 {
61         CT ct, *ctp;
62
63         printf(LSTFMT1, "msg", "part", "type/subtype", "size", "description");
64         for (ctp = cts; *ctp; ctp++) {
65                 ct = *ctp;
66                 list_single_message(ct, verbose, debug);
67         }
68         flush_errors();
69 }
70
71
72 /*
73 ** Entry point to list a single message
74 */
75 static void
76 list_single_message(CT ct, int verbose, int debug)
77 {
78         if (type_ok(ct, 1)) {
79                 umask(ct->c_umask);
80                 list_switch(ct, 1, verbose, debug);
81                 if (ct->c_fp) {
82                         fclose(ct->c_fp);
83                         ct->c_fp = NULL;
84                 }
85                 if (ct->c_ceclosefnx)
86                         (*ct->c_ceclosefnx) (ct);
87         }
88 }
89
90
91 /*
92 ** Primary switching routine to list information about a content
93 */
94 int
95 list_switch(CT ct, int toplevel, int verbose, int debug)
96 {
97         switch (ct->c_type) {
98         case CT_MULTIPART:
99                 return list_multi(ct, toplevel, verbose, debug);
100                 break;
101
102         case CT_MESSAGE:
103                 if (ct->c_subtype == MESSAGE_PARTIAL) {
104                         return list_partial(ct, toplevel, verbose, debug);
105                 } else {
106                         return list_content(ct, toplevel, verbose, debug);
107                 }
108                 break;
109
110         case CT_TEXT:
111         case CT_AUDIO:
112         case CT_IMAGE:
113         case CT_VIDEO:
114         case CT_APPLICATION:
115                 return list_content(ct, toplevel, verbose, debug);
116                 break;
117
118         default:
119                 /* list_debug (ct); */
120                 adios(NULL, "unknown content type %d", ct->c_type);
121                 break;
122         }
123
124         return 0;  /* NOT REACHED */
125 }
126
127
128 #define empty(s) ((s) ? (s) : "")
129
130 /*
131 ** Method for listing information about a simple/generic content
132 */
133 int
134 list_content(CT ct, int toplevel, int verbose, int debug)
135 {
136         unsigned long size;
137         char *cp, buffer[BUFSIZ];
138         CI ci = &ct->c_ctinfo;
139
140         printf(toplevel > 0 ? LSTFMT2a : toplevel < 0 ? "part " : "     ",
141                         atoi(mhbasename(empty(ct->c_file))));
142         snprintf(buffer, sizeof(buffer), "%s/%s", empty(ci->ci_type),
143                         empty(ci->ci_subtype));
144         printf(LSTFMT2b, empty(ct->c_partno), buffer);
145
146         if (ct->c_cesizefnx)
147                 size = (*ct->c_cesizefnx) (ct);
148         else
149                 size = ct->c_end - ct->c_begin;
150
151         /* find correct scale for size (Kilo/Mega/Giga/Tera) */
152         for (cp = " KMGT"; size > 9999; size >>= 10)
153                 if (!*++cp)
154                         break;
155
156         /* print size of this body part */
157         switch (*cp) {
158         case ' ':
159                 if (size > 0 || ct->c_encoding != CE_EXTERNAL)
160                         printf(LSTFMT2c1, size);
161                 else
162                         printf(LSTFMT2c4);
163                 break;
164
165         default:
166                 printf(LSTFMT2c2, size, *cp);
167                 break;
168
169         case '\0':
170                 printf(LSTFMT2c3);
171         }
172
173         /* print Content-Description */
174         if (ct->c_descr) {
175                 char *dp;
176
177                 dp = trimcpy(cp = getcpy(ct->c_descr));
178                 free(cp);
179                 printf(LSTFMT2d1, dp);
180                 free(dp);
181         }
182
183         printf("\n");
184
185         if (verbose) {
186                 char **ap, **ep;
187                 CI ci = &ct->c_ctinfo;
188
189                 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
190                         printf("\t\t%s=\"%s\"\n", *ap, *ep);
191                 }
192
193                 /*
194                 ** If verbose, print any RFC-822 comments in the
195                 ** Content-Type line.
196                 */
197                 if (ci->ci_comment) {
198                         char *dp;
199
200                         dp = trimcpy(cp = add(ci->ci_comment, NULL));
201                         free (cp);
202                         snprintf(buffer, sizeof(buffer), "(%s)", dp);
203                         free(dp);
204                         printf(LSTFMT2d2, buffer);
205                 }
206         }
207
208         if (debug)
209                 list_debug(ct);
210
211         return OK;
212 }
213
214
215 /*
216 ** Print debugging information about a content
217 */
218 static int
219 list_debug(CT ct)
220 {
221         char **ap, **ep;
222         CI ci = &ct->c_ctinfo;
223
224         fflush(stdout);
225         fprintf(stderr, "  partno \"%s\"\n", empty(ct->c_partno));
226
227         /* print MIME-Version line */
228         if (ct->c_vrsn)
229                 fprintf(stderr, "  %s:%s\n", VRSN_FIELD, ct->c_vrsn);
230
231         /* print Content-Type line */
232         if (ct->c_ctline)
233                 fprintf(stderr, "  %s:%s\n", TYPE_FIELD, ct->c_ctline);
234
235         /* print parsed elements of content type */
236         fprintf(stderr, "    type    \"%s\"\n", empty(ci->ci_type));
237         fprintf(stderr, "    subtype \"%s\"\n", empty(ci->ci_subtype));
238         fprintf(stderr, "    comment \"%s\"\n", empty(ci->ci_comment));
239         fprintf(stderr, "    magic   \"%s\"\n", empty(ci->ci_magic));
240
241         /* print parsed parameters attached to content type */
242         fprintf(stderr, "    parameters\n");
243         for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
244                 fprintf(stderr, "      %s=\"%s\"\n", *ap, *ep);
245
246         /* print internal flags for type/subtype */
247         fprintf(stderr, "    type 0x%x subtype 0x%x params 0x%x\n",
248                          ct->c_type, ct->c_subtype,
249                          (unsigned int)(unsigned long) ct->c_ctparams);
250
251         fprintf(stderr, "    charset  \"%s\"\n", empty(ct->c_charset));
252         fprintf(stderr, "    showproc  \"%s\"\n", empty(ct->c_showproc));
253         fprintf(stderr, "    storeproc \"%s\"\n", empty(ct->c_storeproc));
254
255         /* print transfer encoding information */
256         if (ct->c_celine)
257                 fprintf(stderr, "  %s:%s", ENCODING_FIELD, ct->c_celine);
258
259         /* print internal flags for transfer encoding */
260         fprintf(stderr, "    transfer encoding 0x%x params 0x%x\n",
261                         ct->c_encoding,
262                         (unsigned int)(unsigned long) ct->c_cefile);
263
264         /* print Content-ID */
265         if (ct->c_id)
266                 fprintf(stderr, "  %s:%s", ID_FIELD, ct->c_id);
267
268         /* print Content-Description */
269         if (ct->c_descr)
270                 fprintf(stderr, "  %s:%s", DESCR_FIELD, ct->c_descr);
271
272         fprintf(stderr, "    read fp 0x%x file \"%s\" begin %ld end %ld\n",
273                         (unsigned int)(unsigned long) ct->c_fp,
274                         empty(ct->c_file), ct->c_begin, ct->c_end);
275
276         /* print more information about transfer encoding */
277         list_encoding(ct);
278
279         return OK;
280 }
281
282 #undef empty
283
284
285 /*
286 ** list content information for type "multipart"
287 */
288 static int
289 list_multi(CT ct, int toplevel, int verbose, int debug)
290 {
291         struct multipart *m = (struct multipart *) ct->c_ctparams;
292         struct part *part;
293
294         /* list the content for toplevel of this multipart */
295         list_content(ct, toplevel, verbose, debug);
296
297         /* now list for all the subparts */
298         for (part = m->mp_parts; part; part = part->mp_next) {
299                 CT p = part->mp_part;
300
301                 if (part_ok(p, 1) && type_ok(p, 1))
302                         list_switch(p, 0, verbose, debug);
303         }
304
305         return OK;
306 }
307
308
309 /*
310 ** list content information for type "message/partial"
311 */
312 static int
313 list_partial(CT ct, int toplevel, int verbose, int debug)
314 {
315         struct partial *p = (struct partial *) ct->c_ctparams;
316
317         list_content(ct, toplevel, verbose, debug);
318         if (verbose) {
319                 printf("\t     [message %s, part %d",
320                                 p->pm_partid, p->pm_partno);
321                 if (p->pm_maxno)
322                         printf(" of %d", p->pm_maxno);
323                 printf("]\n");
324         }
325
326         return OK;
327 }
328
329
330 /*
331 ** list information about the Content-Transfer-Encoding
332 ** used by a content.
333 */
334 static int
335 list_encoding(CT ct)
336 {
337         CE ce;
338
339         if ((ce = ct->c_cefile))
340                 fprintf(stderr, "    decoded fp 0x%x file \"%s\"\n",
341                         (unsigned int)(unsigned long) ce->ce_fp,
342                         ce->ce_file ? ce->ce_file : "");
343
344         return OK;
345 }