require "forwardable" require "mail" module Imap; end module Imap::Backup module Email; end module Email::Mboxrd class Message attr_reader :supplied_body def self.clean_serialized(serialized) cleaned = serialized.gsub(/^>(>*From)/, "\\1") # Serialized messages in this format *should* start with a line # From xxx yy zz # rubocop:disable Style/IfUnlessModifier if cleaned.start_with?("From ") cleaned = cleaned.sub(/^From .*[\r\n]*/, "") end # rubocop:enable Style/IfUnlessModifier cleaned end def self.from_serialized(serialized) new(clean_serialized(serialized)) end def initialize(supplied_body) @supplied_body = supplied_body.clone end def to_serialized from_line = "From #{from}\n" body = mboxrd_body.dup.force_encoding(Encoding::UTF_8) from_line + body end def date parsed.date rescue StandardError nil end def subject parsed.subject end def imap_body supplied_body.gsub(/(?' so that any remaining lines which start with # 'From ' can be taken as the beginning of messages. # http://www.digitalpreservation.gov/formats/fdd/fdd000385.shtml # Here we add an extra '>' before any "From" or ">From". body.gsub(/\n(>*From)/, "\n>\\1") end def asctime @asctime ||= date ? date.asctime : "" end end end end