aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/incoming_message.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/incoming_message.rb')
-rw-r--r--app/models/incoming_message.rb35
1 files changed, 20 insertions, 15 deletions
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb
index bcf0b6ec9..6db145348 100644
--- a/app/models/incoming_message.rb
+++ b/app/models/incoming_message.rb
@@ -35,7 +35,7 @@
require 'htmlentities'
require 'rexml/document'
require 'zip/zip'
-require 'iconv' unless RUBY_VERSION >= '1.9'
+require 'iconv' unless String.method_defined?(:encode)
class IncomingMessage < ActiveRecord::Base
extend MessageProminence
@@ -294,7 +294,7 @@ class IncomingMessage < ActiveRecord::Base
emails = ascii_chars.scan(MySociety::Validate.email_find_regexp)
# Convert back to UCS-2, making a mask at the same time
- if RUBY_VERSION >= '1.9'
+ if String.method_defined?(:encode)
emails.map! do |email|
# We want the ASCII representation of UCS-2
[email[0].encode('UTF-16LE').force_encoding('US-ASCII'),
@@ -385,6 +385,10 @@ class IncomingMessage < ActiveRecord::Base
multiline_original_message = '(' + '''>>>.* \d\d/\d\d/\d\d\d\d\s+\d\d:\d\d(?::\d\d)?\s*>>>''' + ')'
text.gsub!(/^(#{multiline_original_message}\n.*)$/m, replacement)
+ # On Thu, Nov 28, 2013 at 9:08 AM, A User
+ # <[1]request-7-skm40s2ls@xxx.xxxx> wrote:
+ text.gsub!(/^( On [^\n]+\n\s*\<[^>\n]+\> (wrote|said):\s*\n.*)$/m, replacement)
+
# Single line sections
text.gsub!(/^(>.*\n)/, replacement)
text.gsub!(/^(On .+ (wrote|said):\n)/, replacement)
@@ -516,7 +520,7 @@ class IncomingMessage < ActiveRecord::Base
# should instead tell elinks to respect the source
# charset
use_charset = "utf-8"
- if RUBY_VERSION.to_f >= 1.9
+ if String.method_defined?(:encode)
begin
text.encode('utf-8')
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
@@ -547,7 +551,7 @@ class IncomingMessage < ActiveRecord::Base
end
def _sanitize_text(text)
- if RUBY_VERSION.to_f >= 1.9
+ if String.method_defined?(:encode)
begin
# Test if it's good UTF-8
text.encode('utf-8')
@@ -792,27 +796,28 @@ class IncomingMessage < ActiveRecord::Base
return self.cached_attachment_text_clipped
end
- def _get_attachment_text_internal
+ def _extract_text
# Extract text from each attachment
- text = ''
- attachments = self.get_attachments_for_display
- for attachment in attachments
- text += MailHandler.get_attachment_text_one_file(attachment.content_type,
+ self.get_attachments_for_display.reduce(''){ |memo, attachment|
+ memo += MailHandler.get_attachment_text_one_file(attachment.content_type,
attachment.body,
attachment.charset)
- end
+ }
+ end
+
+ def _get_attachment_text_internal
+ text = self._extract_text
# Remove any bad characters
- if RUBY_VERSION >= '1.9'
- text.encode("utf-8", :invalid => :replace,
- :undef => :replace,
- :replace => "")
+ if String.method_defined?(:encode)
+ # handle "problematic" encoding
+ text.encode!('UTF-16', 'UTF-8', :invalid => :replace, :undef => :replace, :replace => '')
+ text.encode('UTF-8', 'UTF-16')
else
Iconv.conv('utf-8//IGNORE', 'utf-8', text)
end
end
-
# Returns text for indexing
def get_text_for_indexing_full
return get_body_for_quoting + "\n\n" + get_attachment_text_full