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