lib/mailread.rb


DEFINITIONS

This source file includes following functions.


   1  class Mail
   2  
   3    def initialize(f)
   4      unless defined? f.gets
   5        f = open(f, "r")
   6        opened = true
   7      end
   8  
   9      @header = {}
  10      @body = []
  11      begin
  12        while line = f.gets()
  13          line.chop!
  14          next if /^From /=~line  # skip From-line
  15          break if /^$/=~line     # end of header
  16  
  17          if /^(\S+?):\s*(.*)/=~line
  18            (attr = $1).capitalize!
  19            @header[attr] = $2
  20          elsif attr
  21            line.sub!(/^\s*/, '')
  22            @header[attr] += "\n" + line
  23          end
  24        end
  25    
  26        return unless line
  27  
  28        while line = f.gets()
  29          break if /^From /=~line
  30          @body.push(line)
  31        end
  32      ensure
  33        f.close if opened
  34      end
  35    end
  36  
  37    def header
  38      return @header
  39    end
  40  
  41    def body
  42      return @body
  43    end
  44  
  45    def [](field)
  46      @header[field.capitalize]
  47    end
  48  end