| 1 | package SWISH::Prog::Aggregator::MailFS; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use base qw( SWISH::Prog::Aggregator::FS ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | use Path::Class (); | ||
| 6 | use SWISH::Prog::Aggregator::Mail; # delegate doc creation | ||
| 7 | use Carp; | ||
| 8 | use Data::Dump qw( dump ); | ||
| 9 | |||
| 10 | our $VERSION = '0.75'; | ||
| 11 | |||
| 12 | =pod | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | SWISH::Prog::Aggregator::MailFS - crawl a filesystem of email messages | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use SWISH::Prog::Aggregator::MailFS; | ||
| 21 | my $fs = SWISH::Prog::Aggregator::MailFS->new( | ||
| 22 | indexer => SWISH::Prog::Indexer->new | ||
| 23 | ); | ||
| 24 | |||
| 25 | $fs->indexer->start; | ||
| 26 | $fs->crawl( $path_to_mail ); | ||
| 27 | $fs->indexer->finish; | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | SWISH::Prog::Aggregator::MailFS is a subclass of SWISH::Prog::Aggregator::FS | ||
| 32 | that expects every file in a filesystem to be an email message. | ||
| 33 | This class is useful for crawling a file tree like those managed by ezmlm. | ||
| 34 | |||
| 35 | B<NOTE:> This class will B<not> work with personal email boxes | ||
| 36 | in the Mbox format. It might work with maildir format, but that is | ||
| 37 | coincidental. Use SWISH::Prog::Aggregator::Mail to handle your personal | ||
| 38 | email box. Use this class to handle mail archives as with a mailing list. | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | =head1 METHODS | ||
| 43 | |||
| 44 | See SWISH::Prog::Aggregator::FS. Only new or overridden methods are documented | ||
| 45 | here. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | =head2 init | ||
| 50 | |||
| 51 | Constructor. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub init { | ||
| 56 | my $self = shift; | ||
| 57 | $self->SUPER::init(@_); | ||
| 58 | |||
| 59 | # cache a Mail aggregator to use its get_doc method | ||
| 60 | $self->{_mailer} = SWISH::Prog::Aggregator::Mail->new( | ||
| 61 | indexer => $self->indexer, | ||
| 62 | verbose => $self->verbose, | ||
| 63 | debug => $self->debug, | ||
| 64 | ); | ||
| 65 | |||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | =head2 file_ok( I<full_path> ) | ||
| 70 | |||
| 71 | Like the parent class method, but ignores file extension, assuming | ||
| 72 | that all files are email messages. | ||
| 73 | |||
| 74 | Returns the I<full_path> value if the file is ok for indexing; | ||
| 75 | returns 0 if not ok. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub file_ok { | ||
| 80 | my $self = shift; | ||
| 81 | my $full_path = shift; | ||
| 82 | my $stat = shift; | ||
| 83 | |||
| 84 | $self->debug and warn "checking file $full_path\n"; | ||
| 85 | |||
| 86 | return 0 if $full_path =~ m[\\/]!; # TODO configure this. | ||
| 87 | |||
| 88 | $stat ||= [ stat($full_path) ]; | ||
| 89 | return 0 unless -r _; | ||
| 90 | return 0 if -d _; | ||
| 91 | if ( $self->ok_if_newer_than | ||
| 92 | and $self->ok_if_newer_than >= $stat->[9] ) | ||
| 93 | { | ||
| 94 | return 0; | ||
| 95 | } | ||
| 96 | return 0 | ||
| 97 | if ( $self->_apply_file_rules($full_path) | ||
| 98 | && !$self->_apply_file_match($full_path) ); | ||
| 99 | |||
| 100 | $self->debug and warn " $full_path -> ok\n"; | ||
| 101 | if ( $self->verbose & 4 ) { | ||
| 102 | local $| = 1; # don't buffer | ||
| 103 | print "crawling $full_path\n"; | ||
| 104 | } | ||
| 105 | |||
| 106 | return $full_path; | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 get_doc( I<url> ) | ||
| 110 | |||
| 111 | Overrides parent class to delegate the creation of the | ||
| 112 | SWISH::Prog::Doc object to SWISH::Prog::Aggregator::Mail->get_doc(). | ||
| 113 | |||
| 114 | Returns a SWISH::Prog::Doc object. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | sub get_doc { | ||
| 119 | my $self = shift; | ||
| 120 | |||
| 121 | # there's some wasted overhead here in creating a | ||
| 122 | # SWISH::Prog::Doc 2x. But we're optimizing here for | ||
| 123 | # developer time... | ||
| 124 | |||
| 125 | # mostly a slurp convenience | ||
| 126 | my $doc = $self->SUPER::get_doc(@_); | ||
| 127 | |||
| 128 | #carp "first pass for raw doc: " . dump($doc); | ||
| 129 | |||
| 130 | # get the "folder" | ||
| 131 | my $folder = Path::Class::file( $doc->url )->dir; | ||
| 132 | |||
| 133 | # now convert the buffer to an email message | ||
| 134 | my $msg = Mail::Message->read( \$doc->content ); | ||
| 135 | |||
| 136 | # and finally convert to the SWISH::Prog::Doc we intend to return | ||
| 137 | my $mail = $self->{_mailer}->get_doc( $folder, $msg ); | ||
| 138 | |||
| 139 | # reinstate original url from filesystem | ||
| 140 | $mail->url($doc->url); | ||
| 141 | |||
| 142 | #carp "second pass for mail doc: " . dump($mail); | ||
| 143 | |||
| 144 | return $mail; | ||
| 145 | } | ||
| 146 | |||
| 147 | 1; | ||
| 148 | |||
| 149 | __END__ | ||