lib/mournmail/message_rendering.rb in mournmail-0.2.0 vs lib/mournmail/message_rendering.rb in mournmail-0.3.0
- old
+ new
@@ -1,7 +1,5 @@
-# frozen_string_literal: true
-
require "mail"
module Mournmail
module MessageRendering
refine ::Mail::Message do
@@ -29,21 +27,29 @@
end
if multipart?
parts.each_with_index.map { |part, i|
part.render([*indices, i])
}.join
- else
+ elsif main_type.nil? || (main_type == "text" && sub_type == "plain")
s = body.decoded
- Mournmail.to_utf8(s, charset).gsub(/\r\n/, "\n")
+ Mournmail.to_utf8(s, charset)
+ else
+ type = Mail::Encodings.decode_encode(self["content-type"].to_s,
+ :decode) rescue
+ "broken/type; error=\"#{$!} (#{$!.class})\""
+ "[-1 #{type}]\n"
end + pgp_signature
end
def dig_part(i, *rest_indices)
if HAVE_MAIL_GPG && encrypted?
mail = decrypt(verify: true)
return mail.dig_part(i, *rest_indices)
end
+ if i == -1
+ return self
+ end
part = parts[i]
if rest_indices.empty?
part
else
part.dig_part(*rest_indices)
@@ -74,11 +80,12 @@
refine ::Mail::Part do
def render(indices)
index = indices.join(".")
type = Mail::Encodings.decode_encode(self["content-type"].to_s,
- :decode)
+ :decode) rescue
+ "broken/type; error=\"#{$!} (#{$!.class})\""
"[#{index} #{type}]\n" + render_content(indices)
end
def dig_part(i, *rest_indices)
if main_type == "message" && sub_type == "rfc822"
@@ -111,9 +118,11 @@
decoded.sub(/(?<!\n)\z/, "\n").gsub(/\r\n/, "\n")
else
""
end
end
+ rescue => e
+ "Broken part: #{e} (#{e.class})"
end
end
end
end