line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
1
|
|
|
1
|
|
194678
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
31
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
32
|
1
|
|
|
1
|
|
1338
|
use Encode qw(decode); |
|
1
|
|
|
|
|
8858
|
|
|
1
|
|
|
|
|
72
|
|
33
|
1
|
|
|
1
|
|
7
|
use Scalar::Util qw(blessed); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
74
|
|
34
|
1
|
|
|
1
|
|
5
|
use base 'Email::Folder'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
545
|
|
35
|
1
|
|
|
1
|
|
15865
|
use mro; |
|
1
|
|
|
|
|
683
|
|
|
1
|
|
|
|
|
5
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
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
|
|
|
1
|
1
|
14
|
my $class = shift; |
57
|
1
|
|
|
|
|
4
|
my @args = @_; |
58
|
1
|
|
|
|
|
6
|
my $self = $class->next::method(@args); |
59
|
1
|
|
|
|
|
16490
|
$self->{folder_path} = $args[0]; |
60
|
1
|
|
50
|
|
|
6
|
$self->{timeout} //= 3; |
61
|
1
|
|
|
|
|
5
|
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
|
|
|
7
|
1
|
6829
|
my $self = shift; |
74
|
7
|
|
|
|
|
33
|
my %cond = @_; |
75
|
|
|
|
|
|
|
|
76
|
7
|
100
|
66
|
|
|
109
|
die 'Need email address and subject regexp' unless $cond{email} && $cond{subject} && ref($cond{subject}) eq 'Regexp'; |
|
|
|
100
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
3
|
|
|
|
|
7
|
my $email = $cond{email}; |
79
|
3
|
|
|
|
|
5
|
my $subject_regexp = $cond{subject}; |
80
|
|
|
|
|
|
|
|
81
|
3
|
|
|
|
|
5
|
my @msgs; |
82
|
|
|
|
|
|
|
|
83
|
3
|
|
|
|
|
7
|
my $found = 0; |
84
|
|
|
|
|
|
|
#mailbox maybe late, so we wait 3 seconds |
85
|
3
|
|
|
|
|
12
|
WAIT: for (0 .. $self->{timeout}) { |
86
|
5
|
|
|
|
|
1504
|
MSG: while (my $tmsg = $self->next_message) { |
87
|
4
|
|
|
|
|
2762
|
my $address = $tmsg->header('To'); |
88
|
4
|
|
|
|
|
139
|
my $subject = $tmsg->header('Subject'); |
89
|
4
|
50
|
|
|
|
95
|
if ($subject =~ /=\?UTF\-8/) { |
90
|
0
|
|
|
|
|
0
|
$subject = decode('MIME-Header', $subject); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
4
|
100
|
66
|
|
|
57
|
if ($address eq $email && $subject =~ $subject_regexp) { |
94
|
3
|
|
|
|
|
5
|
my %msg; |
95
|
3
|
|
|
|
|
26
|
$msg{body} = $tmsg->body; |
96
|
3
|
|
|
|
|
39
|
$msg{address} = $address; |
97
|
3
|
|
|
|
|
9
|
$msg{subject} = $subject; |
98
|
3
|
|
|
|
|
6
|
push @msgs, \%msg; |
99
|
3
|
|
|
|
|
72
|
$found = 1; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
5
|
100
|
|
|
|
662
|
last WAIT if $found; |
103
|
|
|
|
|
|
|
# reset reader |
104
|
3
|
|
|
|
|
13
|
$self->reset; |
105
|
3
|
|
|
|
|
2999529
|
sleep 1; |
106
|
|
|
|
|
|
|
} |
107
|
3
|
|
|
|
|
87
|
return @msgs; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub reset { |
111
|
4
|
|
|
4
|
0
|
7
|
my $self = shift; |
112
|
4
|
|
|
|
|
30
|
my $reader_class = blessed($self->{_folder}); |
113
|
4
|
|
|
|
|
220
|
delete $self->{_folder}; |
114
|
4
|
|
|
|
|
46
|
$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
|
|
|
1
|
1
|
1277
|
my $self = shift; |
125
|
1
|
|
50
|
|
|
10
|
my $type = blessed($self->{_folder}) // ''; |
126
|
|
|
|
|
|
|
|
127
|
1
|
|
|
|
|
5
|
$self->reset; |
128
|
|
|
|
|
|
|
|
129
|
1
|
50
|
|
|
|
33
|
if ($type eq 'Email::Folder::Mbox') { |
130
|
1
|
|
50
|
|
|
107
|
truncate($self->{folder_path}, 0) // die "Cannot clear mailbox $self->{folder_path}\n"; |
131
|
|
|
|
|
|
|
} else { |
132
|
0
|
|
|
|
|
0
|
die "Sorry, I can only clear the mailbox with the type Mbox\n"; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
1
|
|
|
|
|
3
|
return 1; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 init |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
init Email folder for test |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=cut |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub init { |
145
|
1
|
|
|
1
|
1
|
5
|
my $self = shift; |
146
|
|
|
|
|
|
|
|
147
|
1
|
|
50
|
|
|
12
|
my $type = blessed($self->{_folder}) // ''; |
148
|
|
|
|
|
|
|
|
149
|
1
|
50
|
|
|
|
5
|
if ($type eq 'Email::Folder::Mbox') { |
150
|
1
|
|
50
|
|
|
108
|
open(my $fh, ">>", $self->{folder_path}) // die "Cannot init mailbox $self->{folder_path}\n"; |
151
|
1
|
|
|
|
|
9
|
close($fh); |
152
|
|
|
|
|
|
|
} else { |
153
|
0
|
|
|
|
|
0
|
die "Sorry, I can only init the mailbox with the type Mbox\n"; |
154
|
|
|
|
|
|
|
} |
155
|
1
|
|
|
|
|
2
|
return 1; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 SEE ALSO |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
L |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |