| 1 | # Copyrights 2001-2019 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.02. | ||
| 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.005'; | ||
| 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 | |||
| 39 | $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | sub create($@) { undef } # fails | ||
| 44 | |||
| 45 | sub foundIn(@) | ||
| 46 | { my $self = shift; | ||
| 47 | unshift @_, 'folder' if @_ % 2; | ||
| 48 | my %options = @_; | ||
| 49 | |||
| 50 | (exists $options{type} && lc $options{type} eq 'pop3') | ||
| 51 | || (exists $options{folder} && $options{folder} =~ m/^pop/); | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub addMessage($) | ||
| 56 | { my ($self, $message) = @_; | ||
| 57 | |||
| 58 | $self->log(ERROR => "You cannot write a message to a pop server (yet)") | ||
| 59 | if defined $message; | ||
| 60 | |||
| 61 | undef; | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | sub addMessages(@) | ||
| 66 | { my $self = shift; | ||
| 67 | |||
| 68 | # error message described in addMessage() | ||
| 69 | $self->log(ERROR => "You cannot write messages to a pop server (yet)") | ||
| 70 | if @_; | ||
| 71 | |||
| 72 | (); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub type() {'pop3'} | ||
| 76 | |||
| 77 | sub close(@) | ||
| 78 | { my $self = shift; | ||
| 79 | |||
| 80 | $self->SUPER::close(@_); | ||
| 81 | |||
| 82 | my $pop = delete $self->{MBP_client}; | ||
| 83 | $pop->disconnect if defined $pop; | ||
| 84 | |||
| 85 | $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | sub delete(@) | ||
| 90 | { my $self = shift; | ||
| 91 | $self->log(WARNING => "POP3 folders cannot be deleted."); | ||
| 92 | undef; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | sub listSubFolders(@) { () } # no | ||
| 97 | |||
| 98 | |||
| 99 | sub openSubFolder($@) { undef } # fails | ||
| 100 | |||
| 101 | sub topFolderWithMessages() { 1 } # Yes: only top folder | ||
| 102 | |||
| 103 | |||
| 104 | sub update() {shift->notImplemented} | ||
| 105 | |||
| 106 | #------------------------------------------- | ||
| 107 | |||
| 108 | |||
| 109 | sub popClient(%) | ||
| 110 | { my ($self, %args) = @_; | ||
| 111 | |||
| 112 | return $self->{MBP_client} | ||
| 113 | if defined $self->{MBP_client}; | ||
| 114 | |||
| 115 | my $auth = $self->{auth}; | ||
| 116 | |||
| 117 | require Mail::Transport::POP3; | ||
| 118 | my $client = Mail::Transport::POP3->new | ||
| 119 | ( username => $self->{MBN_username} | ||
| 120 | , password => $self->{MBN_password} | ||
| 121 | , hostname => $self->{MBN_hostname} | ||
| 122 | , port => $self->{MBN_port} | ||
| 123 | , authenticate => $self->{MBP_auth} | ||
| 124 | , use_ssl => $args{use_ssl} | ||
| 125 | ); | ||
| 126 | |||
| 127 | $self->log(ERROR => "Cannot create POP3 client for $self.") | ||
| 128 | unless defined $client; | ||
| 129 | |||
| 130 | $self->{MBP_client} = $client; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub readMessages(@) | ||
| 134 | { my ($self, %args) = @_; | ||
| 135 | |||
| 136 | my $pop = $self->popClient or return; | ||
| 137 | my @log = $self->logSettings; | ||
| 138 | my $seqnr = 0; | ||
| 139 | |||
| 140 | foreach my $id ($pop->ids) | ||
| 141 | { my $message = $args{message_type}->new | ||
| 142 | ( head => $args{head_delayed_type}->new(@log) | ||
| 143 | , unique => $id | ||
| 144 | , folder => $self | ||
| 145 | , seqnr => $seqnr++ | ||
| 146 | ); | ||
| 147 | |||
| 148 | my $body = $args{body_delayed_type}->new(@log, message => $message); | ||
| 149 | $message->storeBody($body); | ||
| 150 | |||
| 151 | $self->storeMessage($message); | ||
| 152 | } | ||
| 153 | |||
| 154 | $self; | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | sub getHead($) | ||
| 159 | { my ($self, $message) = @_; | ||
| 160 | my $pop = $self->popClient or return; | ||
| 161 | |||
| 162 | my $uidl = $message->unique; | ||
| 163 | my $lines = $pop->header($uidl); | ||
| 164 | |||
| 165 | unless(defined $lines) | ||
| 166 | { $lines = []; | ||
| 167 | $self->log(WARNING => "Message $uidl disappeared from POP3 server $self."); | ||
| 168 | } | ||
| 169 | |||
| 170 | my $text = join '', @$lines; | ||
| 171 | my $parser = Mail::Box::Parser::Perl->new # not parseable by C parser | ||
| 172 | ( filename => "$pop" | ||
| 173 | , file => Mail::Box::FastScalar->new(\$text) | ||
| 174 | , fix_headers => $self->{MB_fix_headers} | ||
| 175 | ); | ||
| 176 | |||
| 177 | $self->lazyPermitted(1); | ||
| 178 | |||
| 179 | my $head = $message->readHead($parser); | ||
| 180 | $parser->stop; | ||
| 181 | |||
| 182 | $self->lazyPermitted(0); | ||
| 183 | |||
| 184 | $self->log(PROGRESS => "Loaded head of $uidl."); | ||
| 185 | $head; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | sub getHeadAndBody($) | ||
| 190 | { my ($self, $message) = @_; | ||
| 191 | my $pop = $self->popClient or return; | ||
| 192 | |||
| 193 | my $uidl = $message->unique; | ||
| 194 | my $lines = $pop->message($uidl); | ||
| 195 | |||
| 196 | unless(defined $lines) | ||
| 197 | { $lines = []; | ||
| 198 | $self->log(WARNING => "Message $uidl disappeared from POP3 server $self."); | ||
| 199 | } | ||
| 200 | |||
| 201 | my $parser = Mail::Box::Parser::Perl->new # not parseable by C parser | ||
| 202 | ( filename => "$pop" | ||
| 203 | , file => IO::ScalarArray->new($lines) | ||
| 204 | ); | ||
| 205 | |||
| 206 | my $head = $message->readHead($parser); | ||
| 207 | unless(defined $head) | ||
| 208 | { $self->log(ERROR => "Cannot find head back for $uidl on POP3 server $self."); | ||
| 209 | $parser->stop; | ||
| 210 | return undef; | ||
| 211 | } | ||
| 212 | |||
| 213 | my $body = $message->readBody($parser, $head); | ||
| 214 | unless(defined $body) | ||
| 215 | { $self->log(ERROR => "Cannot read body for $uidl on POP3 server $self."); | ||
| 216 | $parser->stop; | ||
| 217 | return undef; | ||
| 218 | } | ||
| 219 | |||
| 220 | $parser->stop; | ||
| 221 | |||
| 222 | $self->log(PROGRESS => "Loaded message $uidl."); | ||
| 223 | ($head, $body); | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | sub writeMessages($@) | ||
| 228 | { my ($self, $args) = @_; | ||
| 229 | |||
| 230 | if(my $modifications = grep {$_->isModified} @{$args->{messages}}) | ||
| 231 | { $self->log(WARNING => | ||
| 232 | "Update of $modifications messages ignored for POP3 folder $self."); | ||
| 233 | } | ||
| 234 | |||
| 235 | $self; | ||
| 236 | } | ||
| 237 | |||
| 238 | #------------------------------------------- | ||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | 1; |