File Coverage

blib/lib/Mail/Box/MH/Labels.pm
Criterion Covered Total %
statement 89 91 97.8
branch 23 30 76.6
condition 3 3 100.0
subroutine 14 14 100.0
pod 6 7 85.7
total 135 145 93.1


line stmt bran cond sub pod time code
1             # Copyrights 2001-2023 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.03.
5             # This code is part of distribution Mail-Box. Meta-POD processed with
6             # OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package Mail::Box::MH::Labels;
10 11     11   999 use vars '$VERSION';
  11         25  
  11         600  
11             $VERSION = '3.010';
12              
13 11     11   83 use base 'Mail::Reporter';
  11         25  
  11         1063  
14              
15 11     11   79 use strict;
  11         29  
  11         269  
16 11     11   55 use warnings;
  11         26  
  11         368  
17              
18 11     11   85 use Mail::Message::Head::Subset;
  11         22  
  11         310  
19              
20 11     11   57 use File::Copy;
  11         47  
  11         631  
21 11     11   78 use Carp;
  11         37  
  11         11778  
22              
23              
24             #-------------------------------------------
25              
26              
27             sub init($)
28 21     21 0 458 { my ($self, $args) = @_;
29 21         100 $self->SUPER::init($args);
30             $self->{MBML_filename} = $args->{filename}
31 21 50       313 or croak "No label filename specified.";
32              
33 21         112 $self;
34             }
35              
36             #-------------------------------------------
37              
38              
39 29     29 1 84 sub filename() {shift->{MBML_filename}}
40              
41             #-------------------------------------------
42              
43              
44             sub get($)
45 434     434 1 849 { my ($self, $msgnr) = @_;
46 434         903 $self->{MBML_labels}[$msgnr];
47             }
48              
49             #-------------------------------------------
50              
51              
52             sub read()
53 17     17 1 49 { my $self = shift;
54 17         66 my $seq = $self->filename;
55              
56 17 100       700 open SEQ, '<:raw', $seq
57             or return;
58              
59 9         40 my @labels;
60              
61 9         29 local $_;
62 9         255 while()
63 16         66 { s/\s*\#.*$//;
64 16 50       50 next unless length;
65              
66 16 50       116 next unless s/^\s*(\w+)\s*\:\s*//;
67 16         71 my $label = $1;
68              
69 16         32 my $set = 1;
70 16 100       65 if($label eq 'cur' ) { $label = 'current' }
  1 100       2  
71 2         5 elsif($label eq 'unseen') { $label = 'seen'; $set = 0 }
  2         13  
72              
73 16         90 foreach (split /\s+/)
74 32 100       191 { if( /^(\d+)\-(\d+)\s*$/ )
    50          
75 10         114 { push @{$labels[$_]}, $label, $set foreach $1..$2;
  78         279  
76             }
77             elsif( /^\d+\s*$/ )
78 22         35 { push @{$labels[$_]}, $label, $set;
  22         126  
79             }
80             }
81             }
82              
83 9         115 close SEQ;
84              
85 9         69 $self->{MBML_labels} = \@labels;
86 9         47 $self;
87             }
88              
89             #-------------------------------------------
90              
91              
92             sub write(@)
93 11     11 1 26 { my $self = shift;
94 11         46 my $filename = $self->filename;
95              
96             # Remove when no messages are left.
97 11 50       37 unless(@_)
98 0         0 { unlink $filename;
99 0         0 return $self;
100             }
101              
102 11 50       780 open my $out, '>:raw', $filename or return;
103 11         103 $self->print($out, @_);
104 11         377 close $out;
105              
106 11         86 $self;
107             }
108              
109             #-------------------------------------------
110              
111              
112             sub append(@)
113 1     1 1 2 { my $self = shift;
114 1         4 my $filename = $self->filename;
115              
116 1 50       44 open(my $out, '>>:raw', $filename) or return;
117 1         7 $self->print($out, @_);
118 1         27 close $out;
119              
120 1         9 $self;
121             }
122              
123             #-------------------------------------------
124              
125              
126             sub print($@)
127 12     12 1 39 { my ($self, $out) = (shift, shift);
128              
129             # Collect the labels from the selected messages.
130 12         33 my %labeled;
131 12         41 foreach my $message (@_)
132 310         691 { my $labels = $message->labels;
133 310         1453 (my $seq = $message->filename) =~ s!.*/!!;
134              
135 10         24 push @{$labeled{unseen}}, $seq
136 310 100       725 unless $labels->{seen};
137              
138 310         683 foreach (keys %$labels)
139 351         896 { push @{$labeled{$_}}, $seq
140 369 100       727 if $labels->{$_};
141             }
142             }
143 12         48 delete $labeled{seen};
144              
145             # Write it out
146              
147 12         30 local $" = ' ';
148 12         58 foreach (sort keys %labeled)
149             {
150 12         20 my @msgs = @{$labeled{$_}}; #they are ordered already.
  12         43  
151 12 100       34 $_ = 'cur' if $_ eq 'current';
152 12         74 print $out "$_:";
153              
154 12         34 while(@msgs)
155 21         35 { my $start = shift @msgs;
156 21         35 my $end = $start;
157              
158 21   100     146 $end = shift @msgs
159             while @msgs && $msgs[0]==$end+1;
160              
161 21 100       79 print $out ($start==$end ? " $start" : " $start-$end");
162             }
163 12         33 print $out "\n";
164             }
165              
166 12         41 $self;
167             }
168              
169             #-------------------------------------------
170              
171              
172             1;