Client/Imap.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Client/Elasticsearch/Tasks.pm ↑ Summary Client/Kafka.pm »
1 #
2 # $Id$
3 #
4 # client::imap Brik
5 #
6 package Metabrik::Client::Imap;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::String::Uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 input => [ qw(imap_uri) ],
21 as_array => [ qw(0|1) ],
22 strip_crlf => [ qw(0|1) ],
23 username => [ qw(username) ],
24 password => [ qw(password) ],
25 _imap => [ qw(INTERNAL) ],
26 _id => [ qw(INTERNAL) ],
27 _count => [ qw(INTERNAL) ],
28 },
29 attributes_default => {
30 as_array => 0,
31 strip_crlf => 1,
32 },
33 commands => {
34 open => [ qw(imap_uri|OPTIONAL) ],
35 reset_current => [ ],
36 total => [ ],
37 read => [ ],
38 read_next => [ ],
39 read_next_with_subject => [ qw(subject) ],
40 read_next_with_an_attachment => [ qw(regex|OPTIONAL) ],
41 parse => [ qw(message) ],
42 save_attachments => [ qw(message) ],
43 close => [ ],
44 },
45 require_modules => {
46 'URI::imap' => [ ],
47 'URI::imaps' => [ ],
48 'Metabrik::Email::Message' => [ ],
49 'Net::IMAP::Simple' => [ ],
50 },
51 };
52 }
53  
54 sub open {
55 my $self = shift;
56 my ($input) = @_;
57  
58 $input ||= $self->input;
59 $self->brik_help_set_undef_arg('input', $input) or return;
60  
61 my $parsed = $self->SUPER::parse($input) or return;
62 my $host = $parsed->{host};
63 my $port = $parsed->{port};
64 my $user = $parsed->{user} || $self->username;
65 my $password = $parsed->{password} || $self->password;
66 my $path = $parsed->{path} || 'INBOX';
67 $path =~ s{^/*}{};
68  
69 if (! defined($user) || ! defined($password) || ! defined($host)) {
70 return $self->log->error("open: invalid uri [$input] ".
71 "missing connection information");
72 }
73  
74 my $use_ssl = $self->is_imaps_scheme($parsed) ? 1 : 0;
75  
76 my $imap = Net::IMAP::Simple->new("$host:$port", use_ssl => $use_ssl);
77 if (! defined($imap)) {
78 return $self->log->error("open: can't connect to IMAP: $Net::IMAP::Simple::errstr");
79 }
80  
81 my $r = $imap->login($user, $password);
82 if (! defined($r)) {
83 return $self->log->error("open: login failed [".$imap->errstr."]");
84 }
85  
86 my $count = $imap->select($path);
87 $self->_count($count);
88 $self->_id($count);
89  
90 return $self->_imap($imap);
91 }
92  
93 sub reset_current {
94 my $self = shift;
95  
96 $self->_id($self->_count);
97  
98 return 1;
99 }
100  
101 sub total {
102 my $self = shift;
103  
104 my $imap = $self->_imap;
105 $self->brik_help_run_undef_arg('open', $imap) or return;
106  
107 return $self->_count;
108 }
109  
110 sub read_next {
111 my $self = shift;
112  
113 my $imap = $self->_imap;
114 $self->brik_help_run_undef_arg('open', $imap) or return;
115  
116 my $current = $self->_id;
117  
118 my @lines = $imap->get($current--);
119  
120 $self->_id($current);
121  
122 if ($self->as_array) {
123 if ($self->strip_crlf) {
124 for (@lines) {
125 s{[\r\n]*$}{};
126 }
127 }
128 return [ @lines ];
129 }
130  
131 return join("\n", @lines);
132 }
133  
134 sub read_next_with_subject {
135 my $self = shift;
136 my ($subject) = @_;
137  
138 my $imap = $self->_imap;
139 $self->brik_help_run_undef_arg('open', $imap) or return;
140 $self->brik_help_run_undef_arg('read_next_with_subject', $subject) or return;
141  
142 my $total = $self->total;
143  
144 for (1..$total) {
145 my $next = $self->read_next or return;
146 my $message = $self->parse($next) or return;
147 my $headers = $message->[0];
148  
149 if ($headers->{Subject} =~ m{$subject}i) {
150 return $next;
151 }
152 }
153  
154 return $self->log->error("read_next_with_subject: no message found with that subject ".
155 "in last $total messages.");
156 }
157  
158 sub read_next_with_an_attachment {
159 my $self = shift;
160 my ($regex) = @_;
161  
162 my $imap = $self->_imap;
163 $self->brik_help_run_undef_arg('open', $imap) or return;
164 $regex ||= qr/^.*$/;
165  
166 my $total = $self->total;
167  
168 for (1..$total) {
169 my $next = $self->read_next or return;
170 my $message = $self->parse($next) or return;
171 my $headers = $message->[0];
172  
173 for my $part (@$message) {
174 if (exists($part->{filename}) && length($part->{filename})
175 && $part->{filename} =~ $regex) {
176 return $next;
177 }
178 }
179 }
180  
181 return $self->log->error("read_next_with_an_attachment: no message found with ".
182 "an attachment in last $total messages.");
183 }
184  
185 sub parse {
186 my $self = shift;
187 my ($message) = @_;
188  
189 $self->brik_help_run_undef_arg('parse', $message) or return;
190  
191 my $em = Metabrik::Email::Message->new_from_brik_init($self) or return;
192  
193 return $em->parse($message);
194 }
195  
196 sub save_attachments {
197 my $self = shift;
198 my ($message) = @_;
199  
200 $self->brik_help_run_undef_arg('save_attachments', $message) or return;
201  
202 my $em = Metabrik::Email::Message->new_from_brik_init($self) or return;
203 $em->datadir($self->datadir);
204  
205 return $em->save_attachments($message);
206 }
207  
208 sub close {
209 my $self = shift;
210  
211 my $imap = $self->_imap;
212 if (defined($imap)) {
213 $imap->quit;
214 $self->_imap(undef);
215 $self->_id(undef);
216 }
217  
218 return 1;
219 }
220  
221 1;
222  
223 __END__
« Client/Elasticsearch/Tasks.pm ↑ Summary Client/Kafka.pm »