source: extensions/gsdl-video/trunk/installed/cmdline/lib/ruby/1.8/mailread.rb@ 18425

Last change on this file since 18425 was 18425, checked in by davidb, 15 years ago

Video extension to Greenstone

File size: 1.3 KB
Line 
1# The Mail class represents an internet mail message (as per RFC822, RFC2822)
2# with headers and a body.
3class Mail
4
5 # Create a new Mail where +f+ is either a stream which responds to gets(),
6 # or a path to a file. If +f+ is a path it will be opened.
7 #
8 # The whole message is read so it can be made available through the #header,
9 # #[] and #body methods.
10 #
11 # The "From " line is ignored if the mail is in mbox format.
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
45
46 # Return the headers as a Hash.
47 def header
48 return @header
49 end
50
51 # Return the message body as an Array of lines
52 def body
53 return @body
54 end
55
56 # Return the header corresponding to +field+.
57 #
58 # Matching is case-insensitive.
59 def [](field)
60 @header[field.capitalize]
61 end
62end
Note: See TracBrowser for help on using the repository browser.