File Coverage

blib/lib/Mail/Box/MH/Index.pm
Criterion Covered Total %
statement 61 68 89.7
branch 9 22 40.9
condition 6 18 33.3
subroutine 11 11 100.0
pod 5 6 83.3
total 92 125 73.6


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Mail-Box version 4.01.
2             # The POD got stripped from this file by OODoc version 3.05.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2001-2025 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11              
12             package Mail::Box::MH::Index;{
13             our $VERSION = '4.01';
14             }
15              
16 11     11   3170 use parent 'Mail::Reporter';
  11         30  
  11         77  
17              
18 11     11   924 use strict;
  11         29  
  11         356  
19 11     11   48 use warnings;
  11         41  
  11         743  
20              
21 11     11   82 use Log::Report 'mail-box', import => [ qw/__x error info/ ];
  11         22  
  11         108  
22              
23 11     11   8904 use Mail::Message::Head::Subset ();
  11         38  
  11         9804  
24              
25             #--------------------
26              
27             sub init($)
28 4     4 0 64 { my ($self, $args) = @_;
29 4         31 $self->SUPER::init($args);
30              
31             $self->{MBMI_filename} = $args->{filename}
32 4 50       40 or error __x"MH index requires a filename.";
33              
34 4   50     22 $self->{MBMI_head_wrap} = $args->{head_wrap} || 72;
35 4   50     24 $self->{MBMI_head_type} = $args->{head_type} || 'Mail::Message::Head::Subset';
36 4         23 $self;
37             }
38              
39             #--------------------
40              
41 10     10 1 38 sub filename() { $_[0]->{MBMI_filename} }
42              
43             #--------------------
44              
45             sub write(@)
46 5     5 1 48 { my ($self, @messages) = @_;
47 5   50     21 my $indexfn = $self->filename // return $self;
48              
49             # Remove empty index-file.
50 5 50       20 unless(@messages)
51 0         0 { unlink $indexfn;
52 0         0 return $self;
53             }
54              
55 5 50       590 open my $index, '>:raw', $indexfn
56             or return $self;
57              
58 5         31 my $written = 0;
59              
60 5         17 foreach my $msg (@messages)
61 178         587 { my $head = $msg->head;
62 178 50 66     2705 next if $head->isDelayed && $head->isa('Mail::Message::Head::Subset');
63              
64 178         1468 my $fn = $msg->filename;
65 178         3040 $index->print(
66             "X-MailBox-Filename: $fn\n",
67             'X-MailBox-Size: ', (-s $fn), "\n",
68             );
69 178         2706 $head->print($index);
70 178         63596 $written++;
71             }
72              
73 5         21 $index->close;
74 5 50       3344 $written or unlink $indexfn;
75              
76 5         50 $self;
77             }
78              
79              
80             sub append(@)
81 1     1 1 3 { my ($self, @messages) = @_;
82 1 50       4 my $indexfn = $self->filename or return $self;
83              
84 1 50       114 open my $index, '>>:raw', $indexfn
85             or return $self;
86              
87 1         5 foreach my $msg (@messages)
88 1         5 { my $head = $msg->head;
89 1 50 33     11 next if $head->isDelayed && $head->isa('Mail::Message::Head::Subset');
90              
91 1         4 my $fn = $msg->filename;
92 1         19 $index->print(
93             "X-MailBox-Filename: $fn\n",
94             'X-MailBox-Size: ', (-s $fn), "\n",
95             );
96 1         11 $head->print($index);
97             }
98 1         171 $index->close;
99 1         62 $self;
100             }
101              
102              
103             sub read(;$)
104 4     4 1 9 { my $self = shift;
105 4         18 my $filename = $self->filename;
106 4 50       40 my $parser = Mail::Box::Parser->new(filename => $filename, mode => 'r') or return;
107              
108 4         1385 my @options = (wrap_length => $self->{MBMI_head_wrap});
109 4         12 my $type = $self->{MBMI_head_type};
110 4         43 my $index_age = -M $filename;
111 4         9 my %index;
112              
113 4         83 while(my $head = $type->new(@options)->read($parser))
114             {
115             # cleanup the index from files which were renamed
116 0         0 my $msgfile = $head->get('x-mailbox-filename');
117 0         0 my $size = int $head->get('x-mailbox-size');
118 0 0 0     0 next unless -f $msgfile && -s _ == $size;
119 0 0 0     0 next if defined $index_age && -M _ < $index_age;
120              
121             # keep this one
122 0         0 $index{$msgfile} = $head;
123             }
124              
125 4         591 $parser->stop;
126              
127 4         319 $self->{MBMI_index} = \%index;
128 4         22 $self;
129             }
130              
131              
132             sub get($)
133 94     94 1 186 { my ($self, $msgfile) = @_;
134 94         210 $self->{MBMI_index}{$msgfile};
135             }
136              
137             1;