diff options
-rw-r--r-- | app/models/incoming_message.rb | 13 | ||||
m--------- | commonlib | 0 | ||||
-rw-r--r-- | lib/tmail_extensions.rb | 58 | ||||
-rw-r--r-- | spec/fixtures/multiple-unquoted-display-names.email | 27 | ||||
-rw-r--r-- | spec/lib/tmail_extensions_spec.rb | 6 |
5 files changed, 94 insertions, 10 deletions
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb index f437ab2c9..c808dc6a1 100644 --- a/app/models/incoming_message.rb +++ b/app/models/incoming_message.rb @@ -395,11 +395,12 @@ class FOIAttachment tempfile.print self.body tempfile.flush - if self.content_type == 'application/vnd.ms-word' - # XXX do something with PNG files this spits out so they view too :) - system("/usr/bin/wvHtml --charset=UTF-8 " + tempfile.path + " " + tempfile.path + ".html") - html = File.read(tempfile.path + ".html") - File.unlink(tempfile.path + ".html") + # Use google docs for the view for these - hanging server + # if self.content_type == 'application/vnd.ms-word' + # # XXX do something with PNG files this spits out so they view too :) + # system("/usr/bin/wvHtml --charset=UTF-8 " + tempfile.path + " " + tempfile.path + ".html") + # html = File.read(tempfile.path + ".html") + # File.unlink(tempfile.path + ".html") # elsif self.content_type == 'application/vnd.ms-excel' # # Don't colorise, e.g. otherwise this one comes out with white # # text which is nasty: @@ -408,7 +409,7 @@ class FOIAttachment # html = child.read() # wrapper_id = "wrapper_xlhtml" # end - elsif self.content_type == 'application/pdf' + if self.content_type == 'application/pdf' IO.popen("/usr/bin/pdftohtml -nodrm -zoom 1.0 -stdout -enc UTF-8 -noframes " + tempfile.path + "", "r") do |child| html = child.read() end diff --git a/commonlib b/commonlib -Subproject b43b5949501f763e84bed7fce30458578bf92f0 +Subproject 38e0a641eb63323f6a2f895bfbc1274b23e98a7 diff --git a/lib/tmail_extensions.rb b/lib/tmail_extensions.rb index c0ed1033b..6a5044cdb 100644 --- a/lib/tmail_extensions.rb +++ b/lib/tmail_extensions.rb @@ -5,8 +5,10 @@ # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # # $Id: tmail_extensions.rb,v 1.7 2009-10-02 23:31:01 francis Exp $ - +require 'racc/parser' require 'tmail' +require 'tmail/scanner' +require 'tmail/utils' require 'tmail/interface' # Monkeypatch! @@ -32,7 +34,7 @@ module TMail else return nil end - end + end # Monkeypatch! Generalisation of To:, Cc: def envelope_to(default = nil) @@ -72,7 +74,7 @@ module TMail # a name and an email def Address.address_from_name_and_email(name, email) if !MySociety::Validate.is_valid_email(email) - raise "invalid email " + email + " passed to address_from_name_and_email" + raise "invalid email " + email + " passed to address_from_name_and_email" end if name.nil? return TMail::Address.parse(email) @@ -84,7 +86,7 @@ module TMail end module TextUtils - # Monkeypatch! Much more aggressive list of characters to cause quoting + # Monkeypatch! Much more aggressive list of characters to cause quoting # than in normal TMail. e.g. Have found real cases where @ needs quoting. # We list characters to allow, rather than characters not to allow. NEW_PHRASE_UNSAFE=/[^A-Za-z0-9!#\$%&'*+\-\/=?^_`{|}~ ]/n @@ -94,4 +96,52 @@ module TMail end end +# Monkeypatch! TMail 1.2.7.1 will parse only one address out of a list of addresses with +# unquoted display parts https://github.com/mikel/tmail/issues#issue/9 - this monkeypatch +# fixes this issue. +module TMail + + class Parser < Racc::Parser + +module_eval <<'..end lib/tmail/parser.y modeval..id2dd1c7d21d', 'lib/tmail/parser.y', 340 + + def self.special_quote_address(str) #:nodoc: + # Takes a string which is an address and adds quotation marks to special + # edge case methods that the RACC parser can not handle. + # + # Right now just handles two edge cases: + # + # Full stop as the last character of the display name: + # Mikel L. <mikel@me.com> + # Returns: + # "Mikel L." <mikel@me.com> + # + # Unquoted @ symbol in the display name: + # mikel@me.com <mikel@me.com> + # Returns: + # "mikel@me.com" <mikel@me.com> + # + # Any other address not matching these patterns just gets returned as is. + case + # This handles the missing "" in an older version of Apple Mail.app + # around the display name when the display name contains a '@' + # like 'mikel@me.com <mikel@me.com>' + # Just quotes it to: '"mikel@me.com" <mikel@me.com>' + when str =~ /\A([^"][^<]+@[^>]+[^"])\s(<.*?>)\Z/ + return "\"#{$1}\" #{$2}" + # This handles cases where 'Mikel A. <mikel@me.com>' which is a trailing + # full stop before the address section. Just quotes it to + # '"Mikel A." <mikel@me.com>' + when str =~ /\A(.*?\.)\s(<.*?>)\s*\Z/ + return "\"#{$1}\" #{$2}" + else + str + end + end + +..end lib/tmail/parser.y modeval..id2dd1c7d21d + end # class Parser + +end # module TMail + diff --git a/spec/fixtures/multiple-unquoted-display-names.email b/spec/fixtures/multiple-unquoted-display-names.email new file mode 100644 index 000000000..e3a742c83 --- /dev/null +++ b/spec/fixtures/multiple-unquoted-display-names.email @@ -0,0 +1,27 @@ +From foi@example.com Mon Mar 21 09:58:48 2011 +Return-path: <foi@example.com> +Envelope-to: foi@wildfire.ukcod.org.uk +Delivery-date: Mon, 21 Mar 2011 09:58:48 +0000 +From: UAS Foi <foi@admin.ox.ac.uk> +To: WDTK User <request-66666-caa77777@whatdotheyknow.com>, Another address + <foi@example.com> +Date: Mon, 21 Mar 2011 09:58:30 +0000 +Subject: RE: Freedom of Information request - Participation in the IPCC + Assessment Process +Thread-Topic: Freedom of Information request - Participation in the IPCC + Assessment Process +Thread-Index: Acvlavo2wQr6iwN5SQy7Y/pEUUVYaQCQnamw +Message-ID: <C351A7C3885B7141ACFE44C69D66A98B387D486FAB@EXMBX02.ad.oak.ox.ac.uk> +References: <E1Q0Z7o-0007ie-FL@wildfire.ukcod.org.uk> +In-Reply-To: <E1Q0Z7o-0007ie-FL@wildfire.ukcod.org.uk> +Accept-Language: en-US, en-GB +Content-Language: en-US +X-MS-Has-Attach: +X-MS-TNEF-Correlator: +acceptlanguage: en-US, en-GB +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: base64 +MIME-Version: 1.0 + +stuff + diff --git a/spec/lib/tmail_extensions_spec.rb b/spec/lib/tmail_extensions_spec.rb index d1a56f2e2..41e8c8f4e 100644 --- a/spec/lib/tmail_extensions_spec.rb +++ b/spec/lib/tmail_extensions_spec.rb @@ -22,5 +22,11 @@ describe "when using TMail" do incoming_message.get_body_for_html_display() end + it 'should parse multiple to addresses with unqoted display names' do + example_file = File.join(Spec::Runner.configuration.fixture_path, 'multiple-unquoted-display-names.email') + mail = TMail::Mail.parse(File.read(example_file)) + mail.to.should == ["request-66666-caa77777@whatdotheyknow.com", "foi@example.com"] + end + end |