aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2012-11-15 16:12:23 +0000
committerLouise Crow <louise.crow@gmail.com>2012-11-15 16:17:45 +0000
commit388c75bfbd18fcaf273d95c21dc132ad19f0cefe (patch)
tree74da159b16879ed9ee52b7d1abddc9005f606877 /lib
parentc22653c85c8029bf2ee193eb892bad1f3d0e93fe (diff)
Move handling of TNEF mail attachments to mail handler
Diffstat (limited to 'lib')
-rw-r--r--lib/mail_handler/backends/mail_backend.rb9
-rw-r--r--lib/mail_handler/backends/tmail_backend.rb13
-rw-r--r--lib/mail_handler/mail_handler.rb34
-rw-r--r--lib/tnef.rb40
4 files changed, 56 insertions, 40 deletions
diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb
index 6a5fff13f..a6f2a6a44 100644
--- a/lib/mail_handler/backends/mail_backend.rb
+++ b/lib/mail_handler/backends/mail_backend.rb
@@ -13,6 +13,15 @@ module MailHandler
Mail.new(data)
end
+ # Extracts all attachments from the given TNEF file as a Mail object
+ def mail_from_tnef(content)
+ main = Mail.new
+ tnef_attachments(content).each do |attachment|
+ main.add_file(attachment)
+ end
+ main.ready_to_send!
+ main
+ end
end
end
end \ No newline at end of file
diff --git a/lib/mail_handler/backends/tmail_backend.rb b/lib/mail_handler/backends/tmail_backend.rb
index fc675c1ed..4daa5469f 100644
--- a/lib/mail_handler/backends/tmail_backend.rb
+++ b/lib/mail_handler/backends/tmail_backend.rb
@@ -18,6 +18,19 @@ module MailHandler
mail
end
+ # Extracts all attachments from the given TNEF file as a TMail::Mail object
+ def mail_from_tnef(content)
+ main = TMail::Mail.new
+ main.set_content_type 'multipart', 'mixed', { 'boundary' => TMail.new_boundary }
+ tnef_attachments(content).each do |attachment|
+ tmail_attachment = TMail::Mail.new
+ tmail_attachment['content-location'] = attachment[:filename]
+ tmail_attachment.body = attachment[:content]
+ main.parts << tmail_attachment
+ end
+ main
+ end
+
end
end
end \ No newline at end of file
diff --git a/lib/mail_handler/mail_handler.rb b/lib/mail_handler/mail_handler.rb
index f0c75670a..0bd9a82f0 100644
--- a/lib/mail_handler/mail_handler.rb
+++ b/lib/mail_handler/mail_handler.rb
@@ -1,4 +1,6 @@
# Handles the parsing of email
+require 'tmpdir'
+
module MailHandler
if RUBY_VERSION.to_f >= 1.9
@@ -10,6 +12,38 @@ module MailHandler
include Backends::TmailBackend
end
+ # Returns a set of attachments from the given TNEF contents
+ # The TNEF contents also contains the message body, but in general this is the
+ # same as the message body in the message proper.
+ def tnef_attachments(content)
+ attachments = []
+ Dir.mktmpdir do |dir|
+ IO.popen("#{`which tnef`.chomp} -K -C #{dir}", "w") do |f|
+ f.write(content)
+ f.close
+ if $?.signaled?
+ raise IOError, "tnef exited with signal #{$?.termsig}"
+ end
+ if $?.exited? && $?.exitstatus != 0
+ raise IOError, "tnef exited with status #{$?.exitstatus}"
+ end
+ end
+ found = 0
+ Dir.new(dir).sort.each do |file| # sort for deterministic behaviour
+ if file != "." && file != ".."
+ file_content = File.open("#{dir}/#{file}", "r").read
+ attachments << { :content => file_content,
+ :filename => file }
+ found += 1
+ end
+ end
+ if found == 0
+ raise IOError, "tnef produced no attachments"
+ end
+ end
+ attachments
+ end
+
# Turn instance methods into class methods
extend self
diff --git a/lib/tnef.rb b/lib/tnef.rb
deleted file mode 100644
index 1c941f8b0..000000000
--- a/lib/tnef.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-require 'tmpdir'
-
-class TNEF
-
- # Extracts all attachments from the given TNEF file as a TMail::Mail object
- # The TNEF file also contains the message body, but in general this is the
- # same as the message body in the message proper.
- def self.as_tmail(content)
- main = TMail::Mail.new
- main.set_content_type 'multipart', 'mixed', { 'boundary' => TMail.new_boundary }
- Dir.mktmpdir do |dir|
- IO.popen("#{`which tnef`.chomp} -K -C #{dir}", "w") do |f|
- f.write(content)
- f.close
- if $?.signaled?
- raise IOError, "tnef exited with signal #{$?.termsig}"
- end
- if $?.exited? && $?.exitstatus != 0
- raise IOError, "tnef exited with status #{$?.exitstatus}"
- end
- end
- found = 0
- Dir.new(dir).sort.each do |file| # sort for deterministic behaviour
- if file != "." && file != ".."
- file_content = File.open("#{dir}/#{file}", "r").read
- attachment = TMail::Mail.new
- attachment['content-location'] = file
- attachment.body = file_content
- main.parts << attachment
- found += 1
- end
- end
- if found == 0
- raise IOError, "tnef produced no attachments"
- end
- end
- main
- end
-
-end