
=head1 NAME


Mail::Box::Manager - manage a set of folders

=head1 CLASS INHERITANCE

Mail::Box::Manager
   is a Mail::Reporter

=head1 SYNOPSIS


 use Mail::Box::Manager;
 my $mgr     = new Mail::Box::Manager;
 $mgr->registerType(mbox => 'Mail::Box::Mbox');

 # Create folder objects.
 my $folder  = $mgr->open(folder => $ENV{MAIL});
 my $message = $folder->message(0);
 my ($message1, $message2) = ($folder->message(1), $folder->message(2));
 $mgr->copyMessage('Draft', $message);
 $mgr->moveMessage('Outbox', $message1, $message2, create => 1 );
 $mgr->close($folder);

 # Create thread-detectors (see Mail::Box::Thread::Manager)
 my $t       = $mgr->threads($inbox, $outbox);

 my $threads = $mgr->threads(folder => $folder);
 foreach my $thread ($threads->all)
 {   $thread->print;
 }

=head1 DESCRIPTION


The manager keeps track on a set of open folders and a set of message-thread
supporting objects.  You are not obliged to use this object (you can
directly create a Mail::Box::Mbox if you prefer), but you will create
more portable and safer code if you do use it.

=head1 METHODS

=head2 Initiation

=over 4

=item B<new> ARGS X<new>

 OPTION               DEFAULT
 default_folder_type  'mbox'
 folder_types         <all standard types>
 folderdir            [ '.' ]
 folderdirs           <synonym for folderdir>
 log                  'WARNINGS'
 trace                'WARNINGS'

=over 4

=item B<default_folder_type> =E<gt> NAME|CLASS X<new(default_folder_type)>

