aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/incoming_message.rb
diff options
context:
space:
mode:
authorGareth Rees <gareth@mysociety.org>2015-06-03 11:26:27 +0100
committerGareth Rees <gareth@mysociety.org>2015-06-03 11:26:27 +0100
commit1e8dc7d5cbb382f6cb94716fd1ff0c990d219792 (patch)
tree3886bb03165172d84a21c85fa7cd6695caaa0001 /app/models/incoming_message.rb
parentcfd737427102f3e8449e6ed46ce768b3c520d387 (diff)
Use Ruby to decode uuencoded attachments
Ruby can natively decode uuencoded text with String#unpack. [1] This avoids the uuencode program dependency and avoids writing tempfiles. The actual implementation is taken from mail [2]. The UnixToUnix module is not available in our bundled version of mail. This commit includes a spec to illustrate the failure of uuencode(1) to decode the particular attachment in incoming-request-bad-uuencoding-2. email. Parsing with uuencode returns the following error: External Command: Error from command "uudecode -o /dev/stdout /tmp/foiuu20150530-14811-u6j936": uudecode: /tmp/foiuu20150530-14811-u6j936: No `end' line The file _is_ created and appears to be OK, but the exit code is 1. This causes AlaveteliExternalCommand to fail and return nil. See #2508 [3] for the bug report. [1] http://ruby-doc.org/core-1.8.7/String.html#method-i-unpack [2] https://github.com/mikel/mail/blob/bc4c9bb9321e9d36a678692f2f562d3146b63f78/lib/mail/encodings/unix_to_unix.rb#L7 [3] https://github.com/mysociety/alaveteli/issues/2508
Diffstat (limited to 'app/models/incoming_message.rb')
-rw-r--r--app/models/incoming_message.rb7
1 files changed, 1 insertions, 6 deletions
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb
index 546964c95..2e697140c 100644
--- a/app/models/incoming_message.rb
+++ b/app/models/incoming_message.rb
@@ -507,12 +507,7 @@ class IncomingMessage < ActiveRecord::Base
attachments = []
uus.each do |uu|
# Decode the string
- content = nil
- tempfile = Tempfile.new('foiuu')
- tempfile.print uu
- tempfile.flush
- content = AlaveteliExternalCommand.run("uudecode", "-o", "/dev/stdout", tempfile.path)
- tempfile.close
+ content = uu.sub(/\Abegin \d+ [^\n]*\n/, '').unpack('u').first
# Make attachment type from it, working out filename and mime type
filename = uu.match(/^begin\s+[0-9]+\s+(.*)$/)[1]
calc_mime = AlaveteliFileTypes.filename_and_content_to_mimetype(filename, content)