Skip to content
Browse files

plain_content method; new subclasses

  • Loading branch information...
1 parent c17864b commit 064f9b1f78bf8cfb3f259c8c1c01c3774aee3ce3 Zbigniew Lukasiak committed Sep 5, 2011
View
14 lib/Courriel/MMS.pm
@@ -25,13 +25,25 @@ around 'parse' => sub {
my $email = $self->$orig( @_ );
for my $class ( $email->plugins ){
- return bless( $email, $class ) if $class->match( $email );
+ return $class->rebless( $email ) if $class->match( $email );
}
return $email;
};
+sub rebless {
+ my( $class, $email ) = @_;
+ return bless $email, $class;
+}
+
# --- Instance methods ---
+sub plain_content {
+ my $self = shift;
+ my $part = $self->plain_body_part;
+ return '' if !defined $part;
+ return $part->content
+}
+
sub _get_image_parts {
my $self = shift;
return $self->all_parts_matching(
View
60 lib/Courriel/MMS/Plugin/O2Ie.pm
@@ -0,0 +1,60 @@
+use strict;
+use warnings;
+
+package Courriel::MMS::Plugin::O2Ie;
+use namespace::autoclean;
+use Moose;
+
+extends 'Courriel::MMS';
+
+# --- Class methods ---
+
+sub match {
+ my $class = shift;
+ my $email = shift;
+
+ return 1 if $email->from =~ /mms\.o2\.ie/;
+ return;
+}
+
+
+# --- 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;
+
+__END__
+
+# ABSTRACT: L<Courriel::MMS> extension for dealing with MMS messages from O2 Ireland.
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head1 CLASS METHODS
+
+=head1 INSTANCE METHODS
+
+=head2 C<get_mms_images>
+
+=head1 SEE ALSO
+
+=head2 L<<< Courriel >>>
+
+=head1 AUTHOR
+
+Zbigniew Łukasiak, E<lt>zlukasiak@opera.comE<gt>
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright (c), 2011 Opera Software ASA.
+All rights reserved.
View
62 lib/Courriel/MMS/Plugin/RadiolinjaFI.pm
@@ -0,0 +1,62 @@
+use strict;
+use warnings;
+
+package Courriel::MMS::Plugin::RadiolinjaFI;
+use namespace::autoclean;
+use Moose;
+
+extends 'Courriel::MMS';
+
+# --- Class methods ---
+
+sub match {
+ my $class = shift;
+ my $email = shift;
+
+ return 1 if $email->from =~ /mmsc\.radiolinja\.fi/;
+ return;
+}
+
+
+# --- Instance methods ---
+
+around plain_content => sub {
+ my $orig = shift;
+ my $self = shift;
+ my $content = $self->$orig();
+ return '' if $content eq 'This is a HTML message, sorry';
+ return $content;
+};
+
+
+
+__PACKAGE__->meta()->make_immutable();
+
+1;
+
+__END__
+
+# ABSTRACT: L<Courriel::MMS> extension for dealing with MMS messages from Telenor SE.
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head1 CLASS METHODS
+
+=head1 INSTANCE METHODS
+
+=head2 C<get_mms_images>
+
+=head1 SEE ALSO
+
+=head2 L<<< Courriel >>>
+
+=head1 AUTHOR
+
+Zbigniew Łukasiak, E<lt>zlukasiak@opera.comE<gt>
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright (c), 2011 Opera Software ASA.
+All rights reserved.
View
7 lib/Courriel/MMS/Plugin/SprintUS.pm
@@ -23,15 +23,12 @@ sub match {
# --- Instance methods ---
-# this should really override the ->content method on $self->text_body_part
-# but how to do that elegantly?
-#
-sub text_content {
+sub plain_content {
my $self = shift;
my $html = $self->html_body_part->content;
- my ($body) = $html->[0] =~ m#<tr>\s+<td><pre[^>]+>(.*?)</pre></td>\s+</tr>#gs;
+ my ($body) = $html =~ m#<tr>\s+<td><pre[^>]+>(.*?)</pre></td>\s+</tr>#gs;
return $body;
}
View
60 lib/Courriel/MMS/Plugin/Tele2LT.pm
@@ -0,0 +1,60 @@
+use strict;
+use warnings;
+
+package Courriel::MMS::Plugin::Tele2LT;
+use namespace::autoclean;
+use Moose;
+
+extends 'Courriel::MMS';
+
+# --- Class methods ---
+
+sub match {
+ my $class = shift;
+ my $email = shift;
+
+ return 1 if $email->from =~ /mms\.tele2/;
+ return;
+}
+
+
+# --- 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;
+
+__END__
+
+# ABSTRACT: L<Courriel::MMS> extension for dealing with MMS messages from Tele2 Lithuania.
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head1 CLASS METHODS
+
+=head1 INSTANCE METHODS
+
+=head2 C<get_mms_images>
+
+=head1 SEE ALSO
+
+=head2 L<<< Courriel >>>
+
+=head1 AUTHOR
+
+Zbigniew Łukasiak, E<lt>zlukasiak@opera.comE<gt>
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright (c), 2011 Opera Software ASA.
+All rights reserved.
View
65 lib/Courriel/MMS/Plugin/TelenorSE.pm
@@ -0,0 +1,65 @@
+use strict;
+use warnings;
+
+package Courriel::MMS::Plugin::TelenorSE;
+use namespace::autoclean;
+use Moose;
+
+extends 'Courriel::MMS';
+
+# --- Class methods ---
+
+sub match {
+ my $class = shift;
+ my $email = shift;
+
+ return 1 if $email->from =~ /mms-email\.telenor\.se/;
+ return;
+}
+
+
+# --- Instance methods ---
+
+around plain_content => sub {
+ my $orig = shift;
+ my $self = shift;
+ my $content = $self->$orig();
+ my $html = $self->html_body_part->content;
+ if( $html =~ m#<h1.*?</div>\n<div class="text-400">\n(.*?)\n</div>#s ){
+ $content = $content . $1;
+ }
+ return $content;
+};
+
+
+
+__PACKAGE__->meta()->make_immutable();
+
+1;
+
+__END__
+
+# ABSTRACT: L<Courriel::MMS> extension for dealing with MMS messages from Telenor SE.
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head1 CLASS METHODS
+
+=head1 INSTANCE METHODS
+
+=head2 C<get_mms_images>
+
+=head1 SEE ALSO
+
+=head2 L<<< Courriel >>>
+
+=head1 AUTHOR
+
+Zbigniew Łukasiak, E<lt>zlukasiak@opera.comE<gt>
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright (c), 2011 Opera Software ASA.
+All rights reserved.
View
76 lib/Courriel/MMS/Plugin/VodafoneDE.pm
@@ -0,0 +1,76 @@
+use strict;
+use warnings;
+
+package Courriel::MMS::Plugin::VodafoneDE;
+use namespace::autoclean;
+use Moose;
+
+extends 'Courriel::MMS';
+
+# --- Class methods ---
+
+sub match {
+ my $class = shift;
+ my $email = shift;
+
+ return 1 if $email->from =~ /mmsmail\.vodafone\.de/;
+ return;
+}
+
+
+# --- 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(
+ sub {
+ my $part = shift;
+ my $mime = $part->mime_type();
+ return if $mime ne 'text/plain';
+ return if $part->content =~ m#http://www.vodafone.de#;
+ return 1;
+ }
+ );
+ return $part->content if $part;
+ return;
+};
+
+
+__PACKAGE__->meta()->make_immutable();
+
+1;
+
+__END__
+
+# ABSTRACT: L<Courriel::MMS> extension for dealing with MMS messages from Vodafone DE.
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head1 CLASS METHODS
+
+=head1 INSTANCE METHODS
+
+=head2 C<get_mms_images>
+
+=head1 SEE ALSO
+
+=head2 L<<< Courriel >>>
+
+=head1 AUTHOR
+
+Zbigniew Łukasiak, E<lt>zlukasiak@opera.comE<gt>
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright (c), 2011 Opera Software ASA.
+All rights reserved.
View
61 lib/Courriel/MMS/Plugin/VodafoneNL.pm
@@ -0,0 +1,61 @@
+use strict;
+use warnings;
+
+package Courriel::MMS::Plugin::VodafoneNL;
+use namespace::autoclean;
+use Moose;
+
+extends 'Courriel::MMS';
+
+# --- Class methods ---
+
+sub match {
+ my $class = shift;
+ my $email = shift;
+
+ return 1 if $email->from =~ /mms2mail\.vodafone\.nl/;
+ return;
+}
+
+
+# --- Instance methods ---
+
+around 'get_mms_images' => sub {
+ my $orig = shift;
+ my $self = shift;
+
+ return grep { $_->[0] !~ /^met:h_left.jpg$/ } $self->$orig( @_ );
+};
+
+
+
+__PACKAGE__->meta()->make_immutable();
+
+1;
+
+__END__
+
+# ABSTRACT: L<Courriel::MMS> extension for dealing with MMS messages from Vodafone NL.
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head1 CLASS METHODS
+
+=head1 INSTANCE METHODS
+
+=head2 C<get_mms_images>
+
+=head1 SEE ALSO
+
+=head2 L<<< Courriel >>>
+
+=head1 AUTHOR
+
+Zbigniew Łukasiak, E<lt>zlukasiak@opera.comE<gt>
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright (c), 2011 Opera Software ASA.
+All rights reserved.
View
60 lib/Courriel/MMS/Plugin/VodafoneNZ.pm
@@ -0,0 +1,60 @@
+use strict;
+use warnings;
+
+package Courriel::MMS::Plugin::VodafoneNZ;
+use namespace::autoclean;
+use Moose;
+
+extends 'Courriel::MMS';
+
+# --- Class methods ---
+
+sub match {
+ my $class = shift;
+ my $email = shift;
+
+ return 1 if $email->from =~ /pxt.vodafone.net.nz/;
+ return;
+}
+
+
+# --- 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;
+
+__END__
+
+# ABSTRACT: L<Courriel::MMS> extension for dealing with MMS messages from Vodafone New Zealand.
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head1 CLASS METHODS
+
+=head1 INSTANCE METHODS
+
+=head2 C<get_mms_images>
+
+=head1 SEE ALSO
+
+=head2 L<<< Courriel >>>
+
+=head1 AUTHOR
+
+Zbigniew Łukasiak, E<lt>zlukasiak@opera.comE<gt>
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright (c), 2011 Opera Software ASA.
+All rights reserved.
View
73 lib/Courriel/MMS/Plugin/VodafoneUK.pm
@@ -0,0 +1,73 @@
+use strict;
+use warnings;
+
+package Courriel::MMS::Plugin::VodafoneUK;
+use namespace::autoclean;
+use Moose;
+
+extends 'Courriel::MMS';
+
+# --- Class methods ---
+
+sub match {
+ my $class = shift;
+ my $email = shift;
+
+ return 1 if $email->from =~ /mms\.vodafone\.co\.uk/;
+ return;
+}
+
+
+# --- Instance methods ---
+
+around 'get_mms_images' => sub {
+ my $orig = shift;
+ my $self = shift;
+
+ return grep { $_->[0] !~ m#images/vf\d+\.jpg# } $self->$orig( @_ );
+};
+
+around plain_content => sub {
+ my $orig = shift;
+ my $self = shift;
+ my $content = $self->$orig();
+ return $content if defined( $content ) and length $content and $content !~ /Message text/;
+ my $html = $self->html_body_part->content;
+ if( $html =~ m#<td class="subject">(.*?)</td>.*?<td class="black">(.*?)</td>#s ){
+ return $2;
+ }
+ return;
+};
+
+
+
+__PACKAGE__->meta()->make_immutable();
+
+1;
+
+__END__
+
+# ABSTRACT: L<Courriel::MMS> extension for dealing with MMS messages from Vodafone UK.
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head1 CLASS METHODS
+
+=head1 INSTANCE METHODS
+
+=head2 C<get_mms_images>
+
+=head1 SEE ALSO
+
+=head2 L<<< Courriel >>>
+
+=head1 AUTHOR
+
+Zbigniew Łukasiak, E<lt>zlukasiak@opera.comE<gt>
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright (c), 2011 Opera Software ASA.
+All rights reserved.
View
159 t/test.t
@@ -7,37 +7,148 @@ use Courriel::MMS;
use Courriel::Builder;
use File::Slurp 'slurp';
-my $email = Courriel::MMS->parse( text => scalar( slurp( 't/data/MymtsRu.eml' ) ) );
+{
+ my $email = Courriel::MMS->parse( text => scalar( slurp( 't/data/MymtsRu.eml' ) ) );
-isa_ok( $email, 'Courriel::MMS::Plugin::MymtsRu', 'MMS from mms.mymts.ru' );
+ isa_ok( $email, 'Courriel::MMS::Plugin::MymtsRu', 'MMS from mms.mymts.ru' );
-my @images = $email->get_mms_images;
-is( scalar( @images ), 1, 'Logo filtered out' );
+ my @images = $email->get_mms_images;
+ is( scalar( @images ), 1, 'Logo filtered out' );
-ok( $email->create_random_image_name( 'image/jpeg' ) =~ /\.jpg$/, 'random image extension' );
+ ok( $email->create_random_image_name( 'image/jpeg' ) =~ /\.jpg$/, 'random image extension' );
-my $c_email = build_email(
- subject('aaa'),
- from('aaa@tmomail.net'),
- to( 'example@example.com' ),
- plain_body( 'test' ),
- attach( file => 't/data/cool.gif', filename => 'masthead.gif' ),
-);
-$email = Courriel::MMS->parse( text => $c_email->as_string );
-isa_ok( $email, 'Courriel::MMS::Plugin::TmobileUS', 'MMS from tmomail.net' );
-@images = $email->get_mms_images;
-is( scalar( @images ), 0, 'Logo filtered out' );
+ my $c_email = build_email(
+ subject('aaa'),
+ from('aaa@tmomail.net'),
+ to( 'example@example.com' ),
+ plain_body( 'test' ),
+ attach( file => 't/data/cool.gif', filename => 'masthead.gif' ),
+ );
+ $email = Courriel::MMS->parse( text => $c_email->as_string );
+ isa_ok( $email, 'Courriel::MMS::Plugin::TmobileUS', 'MMS from tmomail.net' );
+ @images = $email->get_mms_images;
+ is( scalar( @images ), 0, 'Logo filtered out' );
+ is( $email->plain_content, 'test', 'plain_content' );
+}
-$c_email = build_email(
- subject('aaa'),
- from( 'aaa@pm.sprint.com' ),
- to( 'example@example.com' ),
- plain_body( 'test' ),
-);
-$email = Courriel::MMS->parse( text => $c_email->as_string );
-isa_ok( $email, 'Courriel::MMS::Plugin::SprintUS', 'MMS from sprint' );
+{
+ my $c_email = build_email(
+ subject('aaa'),
+ from( 'aaa@pm.sprint.com' ),
+ to( 'example@example.com' ),
+ html_body( '<html><body><table><tr> <td><pre >some text</pre></td> </tr></table></body></html>' ),
+ );
+ my $email = Courriel::MMS->parse( text => $c_email->as_string );
+ isa_ok( $email, 'Courriel::MMS::Plugin::SprintUS', 'MMS from sprint' );
+ is( $email->plain_content, 'some text', 'plain_content extracted from html' );
+}
+{
+ my $c_email = build_email(
+ subject('Multimedia message'),
+ from( 'aaa@mms.o2.ie' ),
+ to( 'example@example.com' ),
+ plain_body( 'test' ),
+ );
+ 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' );
+}
+{
+ my $c_email = build_email(
+ subject('Sie haben eine MMS erhalt'),
+ from( 'aaa@mmsmail.vodafone.de' ),
+ to( 'example@example.com' ),
+ plain_body( 'http://www.vodafone.de' ),
+ attach( mime_type => 'text/plain', content => 'some text' ),
+ html_body( '<html><body><table><tr> <td><pre >some text</pre></td> </tr></table></body></html>' ),
+ );
+ 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->plain_content, 'some text', 'plain_content ignored http://www.vodafone.de' );
+}
+
+{
+ my $c_email = build_email(
+ subject('aaa'),
+ from('aaa@mms2mail.vodafone.nl'),
+ to( 'example@example.com' ),
+ plain_body( 'test' ),
+ attach( file => 't/data/cool.gif', filename => 'met:h_left.jpg' ),
+ );
+ my $email = Courriel::MMS->parse( text => $c_email->as_string );
+ isa_ok( $email, 'Courriel::MMS::Plugin::VodafoneNL', 'MMS from vodafone.nl' );
+ my @images = $email->get_mms_images;
+ is( scalar( @images ), 0, 'Logo filtered out' );
+}
+
+{
+ my $c_email = build_email(
+ subject('You have a PXT from aaa'),
+ from( 'aaa@pxt.vodafone.net.nz' ),
+ to( 'example@example.com' ),
+ plain_body( 'test' ),
+ );
+ 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' );
+}
+
+{
+ my $c_email = build_email(
+ subject('Subject'),
+ from( 'aaa@mms.vodafone.co.uk' ),
+ to( 'example@example.com' ),
+ plain_body( 'Message text' ),
+ html_body( '<td class="subject">aaa</td>bbb<td class="black">some text</td>' ),
+ attach( file => 't/data/cool.gif', filename => 'images/vf00.jpg' ),
+ );
+ my $mtext = $c_email->as_string;
+ $mtext =~ s/vf00.jpg/images\/vf00.jpg/;
+ my $email = Courriel::MMS->parse( text => $mtext );
+ isa_ok( $email, 'Courriel::MMS::Plugin::VodafoneUK', 'MMS from Vodafone UK' );
+ is( $email->plain_content, 'some text', 'plain_content from html' );
+ my @images = $email->get_mms_images;
+ is( scalar( @images ), 0, 'Logo filtered out' );
+}
+
+{
+ my $c_email = build_email(
+ subject('Subject'),
+ from( 'aaa@mms-email.telenor.se' ),
+ to( 'example@example.com' ),
+ html_body( "<h1>aaa</h1></div>\n<div class=\"text-400\">\nsome text\n</div>" ),
+ );
+ my $email = Courriel::MMS->parse( text => $c_email->as_string );
+ isa_ok( $email, 'Courriel::MMS::Plugin::TelenorSE', 'MMS from Telenor SE' );
+ is( $email->plain_content, 'some text', 'plain_content from html' );
+}
+
+{
+ my $c_email = build_email(
+ subject('MMS via e-mail'),
+ from( 'aaa@mms.tele2.lt' ),
+ to( 'example@example.com' ),
+ plain_body( 'test' ),
+ );
+ 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' );
+}
+
+{
+ my $c_email = build_email(
+ subject('Multimedia message'),
+ from( 'aaa@mmsc.radiolinja.fi' ),
+ to( 'example@example.com' ),
+ plain_body( 'This is a HTML message, sorry' ),
+ );
+ my $email = Courriel::MMS->parse( text => $c_email->as_string );
+ isa_ok( $email, 'Courriel::MMS::Plugin::RadiolinjaFI', 'MMS from Radiolinja Finland' );
+ is( $email->plain_content, '', 'content cleared for Radiolinja' );
+}
done_testing();

0 comments on commit 064f9b1

Please sign in to comment.
Something went wrong with that request. Please try again.