Skip to content

Commit

Permalink
Adding Nicolas patches and tests... more work to do... 54 failures
Browse files Browse the repository at this point in the history
  • Loading branch information
mikel committed Aug 7, 2009
1 parent 43a757e commit ac5bfa5
Show file tree
Hide file tree
Showing 42 changed files with 8,138 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/tmail.rb
Expand Up @@ -3,3 +3,4 @@
require 'tmail/mailbox'
require 'tmail/core_extensions'
require 'tmail/net'
require 'vendor/rchardet-1.3/lib/rchardet'
62 changes: 40 additions & 22 deletions lib/tmail/attachments.rb
Expand Up @@ -14,33 +14,51 @@ class Attachment < StringIO

class Mail
def has_attachments?
multipart? && parts.any? { |part| attachment?(part) }
attachment?(self) || multipart? && parts.any? { |part| attachment?(part) }
end

# Returns true if this part's content main type is text, else returns false.
# By main type is meant "text/plain" is text. "text/html" is text
def text_content_type?
self.header['content-type'] && (self.header['content-type'].main_type == 'text')
end

def inline_attachment?(part)
part['content-id'] || (part['content-disposition'] && part['content-disposition'].disposition == 'inline' && !part.text_content_type?)
end

def attachment?(part)
part.disposition_is_attachment? || part.content_type_is_text?
part.disposition_is_attachment? || (!part.content_type.nil? && !part.text_content_type?) unless part.multipart? or inline_attachment?(part)
end

def attachments
scan_parts = multipart? ? parts : [self]
scan_parts.collect { |part|
if part.multipart?
part.attachments
elsif attachment?(part)
content = part.body # unquoted automatically by TMail#body
file_name = (part['content-location'] &&
part['content-location'].body) ||
part.sub_header("content-type", "name") ||
part.sub_header("content-disposition", "filename")

next if file_name.blank? || content.blank?

attachment = Attachment.new(content)
attachment.original_filename = file_name.strip
attachment.content_type = part.content_type
attachment
end
}.flatten.compact
if multipart?
parts.collect { |part| attachment(part) }.flatten.compact
elsif attachment?(self)
[attachment(self)]
end
end

private

def attachment(part)
if part.multipart?
part.attachments
elsif attachment?(part)
content = part.body # unquoted automatically by TMail#body
file_name = (part['content-location'] && part['content-location'].body) ||
part.sub_header('content-type', 'name') ||
part.sub_header('content-disposition', 'filename') ||
'noname'

return if content.blank?

attachment = TMail::Attachment.new(content)
attachment.original_filename = file_name.strip unless file_name.blank?
attachment.content_type = part.content_type
attachment
end
end

end
end
27 changes: 27 additions & 0 deletions lib/tmail/quoting.rb
Expand Up @@ -92,6 +92,29 @@ def unquote_and_convert_to(text, to_charset, from_charset = "iso-8859-1", preser
end
end

def convert_to_with_fallback_on_iso_8859_1(text, to, from)
return text if to == 'utf-8' and text.isutf8

if from.blank? and !text.is_binary_data?
from = CharDet.detect(text)['encoding']

# Chardet ususally detects iso-8859-2 (aka windows-1250), but the text is
# iso-8859-1 (aka windows-1252 and Latin1). http://en.wikipedia.org/wiki/ISO/IEC_8859-2
# This can cause unwanted characters, like ŕ instead of à.
# (I know, could be a very bad decision...)
from = 'iso-8859-1' if from =~ /iso-8859-2/i
end

begin
convert_to_without_fallback_on_iso_8859_1(text, to, from)
rescue Iconv::InvalidCharacter
unless from == 'iso-8859-1'
from = 'iso-8859-1'
retry
end
end
end

def unquote_quoted_printable_and_convert_to(text, to, from, preserve_underscores=false)
text = text.gsub(/_/, " ") unless preserve_underscores
text = text.gsub(/\r\n|\r/, "\n") # normalize newlines
Expand Down Expand Up @@ -123,6 +146,10 @@ def convert_to(text, to, from)
text
end
end

alias_method :convert_to_without_fallback_on_iso_8859_1, :convert_to
alias_method :convert_to, :convert_to_with_fallback_on_iso_8859_1

end
end
end

0 comments on commit ac5bfa5

Please sign in to comment.