forked from sg3des/emlExtractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemlExtractor.pl
executable file
·392 lines (330 loc) · 8.9 KB
/
emlExtractor.pl
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/perl
use MIME::Base64;
use IO::File;
use Fcntl qw(:flock);
use Encode;
use Encode qw(:all);
use Encode::Byte;
use Encode::CN;
use Encode::JP;
use Encode::KR;
use Encode::TW;
use Term::ANSIColor;
use MIME::QuotedPrint::Perl;
use File::Basename;
no warnings 'layer';
my $folder;
my $contentNameByEml = 0;
my $emlFilename;
my $overwrite = 0;
my @childs;
$help = "
USAGE:
/path/to/emlExtractor [options] filename.eml [[options] filename.eml [options] filename.eml ...]
You can unpack multiple EML files in one line, just write them separated by a space.
For each file eml, will be created directory kind filename_eml with same path, where will be unpacked files.
Options:
-o --output set output directory*
-c set filename for html and text message as the same of filename input eml
-v --overwrite overwrite exists files
*You may set output directory for each file, or one at all, or any other way.
eml.header - information header of email message
*.text - text message in plain text format
*.html - text message in html format
";
if(!@ARGV){print $help;}
if(@ARGV[0]=~/--help|-h|\?/){print $help; exit;}
my $arguments = 0;
foreach $filePath(@ARGV){
# print " - ".$filePath." -";
if($filePath=~/--help|-h|\?/){print $help; next;}
if($filePath=~/-o|--output/) {$folder = @ARGV[$arguments+1]; mkdir $folder; next;}
if($filePath=~/-c/){$contentNameByEml = 1; next;}
if($filePath=~/-v|--overwrite/){$overwrite = 1; next;}
if($filePath){openEml($filePath);}
$arguments++;
}
sub openEml
{
my($path) = @_;
if(!$folder){
$folder = $path;
$folder =~ s/\.eml/_eml/i;
mkdir $folder;
}
$emlFilename = basename($path);
$emlFilename =~ s/\..*//i;
print color 'bold green';
print "\n input: $path\n";
print " output: $folder\n\n";
print color 'reset';
my $fh = new IO::File "< $path" or die "Cannot open $path : $!";
flock($fh,LOCK_SH);
binmode($fh);
my $buf;
my $buflen = (stat($fh))[7];
while (read($fh,$buf,$buflen)) {
$boundary = getBoundary($buf);
if(!$boundary){$boundary = 'Content-Type:';}
explode($buf,$boundary);
}
print "\n";
}
sub explode
{
my($buf, $boundary) = @_;
my $step=0;
print $boundary."\n";
foreach $part (split /$boundary/i, $buf){
# print substr($part,0,10000)."\n";
$step++;
if($step==1){header($part); next;}
if($part =~ m/(text\/plain)/mgi){mailText($part,'text'); next;}
if($part =~ m/(text\/html)/mgi){mailText($part,'html'); next;}
if($part =~ m/(filename)/mgi){attachment($part); next;}
if($part =~ m/(attachment)/mgi){attachment($part); next;}
if($part =~ m/(description)/mgi){attachment($part); next;}
if($part =~ m/(name)/mgi){attachment($part); next;}
}
}
sub header
{
my($part) = @_;
# print($part);
$header .= "From: ".headerField($part,'from')."\n";
$header .= "To: ".headerField($part,'to')."\n";
$header .= "CC: ".headerField($part,'cc')."\n";
$header .= "Subject: ".headerField($part,'subject')."\n";
$header .= "Date: ".headerField($part,'date')."\n";
saveFile('eml.header',$header);
return;
}
sub mailText
{
my($part,$type) = @_;
# print $part."\n";
my @part = cropContent($part);
my @content = content(@part[0]);
my $part = absoluteDecode(@part[1],@content);
if($type eq 'html'){$part =~ s/@content[0]/utf-8/gi;}
my $filename = $type.".".$type;
if($contentNameByEml){$filename = $emlFilename.".".$type;}
saveFile($filename,$part);
}
sub attachment
{
my($part) = @_;
my @part = cropContent($part);
my @content = content(@part[0]);
if(@content){$text = absoluteDecode(@part[1],@content);}
else{$text = @part[1];}
print " > ".@content[2];
saveFile(@content[2],$text);
return;
}
sub stringCollect
{
my($part2,$type) = @_;
@part = split /[\r\n]/g, $part2;
$field='';
_FOR: for (my $num = 0; $num <= $#part; $num++) {
if($str = (@part[$num] =~ m/$type/mi)[0]){
$field .= string_decode($str);
if(@part[$num] =~ m/;$/m){return $field;}
_WHILE: while(){
$num++;
if(@part[$num]){
if(@part[$num] =~ m/^\t|^\s/mi){
$field .= string_decode(@part[$num]);
}else{last _WHILE;}
}
if($num>=$#part){last _WHILE;}
}
}
}
return $field;
}
sub content
{
my($part) = @_;
my $charset = stringCollect($part,'charset="?([a-zA-Z0-9\-]*)"?');
my $encoding = stringCollect($part,'encoding:\s?(.*)');
my $filename = stringCollect($part,'filename="?([\w\s\.\,\=\?\-\]\[\`\(\)\'\%\@]*)"?');
if(!$filename){$filename = stringCollect($part,'name="?([\w\s\.\,\=\?\-\]\[\`\(\)\'\%\@]*)"?');}
if($filename !~ m/.\.[\w]/i){
# print $filename;
$extension = stringCollect($part, 'content-type:\s?[\w]*\/([\w]*);?\s?');
$filename = $filename.'.'.$extension;
# print $filename;
}
return ($charset,$encoding,$filename);
}
sub absoluteDecode
{
my($part,@content) = @_;
my $charset = @content[0];
my $encoding = @content[1];
if($encoding=~ m/base64/i){$part=decode_base64($part);}
if($encoding=~ m/quoted-printable/i){$part=decode_qp($part);}
if($charset && $charset!~m/utf/i){$part=decode($charset,$part);}
return $part;
}
sub getBoundary
{
my($buf) = @_;
@boundary = ($buf =~ m/[\t\s]+boundary="(.*?)"/gi);
foreach $boundary(@boundary){
$boundary="--".quotemeta($boundary)."";
}
$boundary = join '|',@boundary;
if(!$boundary){return;}
return $boundary;
}
sub cropContent
{
my($part) = @_;
if(index($part,"\r")+1){$delimter="\r"; $plus=1;}else{$delimter="\n";$plus=0;}
my $num=0;
my $cropLength=0;
my $startSchDelimter=0;
_CROP:
foreach my $line (split $delimter, $part){
$num++;
$length = length($line)+1;
$cropLength += $length;
if($line =~ /charset/i){$startSchDelimter = 1;}
if($line =~ /encoding/i){$startSchDelimter = 1;}
if($startSchDelimter>0 && $length <= 2){
$cropLine=$num;
last _CROP;
}
}
my $content = substr($part,0,$cropLength+$plus); #плюс нужен изза разновидности переноса строк
substr($part,0,$cropLength+$plus)='';
return ($content,$part);
}
sub string_decode
{
my ($string) = @_;
$string =~ s/from:|to:|subject:|cc://gi;
$email = '';
@string = split /[\r\n]/,$string;
foreach $line (@string){
$str_encoding='';$str_charset='';
if($line =~ m/@/){
$email = ($line =~ m/.*(<.*>).*/gi)[0];
$line =~ s/$email//;
}
if($line =~ m/\?/){
@line = ($line =~ m/.*\=\?(.*)\?(.)\?(.*)\?\=.*/gi);
$str_charset = @line[0];
$str_encoding = @line[1];
$line = @line[2];
}
if($str_encoding eq 'B'){$line=decode_base64($line);}
if($str_encoding eq 'Q'){$line=decode_qp($line);}
if($str_charset){$line = decode($str_charset,$line);}
if($email){$line = $line." ".$email;}
$line =~ s/^\s*|\s*$|\r*|\n*|\t//g;
# print " > ".$line."\n";
}
return join "",@string;
}
sub headerField
{
my($part,$type)=@_;
@part = split /[\r\n]/g, $part;
$field='';
for (my $num = 0; $num < $#part; $num++) {
if(@part[$num] =~ /^$type:/mgi){
$field .= string_decode(@part[$num])."\n";
# print $field."\n";
# next;
_WHILE: while(){
$num++;
if(@part[$num]){
if(@part[$num] =~ m/^\t|^\s/mgi){
$field .= string_decode(@part[$num])."\n";
}else{last _WHILE;}
}
if($num>=$#part){last _WHILE;}
}
}
}
$field =~ s/\n/;/g;
$field =~ s/;$//g;
return $field;
}
sub cropGarbage
{
my($part,$delimter) = @_;
my $num=0;
my $cropLength=0;
my $startSchDelimter=0;
_CROP:
foreach my $line (split $delimter, $part){
$num++;
$length = length($line)+1;
$cropLength += $length;
if($line =~ /charset/){$startSchDelimter = 1;}
if($startSchDelimter>0 && $length <= 2){
$cropLine=$num;
last _CROP;
}
}
if($delimter eq "\r"){$plus = 1;}else{$plus=0;} #плюс нужен изза разновидности переноса строк
substr($part,0,$cropLength+$plus)='';
return $part;
}
my $stepOverwriteFilename;
my @files;
sub saveFile{
my ($filename,$document) = @_;
my $filename = lc $filename;
# push(@files,$filename);
my $suffix = 0;
for (my $var = 0; $var < scalar @files; $var++) {
if($files[$var] eq $filename){
$suffix++;
}
}
push(@files,$filename);
if($suffix){
$filename = "(".$suffix.")".$filename;
}
# if(!$files{$filename}){
# $files{$filename} = 0;
# }else{
# print " > ".$filename;
# $files{$filename}++;
# }
# print " > ";
# print $filename." ";
# print scalar @files;
# print "\n";
# my $suffix = '';
# if(grep(/^$filename/, @childs)){
# if(!$suffix){$suffix = 1;}
# else{$suffix++;}
# }
# $filename = $suffix.$filename;
if(!$overwrite){$filename = getFilename($filename);}
my $sfh = new IO::File ">"."$folder/$filename" or die "Cannot open $filename : $!";
flock($sfh,LOCK_EX);
binmode($sfh);
print $sfh $document or die "Write to $filename failed: $!";
close($sfh) or die "Error closing $filename : $!";
print $filename."\n";
push(@childs,$filename);
return '$filename OK';
}
sub getFilename{
my ($filename) = @_;
$stepFilename = '';
while (-e "$folder/$stepFilename$filename") {
if($stepFilename==''){$stepFilename=0;}
$stepFilename++;
}
return $stepFilename.$filename;
}
print "\n";