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