Class: Hermeneutics::MBox

Inherits:
Box
  • Object
show all
Defined in:
lib/hermeneutics/boxes.rb

Constant Summary collapse

RE_F =

:nodoc:

/^From\s+/
RE_N =

:nodoc:

/^$/

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Box

#each, #exists?, find, #initialize, #path, #store, #to_s

Constructor Details

This class inherits a constructor from Hermeneutics::Box

Class Method Details

.check(path) ⇒ Object

:call-seq:

MBox.check( path)     -> true or false

Check whether path is a MBox.



137
138
139
140
141
142
143
# File 'lib/hermeneutics/boxes.rb', line 137

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

Instance Method Details

#createObject

:call-seq:

mbox.create     -> self

Create the MBox.



152
153
154
155
156
157
# File 'lib/hermeneutics/boxes.rb', line 152

def create
  d = File.dirname @mailbox
  Dir.mkdir! d
  File.open @mailbox, File::CREAT do |f| end
  self
end

#each_mailObject

:call-seq:

mbox.each_mail { |mail| ... }    -> nil

Iterate through MBox.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/hermeneutics/boxes.rb', line 189

def each_mail
  File.open @mailbox, encoding: Encoding::ASCII_8BIT do |f|
    nl_seen = false
    from, created, text = nil, nil, nil
    f.each_line { |l|
      l.chomp!
      if l =~ RE_F then
        l = $'
        yield text, from, created if text
        length_tried = false
        from, created = l.split nil, 2
        begin
          created = DateTime.parse created
        rescue Date::Error
          unless length_tried then
            from = $'
            created = from.slice! from.length-Time.now.ctime.length, from.length
            from.strip!
            length_tried = true
            retry
          end
          raise "#@mailbox does not seem to be a mailbox: From line '#{l}'."
        end
        text, nl_seen = "", false
      else
        from or raise "#@mailbox does not seem to be a mailbox. No 'From' line."
        text << "\n" if nl_seen
        nl_seen = l =~ RE_N
        nl_seen or text << l << "\n"
      end
    }
    text and yield text, from, created
  end
end

#store_raw(text, from, created) ⇒ Object

:call-seq:

mbox.store_raw( text, from, created)     -> nil

Store some text that appears like a mail to the local MBox.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/hermeneutics/boxes.rb', line 164

def store_raw text, from, created
  from ||= local_from
  created ||= Time.now
  File.open @mailbox, "r+", encoding: Encoding::ASCII_8BIT do |f|
    f.seek [ f.size - 4, 0].max
    last = nil
    f.read.each_line { |l| last = l }
    f.puts if last and not last =~ RE_N

    f.puts "From #{from.gsub ' ', '_'} #{created.to_time.gmtime.asctime}"
    text.each_line { |l|
      l.chomp!
      f.print ">" if l =~ RE_F
      f.puts l
    }
    f.puts
  end
  nil
end