Class: TMail::UNIXMbox

### Home Classes Methods

### In Files

- lib/tmail/mailbox.rb

### Parent

Object

### Methods

- ::create_from_line
- ::fromaddr
- ::lock
- ::mkfinal
- ::new
- ::new
- ::newobj
- ::static_new
- #close
- #each
- #each_new_port
- #each_port
- #new_port
- #reverse_each
- #reverse_each_port

### Files

- CHANGES
- LICENSE
- NOTES
- README
- Rakefile
- MANIFEST
- Makefile
- depend
- Makefile
- COPYING
- README

### Class/Module Index

Quicksearch

- TMail
- TMail::Address
- TMail::AddressGroup
- TMail::AddressHeader
- TMail::Attachment
- TMail::ContentDispositionHeader
- TMail::ContentTransferEncodingHeader
- TMail::ContentTypeHeader
- TMail::DateTimeHeader
- TMail::EncryptedHeader
- TMail::FilePort
- TMail::HeaderField
- TMail::KeywordsHeader
- TMail::Mail
- TMail::MailFlags
- TMail::Maildir
- TMail::MaildirLoader
- TMail::MaildirPort
- TMail::MboxLoader
- TMail::MessageIdHeader
- TMail::MhLoader
- TMail::MhMailbox
- TMail::MhPort
- TMail::MimeVersionHeader
- TMail::Parser
- TMail::Port
- TMail::ReceivedHeader
- TMail::ReferencesHeader
- TMail::ReturnPathHeader
- TMail::SingleAddressHeader
- TMail::StringPort
- TMail::StructuredHeader
- TMail::SyntaxError
- TMail::TMailScanner
- TMail::TextUtils
- TMail::UNIXMbox
- TMail::UnstructuredHeader
- CharDet
- CharDet::Big5DistributionAnalysis
- CharDet::Big5Prober
- CharDet::CharDistributionAnalysis
- CharDet::CharSetGroupProber
- CharDet::CharSetProber
- CharDet::CodingStateMachine
- CharDet::EUCJPContextAnalysis
- CharDet::EUCJPDistributionAnalysis
- CharDet::EUCJPProber
- CharDet::EUCKRDistributionAnalysis
- CharDet::EUCKRProber
- CharDet::EUCTWDistributionAnalysis
- CharDet::EUCTWProber
- CharDet::EscCharSetProber
- CharDet::GB2312DistributionAnalysis
- CharDet::GB2312Prober
- CharDet::HebrewProber
- CharDet::JapaneseContextAnalysis
- CharDet::Latin1Prober
- CharDet::MBCSGroupProber
- CharDet::MultiByteCharSetProber
- CharDet::SBCSGroupProber
- CharDet::SJISContextAnalysis
- CharDet::SJISDistributionAnalysis
- CharDet::SJISProber
- CharDet::SingleByteCharSetProber
- CharDet::UTF8Prober
- CharDet::UniversalDetector

No matching classes.

# TMail::UNIXMbox

### Public Class Methods

create_from_line( port ) click to toggle source

make _From line

    # 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

fromaddr(port) click to toggle 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

lock( fname ) click to toggle 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

mkfinal( mh, mboxfile, writeback_p, cleanup_p ) click to toggle 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

new( filename, tmpdir = nil, readonly = false ) click to toggle source

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

    # 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

Also aliased as: newobj
new( fname, mhdir, readonly, static ) click to toggle 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

newobj( filename, tmpdir = nil, readonly = false ) click to toggle source
Alias for: new
static_new( fname, dir, readonly = false ) click to toggle 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

close() click to toggle 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 ) click to toggle source
Alias for: each_port
each_new_port( mtime = nil ) click to toggle source

    old #each_mail returns Port

def each_mail( &block )

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

end

    # 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

each_port( &block ) click to toggle source

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

Also aliased as: each
new_port() click to toggle source

    # File lib/tmail/mailbox.rb, line 324
    def new_port
      close_check
      @real.new_port
    end

reverse_each( &block ) click to toggle source
Alias for: reverse_each_port
reverse_each_port( &block ) click to toggle source

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

Also aliased as: reverse_each

[Validate]

Generated with the Darkfish Rdoc Generator 2.