Class: Imap::Backup::Email::Mboxrd::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/imap/backup/email/mboxrd/message.rb

Overview

Handles serialization and deserialization of messages

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(supplied_body) ⇒ Message

Returns a new instance of Message.



38
39
40
# File 'lib/imap/backup/email/mboxrd/message.rb', line 38

def initialize(supplied_body)
  @supplied_body = supplied_body.clone
end

Instance Attribute Details

#supplied_bodyString (readonly)

Returns the original message body.

Returns:

  • (String)

    the original message body



36
37
38
# File 'lib/imap/backup/email/mboxrd/message.rb', line 36

def supplied_body
  @supplied_body
end

Class Method Details

.clean_serialized(serialized) ⇒ String

Returns The message without the initial ‘From ’ line and with one level of ‘>’ quoting removed from other lines that start with ‘From’.

Parameters:

  • serialized (String)

    an email message

Returns:

  • (String)

    The message without the initial ‘From ’ line and with one level of ‘>’ quoting removed from other lines that start with ‘From’



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/imap/backup/email/mboxrd/message.rb', line 16

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

.from_serialized(serialized) ⇒ Message

Returns the original message.

Parameters:

  • serialized (String)

    the on-disk version of the message

Returns:

  • (Message)

    the original message



31
32
33
# File 'lib/imap/backup/email/mboxrd/message.rb', line 31

def self.from_serialized(serialized)
  new(clean_serialized(serialized))
end

Instance Method Details

#dateDate?

Returns the date of the message.

Returns:

  • (Date, nil)

    the date of the message



50
51
52
53
54
# File 'lib/imap/backup/email/mboxrd/message.rb', line 50

def date
  parsed.date
rescue StandardError
  nil
end

#imap_bodyString

Returns the original message ready for transmission to an IMAP server.

Returns:

  • (String)

    the original message ready for transmission to an IMAP server



62
63
64
# File 'lib/imap/backup/email/mboxrd/message.rb', line 62

def imap_body
  supplied_body.gsub(/(?<!\r)\n/, "\r\n")
end

#subjectString

Returns the message’s subject line.

Returns:

  • (String)

    the message’s subject line



57
58
59
# File 'lib/imap/backup/email/mboxrd/message.rb', line 57

def subject
  parsed.subject
end

#to_serializedString

Returns the message with an initial ‘From ADDRESS’ line.

Returns:

  • (String)

    the message with an initial ‘From ADDRESS’ line



43
44
45
46
47
# File 'lib/imap/backup/email/mboxrd/message.rb', line 43

def to_serialized
  from_line = "From #{from}\n"
  body = mboxrd_body.dup.force_encoding(Encoding::UTF_8)
  from_line + body
end