-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailparse.cpp
329 lines (251 loc) · 9.99 KB
/
mailparse.cpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <string>
#include <iostream>
#include <fstream>
#include <vmime/vmime.hpp>
#include <vmime/platforms/posix/posixHandler.hpp>
#include <string.h>
#include "mailparse.h"
using namespace std;
vmime::ref <vmime::message> init_mimeparse(char *email_file)
{
try {
// set platform
vmime::platform::setHandler<vmime::platforms::posix::posixHandler>();
std::ifstream file;
file.open(email_file, std::ios::in | std::ios::binary);
vmime::utility::inputStreamAdapter is(file);
vmime::string data;
vmime::utility::outputStreamStringAdapter os(data);
vmime::utility::bufferedStreamCopy(is, os);
// Actually parse the message
vmime::ref <vmime::message> msg = vmime::create <vmime::message>();
msg->parse(data);
return msg;
} catch (vmime::exception &e) {
// std::cerr << e;
} catch (std::exception &e) {
// std::cerr << e.what();
}
return NULL;
}
int get_attachment_count(vmime::ref <vmime::message> msg)
{
try {
vmime::messageParser mp(msg);
return mp.getAttachmentCount();
} catch (vmime::exception &e) {
} catch (std::exception &e) {
}
return -1;
}
string get_header_for_name(vmime::ref <vmime::header> hdr, string name)
{
try {
string header_value;
// Now, you can extract some of its components
vmime::charset ch(vmime::charsets::UTF_8);
if (hdr->hasField(name)) {
if ((strcasecmp(name.c_str(), "to") == 0)
|| (strcasecmp(name.c_str(), "cc") == 0)
|| (strcasecmp(name.c_str(), "bcc") == 0)) { // addresslist
vmime::ref<vmime::mailboxList> mailbox_list;
if (strcasecmp(name.c_str(), "to") == 0) {
mailbox_list = hdr->To()->getValue().dynamicCast<vmime::addressList>()->toMailboxList();
} else if (strcasecmp(name.c_str(), "cc") == 0) {
mailbox_list = hdr->Cc()->getValue().dynamicCast<vmime::addressList>()->toMailboxList();
} else if (strcasecmp(name.c_str(), "bcc") == 0) {
mailbox_list = hdr->Bcc()->getValue().dynamicCast<vmime::addressList>()->toMailboxList();
}
for (int i=0; i<mailbox_list->getMailboxCount(); i++) {
// get name before email
// cc_str += mailbox_list->getMailboxAt(i)->getName().getConvertedText(ch);
// get value of email
header_value += mailbox_list->getMailboxAt(i)->getEmail();
header_value += " ";
}
return header_value;
} else if ((strcasecmp(name.c_str(), "from") == 0)
|| (strcasecmp(name.c_str(), "sender") == 0)
|| (strcasecmp(name.c_str(), "reply-to") == 0)
|| (strcasecmp(name.c_str(), "delivered-to") == 0)) { // mailbox
if (strcasecmp(name.c_str(), "from") == 0) {
return hdr->From()->getValue().dynamicCast<vmime::mailbox>()->getEmail();
} else if (strcasecmp(name.c_str(), "sender") == 0) {
return hdr->Sender()->getValue().dynamicCast<vmime::mailbox>()->getEmail();
} else if (strcasecmp(name.c_str(), "reply-to") == 0) {
return hdr->ReplyTo()->getValue().dynamicCast<vmime::mailbox>()->getEmail();
} else if (strcasecmp(name.c_str(), "delivered-to") == 0) {
return hdr->DeliveredTo()->getValue().dynamicCast<vmime::mailbox>()->getEmail();
}
} else { // text
vmime::ref<vmime::headerField> hdr_hf = hdr->getField(name);
return hdr_hf->getValue().dynamicCast<vmime::text>()->getConvertedText(ch);
}
}
} catch (vmime::exception &e) {
} catch (std::exception &e) {
}
return "";
}
string get_parsed_body(vmime::ref <vmime::message> msg)
{
try {
vmime::messageParser mp(msg);
string parsed_body;
for (int i=0; i< mp.getTextPartCount(); ++i) {
vmime::ref<const vmime::textPart> tp = mp.getTextPartAt(i);
// text/html
if (tp->getType().getSubType() == vmime::mediaTypes::TEXT_HTML) {
vmime::ref<const vmime::htmlTextPart> htp =
tp.dynamicCast<const vmime::htmlTextPart>();
//std::cout << "Length: " << htp->getText()->getLength() << std::endl;
// HTML text is in tp->getText()
// Plain text is in tp->getPlainText()
vmime::string htmlContents;
vmime::utility::outputStreamStringAdapter htmlOut(htmlContents);
vmime::utility::charsetFilteredOutputStream utf8Out(htp->getCharset(), vmime::charset("utf-8"), htmlOut); // 强制转换正文为utf8编码
htp->getText()->extract(utf8Out);
utf8Out.flush();
// append parsed_body
parsed_body += htmlContents;
} else if (tp->getType().getSubType() == vmime::mediaTypes::TEXT_PLAIN) {
vmime::ref<const vmime::plainTextPart> ptp =
tp.dynamicCast<const vmime::plainTextPart>();
vmime::string plainTextContent;
vmime::utility::outputStreamStringAdapter plainOut(plainTextContent);
vmime::utility::charsetFilteredOutputStream utf8Out(ptp->getCharset(), vmime::charset("utf-8"), plainOut);
ptp->getText()->extract(utf8Out);
utf8Out.flush();
parsed_body += plainTextContent;
} else {
// nothing to do
}
}
return parsed_body;
} catch (vmime::exception &e) {
} catch (std::exception &e) {
}
return "";
}
void init_parse(struct parsed_message_info_s *parsed_mail_info)
{
parsed_mail_info->header_from.len = 0;
parsed_mail_info->header_from.pdata = NULL;
parsed_mail_info->header_to.len = 0;
parsed_mail_info->header_to.pdata = NULL;
parsed_mail_info->header_cc.len = 0;
parsed_mail_info->header_cc.pdata = NULL;
parsed_mail_info->header_bcc.len = 0;
parsed_mail_info->header_bcc.pdata = NULL;
parsed_mail_info->header_subject.len = 0;
parsed_mail_info->header_subject.pdata = NULL;
parsed_mail_info->body.len = 0;
parsed_mail_info->body.pdata = NULL;
}
void clean_parse(struct parsed_message_info_s *parsed_mail_info)
{
if (parsed_mail_info->header_from.pdata != NULL) {
free(parsed_mail_info->header_from.pdata);
parsed_mail_info->header_from.pdata = NULL;
parsed_mail_info->header_from.len = 0;
}
if (parsed_mail_info->header_to.pdata != NULL) {
free(parsed_mail_info->header_to.pdata);
parsed_mail_info->header_to.pdata = NULL;
parsed_mail_info->header_to.len = 0;
}
if (parsed_mail_info->header_cc.pdata != NULL) {
free(parsed_mail_info->header_cc.pdata);
parsed_mail_info->header_cc.pdata = NULL;
parsed_mail_info->header_cc.len = 0;
}
if (parsed_mail_info->header_bcc.pdata != NULL) {
free(parsed_mail_info->header_bcc.pdata);
parsed_mail_info->header_bcc.pdata = NULL;
parsed_mail_info->header_bcc.len = 0;
}
if (parsed_mail_info->header_subject.pdata != NULL) {
free(parsed_mail_info->header_subject.pdata);
parsed_mail_info->header_subject.pdata = NULL;
parsed_mail_info->header_subject.len = 0;
}
if (parsed_mail_info->body.pdata != NULL) {
free(parsed_mail_info->body.pdata);
parsed_mail_info->body.pdata = NULL;
parsed_mail_info->body.len = 0;
}
}
int parse_mail_for_file(char *email, struct parsed_message_info_s *parsed_mail_info)
{
vmime::ref <vmime::message> msg = init_mimeparse(email);
if (msg == NULL) {
return 1;
}
vmime::ref <vmime::header> hdr = msg->getHeader();
// get header from, to, cc, bcc ------------------------
string from = get_header_for_name(hdr, "from");
if (from.length() > 0) {
parsed_mail_info->header_from.pdata = (char *)calloc(1, from.length() + 1);
if (parsed_mail_info->header_from.pdata != NULL) {
parsed_mail_info->header_from.len = from.length();
memcpy(parsed_mail_info->header_from.pdata, from.c_str(), parsed_mail_info->header_from.len);
parsed_mail_info->header_from.pdata[parsed_mail_info->header_from.len] = '\0';
}
}
string to = get_header_for_name(hdr, "to");
if (to.length() > 0) {
parsed_mail_info->header_to.pdata = (char *)calloc(1, to.length() + 1);
if (parsed_mail_info->header_to.pdata != NULL) {
parsed_mail_info->header_to.len = to.length();
memcpy(parsed_mail_info->header_to.pdata, to.c_str(), parsed_mail_info->header_to.len);
parsed_mail_info->header_to.pdata[parsed_mail_info->header_to.len] = '\0';
}
}
string cc = get_header_for_name(hdr, "cc");
if (cc.length() > 0) {
parsed_mail_info->header_cc.pdata = (char *)calloc(1, cc.length() + 1);
if (parsed_mail_info->header_cc.pdata != NULL) {
parsed_mail_info->header_cc.len = cc.length();
memcpy(parsed_mail_info->header_cc.pdata, cc.c_str(), parsed_mail_info->header_cc.len);
parsed_mail_info->header_cc.pdata[parsed_mail_info->header_cc.len] = '\0';
}
}
string bcc = get_header_for_name(hdr, "bcc");
if (bcc.length() > 0) {
parsed_mail_info->header_bcc.pdata = (char *)calloc(1, bcc.length() + 1);
if (parsed_mail_info->header_bcc.pdata != NULL) {
parsed_mail_info->header_bcc.len = bcc.length();
memcpy(parsed_mail_info->header_bcc.pdata, bcc.c_str(), parsed_mail_info->header_bcc.len);
parsed_mail_info->header_bcc.pdata[parsed_mail_info->header_bcc.len] = '\0';
}
}
string spf = get_header_for_name(hdr, "received-spf");
if (spf.length() > 0) {
parsed_mail_info->header_spf.pdata = (char *)calloc(1, spf.length() + 1);
if (parsed_mail_info->header_spf.pdata != NULL) {
parsed_mail_info->header_spf.len = spf.length();
memcpy(parsed_mail_info->header_spf.pdata, spf.c_str(), parsed_mail_info->header_spf.len);
parsed_mail_info->header_spf.pdata[parsed_mail_info->header_spf.len] = '\0';
}
}
string subject = get_header_for_name(hdr, "subject");
if (subject.length() > 0) {
parsed_mail_info->header_subject.pdata = (char *)calloc(1, subject.length() + 1);
if (parsed_mail_info->header_subject.pdata != NULL) {
parsed_mail_info->header_subject.len = subject.length();
memcpy(parsed_mail_info->header_subject.pdata, subject.c_str(), parsed_mail_info->header_subject.len);
parsed_mail_info->header_subject.pdata[parsed_mail_info->header_subject.len] = '\0';
}
}
// get body ----------------------
string body = get_parsed_body(msg);
if (body.length() > 0) {
parsed_mail_info->body.pdata = (char *)calloc(1, body.length() + 1);
if (parsed_mail_info->body.pdata != NULL) {
parsed_mail_info->body.len = body.length();
memcpy(parsed_mail_info->body.pdata, body.c_str(), parsed_mail_info->body.len);
parsed_mail_info->body.pdata[parsed_mail_info->body.len] = '\0';
}
}
return 0;
}