Class: Hermeneutics::Maildir

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

Constant Summary collapse

DIRS =
%w(cur tmp new)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Box

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

Constructor Details

This class inherits a constructor from Hermeneutics::Box

Class Method Details

.check(mailbox) ⇒ Object

:call-seq:

Maildir.check( path)     -> true or false

Check whether path is a Maildir.



238
239
240
241
242
243
244
245
246
# File 'lib/hermeneutics/boxes.rb', line 238

def check mailbox
  if File.directory? mailbox then
    DIRS.each do |d|
      s = File.join mailbox, d
      File.directory? s or return false
    end
    true
  end
end

.seq!Object



321
# File 'lib/hermeneutics/boxes.rb', line 321

def seq! ; @seq += 1 ; end

Instance Method Details

#createObject

:call-seq:

maildir.create     -> self

Create the Maildir.



255
256
257
258
259
260
261
262
# File 'lib/hermeneutics/boxes.rb', line 255

def create
  Dir.mkdir! @mailbox
  DIRS.each do |d|
    s = File.join @mailbox, d
    Dir.mkdir s
  end
  self
end

#each_file(new = nil) ⇒ Object

:call-seq:

mbox.each_file { |filename| ... }    -> nil

Iterate through Maildir.



288
289
290
291
292
293
294
295
296
297
# File 'lib/hermeneutics/boxes.rb', line 288

def each_file new = nil
  p = File.join @mailbox, new ? NEW : CUR
  Dir.open p do |d|
    d.sort.each { |fn|
      next if fn.starts_with? "."
      path = File.join p, fn
      yield path
    }
  end
end

#each_mail(new = nil) ⇒ Object

:call-seq:

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

Iterate through Maildir.



304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/hermeneutics/boxes.rb', line 304

def each_mail new = nil
  lfrom = local_from
  each_file new do |fn|
    created = Time.at fn[ /\A(\d+)/, 1].to_i + fn[ /M(\d+)/, 1].to_i*0.000001
    File.open fn, encoding: Encoding::ASCII_8BIT do |f|
      from_host = fn[ /\.([.a-z0-9_+-]+)/, 1]
      text = f.read
      from = text[ /[a-z0-9.+-]+@#{Regexp.quote from_host}/]
      yield text, from||lfrom, created
    end
  end
end

#store_raw(text, from, created) ⇒ Object

:call-seq:

maildir.store_raw( text, from, created)     -> nil

Store some text that appears like a mail to the local MBox.



269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/hermeneutics/boxes.rb', line 269

def store_raw text, from, created
  filename = mkfilename from, created
  tpath = File.join @mailbox, TMP, filename
  File.open tpath, File::CREAT|File::EXCL|File::WRONLY do |f| f.puts text end
  cpath = File.join @mailbox, NEW, filename
  File.link tpath, cpath
  filename
rescue Errno::EEXIST
  File.unlink tpath rescue nil
  retry
ensure
  File.unlink tpath
end