File Coverage

lib/Sisimai/Mail/Maildir.pm
Criterion Covered Total %
statement 56 56 100.0
branch 15 22 68.1
condition 4 5 80.0
subroutine 8 8 100.0
pod 2 2 100.0
total 85 93 91.4


line stmt bran cond sub pod time code
1             package Sisimai::Mail::Maildir;
2 6     6   68917 use feature ':5.10';
  6         21  
  6         571  
3 6     6   34 use strict;
  6         12  
  6         165  
4 6     6   29 use warnings;
  6         12  
  6         170  
5 6     6   2757 use IO::Dir;
  6         90834  
  6         305  
6 6     6   41 use IO::File;
  6         12  
  6         916  
7             use Class::Accessor::Lite (
8 6         64 'new' => 0,
9             'ro' => [
10             'dir', # [String] Path to Maildir/
11             ],
12             'rw' => [
13             'path', # [String] Path to each file
14             'file', # [String] Each file name of a mail in the Maildir/
15             'size', # [Integer] The number of email files in the Maildir/
16             'offset', # [Integer] The number of email files which have been read
17             'handle', # [IO::Dir] Directory handle
18             ]
19 6     6   959 );
  6         2521  
20              
21             sub new {
22             # Constructor of Sisimai::Mail::Maildir
23             # @param [String] argv1 Path to Maildir/
24             # @return [Sisimai::Mail::Maildir] Object
25             # [Undef] is not a directory or does not exist
26 11     11 1 1951 my $class = shift;
27 11   50     43 my $argv1 = shift // return undef;
28 11         20 my $files = 0;
29 11 50       182 return undef unless -d $argv1;
30              
31 11         40 eval {
32             # Count the number of files in the Maildir/
33 11         4568 opendir MAILDIR, $argv1;
34 11         2267 while( my $e = readdir MAILDIR ) {
35 3195 100       37132 next unless -f sprintf("%s/%s", $argv1, $e);
36 3173         13228 $files += 1;
37             }
38 11         249 closedir MAILDIR;
39             };
40              
41 11         129 my $param = {
42             'dir' => $argv1,
43             'file' => undef,
44             'path' => undef,
45             'size' => $files,
46             'offset' => 0,
47             'handle' => IO::Dir->new($argv1),
48             };
49 11         1670 return bless($param, __PACKAGE__);
50             }
51              
52             sub read {
53             # Maildir reader, works as a iterator.
54             # @return [String] Contents of file in Maildir/
55 2672     2672 1 597761 my $self = shift;
56 2672 50       6853 return undef unless defined $self->{'dir'};
57 2672 50       44896 return undef unless -d $self->{'dir'};
58 2672 100       10397 return undef unless $self->{'offset'} < $self->{'size'};
59              
60 2662         4745 my $seekhandle = $self->{'handle'};
61 2662         3892 my $readbuffer = '';
62              
63 2662         4050 eval {
64 2662 50       5178 $seekhandle = IO::Dir->new($self->{'dir'}) unless $seekhandle;
65 2662         9165 while( my $r = $seekhandle->read ) {
66             # Read each file in the directory
67 2682 100 100     34073 next if( $r eq '.' || $r eq '..' );
68              
69 2662         6469 my $emailindir = $self->{'dir'}.'/'.$r;
70 2662         5716 $emailindir =~ y{/}{}s;
71 2662         4082 $self->{'offset'} += 1;
72 2662 50       35518 next unless -f $emailindir;
73 2662 50       28156 next unless -s $emailindir;
74 2662 50       32690 next unless -r $emailindir;
75              
76 2662         7156 $self->{'path'} = $emailindir;
77 2662         4439 $self->{'file'} = $r;
78 2662         15931 my $filehandle = IO::File->new($emailindir, 'r');
79 2662         267512 $readbuffer = do { local $/; <$filehandle> };
  2662         10029  
  2662         100773  
80 2662         13314 $filehandle->close;
81 2662         51429 last;
82             }
83 2662 100       11592 $seekhandle->close unless $self->{'offset'} < $self->{'size'};
84             };
85 2662         11439 return $readbuffer;
86             }
87              
88             1;
89             __END__