~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to lib/mailread.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 11:42:29 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124114229-jw2f87rdxlq6gp11
Tags: 1.9.0.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# The Mail class represents an internet mail message (as per RFC822, RFC2822)
2
 
# with headers and a body. 
3
 
class 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
62
 
end