require "forwardable" require "mail" module Email; end module Email::Mboxrd class Message extend Forwardable def_delegators :@parsed, :subject 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 #{from}\n" + mboxrd_body end def date parsed.date rescue StandardError nil 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