Class: TMail::UNIXMbox
- Defined in:
- lib/gems/tmail-1.2.3.1/lib/tmail/mailbox.rb,
lib/gems/tmail-1.2.3.1/lib/tmail/obsolete.rb
Class Method Summary collapse
-
.create_from_line(port) ⇒ Object
make _From line.
- .fromaddr(port) ⇒ Object
- .lock(fname) ⇒ Object
- .mkfinal(mh, mboxfile, writeback_p, cleanup_p) ⇒ Object
-
.new(filename, tmpdir = nil, readonly = false) ⇒ Object
Creates a new mailbox object that you can iterate through to collect the emails from with “each_port”.
- .newobj ⇒ Object
- .static_new(fname, dir, readonly = false) ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
-
#each_new_port(mtime = nil) ⇒ Object
(also: #each_newmail)
old #each_mail returns Port def each_mail( &block ) each_port do |port| yield Mail.new(port) end end.
- #each_port(&block) ⇒ Object (also: #each, #each_mail)
-
#initialize(fname, mhdir, readonly, static) ⇒ UNIXMbox
constructor
A new instance of UNIXMbox.
- #new_port ⇒ Object (also: #new_mail)
- #reverse_each_port(&block) ⇒ Object (also: #reverse_each)
Constructor Details
#initialize(fname, mhdir, readonly, static) ⇒ UNIXMbox
Returns a new instance of UNIXMbox.
237 238 239 240 241 242 243 244 245 246 |
# File 'lib/gems/tmail-1.2.3.1/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 |
Class Method Details
.create_from_line(port) ⇒ Object
make _From line
271 272 273 274 |
# File 'lib/gems/tmail-1.2.3.1/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) ⇒ Object
276 277 278 279 280 281 282 |
# File 'lib/gems/tmail-1.2.3.1/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) ⇒ Object
222 223 224 225 226 227 228 229 230 231 |
# File 'lib/gems/tmail-1.2.3.1/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) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/gems/tmail-1.2.3.1/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) ⇒ Object
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
217 218 219 220 |
# File 'lib/gems/tmail-1.2.3.1/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 |
.newobj ⇒ Object
154 |
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/mailbox.rb', line 154 alias newobj new |
Instance Method Details
#close ⇒ Object
284 285 286 287 288 289 290 291 292 293 |
# File 'lib/gems/tmail-1.2.3.1/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_new_port(mtime = nil) ⇒ Object Also known as: each_newmail
old #each_mail returns Port def each_mail( &block )
each_port do |port|
yield Mail.new(port)
end
end
318 319 320 321 322 |
# File 'lib/gems/tmail-1.2.3.1/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) ⇒ Object Also known as: each, each_mail
295 296 297 298 299 |
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/mailbox.rb', line 295 def each_port( &block ) close_check update @real.each_port(&block) end |
#new_port ⇒ Object Also known as: new_mail
324 325 326 327 |
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/mailbox.rb', line 324 def new_port close_check @real.new_port end |
#reverse_each_port(&block) ⇒ Object Also known as: reverse_each
303 304 305 306 307 |
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/mailbox.rb', line 303 def reverse_each_port( &block ) close_check update @real.reverse_each_port(&block) end |