From 37c4df0efb4528d67db42b9c49979f88e3585822 Mon Sep 17 00:00:00 2001 From: Lucas Nussbaum Date: Tue, 17 Jun 2008 08:12:18 +0200 Subject: initial work on inline images --- lib/feed2imap/feed2imap.rb | 4 ++-- lib/feed2imap/itemtomail.rb | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/feed2imap/feed2imap.rb b/lib/feed2imap/feed2imap.rb index 284275a..a7fa627 100644 --- a/lib/feed2imap/feed2imap.rb +++ b/lib/feed2imap/feed2imap.rb @@ -230,13 +230,13 @@ class Feed2Imap begin if !cacherebuild updateditems.each do |i| - email = item_to_mail(i, i.cacheditem.index, true, f.name) + email = item_to_mail(i, i.cacheditem.index, true, f.name, true) f.imapaccount.updatemail(f.folder, email, i.cacheditem.index, i.date || Time::new) end # reverse is needed to upload older items first (fixes gna#8986) newitems.reverse.each do |i| - email = item_to_mail(i, i.cacheditem.index, false, f.name) + email = item_to_mail(i, i.cacheditem.index, false, f.name, true) f.imapaccount.putmail(f.folder, email, i.date || Time::new) end end diff --git a/lib/feed2imap/itemtomail.rb b/lib/feed2imap/itemtomail.rb index 64c3569..0bd7420 100644 --- a/lib/feed2imap/itemtomail.rb +++ b/lib/feed2imap/itemtomail.rb @@ -27,6 +27,7 @@ require 'feedparser/text-output' require 'feedparser/html-output' require 'base64' require 'feed2imap/rubymail_patch' +require 'digest/md5' class String def needMIME @@ -46,7 +47,7 @@ class String end end -def item_to_mail(item, index, updated, from = 'Feed2Imap') +def item_to_mail(item, index, updated, from = 'Feed2Imap', include_images = false) message = RMail::Message::new if item.creator and item.creator != '' if item.creator.include?('@') @@ -83,6 +84,31 @@ def item_to_mail(item, index, updated, from = 'Feed2Imap') htmlpart.header['Content-Type'] = 'text/html; charset=utf-8' htmlpart.header['Content-Transfer-Encoding'] = '8bit' htmlpart.body = item.to_html + + # inline images as attachments + if inline_images + htmlpart.body.gsub!(/(]+)src="(\S+?\/([^\/]+?\.(png|gif|jpe?g)))"([^>]*>)/i) do |match| + # $2 contains url, $3 the image name, $4 the image extension + begin + image = Base64.encode64(HTTPFetcher::fetch($2, Time.at(0))) + cid = "#{Digest::MD5.hexdigest($2)}@feed2imap.acme.com" + imgpart = RMail::Message.new + imgpart.header.set('Content-Type', "image/#{$4}", 'name' => $3) + imgpart.header.set('Content-Transfer-Encoding', 'base64') + imgpart.header.set('Content-ID', "<#{cid}>") + imgpart.body = image + message.add_part(imgpart) + # now to specify what to replace with + newtag = "#{$1}src=\"cid:#{cid}\"#{$5}" + print "#{cid}: Replacing '#{$&}' with '#{newtag}'...\n" + newtag + rescue + print "Error while fetching image #{$2}: #{$!}...\n" + $& # don't modify on exception + end + end + end + message.add_part(textpart) message.add_part(htmlpart) return message.to_s -- cgit v1.2.3-54-g00ecf