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,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mail
|
3
|
+
class ContentTransferEncodingElement
|
4
|
+
|
5
|
+
include Mail::Utilities
|
6
|
+
|
7
|
+
def initialize( string )
|
8
|
+
parser = Mail::ContentTransferEncodingParser.new
|
9
|
+
case
|
10
|
+
when string.blank?
|
11
|
+
@encoding = ''
|
12
|
+
when tree = parser.parse(string.to_s.downcase)
|
13
|
+
@encoding = tree.encoding.text_value
|
14
|
+
else
|
15
|
+
raise Mail::Field::ParseError.new(ContentTransferEncodingElement, string, parser.failure_reason)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def encoding
|
20
|
+
@encoding
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mail
|
3
|
+
class ContentTypeElement # :nodoc:
|
4
|
+
|
5
|
+
include Mail::Utilities
|
6
|
+
|
7
|
+
def initialize( string )
|
8
|
+
parser = Mail::ContentTypeParser.new
|
9
|
+
if tree = parser.parse(cleaned(string))
|
10
|
+
@main_type = tree.main_type.text_value.downcase
|
11
|
+
@sub_type = tree.sub_type.text_value.downcase
|
12
|
+
@parameters = tree.parameters
|
13
|
+
else
|
14
|
+
raise Mail::Field::ParseError.new(ContentTypeElement, string, parser.failure_reason)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def main_type
|
19
|
+
@main_type
|
20
|
+
end
|
21
|
+
|
22
|
+
def sub_type
|
23
|
+
@sub_type
|
24
|
+
end
|
25
|
+
|
26
|
+
def parameters
|
27
|
+
@parameters
|
28
|
+
end
|
29
|
+
|
30
|
+
def cleaned(string)
|
31
|
+
string =~ /(.+);\s*$/ ? $1 : string
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mail
|
3
|
+
class DateTimeElement # :nodoc:
|
4
|
+
|
5
|
+
include Mail::Utilities
|
6
|
+
|
7
|
+
def initialize( string )
|
8
|
+
parser = Mail::DateTimeParser.new
|
9
|
+
if tree = parser.parse(string)
|
10
|
+
@date_string = tree.date.text_value
|
11
|
+
@time_string = tree.time.text_value
|
12
|
+
else
|
13
|
+
raise Mail::Field::ParseError.new(DateTimeElement, string, parser.failure_reason)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def date_string
|
18
|
+
@date_string
|
19
|
+
end
|
20
|
+
|
21
|
+
def time_string
|
22
|
+
@time_string
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mail
|
3
|
+
class EnvelopeFromElement
|
4
|
+
|
5
|
+
include Mail::Utilities
|
6
|
+
|
7
|
+
def initialize( string )
|
8
|
+
parser = Mail::EnvelopeFromParser.new
|
9
|
+
if @tree = parser.parse(string)
|
10
|
+
@address = tree.addr_spec.text_value.strip
|
11
|
+
@date_time = ::DateTime.parse("#{tree.ctime_date.text_value}")
|
12
|
+
else
|
13
|
+
raise Mail::Field::ParseError.new(EnvelopeFromElement, string, parser.failure_reason)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def tree
|
18
|
+
@tree
|
19
|
+
end
|
20
|
+
|
21
|
+
def date_time
|
22
|
+
@date_time
|
23
|
+
end
|
24
|
+
|
25
|
+
def address
|
26
|
+
@address
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_s(*args)
|
30
|
+
"#{@info}; #{@date_time.to_s(*args)}"
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mail
|
3
|
+
class MessageIdsElement
|
4
|
+
|
5
|
+
include Mail::Utilities
|
6
|
+
|
7
|
+
def initialize(string)
|
8
|
+
parser = Mail::MessageIdsParser.new
|
9
|
+
if tree = parser.parse(string)
|
10
|
+
@message_ids = tree.message_ids.map { |msg_id| clean_msg_id(msg_id.text_value) }
|
11
|
+
else
|
12
|
+
raise Mail::Field::ParseError.new(MessageIdsElement, string, parser.failure_reason)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def message_ids
|
17
|
+
@message_ids
|
18
|
+
end
|
19
|
+
|
20
|
+
def message_id
|
21
|
+
@message_ids.first
|
22
|
+
end
|
23
|
+
|
24
|
+
def clean_msg_id( val )
|
25
|
+
val =~ /.*<(.*)>.*/ ; $1
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mail
|
3
|
+
class MimeVersionElement
|
4
|
+
|
5
|
+
include Mail::Utilities
|
6
|
+
|
7
|
+
def initialize( string )
|
8
|
+
parser = Mail::MimeVersionParser.new
|
9
|
+
if tree = parser.parse(string)
|
10
|
+
@major = tree.major.text_value
|
11
|
+
@minor = tree.minor.text_value
|
12
|
+
else
|
13
|
+
raise Mail::Field::ParseError.new(MimeVersionElement, string, parser.failure_reason)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def major
|
18
|
+
@major
|
19
|
+
end
|
20
|
+
|
21
|
+
def minor
|
22
|
+
@minor
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mail
|
3
|
+
class PhraseList
|
4
|
+
|
5
|
+
include Mail::Utilities
|
6
|
+
|
7
|
+
def initialize(string)
|
8
|
+
parser = Mail::PhraseListsParser.new
|
9
|
+
if tree = parser.parse(string)
|
10
|
+
@phrases = tree.phrases
|
11
|
+
else
|
12
|
+
raise Mail::Field::ParseError.new(PhraseList, string, parser.failure_reason)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def phrases
|
17
|
+
@phrases.map { |p| unquote(p.text_value) }
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mail
|
3
|
+
class ReceivedElement
|
4
|
+
|
5
|
+
include Mail::Utilities
|
6
|
+
|
7
|
+
def initialize( string )
|
8
|
+
parser = Mail::ReceivedParser.new
|
9
|
+
if tree = parser.parse(string)
|
10
|
+
@date_time = ::DateTime.parse("#{tree.date_time.date.text_value} #{tree.date_time.time.text_value}")
|
11
|
+
@info = tree.name_val_list.text_value
|
12
|
+
else
|
13
|
+
raise Mail::Field::ParseError.new(ReceivedElement, string, parser.failure_reason)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def date_time
|
18
|
+
@date_time
|
19
|
+
end
|
20
|
+
|
21
|
+
def info
|
22
|
+
@info
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_s(*args)
|
26
|
+
"#{@info}; #{@date_time.to_s(*args)}"
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,274 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mail
|
4
|
+
# Raised when attempting to decode an unknown encoding type
|
5
|
+
class UnknownEncodingType < StandardError #:nodoc:
|
6
|
+
end
|
7
|
+
|
8
|
+
module Encodings
|
9
|
+
|
10
|
+
include Mail::Patterns
|
11
|
+
extend Mail::Utilities
|
12
|
+
|
13
|
+
@transfer_encodings = {}
|
14
|
+
|
15
|
+
# Register transfer encoding
|
16
|
+
#
|
17
|
+
# Example
|
18
|
+
#
|
19
|
+
# Encodings.register "base64", Mail::Encodings::Base64
|
20
|
+
def Encodings.register(name, cls)
|
21
|
+
@transfer_encodings[get_name(name)] = cls
|
22
|
+
end
|
23
|
+
|
24
|
+
# Is the encoding we want defined?
|
25
|
+
#
|
26
|
+
# Example:
|
27
|
+
#
|
28
|
+
# Encodings.defined?(:base64) #=> true
|
29
|
+
def Encodings.defined?( str )
|
30
|
+
@transfer_encodings.include? get_name(str)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Gets a defined encoding type, QuotedPrintable or Base64 for now.
|
34
|
+
#
|
35
|
+
# Each encoding needs to be defined as a Mail::Encodings::ClassName for
|
36
|
+
# this to work, allows us to add other encodings in the future.
|
37
|
+
#
|
38
|
+
# Example:
|
39
|
+
#
|
40
|
+
# Encodings.get_encoding(:base64) #=> Mail::Encodings::Base64
|
41
|
+
def Encodings.get_encoding( str )
|
42
|
+
@transfer_encodings[get_name(str)]
|
43
|
+
end
|
44
|
+
|
45
|
+
def Encodings.get_all
|
46
|
+
@transfer_encodings.values
|
47
|
+
end
|
48
|
+
|
49
|
+
def Encodings.get_name(enc)
|
50
|
+
enc = enc.to_s.gsub("-", "_").downcase
|
51
|
+
end
|
52
|
+
|
53
|
+
# Encodes a parameter value using URI Escaping, note the language field 'en' can
|
54
|
+
# be set using Mail::Configuration, like so:
|
55
|
+
#
|
56
|
+
# Mail.defaults.do
|
57
|
+
# param_encode_language 'jp'
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# The character set used for encoding will either be the value of $KCODE for
|
61
|
+
# Ruby < 1.9 or the encoding on the string passed in.
|
62
|
+
#
|
63
|
+
# Example:
|
64
|
+
#
|
65
|
+
# Mail::Encodings.param_encode("This is fun") #=> "us-ascii'en'This%20is%20fun"
|
66
|
+
def Encodings.param_encode(str)
|
67
|
+
case
|
68
|
+
when str.ascii_only? && str =~ TOKEN_UNSAFE
|
69
|
+
%Q{"#{str}"}
|
70
|
+
when str.ascii_only?
|
71
|
+
str
|
72
|
+
else
|
73
|
+
RubyVer.param_encode(str)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Decodes a parameter value using URI Escaping.
|
78
|
+
#
|
79
|
+
# Example:
|
80
|
+
#
|
81
|
+
# Mail::Encodings.param_decode("This%20is%20fun", 'us-ascii') #=> "This is fun"
|
82
|
+
#
|
83
|
+
# str = Mail::Encodings.param_decode("This%20is%20fun", 'iso-8559-1')
|
84
|
+
# str.encoding #=> 'ISO-8859-1' ## Only on Ruby 1.9
|
85
|
+
# str #=> "This is fun"
|
86
|
+
def Encodings.param_decode(str, encoding)
|
87
|
+
RubyVer.param_decode(str, encoding)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Decodes or encodes a string as needed for either Base64 or QP encoding types in
|
91
|
+
# the =?<encoding>?[QB]?<string>?=" format.
|
92
|
+
#
|
93
|
+
# The output type needs to be :decode to decode the input string or :encode to
|
94
|
+
# encode the input string. The character set used for encoding will either be
|
95
|
+
# the value of $KCODE for Ruby < 1.9 or the encoding on the string passed in.
|
96
|
+
#
|
97
|
+
# On encoding, will only send out Base64 encoded strings.
|
98
|
+
def Encodings.decode_encode(str, output_type)
|
99
|
+
case
|
100
|
+
when output_type == :decode
|
101
|
+
Encodings.value_decode(str)
|
102
|
+
else
|
103
|
+
if str.ascii_only?
|
104
|
+
str
|
105
|
+
else
|
106
|
+
Encodings.b_value_encode(str, find_encoding(str))
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# Decodes a given string as Base64 or Quoted Printable, depending on what
|
112
|
+
# type it is.
|
113
|
+
#
|
114
|
+
# String has to be of the format =?<encoding>?[QB]?<string>?=
|
115
|
+
def Encodings.value_decode(str)
|
116
|
+
# Optimization: If there's no encoded-words in the string, just return it
|
117
|
+
return str unless str.index("=?")
|
118
|
+
|
119
|
+
str = str.gsub(/\?=(\s*)=\?/, '?==?') # Remove whitespaces between 'encoded-word's
|
120
|
+
|
121
|
+
# Split on white-space boundaries with capture, so we capture the white-space as well
|
122
|
+
str.split(/([ \t])/).map do |text|
|
123
|
+
if text.index('=?') .nil?
|
124
|
+
text
|
125
|
+
else
|
126
|
+
# Join QP encoded-words that are adjacent to avoid decoding partial chars
|
127
|
+
text.gsub!(/\?\=\=\?.+?\?[Qq]\?/m, '') if text =~ /\?==\?/
|
128
|
+
|
129
|
+
# Search for occurences of quoted strings or plain strings
|
130
|
+
text.scan(/( # Group around entire regex to include it in matches
|
131
|
+
\=\?[^?]+\?([QB])\?[^?]+?\?\= # Quoted String with subgroup for encoding method
|
132
|
+
| # or
|
133
|
+
.+?(?=\=\?|$) # Plain String
|
134
|
+
)/xmi).map do |matches|
|
135
|
+
string, method = *matches
|
136
|
+
if method == 'b' || method == 'B'
|
137
|
+
b_value_decode(string)
|
138
|
+
elsif method == 'q' || method == 'Q'
|
139
|
+
q_value_decode(string)
|
140
|
+
else
|
141
|
+
string
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end.join("")
|
146
|
+
end
|
147
|
+
|
148
|
+
# Takes an encoded string of the format =?<encoding>?[QB]?<string>?=
|
149
|
+
def Encodings.unquote_and_convert_to(str, to_encoding)
|
150
|
+
original_encoding = split_encoding_from_string( str )
|
151
|
+
|
152
|
+
output = value_decode( str ).to_s
|
153
|
+
|
154
|
+
if original_encoding.to_s.downcase.gsub("-", "") == to_encoding.to_s.downcase.gsub("-", "")
|
155
|
+
output
|
156
|
+
elsif original_encoding && to_encoding
|
157
|
+
begin
|
158
|
+
if RUBY_VERSION >= '1.9'
|
159
|
+
output.encode(to_encoding)
|
160
|
+
else
|
161
|
+
require 'iconv'
|
162
|
+
Iconv.iconv(to_encoding, original_encoding, output).first
|
163
|
+
end
|
164
|
+
rescue Iconv::IllegalSequence, Iconv::InvalidEncoding, Errno::EINVAL
|
165
|
+
# the 'from' parameter specifies a charset other than what the text
|
166
|
+
# actually is...not much we can do in this case but just return the
|
167
|
+
# unconverted text.
|
168
|
+
#
|
169
|
+
# Ditto if either parameter represents an unknown charset, like
|
170
|
+
# X-UNKNOWN.
|
171
|
+
output
|
172
|
+
end
|
173
|
+
else
|
174
|
+
output
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def Encodings.address_encode(address, charset = 'utf-8')
|
179
|
+
if address.is_a?(Array)
|
180
|
+
# loop back through for each element
|
181
|
+
address.map { |a| Encodings.address_encode(a, charset) }.join(", ")
|
182
|
+
else
|
183
|
+
# find any word boundary that is not ascii and encode it
|
184
|
+
encode_non_usascii(address, charset)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def Encodings.encode_non_usascii(address, charset)
|
189
|
+
return address if address.ascii_only? or charset.nil?
|
190
|
+
us_ascii = %Q{\x00-\x7f}
|
191
|
+
# Encode any non usascii strings embedded inside of quotes
|
192
|
+
address.gsub!(/(".*?[^#{us_ascii}].*?")/) { |s| Encodings.b_value_encode(unquote(s), charset) }
|
193
|
+
# Then loop through all remaining items and encode as needed
|
194
|
+
tokens = address.split(/\s/)
|
195
|
+
map_with_index(tokens) do |word, i|
|
196
|
+
if word.ascii_only?
|
197
|
+
word
|
198
|
+
else
|
199
|
+
previous_non_ascii = tokens[i-1] && !tokens[i-1].ascii_only?
|
200
|
+
if previous_non_ascii
|
201
|
+
word = " #{word}"
|
202
|
+
end
|
203
|
+
Encodings.b_value_encode(word, charset)
|
204
|
+
end
|
205
|
+
end.join(' ')
|
206
|
+
end
|
207
|
+
|
208
|
+
# Encode a string with Base64 Encoding and returns it ready to be inserted
|
209
|
+
# as a value for a field, that is, in the =?<charset>?B?<string>?= format
|
210
|
+
#
|
211
|
+
# Example:
|
212
|
+
#
|
213
|
+
# Encodings.b_value_encode('This is あ string', 'UTF-8')
|
214
|
+
# #=> "=?UTF-8?B?VGhpcyBpcyDjgYIgc3RyaW5n?="
|
215
|
+
def Encodings.b_value_encode(encoded_str, encoding = nil)
|
216
|
+
return encoded_str if encoded_str.to_s.ascii_only?
|
217
|
+
string, encoding = RubyVer.b_value_encode(encoded_str, encoding)
|
218
|
+
map_lines(string) do |str|
|
219
|
+
"=?#{encoding}?B?#{str.chomp}?="
|
220
|
+
end.join(" ")
|
221
|
+
end
|
222
|
+
|
223
|
+
# Encode a string with Quoted-Printable Encoding and returns it ready to be inserted
|
224
|
+
# as a value for a field, that is, in the =?<charset>?Q?<string>?= format
|
225
|
+
#
|
226
|
+
# Example:
|
227
|
+
#
|
228
|
+
# Encodings.q_value_encode('This is あ string', 'UTF-8')
|
229
|
+
# #=> "=?UTF-8?Q?This_is_=E3=81=82_string?="
|
230
|
+
def Encodings.q_value_encode(encoded_str, encoding = nil)
|
231
|
+
return encoded_str if encoded_str.to_s.ascii_only?
|
232
|
+
string, encoding = RubyVer.q_value_encode(encoded_str, encoding)
|
233
|
+
string.gsub!("=\r\n", '') # We already have limited the string to the length we want
|
234
|
+
map_lines(string) do |str|
|
235
|
+
"=?#{encoding}?Q?#{str.chomp.gsub(/ /, '_')}?="
|
236
|
+
end.join(" ")
|
237
|
+
end
|
238
|
+
|
239
|
+
private
|
240
|
+
|
241
|
+
# Decodes a Base64 string from the "=?UTF-8?B?VGhpcyBpcyDjgYIgc3RyaW5n?=" format
|
242
|
+
#
|
243
|
+
# Example:
|
244
|
+
#
|
245
|
+
# Encodings.b_value_decode("=?UTF-8?B?VGhpcyBpcyDjgYIgc3RyaW5n?=")
|
246
|
+
# #=> 'This is あ string'
|
247
|
+
def Encodings.b_value_decode(str)
|
248
|
+
RubyVer.b_value_decode(str)
|
249
|
+
end
|
250
|
+
|
251
|
+
# Decodes a Quoted-Printable string from the "=?UTF-8?Q?This_is_=E3=81=82_string?=" format
|
252
|
+
#
|
253
|
+
# Example:
|
254
|
+
#
|
255
|
+
# Encodings.q_value_decode("=?UTF-8?Q?This_is_=E3=81=82_string?=")
|
256
|
+
# #=> 'This is あ string'
|
257
|
+
def Encodings.q_value_decode(str)
|
258
|
+
RubyVer.q_value_decode(str)
|
259
|
+
end
|
260
|
+
|
261
|
+
def Encodings.split_encoding_from_string( str )
|
262
|
+
match = str.match(/\=\?([^?]+)?\?[QB]\?(.+)?\?\=/mi)
|
263
|
+
if match
|
264
|
+
match[1]
|
265
|
+
else
|
266
|
+
nil
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
def Encodings.find_encoding(str)
|
271
|
+
RUBY_VERSION >= '1.9' ? str.encoding : $KCODE
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|