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   70737 use feature ':5.10';
  6         24  
  6         554  
3 6     6   41 use strict;
  6         8  
  6         138  
4 6     6   30 use warnings;
  6         15  
  6         181  
5 6     6   2993 use IO::Dir;
  6         96380  
  6         312  
6 6     6   39 use IO::File;
  6         16  
  6         898  
7             use Class::Accessor::Lite (
8 6         62 '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   939 );
  6         2415  
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 1917 my $class = shift;
27 11   50     41 my $argv1 = shift // return undef;
28 11         28 my $files = 0;
29 11 50       181 return undef unless -d $argv1;
30              
31 11         30 eval {
32             # Count the number of files in the Maildir/
33 11         3243 opendir MAILDIR, $argv1;
34 11         2511 while( my $e = readdir MAILDIR ) {
35 3195 100       44221 next unless -f sprintf("%s/%s", $argv1, $e);
36 3173         12495 $files += 1;
37             }
38 11         277 closedir MAILDIR;
39             };
40              
41 11         171 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         1526 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 604865 my $self = shift;
56 2672 50       6363 return undef unless defined $self->{'dir'};
57 2672 50       43242 return undef unless -d $self->{'dir'};
58 2672 100       9170 return undef unless $self->{'offset'} < $self->{'size'};
59              
60 2662         4317 my $seekhandle = $self->{'handle'};
61 2662         4162 my $readbuffer = '';
62              
63 2662         3783 eval {
64 2662 50       4870 $seekhandle = IO::Dir->new($self->{'dir'}) unless $seekhandle;
65 2662         9258 while( my $r = $seekhandle->read ) {
66             # Read each file in the directory
67 2682 100 100     32557 next if( $r eq '.' || $r eq '..' );
68              
69 2662         6135 my $emailindir = $self->{'dir'}.'/'.$r;
70 2662         5913 $emailindir =~ y{/}{}s;
71 2662         4972 $self->{'offset'} += 1;
72 2662 50       45709 next unless -f $emailindir;
73 2662 50       27794 next unless -s $emailindir;
74 2662 50       31410 next unless -r $emailindir;
75              
76 2662         7371 $self->{'path'} = $emailindir;
77 2662         4130 $self->{'file'} = $r;
78 2662         15257 my $filehandle = IO::File->new($emailindir, 'r');
79 2662         268116 $readbuffer = do { local $/; <$filehandle> };
  2662         9576  
  2662         131094  
80 2662         12198 $filehandle->close;
81 2662         50950 last;
82             }
83 2662 100       10157 $seekhandle->close unless $self->{'offset'} < $self->{'size'};
84             };
85 2662         11164 return $readbuffer;
86             }
87              
88             1;
89             __END__