mail-trunk 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. data/CHANGELOG.rdoc +555 -0
  2. data/Dependencies.txt +3 -0
  3. data/Gemfile +29 -0
  4. data/README.mkd +583 -0
  5. data/Rakefile +66 -0
  6. data/TODO.rdoc +9 -0
  7. data/lib/VERSION +4 -0
  8. data/lib/mail.rb +89 -0
  9. data/lib/mail/attachments_list.rb +105 -0
  10. data/lib/mail/body.rb +292 -0
  11. data/lib/mail/configuration.rb +73 -0
  12. data/lib/mail/core_extensions/nil.rb +17 -0
  13. data/lib/mail/core_extensions/object.rb +13 -0
  14. data/lib/mail/core_extensions/shellwords.rb +57 -0
  15. data/lib/mail/core_extensions/smtp.rb +25 -0
  16. data/lib/mail/core_extensions/string.rb +31 -0
  17. data/lib/mail/core_extensions/string/access.rb +104 -0
  18. data/lib/mail/core_extensions/string/multibyte.rb +78 -0
  19. data/lib/mail/elements.rb +14 -0
  20. data/lib/mail/elements/address.rb +306 -0
  21. data/lib/mail/elements/address_list.rb +74 -0
  22. data/lib/mail/elements/content_disposition_element.rb +30 -0
  23. data/lib/mail/elements/content_location_element.rb +25 -0
  24. data/lib/mail/elements/content_transfer_encoding_element.rb +24 -0
  25. data/lib/mail/elements/content_type_element.rb +35 -0
  26. data/lib/mail/elements/date_time_element.rb +26 -0
  27. data/lib/mail/elements/envelope_from_element.rb +34 -0
  28. data/lib/mail/elements/message_ids_element.rb +29 -0
  29. data/lib/mail/elements/mime_version_element.rb +26 -0
  30. data/lib/mail/elements/phrase_list.rb +21 -0
  31. data/lib/mail/elements/received_element.rb +30 -0
  32. data/lib/mail/encodings.rb +266 -0
  33. data/lib/mail/encodings/7bit.rb +31 -0
  34. data/lib/mail/encodings/8bit.rb +31 -0
  35. data/lib/mail/encodings/base64.rb +33 -0
  36. data/lib/mail/encodings/binary.rb +31 -0
  37. data/lib/mail/encodings/quoted_printable.rb +38 -0
  38. data/lib/mail/encodings/transfer_encoding.rb +58 -0
  39. data/lib/mail/envelope.rb +35 -0
  40. data/lib/mail/field.rb +224 -0
  41. data/lib/mail/field_list.rb +33 -0
  42. data/lib/mail/fields.rb +35 -0
  43. data/lib/mail/fields/bcc_field.rb +56 -0
  44. data/lib/mail/fields/cc_field.rb +55 -0
  45. data/lib/mail/fields/comments_field.rb +41 -0
  46. data/lib/mail/fields/common/address_container.rb +16 -0
  47. data/lib/mail/fields/common/common_address.rb +125 -0
  48. data/lib/mail/fields/common/common_date.rb +42 -0
  49. data/lib/mail/fields/common/common_field.rb +51 -0
  50. data/lib/mail/fields/common/common_message_id.rb +44 -0
  51. data/lib/mail/fields/common/parameter_hash.rb +58 -0
  52. data/lib/mail/fields/content_description_field.rb +19 -0
  53. data/lib/mail/fields/content_disposition_field.rb +69 -0
  54. data/lib/mail/fields/content_id_field.rb +63 -0
  55. data/lib/mail/fields/content_location_field.rb +42 -0
  56. data/lib/mail/fields/content_transfer_encoding_field.rb +50 -0
  57. data/lib/mail/fields/content_type_field.rb +198 -0
  58. data/lib/mail/fields/date_field.rb +57 -0
  59. data/lib/mail/fields/from_field.rb +55 -0
  60. data/lib/mail/fields/in_reply_to_field.rb +55 -0
  61. data/lib/mail/fields/keywords_field.rb +44 -0
  62. data/lib/mail/fields/message_id_field.rb +83 -0
  63. data/lib/mail/fields/mime_version_field.rb +53 -0
  64. data/lib/mail/fields/optional_field.rb +13 -0
  65. data/lib/mail/fields/received_field.rb +75 -0
  66. data/lib/mail/fields/references_field.rb +55 -0
  67. data/lib/mail/fields/reply_to_field.rb +55 -0
  68. data/lib/mail/fields/resent_bcc_field.rb +55 -0
  69. data/lib/mail/fields/resent_cc_field.rb +55 -0
  70. data/lib/mail/fields/resent_date_field.rb +35 -0
  71. data/lib/mail/fields/resent_from_field.rb +55 -0
  72. data/lib/mail/fields/resent_message_id_field.rb +34 -0
  73. data/lib/mail/fields/resent_sender_field.rb +62 -0
  74. data/lib/mail/fields/resent_to_field.rb +55 -0
  75. data/lib/mail/fields/return_path_field.rb +65 -0
  76. data/lib/mail/fields/sender_field.rb +67 -0
  77. data/lib/mail/fields/structured_field.rb +51 -0
  78. data/lib/mail/fields/subject_field.rb +16 -0
  79. data/lib/mail/fields/to_field.rb +55 -0
  80. data/lib/mail/fields/unstructured_field.rb +182 -0
  81. data/lib/mail/header.rb +265 -0
  82. data/lib/mail/indifferent_hash.rb +146 -0
  83. data/lib/mail/mail.rb +255 -0
  84. data/lib/mail/message.rb +2017 -0
  85. data/lib/mail/multibyte.rb +42 -0
  86. data/lib/mail/multibyte/chars.rb +474 -0
  87. data/lib/mail/multibyte/exceptions.rb +8 -0
  88. data/lib/mail/multibyte/unicode.rb +392 -0
  89. data/lib/mail/multibyte/utils.rb +60 -0
  90. data/lib/mail/network.rb +13 -0
  91. data/lib/mail/network/delivery_methods/file_delivery.rb +40 -0
  92. data/lib/mail/network/delivery_methods/sendmail.rb +62 -0
  93. data/lib/mail/network/delivery_methods/smtp.rb +137 -0
  94. data/lib/mail/network/delivery_methods/smtp_connection.rb +74 -0
  95. data/lib/mail/network/delivery_methods/test_mailer.rb +40 -0
  96. data/lib/mail/network/retriever_methods/base.rb +63 -0
  97. data/lib/mail/network/retriever_methods/imap.rb +158 -0
  98. data/lib/mail/network/retriever_methods/pop3.rb +140 -0
  99. data/lib/mail/network/retriever_methods/test_retriever.rb +47 -0
  100. data/lib/mail/parsers/address_lists.rb +64 -0
  101. data/lib/mail/parsers/address_lists.treetop +19 -0
  102. data/lib/mail/parsers/content_disposition.rb +535 -0
  103. data/lib/mail/parsers/content_disposition.treetop +46 -0
  104. data/lib/mail/parsers/content_location.rb +139 -0
  105. data/lib/mail/parsers/content_location.treetop +20 -0
  106. data/lib/mail/parsers/content_transfer_encoding.rb +162 -0
  107. data/lib/mail/parsers/content_transfer_encoding.treetop +20 -0
  108. data/lib/mail/parsers/content_type.rb +967 -0
  109. data/lib/mail/parsers/content_type.treetop +68 -0
  110. data/lib/mail/parsers/date_time.rb +114 -0
  111. data/lib/mail/parsers/date_time.treetop +11 -0
  112. data/lib/mail/parsers/envelope_from.rb +194 -0
  113. data/lib/mail/parsers/envelope_from.treetop +32 -0
  114. data/lib/mail/parsers/message_ids.rb +45 -0
  115. data/lib/mail/parsers/message_ids.treetop +15 -0
  116. data/lib/mail/parsers/mime_version.rb +144 -0
  117. data/lib/mail/parsers/mime_version.treetop +19 -0
  118. data/lib/mail/parsers/phrase_lists.rb +45 -0
  119. data/lib/mail/parsers/phrase_lists.treetop +15 -0
  120. data/lib/mail/parsers/received.rb +71 -0
  121. data/lib/mail/parsers/received.treetop +11 -0
  122. data/lib/mail/parsers/rfc2045.rb +464 -0
  123. data/lib/mail/parsers/rfc2045.treetop +36 -0
  124. data/lib/mail/parsers/rfc2822.rb +5341 -0
  125. data/lib/mail/parsers/rfc2822.treetop +410 -0
  126. data/lib/mail/parsers/rfc2822_obsolete.rb +3757 -0
  127. data/lib/mail/parsers/rfc2822_obsolete.treetop +241 -0
  128. data/lib/mail/part.rb +116 -0
  129. data/lib/mail/parts_list.rb +51 -0
  130. data/lib/mail/patterns.rb +35 -0
  131. data/lib/mail/utilities.rb +215 -0
  132. data/lib/mail/version.rb +24 -0
  133. data/lib/mail/version_specific/ruby_1_8.rb +98 -0
  134. data/lib/mail/version_specific/ruby_1_9.rb +113 -0
  135. data/lib/tasks/corpus.rake +125 -0
  136. data/lib/tasks/treetop.rake +10 -0
  137. metadata +221 -0
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+ #
3
+ # = Sender Field
4
+ #
5
+ # The Sender field inherits sender StructuredField and handles the Sender: header
6
+ # field in the email.
7
+ #
8
+ # Sending sender to a mail message will instantiate a Mail::Field object that
9
+ # has a SenderField as it's field type. This includes all Mail::CommonAddress
10
+ # module instance metods.
11
+ #
12
+ # Only one Sender field can appear in a header, though it can have multiple
13
+ # addresses and groups of addresses.
14
+ #
15
+ # == Examples:
16
+ #
17
+ # mail = Mail.new
18
+ # mail.sender = 'Mikel Lindsaar <mikel@test.lindsaar.net>, ada@test.lindsaar.net'
19
+ # mail.sender #=> ['Mikel Lindsaar <mikel@test.lindsaar.net>', 'ada@test.lindsaar.net']
20
+ # mail[:sender] #=> '#<Mail::Field:0x180e5e8 @field=#<Mail::SenderField:0x180e1c4
21
+ # mail['sender'] #=> '#<Mail::Field:0x180e5e8 @field=#<Mail::SenderField:0x180e1c4
22
+ # mail['Sender'] #=> '#<Mail::Field:0x180e5e8 @field=#<Mail::SenderField:0x180e1c4
23
+ #
24
+ # mail[:sender].encoded #=> 'Sender: Mikel Lindsaar <mikel@test.lindsaar.net>, ada@test.lindsaar.net\r\n'
25
+ # mail[:sender].decoded #=> 'Mikel Lindsaar <mikel@test.lindsaar.net>, ada@test.lindsaar.net'
26
+ # mail[:sender].addresses #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
27
+ # mail[:sender].formatted #=> ['Mikel Lindsaar <mikel@test.lindsaar.net>', 'ada@test.lindsaar.net']
28
+ #
29
+ require 'mail/fields/common/common_address'
30
+
31
+ module Mail
32
+ class SenderField < StructuredField
33
+
34
+ include Mail::CommonAddress
35
+
36
+ FIELD_NAME = 'sender'
37
+ CAPITALIZED_FIELD = 'Sender'
38
+
39
+ def initialize(value = nil, charset = 'utf-8')
40
+ self.charset = charset
41
+ super(CAPITALIZED_FIELD, strip_field(FIELD_NAME, value), charset)
42
+ self.parse
43
+ self
44
+ end
45
+
46
+ def addresses
47
+ [address.address]
48
+ end
49
+
50
+ def address
51
+ result = tree.addresses.first
52
+ end
53
+
54
+ def encoded
55
+ do_encode(CAPITALIZED_FIELD)
56
+ end
57
+
58
+ def decoded
59
+ do_decode
60
+ end
61
+
62
+ def default
63
+ address.address
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ require 'mail/fields/common/common_field'
3
+
4
+ module Mail
5
+ # Provides access to a structured header field
6
+ #
7
+ # ===Per RFC 2822:
8
+ # 2.2.2. Structured Header Field Bodies
9
+ #
10
+ # Some field bodies in this standard have specific syntactical
11
+ # structure more restrictive than the unstructured field bodies
12
+ # described above. These are referred to as "structured" field bodies.
13
+ # Structured field bodies are sequences of specific lexical tokens as
14
+ # described in sections 3 and 4 of this standard. Many of these tokens
15
+ # are allowed (according to their syntax) to be introduced or end with
16
+ # comments (as described in section 3.2.3) as well as the space (SP,
17
+ # ASCII value 32) and horizontal tab (HTAB, ASCII value 9) characters
18
+ # (together known as the white space characters, WSP), and those WSP
19
+ # characters are subject to header "folding" and "unfolding" as
20
+ # described in section 2.2.3. Semantic analysis of structured field
21
+ # bodies is given along with their syntax.
22
+ class StructuredField
23
+
24
+ include Mail::CommonField
25
+ include Mail::Utilities
26
+
27
+ def initialize(name = nil, value = nil, charset = nil)
28
+ self.name = name
29
+ self.value = value
30
+ self.charset = charset
31
+ self
32
+ end
33
+
34
+ def charset
35
+ @charset
36
+ end
37
+
38
+ def charset=(val)
39
+ @charset = val
40
+ end
41
+
42
+ def default
43
+ decoded
44
+ end
45
+
46
+ def errors
47
+ []
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ #
3
+ # subject = "Subject:" unstructured CRLF
4
+ module Mail
5
+ class SubjectField < UnstructuredField
6
+
7
+ FIELD_NAME = 'subject'
8
+ CAPITALIZED_FIELD = "Subject"
9
+
10
+ def initialize(value = nil, charset = 'utf-8')
11
+ self.charset = charset
12
+ super(CAPITALIZED_FIELD, strip_field(FIELD_NAME, value), charset)
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+ #
3
+ # = To Field
4
+ #
5
+ # The To field inherits to StructuredField and handles the To: header
6
+ # field in the email.
7
+ #
8
+ # Sending to to a mail message will instantiate a Mail::Field object that
9
+ # has a ToField as it's field type. This includes all Mail::CommonAddress
10
+ # module instance metods.
11
+ #
12
+ # Only one To field can appear in a header, though it can have multiple
13
+ # addresses and groups of addresses.
14
+ #
15
+ # == Examples:
16
+ #
17
+ # mail = Mail.new
18
+ # mail.to = 'Mikel Lindsaar <mikel@test.lindsaar.net>, ada@test.lindsaar.net'
19
+ # mail.to #=> ['Mikel Lindsaar <mikel@test.lindsaar.net>', 'ada@test.lindsaar.net']
20
+ # mail[:to] #=> '#<Mail::Field:0x180e5e8 @field=#<Mail::ToField:0x180e1c4
21
+ # mail['to'] #=> '#<Mail::Field:0x180e5e8 @field=#<Mail::ToField:0x180e1c4
22
+ # mail['To'] #=> '#<Mail::Field:0x180e5e8 @field=#<Mail::ToField:0x180e1c4
23
+ #
24
+ # mail[:to].encoded #=> 'To: Mikel Lindsaar <mikel@test.lindsaar.net>, ada@test.lindsaar.net\r\n'
25
+ # mail[:to].decoded #=> 'Mikel Lindsaar <mikel@test.lindsaar.net>, ada@test.lindsaar.net'
26
+ # mail[:to].addresses #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
27
+ # mail[:to].formatted #=> ['Mikel Lindsaar <mikel@test.lindsaar.net>', 'ada@test.lindsaar.net']
28
+ #
29
+ require 'mail/fields/common/common_address'
30
+
31
+ module Mail
32
+ class ToField < StructuredField
33
+
34
+ include Mail::CommonAddress
35
+
36
+ FIELD_NAME = 'to'
37
+ CAPITALIZED_FIELD = 'To'
38
+
39
+ def initialize(value = nil, charset = 'utf-8')
40
+ self.charset = charset
41
+ super(CAPITALIZED_FIELD, strip_field(FIELD_NAME, value), charset)
42
+ self.parse
43
+ self
44
+ end
45
+
46
+ def encoded
47
+ do_encode(CAPITALIZED_FIELD)
48
+ end
49
+
50
+ def decoded
51
+ do_decode
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,182 @@
1
+ # encoding: utf-8
2
+ require 'mail/fields/common/common_field'
3
+
4
+ module Mail
5
+ # Provides access to an unstructured header field
6
+ #
7
+ # ===Per RFC 2822:
8
+ # 2.2.1. Unstructured Header Field Bodies
9
+ #
10
+ # Some field bodies in this standard are defined simply as
11
+ # "unstructured" (which is specified below as any US-ASCII characters,
12
+ # except for CR and LF) with no further restrictions. These are
13
+ # referred to as unstructured field bodies. Semantically, unstructured
14
+ # field bodies are simply to be treated as a single line of characters
15
+ # with no further processing (except for header "folding" and
16
+ # "unfolding" as described in section 2.2.3).
17
+ class UnstructuredField
18
+
19
+ include Mail::CommonField
20
+ include Mail::Utilities
21
+
22
+ def initialize(name, value, charset = nil)
23
+ self.charset = charset
24
+ @errors = []
25
+ if charset
26
+ self.charset = charset
27
+ else
28
+ if value.to_s.respond_to?(:encoding)
29
+ self.charset = value.to_s.encoding
30
+ else
31
+ self.charset = $KCODE
32
+ end
33
+ end
34
+ self.name = name
35
+ self.value = value
36
+ self
37
+ end
38
+
39
+ def charset
40
+ @charset
41
+ end
42
+
43
+ def charset=(val)
44
+ @charset = val
45
+ end
46
+
47
+ def errors
48
+ @errors
49
+ end
50
+
51
+ def encoded
52
+ do_encode(self.name)
53
+ end
54
+
55
+ def decoded
56
+ do_decode
57
+ end
58
+
59
+ def default
60
+ decoded
61
+ end
62
+
63
+ def parse # An unstructured field does not parse
64
+ self
65
+ end
66
+
67
+ private
68
+
69
+ def do_encode(name)
70
+ value.nil? ? '' : "#{wrapped_value}\r\n"
71
+ end
72
+
73
+ def do_decode
74
+ result = value.blank? ? nil : Encodings.decode_encode(value, :decode)
75
+ result.encode!(value.encoding || "UTF-8") if RUBY_VERSION >= '1.9' && !result.blank?
76
+ result
77
+ end
78
+
79
+ # 2.2.3. Long Header Fields
80
+ #
81
+ # Each header field is logically a single line of characters comprising
82
+ # the field name, the colon, and the field body. For convenience
83
+ # however, and to deal with the 998/78 character limitations per line,
84
+ # the field body portion of a header field can be split into a multiple
85
+ # line representation; this is called "folding". The general rule is
86
+ # that wherever this standard allows for folding white space (not
87
+ # simply WSP characters), a CRLF may be inserted before any WSP. For
88
+ # example, the header field:
89
+ #
90
+ # Subject: This is a test
91
+ #
92
+ # can be represented as:
93
+ #
94
+ # Subject: This
95
+ # is a test
96
+ #
97
+ # Note: Though structured field bodies are defined in such a way that
98
+ # folding can take place between many of the lexical tokens (and even
99
+ # within some of the lexical tokens), folding SHOULD be limited to
100
+ # placing the CRLF at higher-level syntactic breaks. For instance, if
101
+ # a field body is defined as comma-separated values, it is recommended
102
+ # that folding occur after the comma separating the structured items in
103
+ # preference to other places where the field could be folded, even if
104
+ # it is allowed elsewhere.
105
+ def wrapped_value # :nodoc:
106
+ @folded_line = []
107
+ @unfolded_line = decoded.to_s.split(/[ \t]/)
108
+ fold("#{name}: ".length)
109
+ wrap_lines(name, @folded_line)
110
+ end
111
+
112
+ # 6.2. Display of 'encoded-word's
113
+ #
114
+ # When displaying a particular header field that contains multiple
115
+ # 'encoded-word's, any 'linear-white-space' that separates a pair of
116
+ # adjacent 'encoded-word's is ignored. (This is to allow the use of
117
+ # multiple 'encoded-word's to represent long strings of unencoded text,
118
+ # without having to separate 'encoded-word's where spaces occur in the
119
+ # unencoded text.)
120
+ def wrap_lines(name, folded_lines)
121
+ result = []
122
+ index = 0
123
+ result[index] = "#{name}: #{folded_lines.shift}"
124
+ result.concat(folded_lines)
125
+ result.join("\r\n\s")
126
+ end
127
+
128
+ def fold(prepend = 0) # :nodoc:
129
+ encoding = @charset.to_s.upcase.gsub('_', '-')
130
+ while !@unfolded_line.empty?
131
+ encoded = false
132
+ limit = 78 - prepend
133
+ line = ""
134
+ while !@unfolded_line.empty?
135
+ break unless word = @unfolded_line.first.dup
136
+ # Remember whether it was non-ascii before we encode it ('cause then we can't tell anymore)
137
+ non_ascii = word.not_ascii_only?
138
+ encoded_word = encode(word)
139
+ # Skip to next line if we're going to go past the limit
140
+ # Unless this is the first word, in which case we're going to add it anyway
141
+ # Note: This means that a word that's longer than 998 characters is going to break the spec. Please fix if this is a problem for you.
142
+ # (The fix, it seems, would be to use encoded-word encoding on it, because that way you can break it across multiple lines and
143
+ # the linebreak will be ignored)
144
+ break if !line.empty? && (line.length + encoded_word.length + 1 > limit)
145
+ # If word was the first non-ascii word, we're going to make the entire line encoded and we're going to reduce the limit accordingly
146
+ if non_ascii && !encoded
147
+ encoded = true
148
+ encoded_word_safify!(line)
149
+ limit = limit - 8 - encoding.length # minus the =?...?Q?...?= part, the possible leading white-space, and the name of the encoding
150
+ end
151
+ # Remove the word from the queue ...
152
+ @unfolded_line.shift
153
+ # ... add it in encoded form to the current line
154
+ line << " " unless line.empty?
155
+ encoded_word_safify!(encoded_word) if encoded
156
+ line << encoded_word
157
+ end
158
+ # Add leading whitespace if both this and the last line were encoded, because whitespace between two encoded-words is ignored when decoding
159
+ line = " " + line if encoded && @folded_line.last && @folded_line.last.index('=?') == 0
160
+ # Encode the line if necessary
161
+ line = "=?#{encoding}?Q?#{line.gsub(/ /, '_')}?=" if encoded
162
+ # Add the line to the output and reset the prepend
163
+ @folded_line << line
164
+ prepend = 0
165
+ end
166
+ end
167
+
168
+ def encode(value)
169
+ value.encode!(charset) if defined?(Encoding) && charset
170
+ (value.not_ascii_only? ? [value].pack("M").gsub("=\n", '') : value).gsub("\r", "=0D").gsub("\n", "=0A")
171
+ end
172
+
173
+ def encoded_word_safify!(value)
174
+ value.gsub!(/"/, '=22')
175
+ value.gsub!(/\(/, '=28')
176
+ value.gsub!(/\)/, '=29')
177
+ value.gsub!(/\?/, '=3F')
178
+ value.gsub!(/_/, '=5F')
179
+ end
180
+
181
+ end
182
+ end
@@ -0,0 +1,265 @@
1
+ # encoding: utf-8
2
+ module Mail
3
+
4
+ # Provides access to a header object.
5
+ #
6
+ # ===Per RFC2822
7
+ #
8
+ # 2.2. Header Fields
9
+ #
10
+ # Header fields are lines composed of a field name, followed by a colon
11
+ # (":"), followed by a field body, and terminated by CRLF. A field
12
+ # name MUST be composed of printable US-ASCII characters (i.e.,
13
+ # characters that have values between 33 and 126, inclusive), except
14
+ # colon. A field body may be composed of any US-ASCII characters,
15
+ # except for CR and LF. However, a field body may contain CRLF when
16
+ # used in header "folding" and "unfolding" as described in section
17
+ # 2.2.3. All field bodies MUST conform to the syntax described in
18
+ # sections 3 and 4 of this standard.
19
+ class Header
20
+ include Patterns
21
+ include Utilities
22
+ include Enumerable
23
+
24
+ # Creates a new header object.
25
+ #
26
+ # Accepts raw text or nothing. If given raw text will attempt to parse
27
+ # it and split it into the various fields, instantiating each field as
28
+ # it goes.
29
+ #
30
+ # If it finds a field that should be a structured field (such as content
31
+ # type), but it fails to parse it, it will simply make it an unstructured
32
+ # field and leave it alone. This will mean that the data is preserved but
33
+ # no automatic processing of that field will happen. If you find one of
34
+ # these cases, please make a patch and send it in, or at the least, send
35
+ # me the example so we can fix it.
36
+ def initialize(header_text = nil, charset = nil)
37
+ @errors = []
38
+ @charset = charset
39
+ self.raw_source = header_text.to_crlf
40
+ split_header if header_text
41
+ end
42
+
43
+ # The preserved raw source of the header as you passed it in, untouched
44
+ # for your Regexing glory.
45
+ def raw_source
46
+ @raw_source
47
+ end
48
+
49
+ # Returns an array of all the fields in the header in order that they
50
+ # were read in.
51
+ def fields
52
+ @fields ||= FieldList.new
53
+ end
54
+
55
+ # 3.6. Field definitions
56
+ #
57
+ # It is important to note that the header fields are not guaranteed to
58
+ # be in a particular order. They may appear in any order, and they
59
+ # have been known to be reordered occasionally when transported over
60
+ # the Internet. However, for the purposes of this standard, header
61
+ # fields SHOULD NOT be reordered when a message is transported or
62
+ # transformed. More importantly, the trace header fields and resent
63
+ # header fields MUST NOT be reordered, and SHOULD be kept in blocks
64
+ # prepended to the message. See sections 3.6.6 and 3.6.7 for more
65
+ # information.
66
+ #
67
+ # Populates the fields container with Field objects in the order it
68
+ # receives them in.
69
+ #
70
+ # Acceps an array of field string values, for example:
71
+ #
72
+ # h = Header.new
73
+ # h.fields = ['From: mikel@me.com', 'To: bob@you.com']
74
+ def fields=(unfolded_fields)
75
+ @fields = Mail::FieldList.new
76
+ warn "Warning: more than 1000 header fields only using the first 1000" if unfolded_fields.length > 1000
77
+ unfolded_fields[0..1000].each do |field|
78
+
79
+ field = Field.new(field, nil, charset)
80
+ field.errors.each { |error| self.errors << error }
81
+ selected = select_field_for(field.name)
82
+
83
+ if selected.any? && limited_field?(field.name)
84
+ selected.first.update(field.name, field.value)
85
+ else
86
+ @fields << field
87
+ end
88
+ end
89
+
90
+ end
91
+
92
+ def errors
93
+ @errors
94
+ end
95
+
96
+ # 3.6. Field definitions
97
+ #
98
+ # The following table indicates limits on the number of times each
99
+ # field may occur in a message header as well as any special
100
+ # limitations on the use of those fields. An asterisk next to a value
101
+ # in the minimum or maximum column indicates that a special restriction
102
+ # appears in the Notes column.
103
+ #
104
+ # <snip table from 3.6>
105
+ #
106
+ # As per RFC, many fields can appear more than once, we will return a string
107
+ # of the value if there is only one header, or if there is more than one
108
+ # matching header, will return an array of values in order that they appear
109
+ # in the header ordered from top to bottom.
110
+ #
111
+ # Example:
112
+ #
113
+ # h = Header.new
114
+ # h.fields = ['To: mikel@me.com', 'X-Mail-SPAM: 15', 'X-Mail-SPAM: 20']
115
+ # h['To'] #=> 'mikel@me.com'
116
+ # h['X-Mail-SPAM'] #=> ['15', '20']
117
+ def [](name)
118
+ name = dasherize(name).downcase
119
+ selected = select_field_for(name)
120
+ case
121
+ when selected.length > 1
122
+ selected.map { |f| f }
123
+ when !selected.blank?
124
+ selected.first
125
+ else
126
+ nil
127
+ end
128
+ end
129
+
130
+ # Sets the FIRST matching field in the header to passed value, or deletes
131
+ # the FIRST field matched from the header if passed nil
132
+ #
133
+ # Example:
134
+ #
135
+ # h = Header.new
136
+ # h.fields = ['To: mikel@me.com', 'X-Mail-SPAM: 15', 'X-Mail-SPAM: 20']
137
+ # h['To'] = 'bob@you.com'
138
+ # h['To'] #=> 'bob@you.com'
139
+ # h['X-Mail-SPAM'] = '10000'
140
+ # h['X-Mail-SPAM'] # => ['15', '20', '10000']
141
+ # h['X-Mail-SPAM'] = nil
142
+ # h['X-Mail-SPAM'] # => nil
143
+ def []=(name, value)
144
+ name = dasherize(name)
145
+ fn = name.downcase
146
+ selected = select_field_for(fn)
147
+
148
+ case
149
+ # User wants to delete the field
150
+ when !selected.blank? && value == nil
151
+ fields.delete_if { |f| selected.include?(f) }
152
+
153
+ # User wants to change the field
154
+ when !selected.blank? && limited_field?(fn)
155
+ selected.first.update(fn, value)
156
+
157
+ # User wants to create the field
158
+ else
159
+ # Need to insert in correct order for trace fields
160
+ self.fields << Field.new(name.to_s, value, charset)
161
+ end
162
+ end
163
+
164
+ def charset
165
+ params = self[:content_type].parameters rescue nil
166
+ if params
167
+ params[:charset]
168
+ else
169
+ @charset
170
+ end
171
+ end
172
+
173
+ def charset=(val)
174
+ params = self[:content_type].parameters rescue nil
175
+ if params
176
+ params[:charset] = val
177
+ end
178
+ @charset = val
179
+ end
180
+
181
+ LIMITED_FIELDS = %w[ date from sender reply-to to cc bcc
182
+ message-id in-reply-to references subject
183
+ return-path content-type mime-version
184
+ content-transfer-encoding content-description
185
+ content-id content-disposition content-location]
186
+
187
+ def encoded
188
+ buffer = ''
189
+ fields.each do |field|
190
+ buffer << field.encoded
191
+ end
192
+ buffer
193
+ end
194
+
195
+ def to_s
196
+ encoded
197
+ end
198
+
199
+ def decoded
200
+ raise NoMethodError, 'Can not decode an entire header as there could be character set conflicts, try calling #decoded on the various fields.'
201
+ end
202
+
203
+ def field_summary
204
+ fields.map { |f| "<#{f.name}: #{f.value}>" }.join(", ")
205
+ end
206
+
207
+ # Returns true if the header has a Message-ID defined (empty or not)
208
+ def has_message_id?
209
+ !fields.select { |f| f.responsible_for?('Message-ID') }.empty?
210
+ end
211
+
212
+ # Returns true if the header has a Content-ID defined (empty or not)
213
+ def has_content_id?
214
+ !fields.select { |f| f.responsible_for?('Content-ID') }.empty?
215
+ end
216
+
217
+ # Returns true if the header has a Date defined (empty or not)
218
+ def has_date?
219
+ !fields.select { |f| f.responsible_for?('Date') }.empty?
220
+ end
221
+
222
+ # Returns true if the header has a MIME version defined (empty or not)
223
+ def has_mime_version?
224
+ !fields.select { |f| f.responsible_for?('Mime-Version') }.empty?
225
+ end
226
+
227
+ private
228
+
229
+ def raw_source=(val)
230
+ @raw_source = val
231
+ end
232
+
233
+ # 2.2.3. Long Header Fields
234
+ #
235
+ # The process of moving from this folded multiple-line representation
236
+ # of a header field to its single line representation is called
237
+ # "unfolding". Unfolding is accomplished by simply removing any CRLF
238
+ # that is immediately followed by WSP. Each header field should be
239
+ # treated in its unfolded form for further syntactic and semantic
240
+ # evaluation.
241
+ def unfold(string)
242
+ string.gsub(/#{CRLF}#{WSP}+/, ' ').gsub(/#{WSP}+/, ' ')
243
+ end
244
+
245
+ # Returns the header with all the folds removed
246
+ def unfolded_header
247
+ @unfolded_header ||= unfold(raw_source)
248
+ end
249
+
250
+ # Splits an unfolded and line break cleaned header into individual field
251
+ # strings.
252
+ def split_header
253
+ self.fields = unfolded_header.split(CRLF)
254
+ end
255
+
256
+ def select_field_for(name)
257
+ fields.select { |f| f.responsible_for?(name.to_s) }
258
+ end
259
+
260
+ def limited_field?(name)
261
+ LIMITED_FIELDS.include?(name.to_s.downcase)
262
+ end
263
+
264
+ end
265
+ end