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

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