| 1 | package Email::Folder::Search; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: wait and search emails from mailbox | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | Email::Folder::Search | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | version 0.011 | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | Search email from mailbox file. This module is mainly to test that the emails are received or not. | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | use Email::Folder::Search; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | my $folder = Email::Folder::Search->new('/var/spool/mbox'); | ||||||||||||||||||||||||||||||||||||||||||
| 21 | my %msg = $folder->get_email_by_address_subject(email => 'hello@test.com', subject => qr/this is a subject/); | ||||||||||||||||||||||||||||||||||||||||||
| 22 | $folder->clear(); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 Methods | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | ▶ | use Encode qw(decode); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | use base 'Email::Folder'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | use mro; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | our $VERSION = '0.011'; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =head2 new($folder, %options) | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | takes the name of a folder, and a hash of options | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | options: | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =item timeout | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | The seconds that get_email_by_address_subject will wait if the email cannot be found. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | my @args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | my $self = $class->next::method(@args); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | $self->{folder_path} = $args[0]; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | $self->{timeout} //= 3; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"timeout"} //= 3
MC/DC0%0/1
$self->{"timeout"} //= 3
$self->{"timeout"}
Truth table50%1/2
A
A= $self->{"timeout"}
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =head2 search(email => $email, subject => qr/the subject/); | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | get emails with receiver address and subject(regexp). Return an array of messages which are hashref. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | my $msgs = search(email => 'hello@test.com', subject => qr/this is a subject/); | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | sub search { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 7 | my %cond = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 7 | ▶ | die 'Need email address and subject regexp' unless $cond{email} && $cond{subject} && ref($cond{subject}) eq 'Regexp'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$cond{"email"} and $cond{"subject"} and ref $cond{"subject"} eq "Regexp"
$cond{"email"} and $cond{"subject"}
MC/DC100%3/3
$cond{"email"} and $cond{"subject"} and ref $cond{"subject"} eq "Regexp"
$cond{"email"}
$cond{"subject"}
ref $cond{"subject"} eq "Regexp"
Truth table100%4/4
A and B and C
A= $cond{"email"}B= $cond{"subject"}C= ref $cond{"subject"} eq "Regexp"
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | my $email = $cond{email}; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | my $subject_regexp = $cond{subject}; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | my @msgs; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | my $found = 0; | |||||||||||||||||||||||||||||||||||||||||
| 84 | #mailbox maybe late, so we wait 3 seconds | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | WAIT: for (0 .. $self->{timeout}) { | |||||||||||||||||||||||||||||||||||||||||
| 86 | 5 | ▶ | MSG: while (my $tmsg = $self->next_message) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | my $address = $tmsg->header('To'); | |||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | my $subject = $tmsg->header('Subject'); | |||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | if ($subject =~ /=\?UTF\-8/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $subject = decode('MIME-Header', $subject); | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | ▶ | if ($address eq $email && $subject =~ $subject_regexp) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$address eq $email and $subject =~ /$subject_regexp/
MC/DC50%1/2
$address eq $email and $subject =~ /$subject_regexp/
$address eq $email
$subject =~ /$subject_regexp/
Truth table66%2/3
A and B
A= $address eq $emailB= $subject =~ /$subject_regexp/
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | my %msg; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | $msg{body} = $tmsg->body; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 3 | $msg{address} = $address; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | $msg{subject} = $subject; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | push @msgs, \%msg; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | $found = 1; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 5 | ▶ | last WAIT if $found; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | # reset reader | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | $self->reset; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 3 | sleep 1; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | return @msgs; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | sub reset { | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | my $reader_class = blessed($self->{_folder}); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | delete $self->{_folder}; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | $self->{_folder} = $reader_class->new($self->{folder_path}, %$self); | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =head2 clear | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | clear the content of mailbox | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | sub clear { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | my $type = blessed($self->{_folder}) // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
builtin::blessed($self->{"_folder"}) // ""
MC/DC0%0/1
builtin::blessed($self->{"_folder"}) // ""
builtin::blessed($self->{"_folder"})
Truth table50%1/2
A
A= builtin::blessed($self->{"_folder"})
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | $self->reset; | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | ▶ | if ($type eq 'Email::Folder::Mbox') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | ▶ | truncate($self->{folder_path}, 0) // die "Cannot clear mailbox $self->{folder_path}\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
truncate($self->{"folder_path"}, 0) // die(qq[Cannot clear mailbox $self->{"folder_path"}\n])
MC/DC0%0/1
truncate($self->{"folder_path"}, 0) // die(qq[Cannot clear mailbox $self->{"folder_path"}\n])
truncate($self->{"folder_path"}, 0)
Truth table50%1/2
A
A= truncate($self->{"folder_path"}, 0)
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | die "Sorry, I can only clear the mailbox with the type Mbox\n"; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | =head2 init | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | init Email folder for test | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | sub init { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | ▶ | my $type = blessed($self->{_folder}) // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
builtin::blessed($self->{"_folder"}) // ""
MC/DC0%0/1
builtin::blessed($self->{"_folder"}) // ""
builtin::blessed($self->{"_folder"})
Truth table50%1/2
A
A= builtin::blessed($self->{"_folder"})
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | ▶ | if ($type eq 'Email::Folder::Mbox') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | ▶ | open(my $fh, ">>", $self->{folder_path}) // die "Cannot init mailbox $self->{folder_path}\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
open(my $fh, ">>", $self->{"folder_path"}) // die(qq[Cannot init mailbox $self->{"folder_path"}\n])
MC/DC0%0/1
open(my $fh, ">>", $self->{"folder_path"}) // die(qq[Cannot init mailbox $self->{"folder_path"}\n])
open(my $fh, ">>", $self->{"folder_path"})
Truth table50%1/2
A
A= open(my $fh, ">>", $self->{"folder_path"})
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | close($fh); | |||||||||||||||||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | die "Sorry, I can only init the mailbox with the type Mbox\n"; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | L<Email::Folder> | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||