/usr/share/perl5/Mail/Box/Manager.pm is in libmail-box-perl 2.117-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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 | # Copyrights 2001-2014 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.01.
use strict;
use warnings;
package Mail::Box::Manager;
use vars '$VERSION';
$VERSION = '2.117';
use base 'Mail::Reporter';
use Mail::Box;
use List::Util 'first';
use Scalar::Util 'weaken';
# failed compilation will not complain a second time
# so we need to keep track.
my %require_failed;
my @basic_folder_types =
( [ mbox => 'Mail::Box::Mbox' ]
, [ mh => 'Mail::Box::MH' ]
, [ maildir => 'Mail::Box::Maildir' ]
, [ pop => 'Mail::Box::POP3' ]
, [ pop3 => 'Mail::Box::POP3' ]
, [ pop3s => 'Mail::Box::POP3s' ]
, [ imap => 'Mail::Box::IMAP4' ]
, [ imap4 => 'Mail::Box::IMAP4' ]
);
my @managers; # usually only one, but there may be more around :(
sub init($)
{ my ($self, $args) = @_;
$self->SUPER::init($args);
# Register all folder-types. There may be some added later.
my @new_types;
if(exists $args->{folder_types})
{ @new_types = ref $args->{folder_types}[0]
? @{$args->{folder_types}}
: $args->{folder_types};
}
my @basic_types = reverse @basic_folder_types;
if(my $basic = $args->{autodetect})
{ my %types = map { ( $_ => 1) } (ref $basic ? @$basic : ($basic));
@basic_types = grep { $types{$_->[0]} } @basic_types;
}
$self->{MBM_folder_types} = [];
$self->registerType(@$_) foreach @new_types, @basic_types;
$self->{MBM_default_type} = $args->{default_folder_type} || 'mbox';
# Inventory on existing folder-directories.
$self->{MBM_folderdirs} = [ ];
if(exists $args->{folderdir})
{ my @dirs = $args->{folderdir};
@dirs = @{$dirs[0]} if ref $dirs[0];
push @{$self->{MBM_folderdirs}}, @dirs;
}
if(exists $args->{folderdirs})
{ my @dirs = $args->{folderdirs};
@dirs = @{$dirs[0]} if ref $dirs[0];
push @{$self->{MBM_folderdirs}}, @dirs;
}
push @{$self->{MBM_folderdirs}}, '.';
$self->{MBM_folders} = [];
$self->{MBM_threads} = [];
push @managers, $self;
weaken $managers[-1];
$self;
}
#-------------------------------------------
sub registerType($$@)
{ my ($self, $name, $class, @options) = @_;
unshift @{$self->{MBM_folder_types}}, [$name, $class, @options];
$self;
}
#-------------------------------------------
sub folderdir()
{ my $dirs = shift->{MBM_folderdirs} or return ();
wantarray ? @$dirs : $dirs->[0];
}
#-------------------------------------------
sub folderTypes()
{ my $self = shift;
my %uniq;
$uniq{$_->[0]}++ foreach @{$self->{MBM_folder_types}};
sort keys %uniq;
}
#-------------------------------------------
sub defaultFolderType()
{ my $self = shift;
my $name = $self->{MBM_default_type};
return $name if $name =~ m/\:\:/; # obviously a class name
foreach my $def (@{$self->{MBM_folder_types}})
{ return $def->[1] if $def->[0] eq $name || $def->[1] eq $name;
}
undef;
}
#-------------------------------------------
sub open(@)
{ my $self = shift;
my $name = @_ % 2 ? shift : undef;
my %args = @_;
$args{authentication} ||= 'AUTO';
$name = defined $args{folder} ? $args{folder} : ($ENV{MAIL} || '')
unless defined $name;
if($name =~ m/^(\w+)\:/ && grep $_ eq $1, $self->folderTypes)
{ # Complicated folder URL
my %decoded = $self->decodeFolderURL($name);
if(keys %decoded)
{ # accept decoded info
@args{keys %decoded} = values %decoded;
}
else
{ $self->log(ERROR => "Illegal folder URL '$name'.");
return;
}
}
else
{ # Simple folder name
$args{folder} = $name;
}
my $type = $args{type};
if(!defined $type) { ; }
elsif($type eq 'pop3')
{ my $un = $args{username} ||= $ENV{USER} || $ENV{LOGIN};
my $srv = $args{server_name} ||= 'localhost';
my $port = $args{server_port} ||= 110;
$args{folder} = $name = "pop3://$un\@$srv:$port";
}
elsif($type eq 'pop3s')
{ my $un = $args{username} ||= $ENV{USER} || $ENV{LOGIN};
my $srv = $args{server_name} ||= 'localhost';
my $port = $args{server_port} ||= 995;
$args{folder} = $name = "pop3s://$un\@$srv:$port";
}
elsif($type eq 'imap4')
{ my $un = $args{username} ||= $ENV{USER} || $ENV{LOGIN};
my $srv = $args{server_name} ||= 'localhost';
my $port = $args{server_port} ||= 143;
$args{folder} = $name = "imap4://$un\@$srv:$port";
}
unless(defined $name && length $name)
{ $self->log(ERROR => "No foldername specified to open.");
return undef;
}
$args{folderdir} ||= $self->{MBM_folderdirs}->[0]
if $self->{MBM_folderdirs};
$args{access} ||= 'r';
if($args{create} && $args{access} !~ m/w|a/)
{ $self->log(WARNING
=> "Will never create a folder $name without having write access.");
undef $args{create};
}
# Do not open twice.
if(my $folder = $self->isOpenFolder($name))
{ $self->log(ERROR => "Folder $name is already open.");
return undef;
}
#
# Which folder type do we need?
#
my ($folder_type, $class, @defaults);
if($type)
{ # User-specified foldertype prevails.
foreach (@{$self->{MBM_folder_types}})
{ (my $abbrev, $class, @defaults) = @$_;
if($type eq $abbrev || $type eq $class)
{ $folder_type = $abbrev;
last;
}
}
$self->log(ERROR => "Folder type $type is unknown, using autodetect.")
unless $folder_type;
}
unless($folder_type)
{ # Try to autodetect foldertype.
foreach (@{$self->{MBM_folder_types}})
{ next unless $_;
(my $abbrev, $class, @defaults) = @$_;
next if $require_failed{$class};
eval "require $class";
if($@)
{ $require_failed{$class}++;
next;
}
if($class->foundIn($name, @defaults, %args))
{ $folder_type = $abbrev;
last;
}
}
}
unless($folder_type)
{ # Use specified default
if(my $type = $self->{MBM_default_type})
{ foreach (@{$self->{MBM_folder_types}})
{ (my $abbrev, $class, @defaults) = @$_;
if($type eq $abbrev || $type eq $class)
{ $folder_type = $abbrev;
last;
}
}
}
}
unless($folder_type)
{ # use first type (last defined)
($folder_type, $class, @defaults) = @{$self->{MBM_folder_types}[0]};
}
#
# Try to open the folder
#
return if $require_failed{$class};
eval "require $class";
if($@)
{ $self->log(ERROR => "Failed for folder default $class: $@");
$require_failed{$class}++;
return ();
}
push @defaults, manager => $self;
my $folder = $class->new(@defaults, %args);
unless(defined $folder)
{ $self->log(WARNING =>
"Folder does not exist, failed opening $folder_type folder $name.")
unless $args{access} eq 'd';
return;
}
$self->log(PROGRESS => "Opened folder $name ($folder_type).");
push @{$self->{MBM_folders}}, $folder;
$folder;
}
sub openFolders() { @{shift->{MBM_folders}} }
sub isOpenFolder($)
{ my ($self, $name) = @_;
first {$name eq $_->name} $self->openFolders;
}
#-------------------------------------------
sub close($@)
{ my ($self, $folder, %options) = @_;
return unless $folder;
my $name = $folder->name;
my @remaining = grep {$name ne $_->name} @{$self->{MBM_folders}};
# folder opening failed:
return if @{$self->{MBM_folders}} == @remaining;
$self->{MBM_folders} = [ @remaining ];
$_->removeFolder($folder) foreach @{$self->{MBM_threads}};
$folder->close(close_by_manager => 1, %options)
unless $options{close_by_self};
$self;
}
#-------------------------------------------
sub closeAllFolders(@)
{ my ($self, @options) = @_;
$_->close(@options) foreach $self->openFolders;
$self;
}
END {map {defined $_ && $_->closeAllFolders} @managers}
#-------------------------------------------
sub delete($@)
{ my ($self, $name, %args) = @_;
my $recurse = delete $args{recursive};
my $folder = $self->open(folder => $name, access => 'd', %args)
or return $self; # still successful
$folder->delete(recursive => $recurse);
}
#-------------------------------------------
sub appendMessage(@)
{ my $self = shift;
my @appended = $self->appendMessages(@_);
wantarray ? @appended : $appended[0];
}
sub appendMessages(@)
{ my $self = shift;
my $folder;
$folder = shift if !ref $_[0] || $_[0]->isa('Mail::Box');
my @messages;
push @messages, shift while @_ && ref $_[0];
my %options = @_;
$folder ||= $options{folder};
# Try to resolve filenames into opened-files.
$folder = $self->isOpenFolder($folder) || $folder
unless ref $folder;
if(ref $folder)
{ # An open file.
unless($folder->isa('Mail::Box'))
{ $self->log(ERROR =>
"Folder $folder is not a Mail::Box; cannot add a message.\n");
return ();
}
foreach (@messages)
{ next unless $_->isa('Mail::Box::Message') && $_->folder;
$self->log(WARNING =>
"Use moveMessage() or copyMessage() to move between open folders.");
}
return $folder->addMessages(@messages);
}
# Not an open file.
# Try to autodetect the folder-type and then add the message.
my ($name, $class, @gen_options, $found);
foreach (@{$self->{MBM_folder_types}})
{ ($name, $class, @gen_options) = @$_;
next if $require_failed{$class};
eval "require $class";
if($@)
{ $require_failed{$class}++;
next;
}
if($class->foundIn($folder, @gen_options, access => 'a'))
{ $found++;
last;
}
}
# The folder was not found at all, so we take the default folder-type.
my $type = $self->{MBM_default_type};
if(!$found && $type)
{ foreach (@{$self->{MBM_folder_types}})
{ ($name, $class, @gen_options) = @$_;
if($type eq $name || $type eq $class)
{ $found++;
last;
}
}
}
# Even the default foldertype was not found (or nor defined).
($name, $class, @gen_options) = @{$self->{MBM_folder_types}[0]}
unless $found;
$class->appendMessages
( type => $name
, messages => \@messages
, @gen_options
, %options
, folder => $folder
);
}
#-------------------------------------------
sub copyMessage(@)
{ my $self = shift;
my $folder;
$folder = shift if !ref $_[0] || $_[0]->isa('Mail::Box');
my @messages;
while(@_ && ref $_[0])
{ my $message = shift;
$self->log(ERROR =>
"Use appendMessage() to add messages which are not in a folder.")
unless $message->isa('Mail::Box::Message');
push @messages, $message;
}
my %args = @_;
$folder ||= $args{folder};
my $share = exists $args{share} ? $args{share} : $args{_delete};
# Try to resolve filenames into opened-files.
$folder = $self->isOpenFolder($folder) || $folder
unless ref $folder;
unless(ref $folder)
{ my @c = $self->appendMessages(@messages, %args, folder => $folder);
if($args{_delete})
{ $_->label(deleted => 1) for @messages;
}
return @c;
}
my @coerced;
foreach my $msg (@messages)
{ if($msg->folder eq $folder) # ignore move to same folder
{ push @coerced, $msg;
next;
}
push @coerced, $msg->copyTo($folder, share => $args{share});
$msg->label(deleted => 1) if $args{_delete};
}
@coerced;
}
#-------------------------------------------
sub moveMessage(@)
{ my $self = shift;
$self->copyMessage(@_, _delete => 1);
}
#-------------------------------------------
sub threads(@)
{ my $self = shift;
my @folders;
push @folders, shift
while @_ && ref $_[0] && $_[0]->isa('Mail::Box');
my %args = @_;
my $base = 'Mail::Box::Thread::Manager';
my $type = $args{threader_type} || $base;
my $folders = delete $args{folder} || delete $args{folders};
push @folders
, ( !$folders ? ()
: ref $folders eq 'ARRAY' ? @$folders
: $folders
);
$self->log(INTERNAL => "No folders specified.")
unless @folders;
my $threads;
if(ref $type)
{ # Already prepared object.
$self->log(INTERNAL => "You need to pass a $base derived")
unless $type->isa($base);
$threads = $type;
}
else
{ # Create an object. The code is compiled, which safes us the
# need to compile Mail::Box::Thread::Manager when no threads are needed.
eval "require $type";
$self->log(INTERNAL => "Unusable threader $type: $@") if $@;
$self->log(INTERNAL => "You need to pass a $base derived")
unless $type->isa($base);
$threads = $type->new(manager => $self, %args);
}
$threads->includeFolder($_) foreach @folders;
push @{$self->{MBM_threads}}, $threads;
$threads;
}
#-------------------------------------------
sub toBeThreaded($@)
{ my $self = shift;
$_->toBeThreaded(@_) foreach @{$self->{MBM_threads}};
}
sub toBeUnthreaded($@)
{ my $self = shift;
$_->toBeUnthreaded(@_) foreach @{$self->{MBM_threads}};
}
sub decodeFolderURL($)
{ my ($self, $name) = @_;
return unless
my ($type, $username, $password, $hostname, $port, $path)
= $name =~ m!^(\w+)\: # protocol
(?://
(?:([^:@/]*) # username
(?:\:([^@/]*))? # password
\@)?
([\w.-]+)? # hostname
(?:\:(\d+))? # port number
)?
(.*) # foldername
!x;
$username ||= $ENV{USER} || $ENV{LOGNAME};
$password ||= '';
for($username, $password)
{ s/\+/ /g;
s/\%([A-Fa-f0-9]{2})/chr hex $1/ge;
}
$hostname ||= 'localhost';
$path ||= '=';
( type => $type, folder => $path
, username => $username, password => $password
, server_name => $hostname, server_port => $port
);
}
#-------------------------------------------
1;
|