Class: Mapi::Msg

Inherits:
Message show all
Defined in:
lib/mapi/msg.rb,
lib/mapi/convert/note-mime.rb,
lib/mapi/convert/note-tmail.rb

Overview

# Introduction

Primary class interface to the vagaries of .msg files.

The core of the work is done by the PropertyStore class.

Defined Under Namespace

Classes: Attachment, PropertyStore, Recipient

Constant Summary collapse

ATTACH_RX =
/^__attach_version1\.0_.*/
RECIP_RX =
/^__recip_version1\.0_.*/
VALID_RX =
/#{PropertyStore::VALID_RX}|#{ATTACH_RX}|#{RECIP_RX}/

Constants inherited from Message

Mapi::Message::CONVERSION_MAP

Instance Attribute Summary collapse

Attributes inherited from Item

#properties

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Message

#body_to_mime, #body_to_tmail, #convert, #headers, #inspect, #mime, #mime_type, #to_mime, #to_post, #to_tmail, #to_vcard, #type

Constructor Details

#initialize(root, helper) ⇒ Msg

Create an Msg from root, an Ole::Storage::Dirent object

Parameters:

  • root (Ole::Storage::Dirent)
  • helper (Helper)


400
401
402
403
404
405
406
# File 'lib/mapi/msg.rb', line 400

def initialize root, helper
  @root = root
  @helper = helper
  @close_parent = false
  super PropertySet.new(PropertyStore.load(@root, helper))
  Msg.warn_unknown @root
end

Instance Attribute Details

#close_parentBoolean

Returns:

  • (Boolean)


375
376
377
# File 'lib/mapi/msg.rb', line 375

def close_parent
  @close_parent
end

#helperHelper (readonly)

Returns:



373
374
375
# File 'lib/mapi/msg.rb', line 373

def helper
  @helper
end

#rootOle::Storage::Dirent (readonly)

Returns:

  • (Ole::Storage::Dirent)


370
371
372
# File 'lib/mapi/msg.rb', line 370

def root
  @root
end

Class Method Details

.open(arg, mode = nil, helper = nil) ⇒ Ole::Storage::Dirent

Alternate constructor, to create an Mapi::Msg directly from arg and mode, passed directly to Ole::Storage (ie either filename or seekable IO object).

Parameters:

  • arg (Object)
  • mode (Object) (defaults to: nil)
  • helper (Helper) (defaults to: nil)

Returns:

  • (Ole::Storage::Dirent)


384
385
386
387
388
389
390
391
392
393
394
# File 'lib/mapi/msg.rb', line 384

def self.open arg, mode=nil, helper=nil
  msg = new Ole::Storage.open(arg, mode).root, helper || Helper.new
  # we will close the ole when we are #closed
  msg.close_parent = true
  if block_given?
    begin   yield msg
    ensure; msg.close
    end
  else msg
  end
end

.warn_unknown(obj) ⇒ Object

Parameters:

  • obj (Ole::Storage::Dirent)


410
411
412
413
414
415
# File 'lib/mapi/msg.rb', line 410

def self.warn_unknown obj
  # bit of validation. not important if there is extra stuff, though would be
  # interested to know what it is. doesn't check dir/file stuff.
  unknown = obj.children.reject { |child| child.name =~ VALID_RX }
  Log.warn "skipped #{unknown.length} unknown msg object(s)" unless unknown.empty?
end

Instance Method Details

#attachmentsArray<Attachment>

Returns:



422
423
424
425
426
427
# File 'lib/mapi/msg.rb', line 422

def attachments
  @attachments ||= @root.children.
    select { |child| child.dir? and child.name =~ ATTACH_RX }.
    map { |child| Attachment.new child, helper }.
    select { |attach| attach.valid? }
end

#closeObject



417
418
419
# File 'lib/mapi/msg.rb', line 417

def close
  @root.ole.close if @close_parent
end

#populate_headersObject



271
272
273
274
275
276
277
278
279
# File 'lib/mapi/convert/note-mime.rb', line 271

def populate_headers
  super
  if !headers.has_key?('Date')
    # can employ other methods for getting a time. heres one in a similar vein to msgconvert.pl,
    # ie taking the time from an ole object
    time = @root.ole.dirents.map { |dirent| dirent.modify_time || dirent.create_time }.compact.sort.last
    headers['Date'] = [Time.iso8601(time.to_s).rfc2822] if time
  end
end

#recipientsArray<Recipient>

Returns:



430
431
432
433
434
# File 'lib/mapi/msg.rb', line 430

def recipients
  @recipients ||= @root.children.
    select { |child| child.dir? and child.name =~ RECIP_RX }.
    map { |child| Recipient.new child, helper }
end