Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sebbacon/alaveteli
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbacon committed Apr 1, 2011
2 parents 746e78c + 4907a21 commit 17d9a75
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 11 deletions.
13 changes: 7 additions & 6 deletions app/models/incoming_message.rb
Expand Up @@ -395,11 +395,12 @@ def body_as_html(dir)
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:
Expand All @@ -408,7 +409,7 @@ def body_as_html(dir)
# 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
Expand Down
2 changes: 1 addition & 1 deletion commonlib
Submodule commonlib updated from b43b59 to 38e0a6
58 changes: 54 additions & 4 deletions lib/tmail_extensions.rb
Expand Up @@ -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!
Expand All @@ -32,7 +34,7 @@ def from_name_if_present
else
return nil
end
end
end

# Monkeypatch! Generalisation of To:, Cc:
def envelope_to(default = nil)
Expand Down Expand Up @@ -72,7 +74,7 @@ class Address
# 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)
Expand All @@ -84,7 +86,7 @@ def Address.address_from_name_and_email(name, email)
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
Expand All @@ -94,4 +96,52 @@ def quote_phrase( str )
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


27 changes: 27 additions & 0 deletions 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

6 changes: 6 additions & 0 deletions spec/lib/tmail_extensions_spec.rb
Expand Up @@ -22,5 +22,11 @@
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

0 comments on commit 17d9a75

Please sign in to comment.