/usr/share/perl5/Mail/Box/IMAP4.pm is in libmail-box-perl 2.120-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | # Copyrights 2001-2016 by [Mark Overmeer].
# For other contributors see ChangeLog.
# See the manual pages for details on the licensing terms.
# Pod stripped from pm file by OODoc 2.02.
use strict;
use warnings;
package Mail::Box::IMAP4;
use vars '$VERSION';
$VERSION = '2.120';
use base 'Mail::Box::Net';
use Mail::Box::IMAP4::Message;
use Mail::Box::IMAP4::Head;
use Mail::Transport::IMAP4;
use Mail::Box::Parser::Perl;
use Mail::Message::Head::Complete;
use Mail::Message::Head::Delayed;
use Scalar::Util 'weaken';
sub init($)
{ my ($self, $args) = @_;
my $folder = $args->{folder};
# MailBox names top folder directory '=', but IMAP needs '/'
$folder = '/'
if ! defined $folder || $folder eq '=';
# There's a disconnect between the URL parser and this code.
# The URL parser always produces a full path (beginning with /)
# while this code expects to NOT get a full path. So, we'll
# trim the / from the front of the path.
# Also, this code can't handle a trailing slash and there's
# no reason to ever offer one. Strip that too.
if($folder ne '/')
{ $folder =~ s,^/+,,g;
$folder =~ s,/+$,,g;
}
$args->{folder} = $folder;
my $access = $args->{access} ||= 'r';
my $writeable = $access =~ m/w|a/;
my $ch = $self->{MBI_c_head}
= $args->{cache_head} || ($writeable ? 'NO' : 'DELAY');
$args->{head_type} ||= 'Mail::Box::IMAP4::Head'
if $ch eq 'NO' || $ch eq 'PARTIAL';
$args->{body_type} ||= 'Mail::Message::Body::Lines';
$self->SUPER::init($args);
$self->{MBI_domain} = $args->{domain};
$self->{MBI_c_labels}
= $args->{cache_labels} || ($writeable ? 'NO' : 'DELAY');
$self->{MBI_c_body}
= $args->{cache_body} || ($writeable ? 'NO' : 'DELAY');
my $transport = $args->{transporter} || 'Mail::Transport::IMAP4';
$transport = $self->createTransporter($transport, %$args)
unless ref $transport;
$self->transporter($transport);
defined $transport
or return;
$args->{create}
? $self->create($transport, $args)
: $self;
}
sub create($@)
{ my($self, $name, $args) = @_;
if($args->{access} !~ /w|a/)
{ $self->log(ERROR =>
"You must have write access to create folder $name.");
return undef;
}
$self->transporter->createFolder($name);
}
sub foundIn(@)
{ my $self = shift;
unshift @_, 'folder' if @_ % 2;
my %options = @_;
(exists $options{type} && $options{type} =~ m/^imap/i)
|| (exists $options{folder} && $options{folder} =~ m/^imap/);
}
sub type() {'imap4'}
sub close(@)
{ my $self = shift;
$self->SUPER::close(@_) or return ();
$self->transporter(undef);
$self;
}
sub listSubFolders(@)
{ my ($thing, %args) = @_;
my $self = $thing;
$self = $thing->new(%args) or return () # list toplevel
unless ref $thing;
my $imap = $self->transporter;
defined $imap ? $imap->folders($self) : ();
}
sub nameOfSubfolder($;$) { $_[1] }
#-------------------------------------------
sub readMessages(@)
{ my ($self, %args) = @_;
my $name = $self->name;
return $self if $name eq '/';
my $imap = $self->transporter;
defined $imap or return ();
my @log = $self->logSettings;
my $seqnr = 0;
my $cl = $self->{MBI_c_labels} ne 'NO';
my $wl = $self->{MBI_c_labels} ne 'DELAY';
my $ch = $self->{MBI_c_head};
my $ht = $ch eq 'DELAY' ? $args{head_delayed_type} : $args{head_type};
my @ho = $ch eq 'PARTIAL' ? (cache_fields => 1) : ();
$self->{MBI_selectable}
or return $self;
foreach my $id ($imap->ids)
{ my $head = $ht->new(@log, @ho);
my $message = $args{message_type}->new
( head => $head
, unique => $id
, folder => $self
, seqnr => $seqnr++
, cache_labels => $cl
, write_labels => $wl
, cache_head => ($ch eq 'DELAY')
, cache_body => ($ch ne 'NO')
);
my $body = $args{body_delayed_type}
->new(@log, message => $message);
$message->storeBody($body);
$self->storeMessage($message);
}
$self;
}
sub getHead($)
{ my ($self, $message) = @_;
my $imap = $self->transporter or return;
my $uidl = $message->unique;
my @fields = $imap->getFields($uidl, 'ALL');
unless(@fields)
{ $self->log(WARNING => "Message $uidl disappeared from $self.");
return;
}
my $head = $self->{MB_head_type}->new;
$head->addNoRealize($_) for @fields;
$self->log(PROGRESS => "Loaded head of $uidl.");
$head;
}
sub getHeadAndBody($)
{ my ($self, $message) = @_;
my $imap = $self->transporter or return;
my $uid = $message->unique;
my $lines = $imap->getMessageAsString($uid);
unless(defined $lines)
{ $self->log(WARNING => "Message $uid disappeared from $self.");
return ();
}
my $parser = Mail::Box::Parser::Perl->new # not parseable by C parser
( filename => "$imap"
, file => Mail::Box::FastScalar->new(\$lines)
);
my $head = $message->readHead($parser);
unless(defined $head)
{ $self->log(WARNING => "Cannot find head back for $uid in $self.");
$parser->stop;
return ();
}
my $body = $message->readBody($parser, $head);
unless(defined $body)
{ $self->log(WARNING => "Cannot read body for $uid in $self.");
$parser->stop;
return ();
}
$parser->stop;
$self->log(PROGRESS => "Loaded message $uid.");
($head, $body->contentInfoFrom($head));
}
sub body(;$)
{ my $self = shift;
unless(@_)
{ my $body = $self->{MBI_cache_body} ? $self->SUPER::body : undef;
}
$self->unique();
$self->SUPER::body(@_);
}
sub write(@)
{ my ($self, %args) = @_;
my $imap = $self->transporter or return;
$self->SUPER::write(%args, transporter => $imap) or return;
if($args{save_deleted})
{ $self->log(NOTICE => "Impossible to keep deleted messages in IMAP");
}
else { $imap->destroyDeleted($self->name) }
$self;
}
sub delete(@)
{ my $self = shift;
my $transp = $self->transporter;
$self->SUPER::delete(@_); # subfolders
$transp->deleteFolder($self->name);
}
sub writeMessages($@)
{ my ($self, $args) = @_;
my $imap = $args->{transporter};
my $fn = $self->name;
$_->writeDelayed($fn, $imap) for @{$args->{messages}};
$self;
}
my %transporters;
sub createTransporter($@)
{ my ($self, $class, %args) = @_;
my $hostname = $self->{MBN_hostname} || 'localhost';
my $port = $self->{MBN_port} || '143';
my $username = $self->{MBN_username} || $ENV{USER};
my $join = exists $args{join_connection} ? $args{join_connection} : 1;
my $linkid;
if($join)
{ $linkid = "$hostname:$port:$username";
return $transporters{$linkid} if defined $transporters{$linkid};
}
my $transporter = $class->new
( %args,
, hostname => $hostname, port => $port
, username => $username, password => $self->{MBN_password}
, domain => $self->{MBI_domain}
) or return undef;
if(defined $linkid)
{ $transporters{$linkid} = $transporter;
weaken($transporters{$linkid});
}
$transporter;
}
sub transporter(;$)
{ my $self = shift;
my $imap;
if(@_)
{ $imap = $self->{MBI_transport} = shift;
defined $imap or return;
}
else
{ $imap = $self->{MBI_transport};
}
unless(defined $imap)
{ $self->log(ERROR => "No IMAP4 transporter configured");
return undef;
}
my $name = $self->name;
$self->{MBI_selectable} = $imap->currentFolder($name);
return $imap
if defined $self->{MBI_selectable};
$self->log(ERROR => "Couldn't select IMAP4 folder $name");
undef;
}
sub fetch($@)
{ my ($self, $what, @info) = @_;
my $imap = $self->transporter or return [];
$what = $self->messages($what) unless ref $what eq 'ARRAY';
$imap->fetch($what, @info);
}
#-------------------------------------------
1;
|