lib/mournmail/message_rendering.rb in mournmail-0.1.1 vs lib/mournmail/message_rendering.rb in mournmail-0.2.0
- old
+ new
@@ -1,40 +1,41 @@
# frozen_string_literal: true
require "mail"
-require "mail-iso-2022-jp"
module Mournmail
module MessageRendering
refine ::Mail::Message do
def render(indices = [])
render_header + "\n" + render_body(indices)
end
def render_header
CONFIG[:mournmail_display_header_fields].map { |name|
- val = self[name]
+ val = self[name]&.to_s&.gsub(/\t/, " ")
val ? "#{name}: #{val}\n" : ""
}.join
end
def render_body(indices = [])
if HAVE_MAIL_GPG && encrypted?
mail = decrypt(verify: true)
- return mail.render_body(indices)
+ if mail.signatures.empty?
+ sig = ""
+ else
+ sig = "[PGP/MIME signature]\n" +
+ signature_of(mail)
+ end
+ return "[PGP/MIME encrypted message]\n" + mail.render(indices) + sig
end
if multipart?
parts.each_with_index.map { |part, i|
part.render([*indices, i])
}.join
else
s = body.decoded
- if /\Autf-8\z/i =~ charset
- s.force_encoding(Encoding::UTF_8).scrub("?")
- else
- s.encode(Encoding::UTF_8, charset, replace: "?")
- end.gsub(/\r\n/, "\n")
+ Mournmail.to_utf8(s, charset).gsub(/\r\n/, "\n")
end + pgp_signature
end
def dig_part(i, *rest_indices)
if HAVE_MAIL_GPG && encrypted?
@@ -52,19 +53,25 @@
private
def pgp_signature
if HAVE_MAIL_GPG && signed?
verified = verify
- validity = verified.signature_valid? ? "Good" : "Bad"
- from = verified.signatures.map { |sig|
- sig.from rescue sig.fingerprint
- }.join(", ")
- "#{validity} signature from #{from}\n"
+ signature_of(verified)
else
""
end
end
+
+ def signature_of(m)
+ validity = m.signature_valid? ? "Good" : "Bad"
+ from = m.signatures.map { |sig|
+ sig.from rescue sig.fingerprint
+ }.join(", ")
+ s = "#{validity} signature from #{from}"
+ message(s)
+ s + "\n"
+ end
end
refine ::Mail::Part do
def render(indices)
index = indices.join(".")
@@ -73,11 +80,11 @@
"[#{index} #{type}]\n" + render_content(indices)
end
def dig_part(i, *rest_indices)
if main_type == "message" && sub_type == "rfc822"
- mail = Mail.new(body.raw_source)
+ mail = Mail.new(body.to_s)
mail.dig_part(i, *rest_indices)
else
part = parts[i]
if rest_indices.empty?
part
@@ -95,10 +102,10 @@
part.render([*indices, i])
}.join
elsif main_type == "message" && sub_type == "rfc822"
mail = Mail.new(body.raw_source)
mail.render(indices)
- elsif self["content-disposition"]&.disposition_type == "attachment"
+ elsif attachment?
""
else
if main_type == "text" && sub_type == "plain"
decoded.sub(/(?<!\n)\z/, "\n").gsub(/\r\n/, "\n")
else