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.



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

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



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

def seq! ; @seq += 1 ; end

Instance Method Details

#createObject

:call-seq:

maildir.create     -> self

Create the Maildir.



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

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.



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

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

#each_mail(new = nil) ⇒ Object

:call-seq:

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

Iterate through Maildir.



301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/hermeneutics/boxes.rb', line 301

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.



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

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