Class: Sisimai::Mail::Mbox

Inherits:
Object
  • Object
show all
Defined in:
lib/sisimai/mail/mbox.rb

Overview

Sisimai::Mail::Mbox is a mailbox file (UNIX mbox) reader.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv1) ⇒ Sisimai::Mail::Mbox

Constructor of Sisimai::Mail::Mbox

Parameters:

  • argv1 (String)

    Path to mbox

Raises:

  • (Errno::ENOENT)


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sisimai/mail/mbox.rb', line 18

def initialize(argv1)
  raise Errno::ENOENT   if File.exist?(argv1) == false
  raise 'is not a file' if File.ftype(argv1)  != 'file'

  @path   = argv1
  @dir    = File.dirname(argv1)
  @file   = File.basename(argv1)
  @size   = File.size(argv1)
  @offset = 0
  @handle = File.open(argv1, 'r:UTF-8')
end

Instance Attribute Details

#dirObject (readonly)

:dir [String] Directory name of the mbox :file [String] File name of the mbox :path [String] Path to mbox :size [Integer] File size of the mbox :offset [Integer] Offset position for seeking :handle [IO::File] File handle



11
12
13
# File 'lib/sisimai/mail/mbox.rb', line 11

def dir
  @dir
end

#fileObject (readonly)

:dir [String] Directory name of the mbox :file [String] File name of the mbox :path [String] Path to mbox :size [Integer] File size of the mbox :offset [Integer] Offset position for seeking :handle [IO::File] File handle



11
12
13
# File 'lib/sisimai/mail/mbox.rb', line 11

def file
  @file
end

#handleObject

Returns the value of attribute handle.



12
13
14
# File 'lib/sisimai/mail/mbox.rb', line 12

def handle
  @handle
end

#offsetObject

Returns the value of attribute offset.



12
13
14
# File 'lib/sisimai/mail/mbox.rb', line 12

def offset
  @offset
end

#pathObject (readonly)

:dir [String] Directory name of the mbox :file [String] File name of the mbox :path [String] Path to mbox :size [Integer] File size of the mbox :offset [Integer] Offset position for seeking :handle [IO::File] File handle



11
12
13
# File 'lib/sisimai/mail/mbox.rb', line 11

def path
  @path
end

#sizeObject (readonly)

:dir [String] Directory name of the mbox :file [String] File name of the mbox :path [String] Path to mbox :size [Integer] File size of the mbox :offset [Integer] Offset position for seeking :handle [IO::File] File handle



11
12
13
# File 'lib/sisimai/mail/mbox.rb', line 11

def size
  @size
end

Instance Method Details

#readString

Mbox reader, works as an iterator.

Returns:

  • (String)

    Contents of mbox



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sisimai/mail/mbox.rb', line 32

def read
  return nil if self.offset >= self.size

  seekoffset = self.offset || 0
  filehandle = self.handle
  readbuffer = ''
  frombuffer = ''

  begin
    seekoffset = 0 if seekoffset < 0
    filehandle.seek(seekoffset, 0)

    filehandle.each_line do |r|
      # Read the UNIX mbox file from 'From ' to the next 'From '
      if r.start_with?('From ') && !readbuffer.empty?
        frombuffer = r
        break
      end
      readbuffer += r
    end

    seekoffset = filehandle.pos - frombuffer.bytesize
    frombuffer = ''
    self.offset = seekoffset
    filehandle.close if self.offset >= self.size
  end

  return readbuffer.to_s
end