Class | |
In: |
lib/mailread.rb
|
Parent: | Object |
Create a new Mail where f is either a stream which responds to gets(), or a path to a file. If f is a path it will be opened.
The whole message is read so it can be made available through the header, #[] and body methods.
The "From " line is ignored if the mail is in mbox format.
# File lib/mailread.rb, line 12 12: def initialize(f) 13: unless defined? f.gets 14: f = open(f, "r") 15: opened = true 16: end 17: 18: @header = {} 19: @body = [] 20: begin 21: while line = f.gets() 22: line.chop! 23: next if /^From /=~line # skip From-line 24: break if /^$/=~line # end of header 25: 26: if /^(\S+?):\s*(.*)/=~line 27: (attr = $1).capitalize! 28: @header[attr] = $2 29: elsif attr 30: line.sub!(/^\s*/, '') 31: @header[attr] += "\n" + line 32: end 33: end 34: 35: return unless line 36: 37: while line = f.gets() 38: break if /^From /=~line 39: @body.push(line) 40: end 41: ensure 42: f.close if opened 43: end 44: end