line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::Convert::Mbox::ToEml; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
6918
|
use 5.006; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
46
|
|
4
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
5
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
2237
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
11
|
0
|
|
0
|
|
|
|
my $self = { |
|
|
|
0
|
|
|
|
|
12
|
|
|
|
|
|
|
InFile => shift || undef, |
13
|
|
|
|
|
|
|
OutDir => shift || undef, |
14
|
|
|
|
|
|
|
isError => 0, |
15
|
|
|
|
|
|
|
Error => undef |
16
|
|
|
|
|
|
|
}; |
17
|
0
|
|
|
|
|
|
bless $self, $class; |
18
|
0
|
0
|
0
|
|
|
|
if (!$self->{InFile} || !$self->{OutDir}) { return; } |
|
0
|
|
|
|
|
|
|
19
|
0
|
0
|
|
|
|
|
if (!-e $self->{InFile}) { print "file does not exist!\n"; return; } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
20
|
0
|
0
|
|
|
|
|
if (!-d $self->{OutDir}) { |
21
|
0
|
|
|
|
|
|
print "output directory is not a directory!\n"; |
22
|
0
|
|
|
|
|
|
return; |
23
|
|
|
|
|
|
|
} |
24
|
0
|
0
|
|
|
|
|
if (!-e $self->{OutDir}) { |
25
|
0
|
|
|
|
|
|
print "output directory does not exist!\n"; |
26
|
0
|
|
|
|
|
|
return; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
return $self; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub CreateEML { |
34
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
35
|
0
|
|
0
|
|
|
|
my $infile = shift || $self->{InFile}; |
36
|
0
|
|
0
|
|
|
|
my $outDir = shift || $self->{OutDir}; |
37
|
0
|
0
|
|
|
|
|
if ($infile) { |
38
|
0
|
|
|
|
|
|
$self->{InFile} = $infile; |
39
|
0
|
0
|
|
|
|
|
if (!-e $self->{InFile}) { print "file does not exist!\n"; return; } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
} |
41
|
0
|
0
|
|
|
|
|
if ($outDir) { |
42
|
0
|
|
|
|
|
|
$self->{OutDir} = $outDir; |
43
|
0
|
0
|
|
|
|
|
if (!-d $self->{OutDir}) { |
44
|
0
|
|
|
|
|
|
print "output directory is not a directory!\n"; |
45
|
0
|
|
|
|
|
|
return; |
46
|
|
|
|
|
|
|
} |
47
|
0
|
0
|
|
|
|
|
if (!-e $self->{OutDir}) { |
48
|
0
|
|
|
|
|
|
print "output directory does not exist!\n"; |
49
|
0
|
|
|
|
|
|
return; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
$self->Parse(); |
54
|
0
|
|
|
|
|
|
return 1; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub GetMessageCount { |
58
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
59
|
0
|
0
|
|
|
|
|
if ($self->{MessageCount}) { return $self->{MessageCount}; } |
|
0
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
else { return; } |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub GetMessages { |
64
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
65
|
0
|
|
0
|
|
|
|
my $infile = shift || $self->{InFile}; |
66
|
0
|
|
|
|
|
|
my @subjectList = (); |
67
|
0
|
|
|
|
|
|
my $x0d = chr(hex('0x0d')); |
68
|
0
|
0
|
|
|
|
|
if (open(FH, $infile)) { |
69
|
0
|
|
|
|
|
|
my $count = 0; |
70
|
0
|
|
|
|
|
|
while () { |
71
|
0
|
0
|
|
|
|
|
if ($_ =~ /^subject:/i) { |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my $tmp = (split(/^subject:/i, $_))[1]; |
74
|
0
|
|
|
|
|
|
chomp $tmp; |
75
|
0
|
|
|
|
|
|
$tmp =~ s/^\s+//; |
76
|
0
|
|
|
|
|
|
$tmp =~ s/$x0d$//i; |
77
|
0
|
|
|
|
|
|
push(@subjectList, $tmp); |
78
|
0
|
|
|
|
|
|
$count++; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} |
82
|
0
|
|
|
|
|
|
$self->{MessageCount} = $count; |
83
|
0
|
|
|
|
|
|
return @subjectList; |
84
|
|
|
|
|
|
|
} else { |
85
|
0
|
|
|
|
|
|
$self->{Error} = "No messages found!\n"; |
86
|
0
|
|
|
|
|
|
return; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub SetFileAndDir { |
91
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
92
|
0
|
|
|
|
|
|
$self->{InFile} = shift; |
93
|
0
|
|
|
|
|
|
$self->{OutDir} = shift; |
94
|
0
|
0
|
|
|
|
|
if (!-e $self->{InFile}) { print "file does not exist!\n"; return; } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
if (!-d $self->{OutDir}) { |
96
|
0
|
|
|
|
|
|
print "output directory is not a directory!\n"; |
97
|
0
|
|
|
|
|
|
return; |
98
|
|
|
|
|
|
|
} |
99
|
0
|
0
|
|
|
|
|
if (!-e $self->{OutDir}) { |
100
|
0
|
|
|
|
|
|
print "output directory does not exist!\n"; |
101
|
0
|
|
|
|
|
|
return; |
102
|
|
|
|
|
|
|
} |
103
|
0
|
|
|
|
|
|
return 1; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub FindMessage { |
107
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
108
|
0
|
|
|
|
|
|
my $what = shift; |
109
|
0
|
|
0
|
|
|
|
my $infile = shift || $self->{InFile}; |
110
|
0
|
|
|
|
|
|
my $count = 0; |
111
|
0
|
|
|
|
|
|
my $scount = 0; |
112
|
0
|
|
|
|
|
|
my %h; |
113
|
0
|
|
|
|
|
|
my @subjectlist = $self->GetMessages($infile); |
114
|
0
|
0
|
|
|
|
|
if (!@subjectlist) { return; } |
|
0
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
foreach (@subjectlist) { |
117
|
0
|
0
|
|
|
|
|
if (lc($_) =~ /$what/i) { |
118
|
0
|
|
|
|
|
|
$h{$scount} = { MSG => $_, MSGNUM => $count }; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
#$h{MSGNUM}=; |
121
|
0
|
|
|
|
|
|
$scount++; |
122
|
|
|
|
|
|
|
} |
123
|
0
|
|
|
|
|
|
$count++; |
124
|
|
|
|
|
|
|
} |
125
|
0
|
0
|
|
|
|
|
if (%h) { return %h; } |
|
0
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
else { $self->{Error} = "Message(s) not found!\n"; return; } |
|
0
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub Parse { |
130
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
131
|
0
|
|
|
|
|
|
my @currmail = (); |
132
|
0
|
|
|
|
|
|
my $counter = 0; |
133
|
0
|
|
|
|
|
|
my $mailcounter = 1; |
134
|
0
|
|
|
|
|
|
open(FH, $self->{InFile}); |
135
|
0
|
|
|
|
|
|
binmode FH; |
136
|
0
|
|
|
|
|
|
while () { |
137
|
0
|
0
|
|
|
|
|
if ($_ !~ /^From -/) { |
138
|
0
|
|
|
|
|
|
$currmail[$counter] = $_; |
139
|
0
|
|
|
|
|
|
$counter++; |
140
|
|
|
|
|
|
|
} else { |
141
|
|
|
|
|
|
|
|
142
|
0
|
0
|
|
|
|
|
if (@currmail) { |
143
|
0
|
|
|
|
|
|
$self->WriteToFile($mailcounter, \@currmail); |
144
|
0
|
|
|
|
|
|
$counter = 0; |
145
|
0
|
|
|
|
|
|
$mailcounter++; |
146
|
0
|
|
|
|
|
|
undef @currmail; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
0
|
0
|
|
|
|
|
$self->WriteToFile($mailcounter, \@currmail) if @currmail; |
151
|
0
|
|
|
|
|
|
close FH; |
152
|
0
|
|
|
|
|
|
return 1; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# The subject will be used to generate the file name |
156
|
|
|
|
|
|
|
sub WriteToFile { |
157
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
158
|
0
|
|
|
|
|
|
my $mailcount = shift; |
159
|
0
|
|
|
|
|
|
my $tmp = shift; |
160
|
0
|
|
|
|
|
|
my @mail = @{$tmp}; |
|
0
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
my $subject; |
162
|
0
|
|
|
|
|
|
my $x0d = chr(hex('0x0d')); |
163
|
0
|
|
|
|
|
|
my @temp = grep(/subject:/i, @mail); |
164
|
0
|
0
|
|
|
|
|
if (@temp != 0) { |
165
|
0
|
|
|
|
|
|
$subject = (split(/subject:/i, $temp[0]))[1]; |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
chomp $subject; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# remove characters which can not be used in a file name |
170
|
0
|
|
|
|
|
|
$subject =~ s/^\s+//; |
171
|
0
|
|
|
|
|
|
$subject =~ s/\"//g; |
172
|
0
|
|
|
|
|
|
$subject =~ s/\// /g; |
173
|
0
|
|
|
|
|
|
$subject =~ s/\/\//_/g; |
174
|
0
|
|
|
|
|
|
$subject =~ s/\\/_/g; |
175
|
0
|
|
|
|
|
|
$subject =~ s/:/_/g; |
176
|
0
|
|
|
|
|
|
$subject =~ s/'//g; |
177
|
0
|
|
|
|
|
|
$subject =~ s/\?//g; |
178
|
0
|
|
|
|
|
|
$subject =~ s/\/g; |
179
|
0
|
|
|
|
|
|
$subject =~ s/\>//g; |
180
|
0
|
|
|
|
|
|
$subject =~ s/\|//g; |
181
|
0
|
|
|
|
|
|
$subject =~ s/\*//g; |
182
|
0
|
|
|
|
|
|
$subject =~ s/$x0d$//i; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
} else { |
185
|
0
|
|
|
|
|
|
$subject = "No Subject"; |
186
|
|
|
|
|
|
|
} |
187
|
0
|
|
|
|
|
|
@mail = $self->checkLines(\@mail); |
188
|
0
|
|
|
|
|
|
my $file |
189
|
|
|
|
|
|
|
= $self->{OutDir} . "/" |
190
|
|
|
|
|
|
|
. $subject . "_" |
191
|
|
|
|
|
|
|
. $mailcount . "_" |
192
|
|
|
|
|
|
|
. GetCurrentTime() . ".eml"; |
193
|
0
|
|
|
|
|
|
print "writeing | $subject | to file\n"; |
194
|
0
|
0
|
|
|
|
|
if (open(FHOUT, ">$file")) { |
195
|
0
|
|
|
|
|
|
binmode FHOUT; |
196
|
0
|
|
|
|
|
|
print FHOUT @mail; |
197
|
0
|
|
|
|
|
|
close FHOUT; |
198
|
0
|
|
|
|
|
|
return 1; |
199
|
|
|
|
|
|
|
} else { |
200
|
0
|
|
|
|
|
|
print "can not open $file for writeing! $!\n"; |
201
|
0
|
|
|
|
|
|
return; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
# function to check if there are EOF characters and if the from: is correct |
206
|
|
|
|
|
|
|
# EOF characters are removed. |
207
|
|
|
|
|
|
|
sub checkLines { |
208
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
209
|
0
|
|
|
|
|
|
my $tmp = shift; |
210
|
0
|
|
|
|
|
|
my @newmail = (); |
211
|
0
|
|
|
|
|
|
my $count = 0; |
212
|
0
|
|
|
|
|
|
my @mail = @{$tmp}; |
|
0
|
|
|
|
|
|
|
213
|
0
|
|
|
|
|
|
my $attachment = 0; |
214
|
0
|
|
|
|
|
|
my $attach = "Content-Type: application"; |
215
|
0
|
|
|
|
|
|
my $attach1 = "Content-Disposition: attachment"; |
216
|
0
|
|
|
|
|
|
my $EOF = chr(hex('0x1A')); |
217
|
0
|
|
|
|
|
|
my $ToVal; |
218
|
0
|
|
|
|
|
|
my @TVal = grep /^To:/i, @mail; |
219
|
|
|
|
|
|
|
|
220
|
0
|
0
|
|
|
|
|
if ($TVal[0]) { |
221
|
0
|
|
|
|
|
|
$ToVal = (split(/:/, $TVal[0]))[1]; |
222
|
0
|
|
|
|
|
|
$ToVal =~ s/^\s+//; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
0
|
|
|
|
|
|
foreach (@mail) { |
226
|
0
|
0
|
|
|
|
|
if ($_ =~ /^from:/i) { |
227
|
0
|
|
|
|
|
|
$tmp = (split(/from:/i, $_))[1] |
228
|
|
|
|
|
|
|
; # correct the From: line, insert the mail address in To: |
229
|
0
|
0
|
|
|
|
|
if (length($tmp) <= 2) { |
230
|
0
|
0
|
|
|
|
|
$_ = "From: " . $ToVal if $ToVal; |
231
|
|
|
|
|
|
|
} |
232
|
0
|
0
|
0
|
|
|
|
if ($_ =~ /^>from/i || $_ =~ /^>from:/i) # correct the From: line |
233
|
|
|
|
|
|
|
{ |
234
|
0
|
|
|
|
|
|
$_ = substr($_, 1, length($_) - 1); |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
} |
237
|
0
|
0
|
0
|
|
|
|
if ($_ =~ /^$attach/ || $_ =~ /^$attach1/) { $attachment = 1; } |
|
0
|
|
|
|
|
|
|
238
|
0
|
0
|
|
|
|
|
$_ =~ s/$EOF//g if $attachment == 1; # removes EOF's in the line |
239
|
0
|
|
|
|
|
|
push(@newmail, $_); |
240
|
0
|
|
|
|
|
|
$count++; |
241
|
|
|
|
|
|
|
} |
242
|
0
|
|
|
|
|
|
return @newmail; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
sub GetCurrentTime { |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
#my $self=shift; |
248
|
0
|
|
|
0
|
0
|
|
return time; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
1; |
252
|
|
|
|
|
|
|
__END__ |