forked from tat/mimetic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexbin.cxx
275 lines (252 loc) · 7.6 KB
/
exbin.cxx
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
/***************************************************************************
copyright : (C) 2002-2008 by Stefano Barbato
email : [email protected]
$Id: exbin.cxx,v 1.5 2008-10-07 11:06:25 tat Exp $
***************************************************************************/
/** \example exbin.cc
* extract a Part based on command line parameters
* more info on:
* exbin -h
*/
#include <iostream>
#include <sstream>
#include <iterator>
#include <fstream>
#include <cassert>
#include <mimetic/mimetic.h>
using namespace std;
using namespace mimetic;
unsigned int g_decoded = 0, g_messages = 0, g_files = 0, g_parts = 0;
void usage()
{
cout << "exbin [params] [in_file...]" << endl;
cout << "\t-t type[/subtype] matches Content-Type" << endl;
cout << "\t-q totaly quiet; exit code = num of decoded entities" << endl;
exit(-1);
}
struct MatchParamRq
{
MatchParamRq()
: m_quiet(0), m_set(0)
{
}
bool operator()(const MimeEntity* pMe) const
{
const Header& h = pMe->header();
// if not set consider a part to be an attach if its
// MIME type is not text/*
if(!m_set)
return h.contentType().type() != "text" &&
h.contentType().type() != "message";
// check for content type match
if(m_type.length() && m_type != h.contentType().type())
return false;
if(m_subtype.length() && m_subtype != h.contentType().subtype())
return false;
return true;
}
void type(const string& s) { m_type = s; ++m_set; }
const istring& type() const { return m_type; }
void subtype(const string& s) { m_subtype = s; ++m_set; }
const istring& subtype() const { return m_subtype; }
void quiet(bool b) { m_quiet = b; }
bool quiet() const { return m_quiet; }
private:
istring m_type, m_subtype;
bool m_quiet, m_set;
} mpr;
void die(bool b, const string& msg)
{
if(b)
{
cerr << "Error: " << msg << endl << endl;
usage();
}
}
string get_filename(const MimeEntity& me)
{
if(me.hasField(ContentDisposition::label))
{
const ContentDisposition& cd = me.header().contentDisposition();
if(cd.param("filename").length())
return cd.param("filename");
} else if (me.hasField(ContentType::label)) {
const ContentType& ct = me.header().contentType();
return string("unnamed_" + ct.type() + "." + ct.subtype());
}
return "unknown_attachment";
}
static bool is_known_mechanism(const string& mechanism)
{
istring m = mechanism;
if(m == "base64" || m == "quoted-printable" || m == "binary" ||
m == "7bit" || m == "8bit")
return true;
return false;
}
void decode_bin(const MimeEntity& me)
{
const ContentTransferEncoding& cte = me.header().contentTransferEncoding();
// const ContentDisposition& cd = me.header().contentDisposition();
if(is_known_mechanism(cte.mechanism()))
{
string filename = get_filename(me);
if(File::exists(filename))
{
int t;
for(t = 0; File::exists(utils::int2str(t)+"-"+filename);t++)
;
filename = utils::int2str(t) + "-" + filename;
}
const ContentType& ct = me.header().contentType();
if(!mpr.quiet())
cout << "\tdecoding " << filename
<< " (" << ct.type() << "/" << ct.subtype() << ")"
<< endl;
ofstream of(filename.c_str());
if(!of.is_open())
{
cerr << "ERR: unable to write to " << filename << endl;
return;
}
ostreambuf_iterator<char> oi(of);
istring enc_algo = cte.mechanism();
if(enc_algo == ContentTransferEncoding::base64)
{
Base64::Decoder b64;
g_decoded++;
decode(me.body().begin(), me.body().end(), b64 ,oi);
} else if (enc_algo == ContentTransferEncoding::quoted_printable) {
QP::Decoder qp;
g_decoded++;
decode(me.body().begin(), me.body().end(), qp, oi);
} else if (enc_algo == ContentTransferEncoding::eightbit ||
enc_algo == ContentTransferEncoding::sevenbit ||
enc_algo == ContentTransferEncoding::binary) {
copy(me.body().begin(), me.body().end(), oi);
} else {
cerr << "ERR: unknown encoding algorithm "
<< enc_algo << endl;
}
}
}
void parsePart(const MimeEntity& me, string& fqn)
{
g_parts++;
if(mpr(&me))
{
if(!mpr.quiet() && fqn.length() > 0)
{
cout << fqn << endl;
fqn.clear();
}
decode_bin(me);
}
MimeEntityList::const_iterator mbit, meit;
mbit = me.body().parts().begin(), meit = me.body().parts().end();
for(; mbit != meit; ++mbit)
parsePart(**mbit, fqn);
}
inline int isnl(char c)
{
return c == '\n' || c == '\r';
}
template<typename Iterator>
void parseMboxFile(Iterator bit, Iterator eit, string& fqn)
{
char prev;
Iterator it = bit;
for(; bit != eit; )
{
for(;;)
{
it = utils::find_bm(it, eit, "From ");
//it = find_n(it, eit, "From ");
prev = *(it-1); // previous char (must be a newline)
if(it == eit || isnl(prev) )
break;
else
++it; // From in the middle of a line
}
g_messages++;
MimeEntity me(bit, it);
parsePart(me, fqn);
if(it == eit)
return;
bit = it;
++it; // skip the current From
}
}
template<typename Iterator>
void parse(Iterator bit, Iterator eit, string& fqn)
{
string sep = "From ";
Iterator it = utils::find_bm(bit, bit + sep.length(), sep);
if(it == bit)
{
parseMboxFile(bit, eit, fqn);
} else {
g_messages++;
MimeEntity me(bit, eit);
parsePart(me, fqn);
}
}
int main(int argc, char** argv)
{
std::ios_base::sync_with_stdio(false);
// handle command line parameters
int p = 1;
while(p < argc)
{
string param = argv[p];
if(param == "-h")
usage();
else if (param == "-q")
mpr.quiet(true);
else if (param == "-t") {
die( ++p == argc, param + " requires an argument");
ContentType ct(argv[p]);
die(mpr.type().length() != 0, "just one -t allowed");
mpr.type(ct.type());
mpr.subtype(ct.subtype());
} else {
// filename list starts here
// first filename: argv[p]
break;
}
++p;
}
string fqn;
if(argc == p)
{ // read from stdin
fqn = "stdin";
string buf;
enum { page_sz = 4096 };
char page[page_sz];
int count;
while((count = cin.rdbuf()->sgetn(page, page_sz)) > 0)
buf.append(page);
parse(buf.begin(), buf.end(), fqn);
g_files++;
} else {
for(int fc = p; fc < argc; ++fc)
{
fqn = argv[fc];
File in(fqn);
if(!in)
{
cerr << "ERR: unable to open file " << argv[fc]
<< endl;
continue;
}
g_files++;
parse(in.begin(), in.end(), fqn);
}
}
if(!mpr.quiet())
cout << g_files << " file(s) analyzed, " <<
g_messages << " message(s) and " <<
g_parts << " entitie(s) parsed, " <<
g_decoded << " attachment(s) extracted." << endl;
return g_decoded;
}