| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package Mail::Summary::Tools::ThreadFilter::Util; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Sub::Exporter -setup => { | ||
| 9 | exports => [qw/ | ||
| 10 | get_root_message guess_mailing_list | ||
| 11 | thread_root last_in_thread any_in_thread all_in_thread | ||
| 12 | negate | ||
| 13 | mailing_list_is in_date_range | ||
| 14 | /], | ||
| 15 | }; | ||
| 16 | |||
| 17 | use Mail::ListDetector; | ||
| 18 | use Date::Range; | ||
| 19 | use DateTime::Format::Mail; | ||
| 20 | |||
| 21 | { | ||
| 22 | package Date::Range::Forgiving; | ||
| 23 | use base qw/Date::Range/; | ||
| 24 | sub want_class { "UNIVERSAL" } | ||
| 25 | } | ||
| 26 | |||
| 27 | use Scalar::Util qw/reftype blessed/; | ||
| 28 | |||
| 29 | sub get_root_message ($) { | ||
| 30 | my $thread = shift; | ||
| 31 | my $message = $thread->message; | ||
| 32 | $message = ($thread->threadMessages)[0] if !$message or $message->isDummy; | ||
| 33 | |||
| 34 | die "Couldn't determine thread root!" if !$message or $message->isDummy; | ||
| 35 | |||
| 36 | return $message; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub negate ($) { | ||
| 40 | my $filter = shift; | ||
| 41 | return sub { not( $filter->( @_ ) ) }; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub thread_root ($) { | ||
| 45 | my $filter = shift; | ||
| 46 | |||
| 47 | return sub { | ||
| 48 | my $thread = shift; | ||
| 49 | $filter->( get_root_message( $thread ) ); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | sub last_in_thread ($) { | ||
| 54 | my $filter = shift; | ||
| 55 | |||
| 56 | return sub { | ||
| 57 | my $thread = shift; | ||
| 58 | my $last = ($thread->threadMessages)[-1]; | ||
| 59 | $filter->( $last ); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | sub any_in_thread ($) { | ||
| 64 | my $filter = shift; | ||
| 65 | return sub { | ||
| 66 | my $thread = shift; | ||
| 67 | |||
| 68 | my $match; | ||
| 69 | $thread->recurse(sub { | ||
| 70 | my $message = shift->message; | ||
| 71 | return 1 if $message->isDummy; | ||
| 72 | |||
| 73 | if ( $filter->( $message ) ) { | ||
| 74 | $match = 1; | ||
| 75 | return 0; # short circuit the recursion | ||
| 76 | } else { | ||
| 77 | return 1; | ||
| 78 | } | ||
| 79 | }); | ||
| 80 | |||
| 81 | return $match; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | sub all_in_thread ($) { | ||
| 86 | my $filter = shift; | ||
| 87 | |||
| 88 | return sub { | ||
| 89 | my $thread = shift; | ||
| 90 | |||
| 91 | my $match = 1; | ||
| 92 | $thread->recurse(sub { | ||
| 93 | my $message = shift->message; | ||
| 94 | return 1 if $message->isDummy; | ||
| 95 | |||
| 96 | unless ( $filter->( $message ) ) { | ||
| 97 | $match = 0; | ||
| 98 | return 0; # short circuit the recursion | ||
| 99 | } else { | ||
| 100 | return 1; | ||
| 101 | } | ||
| 102 | }); | ||
| 103 | |||
| 104 | return $match; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | sub guess_mailing_list ($) { | ||
| 109 | my $message = shift; | ||
| 110 | Mail::ListDetector->new( $message ); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub mailing_list_is ($) { | ||
| 114 | my $matchsub = _munge_list_match(shift); | ||
| 115 | return sub { | ||
| 116 | my $message = shift; | ||
| 117 | my $list = guess_mailing_list( $message ); | ||
| 118 | $list && $matchsub->( $list ); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | sub _munge_list_match { | ||
| 123 | my $match = shift; | ||
| 124 | if ( blessed($match) ) { return sub { $match->match( shift ) } } | ||
| 125 | elsif ( ref($match) && reftype($match) eq "CODE" ) { return $match } | ||
| 126 | else { return sub { no warnings 'uninitialized'; shift->listname eq $match } } | ||
| 127 | } | ||
| 128 | |||
| 129 | sub in_date_range ($$) { | ||
| 130 | my $range = Date::Range::Forgiving->new( @_ ); | ||
| 131 | |||
| 132 | return sub { | ||
| 133 | my $message = shift; | ||
| 134 | my $date_header = $message->head->get('Date')->unfoldedBody; | ||
| 135 | my $date; | ||
| 136 | |||
| 137 | $date_header =~ s/\s*<\S+@\S+>\s*$//; # seen numerous times | ||
| 138 | |||
| 139 | my @errors; | ||
| 140 | $date = eval { DateTime::Format::Mail->new->loose->parse_datetime( $date_header ) }; | ||
| 141 | push @errors, $@ if $@; | ||
| 142 | $date ||= eval { DateTime::Format::DateManip->parse_datetime( $date_header ) }; | ||
| 143 | push @errors, $@ if $@; | ||
| 144 | |||
| 145 | die "Error parsing date '$date_header': @errors" unless defined $date; | ||
| 146 | |||
| 147 | return $range->includes( $date ); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | __PACKAGE__; | ||
| 152 | |||
| 153 | __END__ |