The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mail::Box::Threads - maintain threads within a folder

SYNOPSIS

   my Mail::Box $folder = ...;
   foreach my $thread ($folder->threads)
   {   $thread->print;
   }

DESCRIPTION

Read Mail::Box::Manager and Mail::Box first. The manual also describes package Mail::Box::Thread, which is one thread.

A (message-)thread is a message, with the messages which followed in reply on that message. And the messages with replied the messages which replied the original message. And so on. Some threads are only one message (never replied to), some threads are very long.

How it works

This module implements thread-detection on a folder. Messages created by the better mailers will include In-Reply-To and References lines, which are used to figure out how messages are related. If you prefer a better thread detection, then you can ask for it, but there may be a serious performance hit (depends on the type of folder used).

In this object, we take special care not to cause unnessesary parsing (loading) of messages. Threads will only be detected on command, and by default only the message headers are used.

How to use it

With allThreads you get the start-messages of each detected threads. When that message was not found in the folder (not saved or already removed), you get a message of the dummy-type. These thread descriptions are in perfect state: all messages are included somewhere.

However, to be able to detect all threads it is required to have the headers of all messages, which is very slow for some types of folders. For interactive mail-readers, it is prefered to detect threads only on messages which are in the viewport of the user. This may be sloppy in some situations, but everything is preferable over reading an MH mailbox with 10k e-mails to read only the most recent messages.

PUBLIC INTERFACE

new ARGS

Mail::Box::Threads is sub-classed by Mail::Box itself. This object is not meant to be instantiated itself: do not call new on it (you'll see it even fails because there is no new()!).

The construction of thread administration accepts the following options:

  • dummy_type => CLASS

    Of which class are dummy messages? Usually, this needs to be the message_type of the folder prepended with ::Dummy. This will also be the default.

  • thread_body => BOOL

    May thread-detection be based on the content of a message? This has a serious performance implication when there are many messages without In-Reply-To and References headers in the folder, because it will cause many messages to be parsed.

    NOT USED YET. Defaults to TRUE.

createDummy MESSAGE-ID

Create a dummy message for this folder. The dummy is a place-holder in a thread description to represent a message which is not found in the folder (yet).

detectThread MESSAGE

Based on a message, and facts from previously detected threads, try to build solid knowledge about the thread where this message is in.

registerThread MESSAGE|MESSAGE-ID

Register the message as start of a thread.

allThreads

Returns all messages which start a thread. The list may contain dummy messages, and messages which are scheduled for deletion.

To be able to return all threads, thread construction on each message is performed first, which may be slow for some folder-types because is will enforce parsing of message-bodies.

knownThreads

Return the list of all messages which are known to be the start of a thread. Threads are detected based on explicitly calling detectThread with a messages from the folder.

Mail::Box::Thread

A thread implements a list of messages which are related. The main object described in the manual-page is the thread-manager, which is part of a Mail::Box. The Mail::Box::Thread is sub-classed by a Mail::Box::Message; each message is part of a thread.

new ARGS

The instatiation of a thread is done by its subclasses. You will not call this method by yourself (it is even not implemented).

In the current implementation, there are no options added to the Mail::Box::Message's object creation.

myThread

Returns the first message in the thread where this message is part of. This may be this message itself. This also may return any other message in the folder. Even a dummy message can be returned, when the first message in the thread was not stored in the folder.

Example: my $start = $folder->message(42)->myThread;

repliedTo

Returns the message where this one is a reply to. In SCALAR context, this will return the MESSAGE which was replied to by this one. This message object may be a dummy message. In case the message seems to be the first message of a thread, the value undef is returned.

In LIST context, this method also returns how sure these are messages are related. When extended thread discovery in enabled, then some magic is applied to relate messages. In LIST context, the first returned argment is a MESSAGE, and the second a STRING constant. Values for the STRING may be:

  • REPLY

    This relation was directly derived from an `in-reply-to' message header field. The relation is very sure.

  • 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. Let's hope they are stored in the right order.

  • GUESS

    The relation is a big guess, of undetermined type.

More constants may be added later.

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

follows MESSAGE, STRING

Register that the specified MESSAGE is a reply on this message, where the quality of the relation is specified by the constant STRING. The relation may be specified more than once, but there can be only one. Once a reply (STRING equals REPLY) is detected, that value will be kept.

followedBy [MESSAGE-ID|MESSAGE, ...]

Register that the MESSAGEs (or MESSAGE-IDs) are follow-ups to this message. There may be more than one of these follow-ups which are not related to each-other in any other way than sharing the same parent.

If the same relation is defined more than ones, this will not cause duplication of information.

followUps

Returns the list of follow-ups to this message. This list contains parsed, not-parsed, and dummy messages. followUps returns MESSAGE-objects, while followUpIDs returns the IDs only.

Actions on whole threads

Some conveniance methods are added to threads, to simplify retreiving knowledge from it.

recurseThread CODE-REF

Execute a function for all sub-threads.

totalSize

Sum the size of all the messages in the thread.

nrMessages

Number of messages in this thread.

ids

Collect all the ids in this thread.

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

AUTHOR

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

VERSION

This code is alpha, version 0.7

1 POD Error

The following errors were encountered while parsing the POD:

Around line 499:

You forgot a '=back' before '=head1'