| 1 | package Email::Store::Summary; | ||
| 2 | use 5.006; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $VERSION = '1.2'; | ||
| 6 | use base 'Email::Store::DBI'; | ||
| 7 | use Email::MIME; | ||
| 8 | Email::Store::Summary->table("summary"); | ||
| 9 | Email::Store::Summary->columns(All => qw/mail subject original/); | ||
| 10 | Email::Store::Summary->columns(Primary => qw/mail/); | ||
| 11 | |||
| 12 | use Text::Original qw(first_sentence); | ||
| 13 | sub on_store_order { 80 } | ||
| 14 | |||
| 15 | sub on_store { | ||
| 16 | my ($self, $mail) = @_; | ||
| 17 | my $mime = Email::MIME->new($mail->message); | ||
| 18 | my $subject = $mime->header("Subject"); | ||
| 19 | $subject =~ s/^(\s*(re|aw):\s*)+//i; | ||
| 20 | |||
| 21 | # There's a bit of hackery here. | ||
| 22 | my $body = $mail->simple->body; | ||
| 23 | my $charset = $mime->{ct}->{attributes}{charset}; | ||
| 24 | if ($charset and $charset !~ /utf-?8/) { | ||
| 25 | eval { | ||
| 26 | require Encode; | ||
| 27 | $body = Encode::decode($charset, $body); | ||
| 28 | }; | ||
| 29 | } | ||
| 30 | |||
| 31 | Email::Store::Summary->create({ | ||
| 32 | mail => $mail->id, | ||
| 33 | subject => $subject, | ||
| 34 | original => first_sentence($body) | ||
| 35 | }); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub on_gather_plucene_fields_order { 80 } | ||
| 39 | sub on_gather_plucene_fields { | ||
| 40 | my ($self, $mail, $hash) = @_; | ||
| 41 | $hash->{subject} = $mail->subject; | ||
| 42 | } | ||
| 43 | |||
| 44 | Email::Store::Summary->has_a(mail => "Email::Store::Mail"); | ||
| 45 | Email::Store::Mail->might_have( | ||
| 46 | summary => "Email::Store::Summary" => qw(subject original) | ||
| 47 | ); | ||
| 48 | |||
| 49 | =head1 NAME | ||
| 50 | |||
| 51 | Email::Store::Summary - Provide subject and first-sentence for a mail | ||
| 52 | |||
| 53 | =head1 SYNOPSIS | ||
| 54 | |||
| 55 | Remember to create the database table: | ||
| 56 | |||
| 57 | % make install | ||
| 58 | % perl -MEmail::Store="..." -e 'Email::Store->setup' | ||
| 59 | |||
| 60 | And now: | ||
| 61 | |||
| 62 | $mail->subject; | ||
| 63 | $mail->original; | ||
| 64 | |||
| 65 | =head1 DESCRIPTION | ||
| 66 | |||
| 67 | This extension for C<Email::Store> adds the C<summary> table, and exports | ||
| 68 | the C<subject> and C<original> methods to the C<Email::Store::Mail> class; | ||
| 69 | C<subject> is the subject of the mail (duh) and C<original> uses | ||
| 70 | L<Text::Original> to extract the first unquoted sentence of the mail. | ||
| 71 | |||
| 72 | =head1 SEE ALSO | ||
| 73 | |||
| 74 | L<Email::Store::Mail>, L<Text::Original>. | ||
| 75 | |||
| 76 | =head1 AUTHOR | ||
| 77 | |||
| 78 | The original author is Simon Cozens, E<lt>simon@cpan.orgE<gt> | ||
| 79 | Currently maintained by Simon Wistow E<lt>simon@thegestalt.orgE<gt> | ||
| 80 | |||
| 81 | =head1 SUPPORT | ||
| 82 | |||
| 83 | This module is part of the Perl Email Project - http://pep.kwiki.org/ | ||
| 84 | |||
| 85 | There is a mailing list at pep@perl.org (subscribe at pep-subscribe@perl.org) | ||
| 86 | and an archive available at http://nntp.perl.org/group/pep.php | ||
| 87 | |||
| 88 | =head1 COPYRIGHT | ||
| 89 | |||
| 90 | Copyright 2004, 2005 Simon Cozens | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | 1; | ||
| 95 | __DATA__ |