-
Notifications
You must be signed in to change notification settings - Fork 939
Apply patches from kuroda/iso-2022-jp #534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,7 @@ class SyntaxError < FieldError #:nodoc: | |
# | ||
# Field.new('content-type', ['text', 'plain', {:charset => 'UTF-8'}]) | ||
def initialize(name, value = nil, charset = 'utf-8') | ||
value = RubyVer.preprocess(charset, value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should move this into the cases below? |
||
case | ||
when name =~ /:/ # Field.new("field-name: field data") | ||
charset = value unless value.blank? | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,23 +3,45 @@ | |
|
||
module Mail | ||
module CommonAddress # :nodoc: | ||
|
||
|
||
def initialize(value = nil, charset = 'utf-8') | ||
if charset.to_s.downcase == 'iso-2022-jp' | ||
if value.kind_of?(Array) | ||
value = value.map { |e| encode_with_iso_2022_jp(e) } | ||
else | ||
value = encode_with_iso_2022_jp(value) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Special-casing multiple code paths based on charset is a good sign that we should extract the charset-specific coding/decoding operations. I suggest introducing something like a CharsetCodec.find(charset) #=> ISO2022JPCodec
class CharsetCodec
@codecs = Hash.new { |h, k| h[k] = CharsetCodec.new }
def self.find(name) @codecs[name] end
def self.register(name, codec) @codecs[name] = codec end
def encode_address(value)
value
end
def remap_characters(value)
value
end
end
class ISO2022JPCodec < CharsetCodec
def encode_address(value)
if value.kind_of?(Array)
value.map { |e| encode_address e }
elsif value =~ / <[\x00-\x7f]*>\z/
encode value
else
value
end
end
def remap_characters(value)
# Mail::Preprocessor.process goes here
value.gsub(REMAP_MATCH) { |c| REMAP[c] }
end
def encode(string)
RubyVer.encode_with_iso_2022_jp string
end
end
CharsetCodec.register 'iso-2022-jp', ISO2022JPCodec.new |
||
self.charset = charset | ||
super(self.class.const_get('CAPITALIZED_FIELD'), strip_field(self.class.const_get('FIELD_NAME'), value), charset) | ||
self.parse | ||
self | ||
end | ||
|
||
def encoded | ||
do_encode(self.class.const_get('CAPITALIZED_FIELD')) | ||
end | ||
|
||
def decoded | ||
do_decode | ||
end | ||
|
||
def parse(val = value) | ||
unless val.blank? | ||
@tree = AddressList.new(encode_if_needed(val)) | ||
else | ||
nil | ||
end | ||
end | ||
|
||
def charset | ||
@charset | ||
end | ||
|
||
def encode_if_needed(val) | ||
Encodings.address_encode(val, charset) | ||
end | ||
|
||
# Allows you to iterate through each address object in the syntax tree | ||
def each | ||
tree.addresses.each do |address| | ||
|
@@ -38,19 +60,19 @@ def formatted | |
list = tree.addresses.map { |a| a.format } | ||
Mail::AddressContainer.new(self, list) | ||
end | ||
|
||
# Returns the display name of all the addresses in the address list | ||
def display_names | ||
list = tree.addresses.map { |a| a.display_name } | ||
Mail::AddressContainer.new(self, list) | ||
end | ||
|
||
# Returns the actual address objects in the address list | ||
def addrs | ||
list = tree.addresses | ||
Mail::AddressContainer.new(self, list) | ||
end | ||
|
||
# Returns a hash of group name => address strings for the address list | ||
def groups | ||
@groups = Hash.new | ||
|
@@ -59,7 +81,7 @@ def groups | |
end | ||
@groups | ||
end | ||
|
||
# Returns the addresses that are part of groups | ||
def group_addresses | ||
decoded_group_addresses | ||
|
@@ -79,7 +101,7 @@ def encoded_group_addresses | |
def group_names # :nodoc: | ||
tree.group_names | ||
end | ||
|
||
def default | ||
addresses | ||
end | ||
|
@@ -99,9 +121,9 @@ def value=(val) | |
super | ||
parse(self.value) | ||
end | ||
|
||
private | ||
|
||
def do_encode(field_name) | ||
return '' if value.blank? | ||
address_array = tree.addresses.reject { |a| encoded_group_addresses.include?(a.encoded) }.compact.map { |a| a.encoded } | ||
|
@@ -114,6 +136,7 @@ def do_encode(field_name) | |
|
||
def do_decode | ||
return nil if value.blank? | ||
return value if charset.to_s.downcase == 'iso-2022-jp' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this return early? |
||
address_array = tree.addresses.reject { |a| decoded_group_addresses.include?(a.decoded) }.map { |a| a.decoded } | ||
address_text = address_array.join(", ") | ||
group_array = groups.map { |k,v| "#{k}: #{v.map { |a| a.decoded }.join(", ")};" } | ||
|
@@ -126,7 +149,7 @@ def do_decode | |
def tree # :nodoc: | ||
@tree ||= AddressList.new(value) | ||
end | ||
|
||
def get_group_addresses(group_list) | ||
if group_list.respond_to?(:addresses) | ||
group_list.addresses.map do |address_tree| | ||
|
@@ -136,5 +159,13 @@ def get_group_addresses(group_list) | |
[] | ||
end | ||
end | ||
|
||
def encode_with_iso_2022_jp(string) | ||
if md = string.match(/ <[\x00-\x7f]*>\z/) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be 1 or more matches? |
||
RubyVer.encode_with_iso_2022_jp(md.pre_match) + md[0] | ||
else | ||
string | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we converting the raw body from iso-2022-jp to us-ascii?