From 911d4fce45805d2db99cfc2a6aabb4b8e25d1133 Mon Sep 17 00:00:00 2001 From: Zbigniew Lukasiak Date: Mon, 5 Sep 2011 10:55:09 +0000 Subject: [PATCH] Subject heuristics --- TODO | 4 ++++ lib/Courriel/MMS.pm | 28 +++++++++++++++++++++++++++ lib/Courriel/MMS/Plugin/O2Ie.pm | 9 +-------- lib/Courriel/MMS/Plugin/Tele2LT.pm | 9 +-------- lib/Courriel/MMS/Plugin/VodafoneDE.pm | 9 +-------- lib/Courriel/MMS/Plugin/VodafoneNZ.pm | 9 +-------- t/test.t | 10 +++++----- 7 files changed, 41 insertions(+), 37 deletions(-) create mode 100644 TODO diff --git a/TODO b/TODO new file mode 100644 index 0000000..e749a44 --- /dev/null +++ b/TODO @@ -0,0 +1,4 @@ +When we have too many subclasses - then checking each of them to mach +each of the messages will be too slow. Probably we'll need a hash +translating the email origin domain to the subclass. + diff --git a/lib/Courriel/MMS.pm b/lib/Courriel/MMS.pm index 786b638..25eb9ab 100644 --- a/lib/Courriel/MMS.pm +++ b/lib/Courriel/MMS.pm @@ -35,8 +35,36 @@ sub rebless { return bless $email, $class; } +sub bad_subject {} + # --- Instance methods --- +around subject => sub { + my $orig = shift; + my $self = shift; + my $subject = $self->$orig( @_ ); + if( $self->bad_subject( $subject ) ){ + $subject = ''; + } + if( !length( $subject ) ) { + my $plain_content = $self->plain_content; + if( length( $plain_content ) ) { + ( $subject ) = $plain_content =~ /^([^\.]+\.)/g; # use the first sentence. + if( !$subject ) { + $subject = substr( $plain_content, 0, 25 ); # if still not subject, use some of the text + } + } + else{ + my @images = $self->get_mms_images; + if( @images ) { + $subject = $images[0][0]; # set subject to image filename + } + } + } + return $subject; +}; + + sub plain_content { my $self = shift; my $part = $self->plain_body_part; diff --git a/lib/Courriel/MMS/Plugin/O2Ie.pm b/lib/Courriel/MMS/Plugin/O2Ie.pm index 94e60da..4c9390c 100644 --- a/lib/Courriel/MMS/Plugin/O2Ie.pm +++ b/lib/Courriel/MMS/Plugin/O2Ie.pm @@ -17,17 +17,10 @@ sub match { return; } +sub bad_subject { $_[1] eq 'Multimedia message' } # --- Instance methods --- -around subject => sub { - my $orig = shift; - my $self = shift; - my $subject = $self->$orig; - return undef if $subject eq 'Multimedia message'; - return $subject; -}; - __PACKAGE__->meta()->make_immutable(); 1; diff --git a/lib/Courriel/MMS/Plugin/Tele2LT.pm b/lib/Courriel/MMS/Plugin/Tele2LT.pm index 6c4289b..d06bb99 100644 --- a/lib/Courriel/MMS/Plugin/Tele2LT.pm +++ b/lib/Courriel/MMS/Plugin/Tele2LT.pm @@ -17,17 +17,10 @@ sub match { return; } +sub bad_subject { $_[1] =~ /MMS via e-mail/ } # --- Instance methods --- -around subject => sub { - my $orig = shift; - my $self = shift; - my $subject = $self->$orig; - return undef if $subject =~ /MMS via e-mail/; - return $subject; -}; - __PACKAGE__->meta()->make_immutable(); 1; diff --git a/lib/Courriel/MMS/Plugin/VodafoneDE.pm b/lib/Courriel/MMS/Plugin/VodafoneDE.pm index a2c80ef..e675581 100644 --- a/lib/Courriel/MMS/Plugin/VodafoneDE.pm +++ b/lib/Courriel/MMS/Plugin/VodafoneDE.pm @@ -17,17 +17,10 @@ sub match { return; } +sub bad_subject { $_[1] eq 'Sie haben eine MMS erhalt' } # --- Instance methods --- -around subject => sub { - my $orig = shift; - my $self = shift; - my $subject = $self->$orig; - return undef if $subject eq 'Sie haben eine MMS erhalt'; - return $subject; -}; - override plain_content => sub { my $self = shift; my $part = $self->first_part_matching( diff --git a/lib/Courriel/MMS/Plugin/VodafoneNZ.pm b/lib/Courriel/MMS/Plugin/VodafoneNZ.pm index 547e570..774727f 100644 --- a/lib/Courriel/MMS/Plugin/VodafoneNZ.pm +++ b/lib/Courriel/MMS/Plugin/VodafoneNZ.pm @@ -17,17 +17,10 @@ sub match { return; } +sub bad_subject { $_[1] =~ /^You have a PXT from/ } # --- Instance methods --- -around subject => sub { - my $orig = shift; - my $self = shift; - my $subject = $self->$orig; - return undef if $subject =~ /^You have a PXT from/; - return $subject; -}; - __PACKAGE__->meta()->make_immutable(); 1; diff --git a/t/test.t b/t/test.t index d2a29e0..80c6a6c 100644 --- a/t/test.t +++ b/t/test.t @@ -48,11 +48,11 @@ use File::Slurp 'slurp'; subject('Multimedia message'), from( 'aaa@mms.o2.ie' ), to( 'example@example.com' ), - plain_body( 'test' ), + plain_body( '' ), ); my $email = Courriel::MMS->parse( text => $c_email->as_string ); isa_ok( $email, 'Courriel::MMS::Plugin::O2Ie', 'MMS from O2 Ireland' ); - is( $email->subject, undef, 'subject cleared for O2Ie' ); + is( $email->subject, '', 'subject cleared for O2Ie' ); } { @@ -66,7 +66,7 @@ use File::Slurp 'slurp'; ); my $email = Courriel::MMS->parse( text => $c_email->as_string ); isa_ok( $email, 'Courriel::MMS::Plugin::VodafoneDE', 'MMS from Vodafone DE' ); - is( $email->subject, undef, 'subject cleared for VodafoneDE' ); + is( $email->subject, 'some text', 'subject cleared for VodafoneDE' ); is( $email->plain_content, 'some text', 'plain_content ignored http://www.vodafone.de' ); } @@ -93,7 +93,7 @@ use File::Slurp 'slurp'; ); my $email = Courriel::MMS->parse( text => $c_email->as_string ); isa_ok( $email, 'Courriel::MMS::Plugin::VodafoneNZ', 'MMS from Vodafone New Zealand' ); - is( $email->subject, undef, 'subject cleared for VodafoneNZ' ); + is( $email->subject, 'test', 'subject cleared for VodafoneNZ' ); } { @@ -135,7 +135,7 @@ use File::Slurp 'slurp'; ); my $email = Courriel::MMS->parse( text => $c_email->as_string ); isa_ok( $email, 'Courriel::MMS::Plugin::Tele2LT', 'MMS from Tele2 Lithuania' ); - is( $email->subject, undef, 'subject cleared for Tele2LT' ); + is( $email->subject, 'test', 'subject cleared for Tele2LT' ); } {