otherinbox-mail 2.4.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +607 -0
- data/CONTRIBUTING.md +45 -0
- data/Dependencies.txt +3 -0
- data/Gemfile +26 -0
- data/Gemfile.lock +44 -0
- data/README.md +663 -0
- data/Rakefile +40 -0
- data/TODO.rdoc +9 -0
- data/lib/VERSION +4 -0
- data/lib/mail.rb +101 -0
- data/lib/mail/attachments_list.rb +104 -0
- data/lib/mail/body.rb +291 -0
- data/lib/mail/configuration.rb +75 -0
- data/lib/mail/core_extensions/nil.rb +17 -0
- data/lib/mail/core_extensions/object.rb +13 -0
- data/lib/mail/core_extensions/shell_escape.rb +56 -0
- data/lib/mail/core_extensions/smtp.rb +25 -0
- data/lib/mail/core_extensions/string.rb +33 -0
- data/lib/mail/core_extensions/string/access.rb +145 -0
- data/lib/mail/core_extensions/string/multibyte.rb +78 -0
- data/lib/mail/elements.rb +14 -0
- data/lib/mail/elements/address.rb +306 -0
- data/lib/mail/elements/address_list.rb +74 -0
- data/lib/mail/elements/content_disposition_element.rb +30 -0
- data/lib/mail/elements/content_location_element.rb +25 -0
- data/lib/mail/elements/content_transfer_encoding_element.rb +24 -0
- data/lib/mail/elements/content_type_element.rb +35 -0
- data/lib/mail/elements/date_time_element.rb +26 -0
- data/lib/mail/elements/envelope_from_element.rb +34 -0
- data/lib/mail/elements/message_ids_element.rb +29 -0
- data/lib/mail/elements/mime_version_element.rb +26 -0
- data/lib/mail/elements/phrase_list.rb +21 -0
- data/lib/mail/elements/received_element.rb +30 -0
- data/lib/mail/encodings.rb +274 -0
- data/lib/mail/encodings/7bit.rb +31 -0
- data/lib/mail/encodings/8bit.rb +31 -0
- data/lib/mail/encodings/base64.rb +33 -0
- data/lib/mail/encodings/binary.rb +31 -0
- data/lib/mail/encodings/quoted_printable.rb +38 -0
- data/lib/mail/encodings/transfer_encoding.rb +58 -0
- data/lib/mail/envelope.rb +35 -0
- data/lib/mail/field.rb +234 -0
- data/lib/mail/field_list.rb +33 -0
- data/lib/mail/fields.rb +35 -0
- data/lib/mail/fields/bcc_field.rb +56 -0
- data/lib/mail/fields/cc_field.rb +55 -0
- data/lib/mail/fields/comments_field.rb +41 -0
- data/lib/mail/fields/common/address_container.rb +16 -0
- data/lib/mail/fields/common/common_address.rb +125 -0
- data/lib/mail/fields/common/common_date.rb +42 -0
- data/lib/mail/fields/common/common_field.rb +51 -0
- data/lib/mail/fields/common/common_message_id.rb +44 -0
- data/lib/mail/fields/common/parameter_hash.rb +58 -0
- data/lib/mail/fields/content_description_field.rb +19 -0
- data/lib/mail/fields/content_disposition_field.rb +69 -0
- data/lib/mail/fields/content_id_field.rb +63 -0
- data/lib/mail/fields/content_location_field.rb +42 -0
- data/lib/mail/fields/content_transfer_encoding_field.rb +50 -0
- data/lib/mail/fields/content_type_field.rb +198 -0
- data/lib/mail/fields/date_field.rb +57 -0
- data/lib/mail/fields/from_field.rb +55 -0
- data/lib/mail/fields/in_reply_to_field.rb +55 -0
- data/lib/mail/fields/keywords_field.rb +44 -0
- data/lib/mail/fields/message_id_field.rb +83 -0
- data/lib/mail/fields/mime_version_field.rb +53 -0
- data/lib/mail/fields/optional_field.rb +13 -0
- data/lib/mail/fields/received_field.rb +75 -0
- data/lib/mail/fields/references_field.rb +55 -0
- data/lib/mail/fields/reply_to_field.rb +55 -0
- data/lib/mail/fields/resent_bcc_field.rb +55 -0
- data/lib/mail/fields/resent_cc_field.rb +55 -0
- data/lib/mail/fields/resent_date_field.rb +35 -0
- data/lib/mail/fields/resent_from_field.rb +55 -0
- data/lib/mail/fields/resent_message_id_field.rb +34 -0
- data/lib/mail/fields/resent_sender_field.rb +62 -0
- data/lib/mail/fields/resent_to_field.rb +55 -0
- data/lib/mail/fields/return_path_field.rb +65 -0
- data/lib/mail/fields/sender_field.rb +67 -0
- data/lib/mail/fields/structured_field.rb +51 -0
- data/lib/mail/fields/subject_field.rb +16 -0
- data/lib/mail/fields/to_field.rb +55 -0
- data/lib/mail/fields/unstructured_field.rb +191 -0
- data/lib/mail/header.rb +265 -0
- data/lib/mail/indifferent_hash.rb +146 -0
- data/lib/mail/mail.rb +255 -0
- data/lib/mail/matchers/has_sent_mail.rb +124 -0
- data/lib/mail/message.rb +2059 -0
- data/lib/mail/multibyte.rb +42 -0
- data/lib/mail/multibyte/chars.rb +474 -0
- data/lib/mail/multibyte/exceptions.rb +8 -0
- data/lib/mail/multibyte/unicode.rb +392 -0
- data/lib/mail/multibyte/utils.rb +60 -0
- data/lib/mail/network.rb +14 -0
- data/lib/mail/network/delivery_methods/exim.rb +53 -0
- data/lib/mail/network/delivery_methods/file_delivery.rb +40 -0
- data/lib/mail/network/delivery_methods/sendmail.rb +62 -0
- data/lib/mail/network/delivery_methods/smtp.rb +153 -0
- data/lib/mail/network/delivery_methods/smtp_connection.rb +74 -0
- data/lib/mail/network/delivery_methods/test_mailer.rb +40 -0
- data/lib/mail/network/retriever_methods/base.rb +63 -0
- data/lib/mail/network/retriever_methods/imap.rb +168 -0
- data/lib/mail/network/retriever_methods/pop3.rb +140 -0
- data/lib/mail/network/retriever_methods/test_retriever.rb +47 -0
- data/lib/mail/parsers/address_lists.rb +64 -0
- data/lib/mail/parsers/address_lists.treetop +19 -0
- data/lib/mail/parsers/content_disposition.rb +535 -0
- data/lib/mail/parsers/content_disposition.treetop +46 -0
- data/lib/mail/parsers/content_location.rb +139 -0
- data/lib/mail/parsers/content_location.treetop +20 -0
- data/lib/mail/parsers/content_transfer_encoding.rb +162 -0
- data/lib/mail/parsers/content_transfer_encoding.treetop +20 -0
- data/lib/mail/parsers/content_type.rb +967 -0
- data/lib/mail/parsers/content_type.treetop +68 -0
- data/lib/mail/parsers/date_time.rb +114 -0
- data/lib/mail/parsers/date_time.treetop +11 -0
- data/lib/mail/parsers/envelope_from.rb +194 -0
- data/lib/mail/parsers/envelope_from.treetop +32 -0
- data/lib/mail/parsers/message_ids.rb +45 -0
- data/lib/mail/parsers/message_ids.treetop +15 -0
- data/lib/mail/parsers/mime_version.rb +144 -0
- data/lib/mail/parsers/mime_version.treetop +19 -0
- data/lib/mail/parsers/phrase_lists.rb +45 -0
- data/lib/mail/parsers/phrase_lists.treetop +15 -0
- data/lib/mail/parsers/received.rb +71 -0
- data/lib/mail/parsers/received.treetop +11 -0
- data/lib/mail/parsers/rfc2045.rb +464 -0
- data/lib/mail/parsers/rfc2045.treetop +36 -0
- data/lib/mail/parsers/rfc2822.rb +5341 -0
- data/lib/mail/parsers/rfc2822.treetop +410 -0
- data/lib/mail/parsers/rfc2822_obsolete.rb +3768 -0
- data/lib/mail/parsers/rfc2822_obsolete.treetop +241 -0
- data/lib/mail/part.rb +116 -0
- data/lib/mail/parts_list.rb +55 -0
- data/lib/mail/patterns.rb +34 -0
- data/lib/mail/utilities.rb +215 -0
- data/lib/mail/version.rb +24 -0
- data/lib/mail/version_specific/ruby_1_8.rb +98 -0
- data/lib/mail/version_specific/ruby_1_9.rb +113 -0
- data/lib/tasks/corpus.rake +125 -0
- data/lib/tasks/treetop.rake +10 -0
- metadata +253 -0
@@ -0,0 +1,65 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# 4.4.3. REPLY-TO / RESENT-REPLY-TO
|
4
|
+
#
|
5
|
+
# Note: The "Return-Path" field is added by the mail transport
|
6
|
+
# service, at the time of final deliver. It is intended
|
7
|
+
# to identify a path back to the orginator of the mes-
|
8
|
+
# sage. The "Reply-To" field is added by the message
|
9
|
+
# originator and is intended to direct replies.
|
10
|
+
#
|
11
|
+
# trace = [return]
|
12
|
+
# 1*received
|
13
|
+
#
|
14
|
+
# return = "Return-Path:" path CRLF
|
15
|
+
#
|
16
|
+
# path = ([CFWS] "<" ([CFWS] / addr-spec) ">" [CFWS]) /
|
17
|
+
# obs-path
|
18
|
+
#
|
19
|
+
# received = "Received:" name-val-list ";" date-time CRLF
|
20
|
+
#
|
21
|
+
# name-val-list = [CFWS] [name-val-pair *(CFWS name-val-pair)]
|
22
|
+
#
|
23
|
+
# name-val-pair = item-name CFWS item-value
|
24
|
+
#
|
25
|
+
# item-name = ALPHA *(["-"] (ALPHA / DIGIT))
|
26
|
+
#
|
27
|
+
# item-value = 1*angle-addr / addr-spec /
|
28
|
+
# atom / domain / msg-id
|
29
|
+
#
|
30
|
+
require 'mail/fields/common/common_address'
|
31
|
+
|
32
|
+
module Mail
|
33
|
+
class ReturnPathField < StructuredField
|
34
|
+
|
35
|
+
include Mail::CommonAddress
|
36
|
+
|
37
|
+
FIELD_NAME = 'return-path'
|
38
|
+
CAPITALIZED_FIELD = 'Return-Path'
|
39
|
+
|
40
|
+
def initialize(value = nil, charset = 'utf-8')
|
41
|
+
value = nil if value == '<>'
|
42
|
+
self.charset = charset
|
43
|
+
super(CAPITALIZED_FIELD, strip_field(FIELD_NAME, value), charset)
|
44
|
+
self.parse
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
def encoded
|
49
|
+
"#{CAPITALIZED_FIELD}: <#{address}>\r\n"
|
50
|
+
end
|
51
|
+
|
52
|
+
def decoded
|
53
|
+
do_decode
|
54
|
+
end
|
55
|
+
|
56
|
+
def address
|
57
|
+
addresses.first
|
58
|
+
end
|
59
|
+
|
60
|
+
def default
|
61
|
+
address
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -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
|
+
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,191 @@
|
|
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
|
+
attr_accessor :charset
|
23
|
+
attr_reader :errors
|
24
|
+
|
25
|
+
def initialize(name, value, charset = nil)
|
26
|
+
@errors = []
|
27
|
+
if charset
|
28
|
+
self.charset = charset
|
29
|
+
else
|
30
|
+
if value.to_s.respond_to?(:encoding)
|
31
|
+
self.charset = value.to_s.encoding
|
32
|
+
else
|
33
|
+
self.charset = $KCODE
|
34
|
+
end
|
35
|
+
end
|
36
|
+
self.name = name
|
37
|
+
self.value = value
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
def encoded
|
42
|
+
do_encode
|
43
|
+
end
|
44
|
+
|
45
|
+
def decoded
|
46
|
+
do_decode
|
47
|
+
end
|
48
|
+
|
49
|
+
def default
|
50
|
+
decoded
|
51
|
+
end
|
52
|
+
|
53
|
+
def parse # An unstructured field does not parse
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def do_encode
|
60
|
+
value.nil? ? '' : "#{wrapped_value}\r\n"
|
61
|
+
end
|
62
|
+
|
63
|
+
def do_decode
|
64
|
+
result = value.blank? ? nil : Encodings.decode_encode(value, :decode)
|
65
|
+
result.encode!(value.encoding || "UTF-8") if RUBY_VERSION >= '1.9' && !result.blank?
|
66
|
+
result
|
67
|
+
end
|
68
|
+
|
69
|
+
# 2.2.3. Long Header Fields
|
70
|
+
#
|
71
|
+
# Each header field is logically a single line of characters comprising
|
72
|
+
# the field name, the colon, and the field body. For convenience
|
73
|
+
# however, and to deal with the 998/78 character limitations per line,
|
74
|
+
# the field body portion of a header field can be split into a multiple
|
75
|
+
# line representation; this is called "folding". The general rule is
|
76
|
+
# that wherever this standard allows for folding white space (not
|
77
|
+
# simply WSP characters), a CRLF may be inserted before any WSP. For
|
78
|
+
# example, the header field:
|
79
|
+
#
|
80
|
+
# Subject: This is a test
|
81
|
+
#
|
82
|
+
# can be represented as:
|
83
|
+
#
|
84
|
+
# Subject: This
|
85
|
+
# is a test
|
86
|
+
#
|
87
|
+
# Note: Though structured field bodies are defined in such a way that
|
88
|
+
# folding can take place between many of the lexical tokens (and even
|
89
|
+
# within some of the lexical tokens), folding SHOULD be limited to
|
90
|
+
# placing the CRLF at higher-level syntactic breaks. For instance, if
|
91
|
+
# a field body is defined as comma-separated values, it is recommended
|
92
|
+
# that folding occur after the comma separating the structured items in
|
93
|
+
# preference to other places where the field could be folded, even if
|
94
|
+
# it is allowed elsewhere.
|
95
|
+
def wrapped_value # :nodoc:
|
96
|
+
wrap_lines(name, fold("#{name}: ".length))
|
97
|
+
end
|
98
|
+
|
99
|
+
# 6.2. Display of 'encoded-word's
|
100
|
+
#
|
101
|
+
# When displaying a particular header field that contains multiple
|
102
|
+
# 'encoded-word's, any 'linear-white-space' that separates a pair of
|
103
|
+
# adjacent 'encoded-word's is ignored. (This is to allow the use of
|
104
|
+
# multiple 'encoded-word's to represent long strings of unencoded text,
|
105
|
+
# without having to separate 'encoded-word's where spaces occur in the
|
106
|
+
# unencoded text.)
|
107
|
+
def wrap_lines(name, folded_lines)
|
108
|
+
result = ["#{name}: #{folded_lines.shift}"]
|
109
|
+
result.concat(folded_lines)
|
110
|
+
result.join("\r\n\s")
|
111
|
+
end
|
112
|
+
|
113
|
+
def fold(prepend = 0) # :nodoc:
|
114
|
+
encoding = normalized_encoding
|
115
|
+
decoded_string = decoded.to_s
|
116
|
+
should_encode = decoded_string.not_ascii_only?
|
117
|
+
if should_encode
|
118
|
+
first = true
|
119
|
+
words = decoded_string.split(/[ \t]/).map do |word|
|
120
|
+
if first
|
121
|
+
first = !first
|
122
|
+
else
|
123
|
+
word = " " << word
|
124
|
+
end
|
125
|
+
if word.not_ascii_only?
|
126
|
+
word
|
127
|
+
else
|
128
|
+
word.scan(/.{7}|.+$/)
|
129
|
+
end
|
130
|
+
end.flatten
|
131
|
+
else
|
132
|
+
words = decoded_string.split(/[ \t]/)
|
133
|
+
end
|
134
|
+
|
135
|
+
folded_lines = []
|
136
|
+
while !words.empty?
|
137
|
+
limit = 78 - prepend
|
138
|
+
limit = limit - 7 - encoding.length if should_encode
|
139
|
+
line = ""
|
140
|
+
while !words.empty?
|
141
|
+
break unless word = words.first.dup
|
142
|
+
word.encode!(charset) if defined?(Encoding) && charset
|
143
|
+
word = encode(word) if should_encode
|
144
|
+
word = encode_crlf(word)
|
145
|
+
# Skip to next line if we're going to go past the limit
|
146
|
+
# Unless this is the first word, in which case we're going to add it anyway
|
147
|
+
# 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.
|
148
|
+
# (The fix, it seems, would be to use encoded-word encoding on it, because that way you can break it across multiple lines and
|
149
|
+
# the linebreak will be ignored)
|
150
|
+
break if !line.empty? && (line.length + word.length + 1 > limit)
|
151
|
+
# Remove the word from the queue ...
|
152
|
+
words.shift
|
153
|
+
# Add word separator
|
154
|
+
line << " " unless (line.empty? || should_encode)
|
155
|
+
# ... add it in encoded form to the current line
|
156
|
+
line << word
|
157
|
+
end
|
158
|
+
# Encode the line if necessary
|
159
|
+
line = "=?#{encoding}?Q?#{line}?=" if should_encode
|
160
|
+
# Add the line to the output and reset the prepend
|
161
|
+
folded_lines << line
|
162
|
+
prepend = 0
|
163
|
+
end
|
164
|
+
folded_lines
|
165
|
+
end
|
166
|
+
|
167
|
+
def encode(value)
|
168
|
+
value = [value].pack("M").gsub("=\n", '')
|
169
|
+
value.gsub!(/"/, '=22')
|
170
|
+
value.gsub!(/\(/, '=28')
|
171
|
+
value.gsub!(/\)/, '=29')
|
172
|
+
value.gsub!(/\?/, '=3F')
|
173
|
+
value.gsub!(/_/, '=5F')
|
174
|
+
value.gsub!(/ /, '_')
|
175
|
+
value
|
176
|
+
end
|
177
|
+
|
178
|
+
def encode_crlf(value)
|
179
|
+
value.gsub!("\r", '=0D')
|
180
|
+
value.gsub!("\n", '=0A')
|
181
|
+
value
|
182
|
+
end
|
183
|
+
|
184
|
+
def normalized_encoding
|
185
|
+
encoding = charset.to_s.upcase.gsub('_', '-')
|
186
|
+
encoding = 'UTF-8' if encoding == 'UTF8' # Ruby 1.8.x and $KCODE == 'u'
|
187
|
+
encoding
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
end
|