blib/lib/Email/Outlook/Message/Base.pm

filter: Statementstmt 79.2% 134 / 169 Branchbran 55.5% 30 / 54 Conditioncond 60.0% 3 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 92.3% 24 / 26 Podpod 100% 5 / 5 totaltotal 75.5% 198 / 262 CC 33 SCAR 18.3
CC
33
Cov
73%
_log_property
20.0
_decode_mapi_property
14.9
_warn_about_unknown_file
12.0
CRAP 6.2
« blib/lib/Email/Outlook/Message/Attachment.pm ↑ Summary blib/lib/Email/Outlook/Message.pm »
1 package Email::Outlook::Message::Base;
2 =head1 NAME
3  
4 Email::Outlook::Message::Base - Base parser for .msg files.
5  
6 =head1 DESCRIPTION
7  
8 This is an internal module of Email::Outlook::Message.
9  
10 =head1 METHODS
11  
12 =over 8
13  
14 =item B<new($pps, $verbose)>
15  
16 =item B<get_mapi_property($code)>
17  
18 =item B<set_mapi_property($code, $data)>
19  
20 =item B<mapi_property_names()>
21  
22 =item B<property($name)>
23  
24 =back
25  
26 =head1 AUTHOR
27  
28 Matijs van Zuijlen, C<matijs@matijs.net>
29  
30 =head1 COPYRIGHT AND LICENSE
31  
32 Copyright 2002--2014 by Matijs van Zuijlen
33  
34 This module is free software; you can redistribute it and/or modify
35 it under the same terms as Perl itself.
36  
37 =cut
38 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 8 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 8 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 8 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 8 ▶use OLE::Storage_Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 8 my $DIR_TYPE = 1;
47 8 my $FILE_TYPE = 2;
48  
49 # Variable encodings
50 8 my $ENCODING_UNICODE = '001F';
51 8 my $ENCODING_ASCII = '001E';
52 8 my $ENCODING_BINARY = '0102';
53 8 my $ENCODING_DIRECTORY = '000D';
54  
55 8 our $VARIABLE_ENCODINGS = {
56 '000D' => 'Directory',
57 '001F' => 'Unicode',
58 '001E' => 'Ascii?',
59 '0102' => 'Binary',
60 };
61  
62 # Fixed encodings
63 8 my $ENCODING_INTEGER16 = '0002';
64 8 my $ENCODING_INTEGER32 = '0003';
65 8 my $ENCODING_BOOLEAN = '000B';
66 8 my $ENCODING_DATE = '0040';
67  
68 #
69 # Descriptions partially based on mapitags.h
70 #
71 8 our $skipproperties = {
72 # Envelope properties
73 '0002' => "Alternate Recipient Allowed",
74 '000B' => "Conversation Key",
75 '0017' => "Importance", #TODO: Use this.
76 '001A' => "Message Class",
77 '0023' => "Originator Delivery Report Requested",
78 '0026' => "Priority", #TODO: Use this.
79 '0029' => "Read Receipt Requested", #TODO: Use this.
80 '0036' => "Sensitivity", # As assessed by the Sender
81 '003B' => "Sent Representing Search Key",
82 '003D' => "Subject Prefix",
83 '003F' => "Received By EntryId",
84 '0040' => "Received By Name",
85 # TODO: These two fields are part of the Sender field.
86 '0041' => "Sent Representing EntryId",
87 '0042' => "Sent Representing Name",
88 '0043' => "Received Representing EntryId",
89 '0044' => "Received Representing Name",
90 '0046' => "Read Receipt EntryId",
91 '0051' => "Received By Search Key",
92 '0052' => "Received Representing Search Key",
93 '0053' => "Read Receipt Search Key",
94 # TODO: These two fields are part of the Sender field.
95 '0064' => "Sent Representing Address Type",
96 '0065' => "Sent Representing Email Address",
97 '0070' => "Conversation Topic",
98 '0071' => "Conversation Index",
99 '0075' => "Received By Address Type",
100 '0076' => "Received By Email Address",
101 '0077' => "Received Representing Address Type",
102 '0078' => "Received Representing Email Address",
103 '007F' => "TNEF Correlation Key",
104 # Recipient properties
105 '0C15' => "Recipient Type",
106 # Sender properties
107 '0C19' => "Sender Entry Id",
108 '0C1D' => "Sender Search Key",
109 '0C1E' => "Sender Address Type",
110 # Non-transmittable properties
111 '0E02' => "Display Bcc",
112 '0E06' => "Message Delivery Time",
113 '0E07' => "Message Flags",
114 '0E0A' => "Sent Mail EntryId",
115 '0E0F' => "Responsibility",
116 '0E1B' => "Has Attachments",
117 '0E1D' => "Normalized Subject",
118 '0E1F' => "RTF In Sync",
119 '0E20' => "Attachment Size",
120 '0E21' => "Attachment Number",
121 '0E23' => "Internet Article Number",
122 '0E27' => "Security Descriptor",
123 '0E79' => "Trust Sender",
124 '0FF4' => "Access",
125 '0FF6' => "Instance Key",
126 '0FF7' => "Access Level",
127 '0FF9' => "Record Key",
128 '0FFE' => "Object Type",
129 '0FFF' => "EntryId",
130 # Content properties
131 '1006' => "RTF Sync Body CRC",
132 '1007' => "RTF Sync Body Count",
133 '1008' => "RTF Sync Body Tag",
134 '1010' => "RTF Sync Prefix Count",
135 '1011' => "RTF Sync Trailing Count",
136 '1046' => "Original Message ID",
137 '1080' => "Icon Index",
138 '1081' => "Last Verb Executed",
139 '1082' => "Last Verb Execution Time",
140 '10F3' => "URL Component Name",
141 '10F4' => "Attribute Hidden",
142 '10F5' => "Attribute System",
143 '10F6' => "Attribute Read Only",
144 # 'Common property'
145 '3000' => "Row Id",
146 '3001' => "Display Name",
147 '3002' => "Address Type",
148 '3007' => "Creation Time",
149 '3008' => "Last Modification Time",
150 '300B' => "Search Key",
151 # Message store info
152 '340D' => "Store Support Mask",
153 '3414' => "Message Store Provider",
154 # Attachment properties
155 '3702' => "Attachment Encoding",
156 '3703' => "Attachment Extension",
157 # TODO: Use the following to distinguish between nested msg and other OLE
158 # stores.
159 '3705' => "Attachment Method",
160 '3709' => "Attachment Rendering", # Icon as WMF
161 '370A' => "Tag identifying application that supplied the attachment",
162 '370B' => "Attachment Rendering Position",
163 '3713' => "Attachment Content Location", #TODO: Use this?
164 # 3900 -- 39FF: 'Address book'
165 '3900' => "Address Book Display Type",
166 '39FF' => "Address Book 7 Bit Display Name",
167 # Mail User Object
168 '3A00' => "Account",
169 '3A20' => "Transmittable Display Name",
170 '3A40' => "Send Rich Info",
171 '3FDE' => "Internet Code Page", # TODO: Perhaps use this.
172 # 'Display table properties'
173 '3FF8' => "Creator Name",
174 '3FF9' => "Creator EntryId",
175 '3FFA' => "Last Modifier Name",
176 '3FFB' => "Last Modifier EntryId",
177 '3FFD' => "Message Code Page",
178 # 'Transport-defined envelope property'
179 '4019' => "Sender Flags",
180 '401A' => "Sent Representing Flags",
181 '401B' => "Received By Flags",
182 '401C' => "Received Representing Flags",
183 '4029' => "Read Receipt Address Type",
184 '402A' => "Read Receipt Email Address",
185 '402B' => "Read Receipt Name",
186 '5FF6' => "Recipient Display Name",
187 '5FF7' => "Recipient EntryId",
188 '5FFD' => "Recipient Flags",
189 '5FFF' => "Recipient Track Status",
190 # 'Provider-defined internal non-transmittable property'
191 '664A' => "Has Named Properties",
192 '6740' => "Sent Mail Server EntryId",
193 };
194  
195 sub new {
196 7 ▶ my ($class, $pps, $verbose) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
197 7 my $self = bless {
198 _pps_file_entries => {},
199 _pps => $pps
200 }, $class;
201 7 $self->_set_verbosity($verbose);
202 7 $self->_process_pps($pps);
203 7 return $self;
204 }
205  
206 sub mapi_property_names {
207 12 ▶ my $self = shift;
Subroutine mapi_property_names: called CC=1 SCAR=0.0
Pod documented
208 12 return keys %{$self->{_pps_file_entries}};
209 }
210  
211 sub get_mapi_property {
212 89 ▶ my ($self, $code) = @_;
Subroutine get_mapi_property: called CC=1 SCAR=0.0
Pod documented
213 89 return $self->{_pps_file_entries}->{$code};
214 }
215  
216 sub set_mapi_property {
217 303 ▶ my ($self, $code, $data) = @_;
Subroutine set_mapi_property: called CC=1 SCAR=0.0
Pod documented
218 303 $self->{_pps_file_entries}->{$code} = $data;
219 303 return;
220 }
221  
222 sub property {
223 25 ▶ my ($self, $name) = @_;
Subroutine property: called CC=4 SCAR=13.9
Pod documented
224 25 my $map = $self->_property_map;
225 # TODO: Prepare reverse map instead of doing dumb lookup.
226 25 foreach my $code (keys %{$map}) {
227 91 my $key = $map->{$code};
228 91 ▶ next unless $key eq $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $key eq $name 66 25 91
229 25 my $prop = $self->get_mapi_property($code);
230 25 ▶ if ($prop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prop) { } 20 5 25
231 20 my ($encoding, $data) = @{$prop};
232 20 return $self->_decode_mapi_property($encoding, $data);
233 } else {
234 5 return;
235 }
236 }
237 0 return;
238 }
239  
240 sub _decode_mapi_property {
241 83 ▶ my ($self, $encoding, $data) = @_;
Subroutine _decode_mapi_property: called CC=9 SCAR=27.0
242  
243 83 ▶ if ($encoding eq $ENCODING_ASCII or $encoding eq $ENCODING_UNICODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding eq $ENCODING_ASCII or $encoding eq $ENCODING_UNICODE) 69 14 83
Condition100%3/3
$encoding eq $ENCODING_ASCII or $encoding eq $ENCODING_UNICODE
l!l&&r!l&&!r
472214
MC/DC100%2/2
$encoding eq $ENCODING_ASCII or $encoding eq $ENCODING_UNICODE
$encoding eq $ENCODING_ASCII $encoding eq $ENCODING_UNICODE
Truth table100%3/3
A or B
A= $encoding eq $ENCODING_ASCIIB= $encoding eq $ENCODING_UNICODE
ABresult
1X1
011
000
244 69 ▶ if ($encoding eq $ENCODING_UNICODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding eq $ENCODING_UNICODE) 22 47 69
245 22 $data = decode("UTF-16LE", $data);
246 }
247 69 $data =~ s/ \000 $ //sgx;
248 69 return $data;
249 }
250  
251 14 ▶ if ($encoding eq $ENCODING_BINARY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding eq $ENCODING_BINARY) 7 7 14
252 7 return $data;
253 }
254  
255 7 ▶ if ($encoding eq $ENCODING_DATE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding eq $ENCODING_DATE) 7 0 7
256 7 my @a = OLE::Storage_Lite::OLEDate2Local $data;
257 7 return $self->_format_date(\@a);
258 }
259  
260 0 ▶ if ($encoding eq $ENCODING_INTEGER16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding eq $ENCODING_INTEGER16) 0 0 0
261 0 return unpack("v", substr($data, 0, 2));
262 }
263  
264 0 ▶ if ($encoding eq $ENCODING_INTEGER32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding eq $ENCODING_INTEGER32) 0 0 0
265 0 return unpack("V", substr($data, 0, 4));
266 }
267  
268 0 ▶ if ($encoding eq $ENCODING_BOOLEAN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding eq $ENCODING_BOOLEAN) 0 0 0
269 0 return unpack("C", substr($data, 0, 1));
270 }
271  
272 0 warn "Unhandled encoding $encoding\n";
273 0 return $data;
274 }
275  
276 sub _process_pps {
277 12 ▶ my ($self, $pps) = @_;
Subroutine _process_pps: called CC=4 SCAR=14.0
278 12 foreach my $child (@{$pps->{Child}}) {
279 200 ▶ if ($child->{Type} == $DIR_TYPE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child->{'Type'} == $DIR_TYPE) { } 12 188 200
elsif ($child->{'Type'} == $FILE_TYPE) { } 188 0 188
280 12 $self->_process_subdirectory($child);
281 } elsif ($child->{Type} == $FILE_TYPE) {
282 188 $self->_process_pps_file_entry($child);
283 } else {
284 0 carp "Unknown entry type: $child->{Type}";
285 }
286 }
287 12 $self->_check_pps_file_entries($self->_property_map);
288 12 return;
289 }
290  
291 sub _get_pps_name {
292 205 ▶ my ($self, $pps) = @_;
Subroutine _get_pps_name: called CC=1 SCAR=0.0
293 205 my $name = OLE::Storage_Lite::Ucs2Asc($pps->{Name});
294 205 $name =~ s/ \W / /gx;
295 205 return $name;
296 }
297  
298 sub _parse_item_name {
299 188 ▶ my ($self, $name) = @_;
Subroutine _parse_item_name: called CC=2 SCAR=6.9
300  
301 188 ▶ if ($name =~ / ^ __substg1 [ ] 0_ (....) (....) $ /x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ / ^ __substg1 [ ] 0_ (....) (....) $ /x) { } 176 12 188
302 176 my ($property, $encoding) = ($1, $2);
303 176 return ($property, $encoding);
304 } else {
305 12 return (undef, undef);
306 }
307 }
308  
309 sub _warn_about_unknown_directory {
310 5 ▶ my ($self, $pps) = @_;
Subroutine _warn_about_unknown_directory: called CC=3 SCAR=11.8
311  
312 5 my $name = $self->_get_pps_name($pps);
313 5 ▶ if ($name eq '__nameid_version1 0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq '__nameid_version1 0') { } 5 0 5
314 # TODO: Use this data to access so-called named properties.
315 $self->{VERBOSE}
316 5 ▶ and warn "Skipping DIR entry $name (Introductory stuff)\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"VERBOSE"} and warn "Skipping DIR entry $name (Introductory stuff)\n" 0 5 5
317 } else {
318 0 warn "Unknown DIR entry $name\n";
319 }
320 5 return;
321 }
322  
323 sub _warn_about_unknown_file {
324 0 ▶ my ($self, $pps) = @_;
Subroutine _warn_about_unknown_file: not called CC=3 SCAR=24.8
325  
326 0 my $name = $self->_get_pps_name($pps);
327  
328 0 ▶ if ($name eq 'Olk10SideProps_0001') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq 'Olk10SideProps_0001') { } 0 0 0
329 $self->{VERBOSE}
330 0 ▶ and warn "Skipping FILE entry $name (Properties)\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"VERBOSE"} and warn "Skipping FILE entry $name (Properties)\n" 0 0 0
331 } else {
332 0 warn "Unknown FILE entry $name\n";
333 }
334 0 return;
335 }
336  
337 #
338 # Generic processor for a file entry: Inserts the entry's data into the
339 # $self's mapi property list.
340 #
341 sub _process_pps_file_entry {
342 188 ▶ my ($self, $pps) = @_;
Subroutine _process_pps_file_entry: called CC=3 SCAR=11.1
343 188 my $name = $self->_get_pps_name($pps);
344 188 my ($property, $encoding) = $self->_parse_item_name($name);
345  
346 188 ▶ if (defined $property) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $property) { } 176 12 188
elsif ($name eq '__properties_version1 0') { } 12 0 12
347 176 $self->set_mapi_property($property, [$encoding, $pps->{Data}]);
348 } elsif ($name eq '__properties_version1 0') {
349 12 $self->_process_property_stream ($pps->{Data});
350 } else {
351 0 $self->_warn_about_unknown_file($pps);
352 }
353 188 return;
354 }
355  
356 sub _process_property_stream {
357 12 ▶ my ($self, $data) = @_;
Subroutine _process_property_stream: called CC=3 SCAR=11.0
358 12 my ($n, $len) = ($self->_property_stream_header_length, length $data) ;
359  
360 12 ▶ while ($n + 16 <= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n + 16 <= $len) 303 12 315
361 303 my @f = unpack "v4", substr $data, $n, 8;
362  
363 303 my $encoding = sprintf("%04X", $f[0]);
364  
365 303 ▶ unless ($VARIABLE_ENCODINGS->{$encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Email::Outlook::Message::Base::VARIABLE_ENCODINGS->{$encoding}) 127 176 303
366 127 my $property = sprintf("%04X", $f[1]);
367 127 my $propdata = substr $data, $n+8, 8;
368 127 $self->set_mapi_property($property, [$encoding, $propdata]);
369 }
370 } continue {
371 303 $n += 16 ;
372 }
373 12 return;
374 }
375  
376 sub _check_pps_file_entries {
377 12 ▶ my ($self, $map) = @_;
Subroutine _check_pps_file_entries: called CC=3 SCAR=11.0
378  
379 12 foreach my $property ($self->mapi_property_names) {
380 303 ▶ if (my $key = $map->{$property}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = $map->{$property}) { } 63 240 303
381 63 $self->_use_property($key, $property);
382 } else {
383 240 $self->_warn_about_skipped_property($property);
384 }
385 }
386 12 return;
387 }
388  
389 sub _use_property {
390 63 ▶ my ($self, $key, $property) = @_;
Subroutine _use_property: called CC=2 SCAR=7.0
391 63 my ($encoding, $data) = @{$self->get_mapi_property($property)};
392 63 $self->{$key} = $self->_decode_mapi_property($encoding, $data);
393  
394 $self->{VERBOSE}
395 63 ▶ and $self->_log_property("Using ", $property, $key);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"VERBOSE"} and $self->_log_property("Using ", $property, $key) 0 63 63
396 63 return;
397 }
398  
399 sub _warn_about_skipped_property {
400 240 ▶ my ($self, $property) = @_;
Subroutine _warn_about_skipped_property: called CC=3 SCAR=17.3
401  
402 240 ▶ return unless $self->{VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"VERBOSE"} 240 0 240
403  
404 0 ▶ my $meaning = $skipproperties->{$property} || "UNKNOWN";
Condition0%0/2
$Email::Outlook::Message::Base::skipproperties->{$property} || 'UNKNOWN'
l!l
00
MC/DC0%0/1
$Email::Outlook::Message::Base::skipproperties->{$property} || 'UNKNOWN'
$Email::Outlook::Message::Base::skipproperties->{$property}
Truth table0%0/2
A
A= $Email::Outlook::Message::Base::skipproperties->{$property}
Aresult
11
00
405  
406 0 $self->_log_property("Skipping", $property, $meaning);
407 0 return;
408 }
409  
410 sub _log_property {
411 0 ▶ my ($self, $message, $property, $meaning) = @_;
Subroutine _log_property: not called CC=4 SCAR=30.0
412  
413 0 my ($encoding, $data) = @{$self->get_mapi_property($property)};
414 0 my $value = $self->_decode_mapi_property($encoding, $data);
415 0 $value = substr($value, 0, 50);
416  
417 0 ▶ if ($encoding eq $ENCODING_BINARY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding eq $ENCODING_BINARY) 0 0 0
418 0 ▶ if ($value =~ / [[:print:]] /x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ / [[:print:]] /x) { } 0 0 0
419 0 $value =~ s/ [^[:print:]] /./gx;
420 } else {
421 0 $value =~ s/ . / sprintf("%02x ", ord($&)) /sgex;
422 }
423 }
424  
425 0 ▶ if (length($value) > 45) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value > 45) 0 0 0
426 0 $value = substr($value, 0, 41) . " ...";
427 }
428  
429 0 warn "$message property $encoding:$property ($meaning): $value\n";
430 0 return;
431 }
432  
433 sub _set_verbosity {
434 12 ▶ my ($self, $verbosity) = @_;
Subroutine _set_verbosity: called CC=2 SCAR=7.1
435 12 ▶ $self->{VERBOSE} = $verbosity ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$verbosity ? : 0 12 12
436 12 return;
437 }
438  
439 #
440 # Format a gmt date according to RFC822
441 #
442 sub _format_date {
443 17 ▶ my ($self, $datearr) = @_;
Subroutine _format_date: called CC=1 SCAR=0.0
444 17 my $day = qw(Sun Mon Tue Wed Thu Fri Sat)[strftime("%w", @{$datearr})];
445 17 my $month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[strftime("%m", @{$datearr}) - 1];
446 17 return strftime("$day, %d $month %Y %H:%M:%S +0000", @{$datearr});
447 }
448  
449 8 1;
« blib/lib/Email/Outlook/Message/Attachment.pm ↑ Summary blib/lib/Email/Outlook/Message.pm »