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