Class: MsgExtractor::MessageObject

Inherits:
Object
  • Object
show all
Defined in:
lib/msg_extractor/message_object.rb

Overview

Shared base for every MSG item type: property access, bodies, recipients, attachments. Works for the root message and for embedded messages.

Direct Known Subclasses

Appointment, Contact, Message, Task

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfbf, storage: nil, named: nil, kind: :root, properties: nil) ⇒ MessageObject

Returns a new instance of MessageObject.



11
12
13
14
15
16
17
# File 'lib/msg_extractor/message_object.rb', line 11

def initialize(cfbf, storage: nil, named: nil, kind: :root, properties: nil)
  @cfbf = cfbf
  @storage = storage || cfbf.root
  @kind = kind
  @properties = properties || Mapi::PropertyStore.new(cfbf, @storage, kind)
  @named = named || Mapi::NamedPropertyMap.read(cfbf, @storage)
end

Instance Attribute Details

#namedObject (readonly)

:nodoc: internal reuse by MsgExtractor.from_storage



9
10
11
# File 'lib/msg_extractor/message_object.rb', line 9

def named
  @named
end

#propertiesObject (readonly)

Returns the value of attribute properties.



7
8
9
# File 'lib/msg_extractor/message_object.rb', line 7

def properties
  @properties
end

Instance Method Details

#attachmentsObject



70
71
72
73
# File 'lib/msg_extractor/message_object.rb', line 70

def attachments
  @attachments ||= child_storages("__ATTACH_VERSION1.0_#")
                   .map { |e| Attachment.new(@cfbf, e, named: @named) }
end

#bccObject



68
# File 'lib/msg_extractor/message_object.rb', line 68

def bcc = recipients.select { |r| r.type == Recipient::BCC }

#bodyObject



26
27
28
29
# File 'lib/msg_extractor/message_object.rb', line 26

def body
  return @body if defined?(@body)
  @body = properties[Mapi::PR_BODY] || (html_body && Util.html_to_text(html_body))
end

#ccObject



67
# File 'lib/msg_extractor/message_object.rb', line 67

def cc = recipients.select { |r| r.type == Recipient::CC }

#dateObject



22
23
24
# File 'lib/msg_extractor/message_object.rb', line 22

def date
  properties[Mapi::PR_CLIENT_SUBMIT_TIME] || properties[Mapi::PR_MESSAGE_DELIVERY_TIME]
end

#headersObject



49
50
51
# File 'lib/msg_extractor/message_object.rb', line 49

def headers
  @headers ||= Headers.parse(properties[Mapi::PR_TRANSPORT_HEADERS])
end

#html_bodyObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/msg_extractor/message_object.rb', line 37

def html_body
  return @html_body if defined?(@html_body)
  @html_body =
    if properties.type_of(Mapi::PR_HTML) == Mapi::PT_UNICODE
      properties[Mapi::PR_HTML]
    elsif (raw = properties.raw(Mapi::PR_HTML))
      Mapi::Decoders.bytes_to_utf8(raw, properties.internet_codepage)
    elsif rtf_body
      Rtf::Decapsulator.html_from(rtf_body)
    end
end

#message_classObject



19
# File 'lib/msg_extractor/message_object.rb', line 19

def message_class = properties[Mapi::PR_MESSAGE_CLASS]

#named_value(guid, lid_or_name) ⇒ Object



75
76
77
78
# File 'lib/msg_extractor/message_object.rb', line 75

def named_value(guid, lid_or_name)
  id = @named.resolve(guid, lid_or_name)
  id && properties[id]
end

#recipientsObject



61
62
63
64
# File 'lib/msg_extractor/message_object.rb', line 61

def recipients
  @recipients ||= child_storages("__RECIP_VERSION1.0_#")
                  .map { |e| Recipient.from_storage(@cfbf, e) }
end

#rtf_bodyObject



31
32
33
34
35
# File 'lib/msg_extractor/message_object.rb', line 31

def rtf_body
  return @rtf_body if defined?(@rtf_body)
  raw = properties.raw(Mapi::PR_RTF_COMPRESSED)
  @rtf_body = raw && Rtf::CompressedRtf.decompress(raw)
end

#save(dir: ".") ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/msg_extractor/message_object.rb', line 80

def save(dir: ".")
  name = Util.sanitize_filename(subject || "message")
  base = Util.dedupe_path(::File.join(dir, name))
  FileUtils.mkdir_p(base)
  ::File.write(::File.join(base, "message.txt"), body, encoding: Encoding::UTF_8) if body
  ::File.binwrite(::File.join(base, "message.html"), html_body.b) if html_body
  attachments.each do |attachment|
    attachment.save(dir: base) unless attachment.embedded_message?
  end
  base
end

#senderObject



53
54
55
56
57
58
59
# File 'lib/msg_extractor/message_object.rb', line 53

def sender
  return @sender if defined?(@sender)
  name = properties[Mapi::PR_SENDER_NAME]
  email = properties[Mapi::PR_SENDER_SMTP] || properties[Mapi::PR_SENDER_EMAIL]
  email = nil unless email&.include?("@")
  @sender = (name || email) && Recipient.new(name: name, email: email, type: nil)
end

#subjectObject



20
# File 'lib/msg_extractor/message_object.rb', line 20

def subject = properties[Mapi::PR_SUBJECT]

#toObject



66
# File 'lib/msg_extractor/message_object.rb', line 66

def to = recipients.select { |r| r.type == Recipient::TO }