summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.net>2014-06-15 08:41:04 +0200
committerRené 'Necoro' Neumann <necoro@necoro.net>2016-03-25 15:51:08 +0100
commit99082038ec300153591376f700d5884b31293031 (patch)
tree066b7ebdf9c7d9cf7fb6df5e27d39b379c4d4999
parentd1aea3e925c37a0d8f5966dab0f1c2ebd7145ac9 (diff)
downloadfeed2imap-99082038ec300153591376f700d5884b31293031.tar.gz
feed2imap-99082038ec300153591376f700d5884b31293031.tar.bz2
feed2imap-99082038ec300153591376f700d5884b31293031.zip
Replace RMail by Mail. This also reduces the handling we have to do ourselves.
-rw-r--r--lib/feed2imap/itemtomail.rb108
-rw-r--r--lib/feed2imap/maildir.rb19
2 files changed, 65 insertions, 62 deletions
diff --git a/lib/feed2imap/itemtomail.rb b/lib/feed2imap/itemtomail.rb
index 55bf9d8..a081616 100644
--- a/lib/feed2imap/itemtomail.rb
+++ b/lib/feed2imap/itemtomail.rb
@@ -20,13 +20,11 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
=end
require 'rexml/document'
-require 'time'
-require 'rmail'
+require 'mail'
require 'feedparser'
require 'feedparser/text-output'
require 'feedparser/html-output'
require 'base64'
-require 'rmail'
require 'digest/md5'
class String
@@ -48,81 +46,85 @@ class String
end
def item_to_mail(config, item, id, updated, from = 'Feed2Imap', inline_images = false, wrapto = false)
- message = RMail::Message::new
- if item.creator and item.creator != ''
- if item.creator.include?('@')
- message.header['From'] = item.creator.chomp
- else
- message.header['From'] = "=?utf-8?b?#{Base64::encode64(item.creator.chomp).gsub("\n",'')}?= <#{config.default_email}>"
- end
- else
- message.header['From'] = "=?utf-8?b?#{Base64::encode64(from).gsub("\n",'')}?= <#{config.default_email}>"
- end
- message.header['To'] = "=?utf-8?b?#{Base64::encode64(from).gsub("\n",'')}?= <#{config.default_email}>"
+ message = Mail::new do
+ message_id id
+ to "#{from} <#{config.default_email}>"
+ from (
+ if item.creator and item.creator != ''
+ if item.creator.include?('@')
+ item.creator.chomp
+ else
+ "#{item.creator.chomp} <#{config.default_email}>"
+ end
+ else
+ "#{from} <#{config.default_email}>"
+ end
+ )
- if item.date.nil?
- message.header['Date'] = Time::new.rfc2822
- else
- message.header['Date'] = item.date.rfc2822
- end
- message.header['X-Feed2Imap-Version'] = F2I_VERSION if defined?(F2I_VERSION)
- message.header['Message-Id'] = id
- message.header['X-F2IStatus'] = "Updated" if updated
- # treat subject. Might need MIME encoding.
- subj = item.title or (item.date and item.date.to_s) or item.link
- if subj
- if subj.needMIME
- message.header['Subject'] = "=?utf-8?b?#{Base64::encode64(subj).gsub("\n",'')}?="
- else
- message.header['Subject'] = subj
- end
+ date item.date unless item.date.nil?
+
+ subject item.title or (item.date and item.date.to_s) or item.link
+ transport_encoding '8bit'
end
+
+ message['X-Feed2Imap-Version'] = F2I_VERSION if defined?(F2I_VERSION)
+ message['X-F2IStatus'] = 'Updated' if updated
+
+
textpart = htmlpart = nil
parts = config.parts
if parts.include?('text')
- textpart = parts.size == 1 ? message : RMail::Message::new
- textpart.header['Content-Type'] = 'text/plain; charset=utf-8; format=flowed'
- textpart.header['Content-Transfer-Encoding'] = '8bit'
- textpart.body = item.to_text(true, wrapto, false)
+ textpart = Mail::Part.new do
+ content_type 'text/plain; charset=utf-8; format=flowed'
+ content_transfer_encoding '8bit'
+ body item.to_text(true, wrapto, false)
+ end
end
if parts.include?('html')
- htmlpart = parts.size == 1 ? message : RMail::Message::new
- htmlpart.header['Content-Type'] = 'text/html; charset=utf-8'
- htmlpart.header['Content-Transfer-Encoding'] = '8bit'
- htmlpart.body = item.to_html
+ htmlpart = Mail::Part.new do
+ content_type 'text/html; charset=utf-8'
+ content_transfer_encoding '8bit'
+ body item.to_html
+ end
end
# inline images as attachments
imgs = []
if inline_images
- cids = []
- htmlpart.body.gsub!(/(<img[^>]+)src="(\S+?\/([^\/]+?\.(png|gif|jpe?g)))"([^>]*>)/i) do |match|
+ html = htmlpart.body.decoded
+ html.gsub!(/(<img[^>]+)src="(\S+?\/([^\/]+?\.(png|gif|jpe?g)))"([^>]*>)/i) do |match|
# $2 contains url, $3 the image name, $4 the image extension
begin
fetcher = HTTPFetcher.new
image = Base64.encode64(fetcher.fetch($2, Time.at(0)).chomp)
"#{$1}src=\"data:image/#{$4};base64,#{image}\"#{$5}"
rescue
- #print "Error while fetching image #{$2}: #{$!}...\n"
+ @logger.error "Error while fetching image #{$2}: #{$!}..."
$& # don't modify on exception
end
end
+ htmlpart.body = html
end
+
+
if imgs.length > 0
- message.header.set('Content-Type', 'multipart/related', 'type'=> 'multipart/alternative')
- texthtml = RMail::Message::new
- texthtml.header.set('Content-Type', 'multipart/alternative')
- texthtml.add_part(textpart)
- texthtml.add_part(htmlpart)
- message.add_part(texthtml)
+ # The old code explicitly used 'multipart/related' here, so force it
+ # We then have the structure "related: (alternative: text/html)/images"
+ #
+ # We could obtain easier code here, if 'alternative: text/html/images' would suffice.
+ message.content_type "multipart/related"
+ message.part do |p|
+ p.text_part = textpart
+ p.html_part = htmlpart
+ end
imgs.each do |i|
- message.add_part(i)
+ message.attachments[i[:name]] = i
end
- elsif parts.size != 1
- message.header['Content-Type'] = 'multipart/alternative'
- message.add_part(textpart)
- message.add_part(htmlpart)
+ else
+ # textpart/htmlpart are nil when not set
+ # Mail then ignores them if nil; if both are given it sets multipart/alternative
+ message.text_part = textpart
+ message.html_part = htmlpart
end
return message.to_s
end
-
diff --git a/lib/feed2imap/maildir.rb b/lib/feed2imap/maildir.rb
index 7335f03..68d591f 100644
--- a/lib/feed2imap/maildir.rb
+++ b/lib/feed2imap/maildir.rb
@@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'uri'
require 'fileutils'
require 'fcntl'
-require 'rmail'
+require 'mail'
require 'socket'
class MaildirAccount
@@ -69,10 +69,8 @@ class MaildirAccount
next if (not flags.index('S') or
flags.index('F') or
mtime > recent_time)
- mail = File.open(fn) do |f|
- RMail::Parser.read(f)
- end
- subject = mail.header['Subject']
+ mail = Mail.read(fn)
+ subject = mail.subject
if dryrun
puts "To remove: #{subject} #{mtime}"
else
@@ -142,10 +140,13 @@ class MaildirAccount
subdir = File.join(dir, d)
raise "#{subdir} not a directory" unless File.directory? subdir
Dir[File.join(subdir, '*')].each do |fn|
- File.open(fn) do |f|
- mail = RMail::Parser.read(f)
- cache_index = mail.header['Message-ID']
- if cache_index && (cache_index == idx || cache_index == "<#{idx}>")
+ mail = Mail.read(fn)
+ cache_index = mail.message_id
+ if cache_index
+ if idx.start_with? '<' and idx.end_with? '>'
+ cache_index = "<#{cache_index}>"
+ end
+ if cache_index == idx
dir_paths.push(File.join(d, File.basename(fn)))
end
end