2006-08-04 14:05:50 -04:00
|
|
|
# The Mail class represents an internet mail message (as per RFC822, RFC2822)
|
|
|
|
# with headers and a body.
|
1998-01-16 07:13:05 -05:00
|
|
|
class Mail
|
1998-01-16 07:19:22 -05:00
|
|
|
|
2006-08-04 14:05:50 -04:00
|
|
|
# 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.
|
1998-01-16 07:19:22 -05:00
|
|
|
def initialize(f)
|
1999-01-19 23:59:39 -05:00
|
|
|
unless defined? f.gets
|
1998-01-16 07:13:05 -05:00
|
|
|
f = open(f, "r")
|
1998-01-16 07:19:22 -05:00
|
|
|
opened = true
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@header = {}
|
|
|
|
@body = []
|
1998-01-16 07:19:22 -05:00
|
|
|
begin
|
2000-10-10 05:24:54 -04:00
|
|
|
while line = f.gets()
|
2000-02-24 22:51:23 -05:00
|
|
|
line.chop!
|
|
|
|
next if /^From /=~line # skip From-line
|
|
|
|
break if /^$/=~line # end of header
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-10-02 00:31:23 -04:00
|
|
|
if /^(\S+?):\s*(.*)/=~line
|
1999-01-19 23:59:39 -05:00
|
|
|
(attr = $1).capitalize!
|
|
|
|
@header[attr] = $2
|
1998-01-16 07:19:22 -05:00
|
|
|
elsif attr
|
2000-02-24 22:51:23 -05:00
|
|
|
line.sub!(/^\s*/, '')
|
|
|
|
@header[attr] += "\n" + line
|
1998-01-16 07:19:22 -05:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2000-02-24 22:51:23 -05:00
|
|
|
return unless line
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-02-24 22:51:23 -05:00
|
|
|
while line = f.gets()
|
|
|
|
break if /^From /=~line
|
|
|
|
@body.push(line)
|
1998-01-16 07:19:22 -05:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
f.close if opened
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-04 14:05:50 -04:00
|
|
|
# Return the headers as a Hash.
|
1998-01-16 07:13:05 -05:00
|
|
|
def header
|
|
|
|
return @header
|
|
|
|
end
|
|
|
|
|
2006-08-04 14:05:50 -04:00
|
|
|
# Return the message body as an Array of lines
|
1998-01-16 07:13:05 -05:00
|
|
|
def body
|
|
|
|
return @body
|
|
|
|
end
|
|
|
|
|
2006-08-04 14:05:50 -04:00
|
|
|
# Return the header corresponding to +field+.
|
|
|
|
#
|
|
|
|
# Matching is case-insensitive.
|
1998-01-16 07:13:05 -05:00
|
|
|
def [](field)
|
1999-08-13 01:45:20 -04:00
|
|
|
@header[field.capitalize]
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|