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   63122 use feature ':5.10';
  6         17  
  6         538  
3 6     6   32 use strict;
  6         10  
  6         108  
4 6     6   55 use warnings;
  6         10  
  6         152  
5 6     6   2330 use IO::Dir;
  6         77684  
  6         280  
6 6     6   39 use IO::File;
  6         7  
  6         726  
7             use Class::Accessor::Lite (
8 6         44 '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   1005 );
  6         2108  
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 1797 my $class = shift;
27 11   50     34 my $argv1 = shift // return undef;
28 11         20 my $files = 0;
29 11 50       161 return undef unless -d $argv1;
30              
31 11         29 eval {
32             # Count the number of files in the Maildir/
33 11         5179 opendir MAILDIR, $argv1;
34 11         1851 while( my $e = readdir MAILDIR ) {
35 3165 100       32713 next unless -f sprintf("%s/%s", $argv1, $e);
36 3143         11922 $files += 1;
37             }
38 11         306 closedir MAILDIR;
39             };
40              
41 11         185 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         2441 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 2647     2647 1 495776 my $self = shift;
56 2647 50       6309 return undef unless defined $self->{'dir'};
57 2647 50       48823 return undef unless -d $self->{'dir'};
58 2647 100       10787 return undef unless $self->{'offset'} < $self->{'size'};
59              
60 2637         5134 my $seekhandle = $self->{'handle'};
61 2637         4037 my $readbuffer = '';
62              
63 2637         4450 eval {
64 2637 50       4911 $seekhandle = IO::Dir->new($self->{'dir'}) unless $seekhandle;
65 2637         9862 while( my $r = $seekhandle->read ) {
66             # Read each file in the directory
67 2657 100 100     31767 next if( $r eq '.' || $r eq '..' );
68              
69 2637         6322 my $emailindir = $self->{'dir'}.'/'.$r;
70 2637         5609 $emailindir =~ y{/}{}s;
71 2637         4468 $self->{'offset'} += 1;
72 2637 50       33744 next unless -f $emailindir;
73 2637 50       24922 next unless -s $emailindir;
74 2637 50       29129 next unless -r $emailindir;
75              
76 2637         7273 $self->{'path'} = $emailindir;
77 2637         4989 $self->{'file'} = $r;
78 2637         17000 my $filehandle = IO::File->new($emailindir, 'r');
79 2637         261585 $readbuffer = do { local $/; <$filehandle> };
  2637         10265  
  2637         98676  
80 2637         12901 $filehandle->close;
81 2637         49171 last;
82             }
83 2637 100       10894 $seekhandle->close unless $self->{'offset'} < $self->{'size'};
84             };
85 2637         11918 return $readbuffer;
86             }
87              
88             1;
89             __END__