
=head1 NAME

Mail::Box::Thread::Node - one node in a message thread


=head1 INHERITANCE

 Mail::Box::Thread::Node
   is a Mail::Reporter


=head1 SYNOPSIS

 my $node = Mail::Box::Thread::Node->new;
 $node->addMessage($message);
 ...


=head1 DESCRIPTION

The C<Mail::Box::Thread::Node> maintains one node in the linked list of
threads.  Each node contains one message, and a list of its follow-ups.
Next to that, it refers to its own ancestor and contains information
about the trustworthiness of that relationship.

To complicate things a little, because the thread-manager can maintain
multiple folders, and merge there content, you may find the same message
in more folders.  All versions of the same message (based on message-id)
are stored in the same node.



=head1 METHODS


=head2 Constructors


Mail::Box::Thread::Node-E<gt>B<new>(OPTIONS)

=over 4

You will not call this method yourself. The L<Mail::Box::Thread::Manager|Mail::Box::Thread::Manager>
object will call it to construct C<Mail::Box::Thread::Node> objects.
Either a C<message> or a C<messageId> must be supplied.

 Option      Defined in       Default      
 dummy_type                   undef        
 log         L<Mail::Reporter>  C<'WARNINGS'>
 message                      undef        
 messageId                    undef        
 trace       L<Mail::Reporter>  C<'WARNINGS'>

. dummy_type CLASS

=over 4

Indicates the class name of dummy messages. Dummy messages are
placeholders in a L<Mail::Box::Thread::Manager|Mail::Box::Thread::Manager> data structure.

=back

. log LEVEL

. message MESSAGE

=over 4

The MESSAGE which is stored in this node.  The message
must be a Mail::Box::Message.

=back

. messageId MESSAGE-ID

=over 4

The MESSAGE-ID for the message which is stored in this node.  Only
specify it when you don't have the message yet.

=back

. trace LEVEL

=back

=head2 The thread node


$obj-E<gt>B<addMessage>(MESSAGE)

=over 4

Add one message to the thread node.  If the node contains a dummy, then
the dummy is replaced. Otherwise, the messages is added to the end of the
list.

=back

$obj-E<gt>B<expand>([BOOLEAN])

=over 4

Returns whether this (part of the) folder has to be shown expanded or not.
This is simply done by a label, which means that most folder types can
store this.

=back

$obj-E<gt>B<isDummy>

=over 4

Returns true if the message is a dummy. A dummy is a "hole" in a thread
which has follow-ups but does not have a message.

=back

$obj-E<gt>B<message>

=over 4

Get the message which is stored in this thread node.  NOTE: the same
message may be located in many folders at the same time, and these
folders may be controlled by the same thread manager.

In scalar context, this method returns the first instance of the
message that is not deleted. If all instances are flagged for deletion,
then you get the first deleted message. When the open folders only
contain references to the message, but no instance, you get a dummy
message (see L<Mail::Message::Dummy|Mail::Message::Dummy>).

In list context, all instances of the message which have been found are
returned.

I<Example:> 

 my $threads = $mgr->threads(folders => [$draft, $sent]);
 my $node    = $draft->message(1)->thread;

 foreach my $instance ($node->message) {
    print "Found in ", $instance->folder, ".\n";
 }

 print "Subject is ", $node->message->subject, ".\n";

=back

$obj-E<gt>B<messageId>

=over 4

Return the message-id related to this thread node.  Each of the messages
listed in this node will have the same ID.

=back

=head2 The thread order


$obj-E<gt>B<followUps>

=over 4

Returns the list of follow-ups to this thread node.  This list
may contain parsed, not-parsed, and dummy messages.

=back

$obj-E<gt>B<followedBy>(THREADS)

=over 4

Register that the THREADS are follow-ups to this message. These
follow-ups need not be related to each other in any way other than
sharing the same parent.

Defining the same relation more than once will not cause information to
be duplicated.

=back

$obj-E<gt>B<follows>(THREAD, QUALITY)

=over 4

Register that the current thread is a reply to the specified THREAD. The
QUALITY of the relation is specified by the second argument.  The method
returns C<undef> if the link is not accepted in order to avoid circular
references.

The relation may be specified more than once, but only the most confident
relation is used. For example, if a reply (QUALITY equals C<REPLY>) is
specified, later calls to the follow method will have no effect. If
C<follows> is called with a QUALITY that matches the current quality, the
new thread overrides the previous.

=back

$obj-E<gt>B<repliedTo>

=over 4

Returns the message(s) to which the message in this node replies. In
scalar context, this method will return the message to which the message
in this node replies. This message object may be a dummy message.

If the message seems to be the first message of a thread, the value C<undef>
is returned.  (Remember that some MUA are not adding reference information
to the message's header, so you can never be sure a message is the
start of a thread)

In list context, this method returns a second string value indicating the
confidence that the messages are related.  When extended thread discovery
is enabled, then some heuristics are applied to determine if messages are
related. Values for the STRING may be:

=over 4

=item * C<'REPLY'>

This relation was directly derived from an `in-reply-to' message header
field. The relation has a high confidence.

=item * C<'REFERENCE'>

This relation is based on information found in a `Reference' message
header field.  One message may reference a list of messages which
precede it in the thread. The heuristic attempts to determine
relationships between messages assuming that the references are in order.
This relation has a lower confidence.

