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