Class TMail::UNIXMbox
In: lib/tmail/mailbox.rb
Parent: Object

Methods

External Aliases

new -> newobj

Public Class methods

make _From line

[Source]

# File lib/tmail/mailbox.rb, line 271
    def UNIXMbox.create_from_line( port )
      sprintf 'From %s %s',
              fromaddr(), TextUtils.time2str(File.mtime(port.filename))
    end

[Source]

# File lib/tmail/mailbox.rb, line 276
    def UNIXMbox.fromaddr(port)
      h = HeaderField.new_from_port(port, 'Return-Path') ||
      HeaderField.new_from_port(port, 'From') ||
      HeaderField.new_from_port(port, 'EnvelopeSender') or return 'nobody'
      a = h.addrs[0] or return 'nobody'
      a.spec
    end

[Source]

# File lib/tmail/mailbox.rb, line 222
    def UNIXMbox.lock( fname )
      begin
        f = File.open(fname, 'r+')
        f.flock File::LOCK_EX
        yield f
      ensure
        f.flock File::LOCK_UN
        f.close if f and not f.closed?
      end
    end

[Source]

# File lib/tmail/mailbox.rb, line 248
    def UNIXMbox.mkfinal( mh, mboxfile, writeback_p, cleanup_p )
      lambda {
          if writeback_p
            lock(mboxfile) {|f|
                mh.each_port do |port|
                  f.puts create_from_line(port)
                  port.ropen {|r|
                      f.puts r.read
                  }
                end
            }
          end
          if cleanup_p
            Dir.foreach(mh.dirname) do |fname|
              next if /\A\.\.?\z/ === fname
              File.unlink "#{mh.dirname}/#{fname}"
            end
            Dir.rmdir mh.dirname
          end
      }
    end

Creates a new mailbox object that you can iterate through to collect the emails from with "each_port".

You need to pass it a filename of a unix mailbox format file, the format of this file can be researched at this page at wikipedia

Parameters

filename: The filename of the mailbox you want to open

tmpdir: Can be set to override TMail using the system environment‘s temp dir. TMail will first use the temp dir specified by you (if any) or then the temp dir specified in the Environment‘s TEMP value then the value in the Environment‘s TMP value or failing all of the above, ’/tmp‘

readonly: If set to false, each email you take from the mail box will be removed from the mailbox. default is false - ie, it WILL truncate your mailbox file to ZERO once it has read the emails out.

Options:

None

Examples:

 # First show using readonly true:

 require 'ftools'
 File.size("../test/fixtures/mailbox")
 #=> 20426

 mailbox = TMail::UNIXMbox.new("../test/fixtures/mailbox", nil, true)
 #=> #<TMail::UNIXMbox:0x14a2aa8 @readonly=true.....>

 mailbox.each_port do |port|
   mail = TMail::Mail.new(port)
   puts mail.subject
 end
 #Testing mailbox 1
 #Testing mailbox 2
 #Testing mailbox 3
 #Testing mailbox 4
 require 'ftools'
 File.size?("../test/fixtures/mailbox")
 #=> 20426

 # Now show with readonly set to the default false

 mailbox = TMail::UNIXMbox.new("../test/fixtures/mailbox")
 #=> #<TMail::UNIXMbox:0x14a2aa8 @readonly=false.....>

 mailbox.each_port do |port|
   mail = TMail::Mail.new(port)
   puts mail.subject
 end
 #Testing mailbox 1
 #Testing mailbox 2
 #Testing mailbox 3
 #Testing mailbox 4

 File.size?("../test/fixtures/mailbox")
 #=> nil

[Source]

# File lib/tmail/mailbox.rb, line 217
    def UNIXMbox.new( filename, tmpdir = nil, readonly = false )
      tmpdir = ENV['TEMP'] || ENV['TMP'] || '/tmp'
      newobj(filename, "#{tmpdir}/ruby_tmail_#{$$}_#{rand()}", readonly, false)
    end

[Source]

# File lib/tmail/mailbox.rb, line 237
    def initialize( fname, mhdir, readonly, static )
      @filename = fname
      @readonly = readonly
      @closed = false

      Dir.mkdir mhdir
      @real = MhMailbox.new(mhdir)
      @finalizer = UNIXMbox.mkfinal(@real, @filename, !@readonly, !static)
      ObjectSpace.define_finalizer self, @finalizer
    end

[Source]

# File lib/tmail/mailbox.rb, line 233
    def UNIXMbox.static_new( fname, dir, readonly = false )
      newobj(fname, dir, readonly, true)
    end

Public Instance methods

[Source]

# File lib/tmail/mailbox.rb, line 284
    def close
      return if @closed

      ObjectSpace.undefine_finalizer self
      @finalizer.call
      @finalizer = nil
      @real = nil
      @closed = true
      @updated = nil
    end
each( &block )

Alias for each_port

 old #each_mail returns Port

def each_mail( &block )

  each_port do |port|
    yield Mail.new(port)
  end

end

[Source]

# File lib/tmail/mailbox.rb, line 318
    def each_new_port( mtime = nil )
      close_check
      update
      @real.each_new_port(mtime) {|p| yield p }
    end

[Source]

# File lib/tmail/mailbox.rb, line 295
    def each_port( &block )
      close_check
      update
      @real.each_port(&block)
    end

[Source]

# File lib/tmail/mailbox.rb, line 324
    def new_port
      close_check
      @real.new_port
    end
reverse_each( &block )

Alias for reverse_each_port

[Source]

# File lib/tmail/mailbox.rb, line 303
    def reverse_each_port( &block )
      close_check
      update
      @real.reverse_each_port(&block)
    end

[Validate]