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