Class: Sisimai::Mail::Maildir

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

Overview

Sisimai::Mail::Maildir is a reader for getting contents of each email in the Maildir/ directory.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Constructor of Sisimai::Mail::Maildir

Parameters:

  • argvs (String)

    Path to Maildir/

Raises:

  • (Errno::ENOENT)


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

def initialize(argv1)
  raise Errno::ENOENT  if File.exist?(argv1) == false
  raise Errno::ENOTDIR if File.ftype(argv1)  != 'directory'

  @path   = nil
  @size   = Dir.entries(argv1).size
  @dir    = argv1
  @file   = nil
  @offset = 0
  @handle = Dir.open(argv1)
end

Instance Attribute Details

#dirObject (readonly)

:dir [String] Path to Maildir/ :size [Integer] The number of files/directories in the Maildir/ :path [String] Path to each file :file, [String] Each file name of a mail in the Maildir/ :offset [Integer] The number of email files in the Maildir/ :handle [IO::Dir] Directory handle



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

def dir
  @dir
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#handleObject

Returns the value of attribute handle.



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

def handle
  @handle
end

#offsetObject

Returns the value of attribute offset.



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

def offset
  @offset
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#sizeObject

Returns the value of attribute size.



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

def size
  @size
end

Instance Method Details

#readString

Maildir reader, works as an iterator.

Returns:

  • (String)

    Contents of file in Maildir/



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
61
62
63
64
65
66
67
68
# File 'lib/sisimai/mail/maildir.rb', line 32

def read
  return nil if self.offset >= self.size
  seekhandle = self.handle
  readbuffer = ''

  begin
    while r = seekhandle.read do
      # Read each file in the directory
      if r == '.' || r == '..'
        # Is a directory
        self.offset += 1
        next
      end

      emailindir = (self.dir + '/' + r).squeeze('/')
      if File.ftype(emailindir) != 'file' ||
         File.size(emailindir) == 0 ||
         File.readable?(emailindir) == false
        # The file is not a file, is empty, is not readable
        self.offset += 1
        next
      end

      File.open(emailindir, 'r:UTF-8') do |f|
        readbuffer = f.read
      end

      self.offset += 1
      self.path    = emailindir
      self.file    = r
      break
    end
    seekhandle.close if self.offset >= self.size
  end

  return readbuffer
end