Navigation Menu

Skip to content

Commit

Permalink
Subject heuristics
Browse files Browse the repository at this point in the history
  • Loading branch information
Zbigniew Lukasiak committed Sep 5, 2011
1 parent 064f9b1 commit 911d4fc
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 37 deletions.
4 changes: 4 additions & 0 deletions 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.

28 changes: 28 additions & 0 deletions lib/Courriel/MMS.pm
Expand Up @@ -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;
Expand Down
9 changes: 1 addition & 8 deletions lib/Courriel/MMS/Plugin/O2Ie.pm
Expand Up @@ -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;
Expand Down
9 changes: 1 addition & 8 deletions lib/Courriel/MMS/Plugin/Tele2LT.pm
Expand Up @@ -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;
Expand Down
9 changes: 1 addition & 8 deletions lib/Courriel/MMS/Plugin/VodafoneDE.pm
Expand Up @@ -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(
Expand Down
9 changes: 1 addition & 8 deletions lib/Courriel/MMS/Plugin/VodafoneNZ.pm
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions t/test.t
Expand Up @@ -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' );
}

{
Expand All @@ -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' );
}

Expand All @@ -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' );
}

{
Expand Down Expand Up @@ -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' );
}

{
Expand Down

0 comments on commit 911d4fc

Please sign in to comment.