class Hermes::MBox

Public Instance Methods

check( path) → true or false click to toggle source

Check whether path is a MBox.

# File lib/hermes/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
create → self click to toggle source

Create the MBox.

# File lib/hermes/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) → nil click to toggle source

Store the mail into the local MBox.

# File lib/hermes/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 { |mail| ... } → nil click to toggle source

Iterate through MBox.

# File lib/hermes/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

Private Instance Methods

is_from_line?(l) click to toggle source
# File lib/hermes/boxes.rb, line 220
def is_from_line? l
  l =~ RE_F or return
  addr, time = $'.split nil, 2
  DateTime.parse time
  addr =~ /@/
rescue ArgumentError, TypeError
end