Box/POP3.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 31 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 224 CC 12 SCAR 17.7
CC
12
Cov
0%
getHeadAndBody
20.0
popClient
12.0
foundIn
6.0
CRAP 5.8
1 # Copyrights 2001-2023 by [Mark Overmeer].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 # This code is part of distribution Mail-Box-POP3. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package Mail::Box::POP3;
10 use vars '$VERSION';
11 $VERSION = '3.006';
12  
13 use base 'Mail::Box::Net';
14  
15 use strict;
16 use warnings;
17  
18 use Mail::Box::POP3::Message;
19 use Mail::Box::Parser::Perl;
20 use Mail::Box::FastScalar;
21  
22 use File::Spec;
23 use File::Basename;
24 use Carp;
25  
26  
27 sub init($)
28 { my ($self, $args) = @_;
29  
30 $args->{server_port} ||= 110;
31 $args->{folder} ||= 'inbox';
32 $args->{message_type} ||= 'Mail::Box::POP3::Message';
33  
34 $self->SUPER::init($args);
35  
36 $self->{MBP_client} = $args->{pop_client};
37 $self->{MBP_auth} = $args->{authenticate} || 'AUTO';
38 $self->{MBP_use_ssl} = $args->{use_ssl} || 0;
39 $self->{MBP_ssl_opts} = $args->{ssl_options};
40  
41 $self;
42 }
43  
44  
45 sub create($@) { undef } # fails
46  
47 sub foundIn(@)
48 { my $self = shift;
49 unshift @_, 'folder' if @_ % 2;
50 my %options = @_;
51  
52 (exists $options{type} && lc $options{type} eq 'pop3')
53 || (exists $options{folder} && $options{folder} =~ m/^pop/);
54 }
55  
56  
57 sub addMessage($)
58 { my ($self, $message) = @_;
59  
60 $self->log(ERROR => "You cannot write a message to a pop server (yet)")
61 if defined $message;
62  
63 undef;
64 }
65  
66  
67 sub addMessages(@)
68 { my $self = shift;
69  
70 # error message described in addMessage()
71 $self->log(ERROR => "You cannot write messages to a pop server (yet)")
72 if @_;
73  
74 ();
75 }
76  
77 sub type() {'pop3'}
78  
79 sub close(@)
80 { my $self = shift;
81  
82 $self->SUPER::close(@_);
83  
84 my $pop = delete $self->{MBP_client};
85 $pop->disconnect if defined $pop;
86  
87 $self;
88 }
89  
90  
91 sub delete(@)
92 { my $self = shift;
93 $self->log(WARNING => "POP3 folders cannot be deleted.");
94 undef;
95 }
96  
97  
98 sub listSubFolders(@) { () } # no
99  
100  
101 sub openSubFolder($@) { undef } # fails
102  
103 sub topFolderWithMessages() { 1 } # Yes: only top folder
104  
105  
106 sub update() {shift->notImplemented}
107  
108 #-------------------------------------------
109  
110  
111 sub popClient(%)
112 { my ($self, %args) = @_;
113  
114 return $self->{MBP_client}
115 if defined $self->{MBP_client};
116  
117 my $auth = $self->{auth};
118  
119 require Mail::Transport::POP3;
120 my $client = Mail::Transport::POP3->new
121 ( username => $self->{MBN_username}
122 , password => $self->{MBN_password}
123 , hostname => $self->{MBN_hostname}
124 , port => $self->{MBN_port}
125 , authenticate => $self->{MBP_auth}
126 , use_ssl => $args{use_ssl} || $self->{MBP_use_ssl}
127 , ssl_options => $args{ssl_options} || $self->{MBP_ssl_opts}
128 );
129  
130 $self->log(ERROR => "Cannot create POP3 client for $self.")
131 unless defined $client;
132  
133 $self->{MBP_client} = $client;
134 }
135  
136 sub readMessages(@)
137 { my ($self, %args) = @_;
138  
139 my $pop = $self->popClient or return;
140 my @log = $self->logSettings;
141 my $seqnr = 0;
142  
143 foreach my $id ($pop->ids)
144 { my $message = $args{message_type}->new
145 ( head => $args{head_delayed_type}->new(@log)
146 , unique => $id
147 , folder => $self
148 , seqnr => $seqnr++
149 );
150  
151 my $body = $args{body_delayed_type}->new(@log, message => $message);
152 $message->storeBody($body);
153  
154 $self->storeMessage($message);
155 }
156  
157 $self;
158 }
159
160  
161 sub getHead($)
162 { my ($self, $message) = @_;
163 my $pop = $self->popClient or return;
164  
165 my $uidl = $message->unique;
166 my $lines = $pop->header($uidl);
167  
168 unless(defined $lines)
169 { $lines = [];
170 $self->log(WARNING => "Message $uidl disappeared from POP3 server $self.");
171 }
172  
173 my $text = join '', @$lines;
174 my $parser = Mail::Box::Parser::Perl->new # not parseable by C parser
175 ( filename => "$pop"
176 , file => Mail::Box::FastScalar->new(\$text)
177 , fix_headers => $self->{MB_fix_headers}
178 );
179  
180 $self->lazyPermitted(1);
181  
182 my $head = $message->readHead($parser);
183 $parser->stop;
184  
185 $self->lazyPermitted(0);
186  
187 $self->log(PROGRESS => "Loaded head of $uidl.");
188 $head;
189 }
190  
191  
192 sub getHeadAndBody($)
193 { my ($self, $message) = @_;
194 my $pop = $self->popClient or return;
195  
196 my $uidl = $message->unique;
197 my $lines = $pop->message($uidl);
198  
199 unless(defined $lines)
200 { $lines = [];
201 $self->log(WARNING => "Message $uidl disappeared from POP3 server $self.");
202 }
203  
204 my $parser = Mail::Box::Parser::Perl->new # not parseable by C parser
205 ( filename => "$pop"
206 , file => IO::ScalarArray->new($lines)
207 );
208  
209 my $head = $message->readHead($parser);
210 unless(defined $head)
211 { $self->log(ERROR => "Cannot find head back for $uidl on POP3 server $self.");
212 $parser->stop;
213 return undef;
214 }
215  
216 my $body = $message->readBody($parser, $head);
217 unless(defined $body)
218 { $self->log(ERROR => "Cannot read body for $uidl on POP3 server $self.");
219 $parser->stop;
220 return undef;
221 }
222  
223 $parser->stop;
224  
225 $self->log(PROGRESS => "Loaded message $uidl.");
226 ($head, $body);
227 }
228  
229  
230 sub writeMessages($@)
231 { my ($self, $args) = @_;
232  
233 if(my $modifications = grep {$_->isModified} @{$args->{messages}})
234 { $self->log(WARNING =>
235 "Update of $modifications messages ignored for POP3 folder $self.");
236 }
237  
238 $self;
239 }
240  
241 #-------------------------------------------
242  
243  
244  
245 1;