=item * C<'GUESS'>

The relation is a big guess, with low confidence.  It may be based on
a subject which seems to be related, or commonalities in the message's
body.

=back

More constants may be added later.

I<Example:> 

 my $question = $answer->repliedTo;
 my ($question, $quality) = $answer->repliedTo;
 if($question && $quality eq 'REPLY') { ... };

=back

$obj-E<gt>B<sortedFollowUps>([PREPARE [,COMPARE]])

=over 4

Returns the list of L<followUps()|Mail::Box::Thread::Node/"The thread order">, but sorted.  By default
sorting is based on the estimated time of the reply. See
startTimeEstimate().

=back

=head2 On the whole thread

Some convenience methods are added to threads, to simplify retrieving
information from it.

$obj-E<gt>B<endTimeEstimate>

=over 4

Returns a guess as to when the thread has ended (although you never
know for sure whether there fill follow messages in the future).

=back

$obj-E<gt>B<ids>

=over 4

Returns all the ids in the thread starting at the current thread node.

I<Example:> 

 $newfolder->addMessages($folder->ids($thread->ids));
 $folder->delete($thread->ids);

=back

$obj-E<gt>B<numberOfMessages>

=over 4

Number of messages in the thread starting at the current thread node, but
not counting the dummies.

=back

$obj-E<gt>B<recurse>(CODE-REF)

=over 4

Execute a function for all sub-threads.  If the subroutine returns true,
sub-threads are visited recursively. Otherwise, the current branch
traversal is aborted. The routine is called with the thread-node as the
only argument.

=back

$obj-E<gt>B<startTimeEstimate>

=over 4

Returns a guess as to when the thread was started.  Each message contains
various date specifications (each with various uncertainties resulting
from timezones and out-of-sync clocks). One of these date specifications
is used as the timestamp for the message. If the node contains a dummy
message the lowest timestamp of the replies is returned. Otherwise the
estimated timestamp of the node's message is returned.

=back

$obj-E<gt>B<threadMessages>

=over 4

Returns all the messages in the thread starting at the current thread
node.  This list will not include dummies.

I<Example:> 

 my @t = $folder->message(3)
                ->threadStart
                ->threadMessages;

=back

$obj-E<gt>B<threadToString>([CODE])

=over 4

Translate a thread into a string. The string will contain at least one
line for each message which was found, but tries to fold dummies.  This
is useful for debugging, but most message readers will prefer to
implement their own thread printer.

The optional CODE argument is a reference to a routine which will be called
for each message in the thread.  The routine will be called with the
message as the first argument.  The default shows the subject of the message.
In the first example below, this routine is called seven times.

I<Example:> 

 print $node->threadToString;

may result in

 Subject of this message
 |- Re: Subject of this message
 |-*- Re: Re: Subject of this message
 | |- Re(2) Subject of this message
 | |- [3] Re(2) Subject of this message
 | `- Re: Subject of this message (reply)
 `- Re: Subject of this message

The `*' represents a missing message (a "dummy" message).  The `[3]'
presents a folded thread with three messages.

 print $node->threadToString(\&show);

 sub show($) {
    my $message = shift;
    my $subject = $message->head->get('subject');
    length $subject ? $subject : '<no subject>';
 }

=back

$obj-E<gt>B<totalSize>

=over 4

Returns the sum of the size of all the messages in the thread.

=back

=head2 Error handling


$obj-E<gt>B<AUTOLOAD>

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<addReport>(OBJECT)

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<defaultTrace>([LEVEL]|[LOGLEVEL, TRACELEVEL]|[LEVEL, CALLBACK])

Mail::Box::Thread::Node-E<gt>B<defaultTrace>([LEVEL]|[LOGLEVEL, TRACELEVEL]|[LEVEL, CALLBACK])

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<errors>

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<log>([LEVEL [,STRINGS]])

Mail::Box::Thread::Node-E<gt>B<log>([LEVEL [,STRINGS]])

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<logPriority>(LEVEL)

Mail::Box::Thread::Node-E<gt>B<logPriority>(LEVEL)

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<logSettings>

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<notImplemented>

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<report>([LEVEL])

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<reportAll>([LEVEL])

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<trace>([LEVEL])

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<warnings>

=over 4

See L<Mail::Reporter/"Error handling">

=back

=head2 Cleanup


$obj-E<gt>B<DESTROY>

=over 4

See L<Mail::Reporter/"Cleanup">

=back

$obj-E<gt>B<inGlobalDestruction>

=over 4

See L<Mail::Reporter/"Cleanup">

=back



=head1 DIAGNOSTICS

I<Error:> Package $package does not implement $method.

Fatal error: the specific package (or one of its superclasses) does not
implement this method where it should. This message means that some other
related classes do implement this method however the class at hand does
not.  Probably you should investigate this and probably inform the author
of the package.






=head1 REFERENCES

See the MailBox website at L<http://perl.overmeer.net/mailbox/> for more details.

=head1 COPYRIGHTS

Distribution version 2.058.
Written by Mark Overmeer (mark@overmeer.net).  See the ChangeLog for
other contributors.

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


