Class: TMail::UNIXMbox

Inherits:
Object
  • Object
show all
Extended by:
TextUtils
Defined in:
lib/tmail-pure/mailbox.rb,
lib/tmail-pure/obsolete.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fname, mhdir, readonly, static) ⇒ UNIXMbox

Returns a new instance of UNIXMbox.



138
139
140
141
142
143
144
145
146
147
# File 'lib/tmail-pure/mailbox.rb', line 138

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



172
173
174
175
# File 'lib/tmail-pure/mailbox.rb', line 172

def UNIXMbox.create_from_line(port)
  sprintf 'From %s %s',
          fromaddr(port), time2str(File.mtime(port.filename))
end

.lock(fname, mode) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/tmail-pure/mailbox.rb', line 113

def UNIXMbox.lock(fname, mode)
  begin
    f = File.open(fname, mode)
    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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/tmail-pure/mailbox.rb', line 149

def UNIXMbox.mkfinal(mh, mboxfile, writeback_p, cleanup_p)
  lambda {
    if writeback_p
      lock(mboxfile, "r+") {|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(fname, tmpdir = nil, readonly = false) ⇒ Object



129
130
131
132
# File 'lib/tmail-pure/mailbox.rb', line 129

def UNIXMbox.new(fname, tmpdir = nil, readonly = false)
  tmpdir = ENV['TEMP'] || ENV['TMP'] || '/tmp'
  newobj(fname, "#{tmpdir}/ruby_tmail_#{$$}_#{rand()}", readonly, false)
end

.newobjObject



125
# File 'lib/tmail-pure/mailbox.rb', line 125

alias newobj new

.static_new(fname, dir, readonly = false) ⇒ Object



134
135
136
# File 'lib/tmail-pure/mailbox.rb', line 134

def UNIXMbox.static_new(fname, dir, readonly = false)
  newobj(fname, dir, readonly, true)
end

Instance Method Details

#closeObject



185
186
187
188
189
190
191
192
193
194
# File 'lib/tmail-pure/mailbox.rb', line 185

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



219
220
221
222
223
# File 'lib/tmail-pure/mailbox.rb', line 219

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



196
197
198
199
200
# File 'lib/tmail-pure/mailbox.rb', line 196

def each_port(&block)
  close_check
  update
  @real.each_port(&block)
end

#new_portObject Also known as: new_mail



225
226
227
228
# File 'lib/tmail-pure/mailbox.rb', line 225

def new_port
  close_check
  @real.new_port
end

#reverse_each_port(&block) ⇒ Object Also known as: reverse_each



204
205
206
207
208
# File 'lib/tmail-pure/mailbox.rb', line 204

def reverse_each_port(&block)
  close_check
  update
  @real.reverse_each_port(&block)
end