bs.c
5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011-2012, 2014-2016 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <mailutils/mailutils.h>
#define S(str) ((str) ? (str) : "")
static void
print_param (const char *prefix, mu_assoc_t assoc, int indent)
{
mu_iterator_t itr;
int i;
mu_printf ("%*s%s:\n", indent, "", prefix);
if (!assoc)
return;
indent += 4;
MU_ASSERT (mu_assoc_get_iterator (assoc, &itr));
for (i = 0, mu_iterator_first (itr);
!mu_iterator_is_done (itr);
i++, mu_iterator_next (itr))
{
const char *name;
struct mu_mime_param *p;
mu_iterator_current_kv (itr, (const void **)&name, (void**)&p);
mu_printf ("%*s%d: %s=%s\n", indent, "", i, name, p->value);
}
mu_iterator_destroy (&itr);
}
struct print_data
{
int num;
int level;
};
static void print_bs (struct mu_bodystructure *bs, int level);
static int
print_item (void *item, void *data)
{
struct mu_bodystructure *bs = item;
struct print_data *pd = data;
mu_printf ("%*sPart #%d\n", (pd->level-1) << 2, "", pd->num);
print_bs (bs, pd->level);
++pd->num;
return 0;
}
static void
print_address (const char *title, mu_address_t addr, int indent)
{
mu_printf ("%*s%s: ", indent, "", title);
mu_stream_format_address (mu_strout, addr);
mu_printf ("\n");
}
static void
print_imapenvelope (struct mu_imapenvelope *env, int level)
{
int indent = (level << 2);
mu_printf ("%*sEnvelope:\n", indent, "");
indent += 4;
mu_printf ("%*sTime: ", indent, "");
mu_c_streamftime (mu_strout, "%c%n", &env->date, &env->tz);
mu_printf ("%*sSubject: %s\n", indent, "", S(env->subject));
print_address ("From", env->from, indent);
print_address ("Sender", env->sender, indent);
print_address ("Reply-to", env->reply_to, indent);
print_address ("To", env->to, indent);
print_address ("Cc", env->cc, indent);
print_address ("Bcc", env->bcc, indent);
mu_printf ("%*sIn-Reply-To: %s\n", indent, "", S(env->in_reply_to));
mu_printf ("%*sMessage-ID: %s\n", indent, "", S(env->message_id));
}
static void
print_bs (struct mu_bodystructure *bs, int level)
{
int indent = level << 2;
mu_printf ("%*sbody_type=%s\n", indent, "", S(bs->body_type));
mu_printf ("%*sbody_subtype=%s\n", indent, "", S(bs->body_subtype));
print_param ("Parameters", bs->body_param, indent);
mu_printf ("%*sbody_id=%s\n", indent, "", S(bs->body_id));
mu_printf ("%*sbody_descr=%s\n", indent, "", S(bs->body_descr));
mu_printf ("%*sbody_encoding=%s\n", indent, "", S(bs->body_encoding));
mu_printf ("%*sbody_size=%lu\n", indent, "", (unsigned long) bs->body_size);
/* Optional */
mu_printf ("%*sbody_md5=%s\n", indent, "", S(bs->body_md5));
mu_printf ("%*sbody_disposition=%s\n", indent, "", S(bs->body_disposition));
print_param ("Disposition Parameters", bs->body_disp_param, indent);
mu_printf ("%*sbody_language=%s\n", indent, "", S(bs->body_language));
mu_printf ("%*sbody_location=%s\n", indent, "", S(bs->body_location));
mu_printf ("%*sType ", indent, "");
switch (bs->body_message_type)
{
case mu_message_other:
mu_printf ("mu_message_other\n");
break;
case mu_message_text:
mu_printf ("mu_message_text:\n%*sbody_lines=%lu\n", indent + 4, "",
(unsigned long) bs->v.text.body_lines);
break;
case mu_message_rfc822:
mu_printf ("mu_message_rfc822:\n%*sbody_lines=%lu\n", indent + 4, "",
(unsigned long) bs->v.rfc822.body_lines);
print_imapenvelope (bs->v.rfc822.body_env, level + 1);
print_bs (bs->v.rfc822.body_struct, level + 1);
break;
case mu_message_multipart:
{
struct print_data pd;
pd.num = 0;
pd.level = level + 1;
mu_printf ("mu_message_multipart:\n");
mu_list_foreach (bs->v.multipart.body_parts, print_item, &pd);
}
}
}
int
main (int argc, char **argv)
{
mu_mailbox_t mbox;
mu_message_t mesg;
struct mu_bodystructure *bs;
if (argc != 3)
{
fprintf (stderr, "usage: %s URL NUM\n", argv[0]);
return 1;
}
mu_register_all_mbox_formats ();
MU_ASSERT (mu_mailbox_create (&mbox, argv[1]));
MU_ASSERT (mu_mailbox_open (mbox, MU_STREAM_READ));
MU_ASSERT (mu_mailbox_get_message (mbox, atoi (argv[2]), &mesg));
MU_ASSERT (mu_message_get_bodystructure (mesg, &bs));
print_bs (bs, 0);
mu_bodystructure_free (bs);
return 0;
}