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