summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Maischein <corion@corion.net>2013-07-13 17:15:48 +0200
committerdakkar <dakkar@thenautilus.net>2013-07-31 20:16:21 +0100
commit4ac055111effc3c6e068f5df61e82fca903b1e4a (patch)
tree33c08c85b7d07869edec01d4ea9593929f32cbb0
parentAdd page fetcher and content extractor (diff)
downloadfeeder-4ac055111effc3c6e068f5df61e82fca903b1e4a.tar.gz
feeder-4ac055111effc3c6e068f5df61e82fca903b1e4a.tar.bz2
feeder-4ac055111effc3c6e068f5df61e82fca903b1e4a.zip
Add `inline_images` setting to optionally inline images in the MIME object
This is higly convenient if you want to read RSS content offline.
-rw-r--r--lib/Feed/HelperRole/Mail.pm46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/Feed/HelperRole/Mail.pm b/lib/Feed/HelperRole/Mail.pm
index e0e239e..1279ac8 100644
--- a/lib/Feed/HelperRole/Mail.pm
+++ b/lib/Feed/HelperRole/Mail.pm
@@ -13,6 +13,10 @@ use Template::Stash::ForceUTF8;
use Moose::Util::TypeConstraints;
use Try::Tiny;
+use List::MoreUtils qw(uniq);
+use Text::CleanFragment;
+use URI;
+
subtype 'ArrayOfStr', as 'ArrayRef[Str]';
coerce 'ArrayOfStr', from 'Str', via { [ $_ ] };
@@ -45,6 +49,11 @@ has date_formatter => (
lazy_build => 1,
);
+has inline_images => (
+ is => 'ro',
+ isa => 'Bool',
+);
+
sub _build_tt {
my ($self) = @_;
@@ -91,12 +100,49 @@ sub entry_to_mime {
Subject => encode('MIME-Header',$subject),
Type => 'multipart/related',
);
+
+ my @images;
+ if( $self->inline_images ) {
+ # We expect fairly clean HTML here...
+ my $base= $entry->link;
+ my @links= uniq $body=~ m/<img\s[^>]*\bsrc="([^"]+)".*?>/ig;
+
+ # Fetch all images, append them
+ my $ua= $self->user_agent;
+ for my $image ( @links ) {
+ my $url= URI->new_abs( $image, $base );
+ $self->log->trace( "Retrieving '$url' for inlining" );
+ my $res= $ua->get( $url, Referer => $base );
+ if( not $res->is_success ) {
+ $self->log->error("Error retrieving linked image URL '$url': " . $res->status_line );
+ next
+ };
+
+ my $name= clean_fragment( $image );
+ my $id= sprintf "cid:$name";
+
+ $body=~ s!src\s*=\s*"\Q$image\E"!src="$id"!g;
+ push @images, {
+ Type => $res->content_type,
+ Id => $name,
+ Data => $res->content,
+ Filename => $name,
+ };
+ };
+ };
+
$msg->attach(
Type => 'text/html; charset=utf-8',
Data => encode('utf-8',$body),
Encoding => 'quoted-printable',
);
+ for my $image (@images) {
+ # rewrite the HTML to reference the images
+ $self->log->trace( "Attaching " . $image->{Type} );
+ $msg->attach(%$image);
+ };
+
$msg->add('Message-Id', "<$id.feeder\@localhost>");
my @tags = $entry->tags;
try { @tags = map { decode('utf-8',$_) } @tags };