mhshow/mhstore: Removed support for retrieving message/external-body parts.
[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_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 realsize, 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, realsize, 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 realsize, int verbose, int debug)
82 {
83         if (type_ok(ct, 1)) {
84                 umask(ct->c_umask);
85                 list_switch(ct, 1, realsize, 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 realsize, int verbose, int debug)
101 {
102         switch (ct->c_type) {
103         case CT_MULTIPART:
104                 return list_multi(ct, toplevel, realsize, verbose, debug);
105                 break;
106
107         case CT_MESSAGE:
108                 if (ct->c_subtype == MESSAGE_PARTIAL) {
109                         return list_partial(ct, toplevel, realsize, verbose,
110                                                 debug);
111                 } else {
112                         return list_content(ct, toplevel, realsize, verbose,
113                                                 debug);
114                 }
115                 break;
116
117         case CT_TEXT:
118         case CT_AUDIO:
119         case CT_IMAGE:
120         case CT_VIDEO:
121         case CT_APPLICATION:
122                 return list_content(ct, toplevel, realsize, verbose, debug);
123                 break;
124
125         default:
126                 /* list_debug (ct); */
127                 adios(NULL, "unknown content type %d", ct->c_type);
128                 break;
129         }
130
131         return 0;  /* NOT REACHED */
132 }
133
134
135 #define empty(s) ((s) ? (s) : "")
136
137 /*
138 ** Method for listing information about a simple/generic content
139 */
140 int
141 list_content(CT ct, int toplevel, int realsize, int verbose, int debug)
142 {
143         unsigned long size;
144         char *cp, buffer[BUFSIZ];
145         CI ci = &ct->c_ctinfo;
146
147         printf(toplevel > 0 ? LSTFMT2a : toplevel < 0 ? "part " : "     ",
148                         atoi(mhbasename(empty(ct->c_file))));
149         snprintf(buffer, sizeof(buffer), "%s/%s", empty(ci->ci_type),
150                         empty(ci->ci_subtype));
151         printf(LSTFMT2b, empty(ct->c_partno), buffer);
152
153         if (ct->c_cesizefnx && realsize)
154                 size = (*ct->c_cesizefnx) (ct);
155         else
156                 size = ct->c_end - ct->c_begin;
157
158         /* find correct scale for size (Kilo/Mega/Giga/Tera) */
159         for (cp = " KMGT"; size > 9999; size >>= 10)
160                 if (!*++cp)
161                         break;
162
163         /* print size of this body part */
164         switch (*cp) {
165         case ' ':
166                 if (size > 0 || ct->c_encoding != CE_EXTERNAL)
167                         printf(LSTFMT2c1, size);
168                 else
169                         printf(LSTFMT2c4);
170                 break;
171
172         default:
173                 printf(LSTFMT2c2, size, *cp);
174                 break;
175
176         case '\0':
177                 printf(LSTFMT2c3);
178         }
179
180         /* print Content-Description */
181         if (ct->c_descr) {
182                 char *dp;
183
184                 dp = trimcpy(cp = getcpy(ct->c_descr));
185                 free(cp);
186                 printf(LSTFMT2d1, dp);
187                 free(dp);
188         }
189
190         printf("\n");
191
192         if (verbose) {
193                 char **ap, **ep;
194                 CI ci = &ct->c_ctinfo;
195
196                 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
197                         printf("\t\t%s=\"%s\"\n", *ap, *ep);
198                 }
199
200                 /*
201                 ** If verbose, print any RFC-822 comments in the
202                 ** Content-Type line.
203                 */
204                 if (ci->ci_comment) {
205                         char *dp;
206
207                         dp = trimcpy(cp = add(ci->ci_comment, NULL));
208                         free (cp);
209                         snprintf(buffer, sizeof(buffer), "(%s)", dp);
210                         free(dp);
211                         printf(LSTFMT2d2, buffer);
212                 }
213         }
214
215         if (debug)
216                 list_debug(ct);
217
218         return OK;
219 }
220
221
222 /*
223 ** Print debugging information about a content
224 */
225 static int
226 list_debug(CT ct)
227 {
228         char **ap, **ep;
229         CI ci = &ct->c_ctinfo;
230
231         fflush(stdout);
232         fprintf(stderr, "  partno \"%s\"\n", empty(ct->c_partno));
233
234         /* print MIME-Version line */
235         if (ct->c_vrsn)
236                 fprintf(stderr, "  %s:%s\n", VRSN_FIELD, ct->c_vrsn);
237
238         /* print Content-Type line */
239         if (ct->c_ctline)
240                 fprintf(stderr, "  %s:%s\n", TYPE_FIELD, ct->c_ctline);
241
242         /* print parsed elements of content type */
243         fprintf(stderr, "    type    \"%s\"\n", empty(ci->ci_type));
244         fprintf(stderr, "    subtype \"%s\"\n", empty(ci->ci_subtype));
245         fprintf(stderr, "    comment \"%s\"\n", empty(ci->ci_comment));
246         fprintf(stderr, "    magic   \"%s\"\n", empty(ci->ci_magic));
247
248         /* print parsed parameters attached to content type */
249         fprintf(stderr, "    parameters\n");
250         for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
251                 fprintf(stderr, "      %s=\"%s\"\n", *ap, *ep);
252
253         /* print internal flags for type/subtype */
254         fprintf(stderr, "    type 0x%x subtype 0x%x params 0x%x\n",
255                          ct->c_type, ct->c_subtype,
256                          (unsigned int)(unsigned long) ct->c_ctparams);
257
258         fprintf(stderr, "    showproc  \"%s\"\n", empty(ct->c_showproc));
259         fprintf(stderr, "    termproc  \"%s\"\n", empty(ct->c_termproc));
260         fprintf(stderr, "    storeproc \"%s\"\n", empty(ct->c_storeproc));
261
262         /* print transfer encoding information */
263         if (ct->c_celine)
264                 fprintf(stderr, "  %s:%s", ENCODING_FIELD, ct->c_celine);
265
266         /* print internal flags for transfer encoding */
267         fprintf(stderr, "    transfer encoding 0x%x params 0x%x\n",
268                         ct->c_encoding,
269                         (unsigned int)(unsigned long) ct->c_cefile);
270
271         /* print Content-ID */
272         if (ct->c_id)
273                 fprintf(stderr, "  %s:%s", ID_FIELD, ct->c_id);
274
275         /* print Content-Description */
276         if (ct->c_descr)
277                 fprintf(stderr, "  %s:%s", DESCR_FIELD, ct->c_descr);
278
279         fprintf(stderr, "    read fp 0x%x file \"%s\" begin %ld end %ld\n",
280                         (unsigned int)(unsigned long) ct->c_fp,
281                         empty(ct->c_file), ct->c_begin, ct->c_end);
282
283         /* print more information about transfer encoding */
284         list_encoding(ct);
285
286         return OK;
287 }
288
289 #undef empty
290
291
292 /*
293 ** list content information for type "multipart"
294 */
295 static int
296 list_multi(CT ct, int toplevel, int realsize, int verbose, int debug)
297 {
298         struct multipart *m = (struct multipart *) ct->c_ctparams;
299         struct part *part;
300
301         /* list the content for toplevel of this multipart */
302         list_content(ct, toplevel, realsize, verbose, debug);
303
304         /* now list for all the subparts */
305         for (part = m->mp_parts; part; part = part->mp_next) {
306                 CT p = part->mp_part;
307
308                 if (part_ok(p, 1) && type_ok(p, 1))
309                         list_switch(p, 0, realsize, verbose, debug);
310         }
311
312         return OK;
313 }
314
315
316 /*
317 ** list content information for type "message/partial"
318 */
319 static int
320 list_partial(CT ct, int toplevel, int realsize, int verbose, int debug)
321 {
322         struct partial *p = (struct partial *) ct->c_ctparams;
323
324         list_content(ct, toplevel, realsize, verbose, debug);
325         if (verbose) {
326                 printf("\t     [message %s, part %d",
327                                 p->pm_partid, p->pm_partno);
328                 if (p->pm_maxno)
329                         printf(" of %d", p->pm_maxno);
330                 printf("]\n");
331         }
332
333         return OK;
334 }
335
336
337 /*
338 ** list information about the Content-Transfer-Encoding
339 ** used by a content.
340 */
341 static int
342 list_encoding(CT ct)
343 {
344         CE ce;
345
346         if ((ce = ct->c_cefile))
347                 fprintf(stderr, "    decoded fp 0x%x file \"%s\"\n",
348                         (unsigned int)(unsigned long) ce->ce_fp,
349                         ce->ce_file ? ce->ce_file : "");
350
351         return OK;
352 }