decode.c
6.28 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program 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 2, or (at your option)
any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "mail.h"
/*
FIXME:
decode, this is temporary, until the API on how to present
mime/attachements etc is less confusing.
*/
static int print_stream __P ((stream_t, FILE *));
static int display_message __P ((message_t, FILE *, int));
static int mailcap_lookup __P ((const char *));
static int get_hdr_value __P ((header_t hdr, const char *name, char **value));
static int get_content_encoding __P ((header_t hdr, char **value));
static int get_content_type __P ((header_t hdr, char **value));
int
mail_decode (int argc, char **argv)
{
if (argc > 1)
return util_msglist_command (mail_print, argc, argv, 1);
else
{
message_t mesg;
int lines = 0;
FILE *out = ofile;
attribute_t attr;
if (mailbox_get_message (mbox, cursor, &mesg) != 0)
return 1;
if (util_isdeleted (cursor))
return 1;
message_lines (mesg, &lines);
if ((util_find_env("crt"))->set && lines > util_getlines ())
out = popen (getenv("PAGER"), "w");
display_message (mesg, out, islower (argv[0][0]));
message_get_attribute (mesg, &attr);
attribute_set_read (attr);
if (out != ofile)
pclose (out);
return 0;
}
return 1;
}
static int
display_message (message_t mesg, FILE *out, int select_hdr)
{
size_t nparts = 0;
header_t hdr = NULL;
size_t j;
/* Print the selected headers only. */
if (select_hdr)
{
size_t num = 0;
size_t i = 0;
char buffer[512];
hdr = NULL;
message_get_header (mesg, &hdr);
header_get_field_count (hdr, &num);
for (i = 1; i <= num; i++)
{
buffer[0] = '\0';
header_get_field_name (hdr, i, buffer, sizeof(buffer), NULL);
if (mail_header_is_visible (buffer))
{
fprintf (out, "%s: ", buffer);
header_get_field_value (hdr, i, buffer, sizeof(buffer), NULL);
fprintf (out, "%s\n", buffer);
}
}
fprintf (out, "\n");
}
else /* Print displays all headers. */
{
stream_t stream = NULL;
if (message_get_header (mesg, &hdr) == 0
&& header_get_stream (hdr, &stream) == 0)
print_stream (stream, out);
}
message_get_num_parts (mesg, &nparts);
for (j = 1; j <= nparts; j++)
{
message_t message = NULL;
if (message_get_part (mesg, j, &message) == 0)
{
char *type = NULL;
char *encoding = NULL;
int ismime = 0;
message_is_multipart (message, &ismime);
if (ismime)
{
display_message (message, out, 0);
continue;
}
message_get_header (message, &hdr);
get_content_type (hdr, &type);
get_content_encoding (hdr, &encoding);
if (strncasecmp (type, "message/rfc822", strlen (type)) == 0)
{
message_t submsg = NULL;
if (message_unencapsulate (message, &submsg, NULL) == 0)
display_message (submsg, out, select_hdr);
}
else if (mailcap_lookup (type))
{
/* FIXME: lookup .mailcap and do the appropriate action when
an match engry is find. */
/* Do something, spawn a process etc .... */
}
else if (strncasecmp (type, "text/plain", strlen ("text/plain")) == 0
|| strncasecmp (type, "text/html", strlen ("text/html")) == 0)
{
body_t body = NULL;
stream_t b_stream = NULL;
if (message_get_body (message, &body) == 0 &&
body_get_stream (body, &b_stream) == 0)
{
stream_t d_stream = NULL;
stream_t stream = NULL;
/* Can we decode. */
if (filter_create(&d_stream, b_stream, encoding,
MU_FILTER_DECODE, MU_STREAM_READ) == 0)
stream = d_stream;
else
stream = b_stream;
print_stream (stream, out);
if (d_stream)
stream_destroy (&d_stream, NULL);
}
}
else
{
int size = util_screen_columns () - 3;
int i;
fputc ('+', out);
for (i = 0; i <= size; i++)
fputc ('-', out);
fputc ('+', out); fputc ('\n', out);
fprintf (out, "| Message=%d[%d]\n", cursor, j);
fprintf (out, "| Type=%s\n", type);
fprintf (out, "| encoding=%s\n", encoding);
fputc ('+', out);
for (i = 0; i <= size; i++)
fputc ('-', out);
fputc ('+', out); fputc ('\n', out);
}
free (type);
free (encoding);
}
}
return 0;
}
static int
print_stream (stream_t stream, FILE *out)
{
char buffer[512];
off_t off = 0;
size_t n = 0;
while (stream_read (stream, buffer, sizeof (buffer) - 1, off, &n) == 0
&& n != 0)
{
if (ml_got_interrupt())
{
util_error("\nInterrupt");
break;
}
buffer[n] = '\0';
fprintf (out, "%s", buffer);
off += n;
}
return 0;
}
static int
get_content_type (header_t hdr, char **value)
{
char *type = NULL;
get_hdr_value (hdr, MU_HEADER_CONTENT_TYPE, &type);
if (type == NULL || *type == '\0')
{
if (type)
free (type);
type = strdup ("text/plain"); /* Default. */
}
*value = type;
return 0;
}
static int
get_content_encoding (header_t hdr, char **value)
{
char *encoding = NULL;
get_hdr_value (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, &encoding);
if (encoding == NULL || *encoding == '\0')
{
if (encoding)
free (encoding);
encoding = strdup ("7bit"); /* Default. */
}
*value = encoding;
return 0;
}
static int
get_hdr_value (header_t hdr, const char *name, char **value)
{
int status = header_aget_value (hdr, name, value);
if (status == 0)
{
/* Remove the newlines. */
char *nl;
while ((nl = strchr (*value, '\n')) != NULL)
{
*nl = ' ';
}
}
return status;
}
static int
mailcap_lookup (const char *type)
{
(void)type;
return 0;
}