Specifies the default folder type for newly created folders.  If this
option is not specified, the most recently registered type is used (see
C<registerType> and the C<folder_types> option.

=item B<folder_types> =E<gt> NEW-TYPE | ARRAY-OF-NEW-TYPES X<new(folder_types)>

Add one or more new folder types to the list of known types.  The order is
important: when you open a file without specifying its type, the
manager will start trying the last added list of types, in order.

Each TYPE is specified as an array which contains name, class, and defaults
for options which overrule the usual defaults.
You may specify folder-specific defaults as OPTIONS.  They override
the settings of the manager.

=item B<folderdir> =E<gt> DIRECTORY X<new(folderdir)>

The default directory, or directories, where folders are
located.  C<Mail::Box::Manager> can autodetect the existing folder-types.
There may be different kinds of folders opened at the same time, and
messages can be moved between those types, although that may result in
a loss of information depending on the folder types.

=item B<folderdirs> =E<gt> [ DIRECTORY, ... ] X<new(folderdirs)>

=item B<log> =E<gt> LEVEL

See Mail::Reporter::new(log)

=item B<trace> =E<gt> LEVEL

See Mail::Reporter::new(trace)

=back

=back

=head2 Manage Folders

=over 4

=item B<close> FOLDER, OPTIONS X<close>

C<close> removes the specified folder from the list of open folders.
Indirectly it will update the files on disk if needed (depends on
the C<save_on_exit> flag for each folder). OPTIONS are passed to
the C<close> method of each folder.

The folder's messages will also be withdrawn from the known message threads.
You may also close the folder directly. The manager will be informed
about this event and take appropriate actions.

Examples:

 my $inbox = $mgr->open('inbox');
 $mgr->close($inbox);
 $inbox->close;        # alternative

=item B<closeAllFolders> , OPTIONS X<closeAllFolders>

C<closeAllFolders> calls close() for each folder managed by
this object.  It is called just before the program stops (before global
cleanup).

=item B<decodeFolderURL> URL X<decodeFolderURL>

Try to decompose a folder name which is specified as URL (see open())
into separate options.

=item B<folderTypes>  X<folderTypes>

The C<folderTypes> returns the list of currently defined folder types.

Examples:

 print join("\n", $manager->folderTypes), "\n";

=item B<isOpenFolder> FOLDER X<isOpenFolder>

Returns true if the FOLDER is currently open.

Examples:

 print "Yes\n" if $mgr->isOpenFolder('Inbox');

=item B<open> [FOLDERNAME], OPTIONS X<open>

Open a folder which name is specified as first parameter or with
the option flag C<folder>.  The folder type is autodetected unless
the C<type> is specified.

C<open> carries options for the manager which are described here, but
may also have additional options for the folder type.  For a
description of the folder options, see the options to the constructor
(the Mail::Box::new() method) for each type of mail box.

 OPTION               DEFAULT
 create               <false>
 folder               $ENV{MAIL}
 folderdir            '.'
 type                 <first, usually mbox>

=over 4

=item B<create> =E<gt> BOOLEAN X<open(create)>

Create the folder if it does not exist. By default, this is not done.
The C<type> option specifies which type of folder is created.

=item B<folder> =E<gt> NAME|URL X<open(folder)>

Which folder to open, specified by NAME or special URL.
The URL format is composed as

 type://username:password@hostname:port/foldername

Like real URLs, all fields are optional and have smart defaults, as long
as the string starts with a known folder type.  Far
from all folder types support all these options, but at least they are
all split-out.

When you specify anything which does not match the URL format, it is
passed directly to the C<new> method of the folder which is opened.

=item B<folderdir> =E<gt> DIRECTORY X<open(folderdir)>

The directory where the folders are usually stored.

=item B<type> =E<gt> FOLDERTYPENAME|FOLDERTYPE X<open(type)>

Specify the type of the folder.  If you do not specify this option while
opening a folder for reading, the manager checks all registered folder
types in order for the ability to open the folder. If you open a new
folder for writing, then the default will be the most recently registered
type. (If you add more than one type at once, the first of the list is
used.)

=back

Examples:

 my $jack  = $manager->open(folder => '=jack', type => 'mbox');
 my $rcvd  = $manager->open(type => 'Mail::Box::Mbox', access => 'rw');

 my $inbox = $manager->open('Inbox')
    or die "Cannot open Inbox.\n";

 my $send  = $manager->open('pop3://myself:secret@pop3.server.com:120/x');
 my $send  = $manager->open(folder => '/x', type => 'pop3'
   , username    => 'myself', password => 'secret'
   , server_name => 'pop3.server.com', server_port => '120');

=item B<openFolders>  X<openFolders>

Returns a list of all open folders.

=item B<registerType> TYPE, CLASS [,OPTIONS] X<registerType>

With C<registerType> you can register one TYPE of folders.  The CLASS
is compiled automatically, so you do not need to C<use> them in your own
modules.  The TYPE is just an arbitrary name.

The added types are prepended to the list of known types, so they are
checked first when a folder is opened in autodetect mode.

Examples:

 $manager->registerType(mbox => 'Mail::Box::Mbox',
     save_on_exit => 0, folderdir => '/tmp');

=back

=head2 Move Messages to Folders

=over 4

=item B<appendMessages> [FOLDER|FOLDERNAME,] MESSAGES, OPTIONS X<appendMessages>

Append one or more messages to a folder. You may specify a FOLDERNAME or
an opened folder as the first argument. When the name is that of an
open folder, it is treated as if the folder-object was specified, and
not directly access the folder-files.  You may also specify the
foldername as part of the options list.

If a message is added to an already opened folder, it is only added to
the structure internally in the program.  The data will not be written to
disk until a write of that folder takes place.  When the name of an
unopened folder is given, the folder is opened, the messages stored on
disk, and then the folder is closed.

A message must be an instance of an Mail::Message.  The actual message
type does not have to match the folder type--the folder will try to
resolve the differences with minimal loss of information.  The coerced
messages (how the were actually written) are returned as list.

The OPTIONS is a list of key/values, which are added to (overriding)
the default options for the detected folder type.

Examples:

 $mgr->appendMessages('=send', $message, folderdir => '/');
 $mgr->appendMessages('=received', $inbox->messages);

 my @appended = $mgr->appendMessages($inbox->messages,
    folder => 'Drafts');
 $_->label(seen => 1) foreach @appended;

=item B<copyMessage> [FOLDER|FOLDERNAME,] MESSAGES, OPTIONS X<copyMessage>

Copy a message from one folder into another folder.  If the destination
folder is already opened, the copied message is stored in memory and
written to disk when a write of the folder is later performed. Otherwise,
the destination folder will be opened, the message written, and then the
folder closed.

You need to specify a folder's name or folder object as the first
argument, or in the options list.  The options are the same as those
which can be specified when opening a folder.

Examples:

 my $drafts = $mgr->open(folder => 'Drafts');
 my $outbox = $mgr->open(folder => 'Outbox');
 $mgr->copyMessage($outbox, $drafts->message(0));

 $mgr->copyMessage('Trash', $drafts->message(1), $drafts->message(2),
    folderdir => '/tmp', create => 1);

 $mgr->copyMessage($drafts->message(1), folder => 'Drafts'
    folderdir => '/tmp', create => 1);

=item B<delete> FOLDERNAME [,OPTIONS] X<delete>

Remove the named folder, including all its sub-folders.  The OPTIONS
are the same as those for open().

The deletion of a folder can take some time.  Dependent on the type of
folder, the folder must be read first.  For some folder-types this will
be fast.

=item B<moveMessage> [FOLDER|FOLDERNAME,] MESSAGES, OPTIONS X<moveMessage>

Move a message from one folder to another.  Be warned that removals from
a folder only take place when the folder is closed, so the message is only
flagged to be deleted in the opened source folder.

 $mgr->moveMessage($received, $inbox->message(1))

is equivalent to

 $mgr->copyMessage($received, $inbox->message(1));
 $inbox->message(1)->delete;

=back

=head2 Manage Threads

=over 4

=item B<threads> [FOLDERS], OPTIONS X<threads>

Create a new object which keeps track of message threads.  You can read
about the possible options in the Mail::Box::Thread::Manager documentation.
As OPTIONS specify one folder or an array of FOLDERS.  It is also
permitted to specify folders before the options.

Examples:

 my $t1 = $mgr->threads(folders => [ $inbox, $send ]);
 my $t2 = $mgr->threads($inbox);
 my $t3 = $mgr->threads($inbox, $send);

=item B<toBeThreaded> FOLDER, MESSAGES X<toBeThreaded>

=item B<toBeUnthreaded> FOLDER, MESSAGES X<toBeUnthreaded>

Signal to the manager that all thread managers which are using the
specified folder must be informed that new messages are
coming in (or going out).

=back

=head2 Logging and Tracing

=over 4

=item B<errors> 

See Mail::Reporter::errors()

=item B<log> [LEVEL [,STRINGS]]

See Mail::Reporter::log()

=item B<report> [LEVEL]

See Mail::Reporter::report()

=item B<reportAll> [LEVEL]

See Mail::Reporter::reportAll()

=item B<trace> [LEVEL]

See Mail::Reporter::trace()

=item B<warnings> 

See Mail::Reporter::warnings()

=back

=head2 Other Methods

=over 4

=item B<AUTOLOAD> 

See Mail::Reporter::AUTOLOAD()

=item B<DESTROY> 

See Mail::Reporter::DESTROY()

=item B<inGlobalDestruction> 

See Mail::Reporter::inGlobalDestruction()

=item B<logPriority> LEVEL

See Mail::Reporter::logPriority()

=item B<logSettings> 

See Mail::Reporter::logSettings()

=item B<notImplemented> 

See Mail::Reporter::notImplemented()

=back

=head1 SEE ALSO

A good start to read is Mail::Box-Overview.
More documentation and a mailinglist are available from the project's
website at L<http://perl.overmeer.net/mailbox/>.

=head1 AUTHOR

Mark Overmeer (L<mark@overmeer.net|mailto:mark@overmeer.net>) with the help of many.

=head1 VERSION

This code is beta, version 2.019.

Copyright (c) 2001-2002 Mark Overmeer. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
