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