Class: Hermeneutics::MBox

Inherits:
Box
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hermeneutics/boxes.rb

Defined Under Namespace

Classes: Region

Constant Summary collapse

RE_F =

:nodoc:

/^From\s+/
RE_N =

:nodoc:

/^$/

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Box

#exists?, find, #initialize, #path, #to_s

Constructor Details

This class inherits a constructor from Hermeneutics::Box

Class Method Details

.check(path) ⇒ Object

:call-seq:

MBox.check( path)     -> true or false

Check whether path is a MBox.



107
108
109
110
111
112
113
# File 'lib/hermeneutics/boxes.rb', line 107

def check path
  if File.file? path then
    File.open path, encoding: Encoding::ASCII_8BIT do |f|
      f.size.zero? or f.readline =~ RE_F
    end
  end
end

Instance Method Details

#createObject

:call-seq:

mbox.create     -> self

Create the MBox.



160
161
162
163
164
165
# File 'lib/hermeneutics/boxes.rb', line 160

def create
  d = File.dirname @mailbox
  Dir.mkdir! d
  File.open @mailbox, File::CREAT do |f| end
  self
end

#deliver(msg) ⇒ Object

:call-seq:

mbox.deliver( msg)     -> nil

Store the mail into the local MBox.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/hermeneutics/boxes.rb', line 172

def deliver msg
  pos = nil
  LockedFile.open @mailbox, "r+", encoding: Encoding::ASCII_8BIT do |f|
    f.seek [ f.size - 4, 0].max
    last = ""
    f.read.each_line { |l| last = l }
    f.puts unless last =~ /^$/
    pos = f.size
    m = msg.to_s
    i = 1
    while (i = m.index RE_F, i rescue nil) do m.insert i, ">" end
    f.write m
    f.puts
  end
  pos
end

#each(&block) ⇒ Object

:call-seq:

mbox.each { |mail| ... }    -> nil

Iterate through MBox.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/hermeneutics/boxes.rb', line 194

def each &block
  File.open @mailbox, encoding: Encoding::ASCII_8BIT do |f|
    m, e = nil, true
    s, t = t, f.tell
    f.each_line { |l|
      s, t = t, f.tell
      if is_from_line? l and e then
        begin
          m and Region.open f, m, e, &block
        ensure
          m, e = s, nil
        end
      else
        m or raise "#@mailbox does not seem to be a mailbox."
        e = l =~ RE_N && s
      end
    }
    # Treat it gracefully when there is no empty last line.
    e ||= f.tell
    m and Region.open f, m, e, &block
  end
end