aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mail_handler/mail_handler.rb
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2012-11-20 16:14:22 +0000
committerLouise Crow <louise.crow@gmail.com>2012-11-20 16:14:22 +0000
commitfcb6cdd6342d0272a8e7f05d20a4b0d87da70de9 (patch)
tree48ea4a5a796c4e95d26e4d4f36170ac09d368637 /lib/mail_handler/mail_handler.rb
parentb0d9551a83c4a101ac808f1c35f66dd138cbc0f1 (diff)
parent24c3ceb2315734ab6e43ae4f75673e251b98a96e (diff)
Merge remote-tracking branch 'origin/feature/isolate-mail-handling' into develop
Diffstat (limited to 'lib/mail_handler/mail_handler.rb')
-rw-r--r--lib/mail_handler/mail_handler.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/mail_handler/mail_handler.rb b/lib/mail_handler/mail_handler.rb
new file mode 100644
index 000000000..24d14b5c8
--- /dev/null
+++ b/lib/mail_handler/mail_handler.rb
@@ -0,0 +1,52 @@
+# Handles the parsing of email
+require 'tmpdir'
+
+module MailHandler
+
+ if RUBY_VERSION.to_f >= 1.9
+ require 'backends/mail_extensions'
+ require 'backends/mail_backend'
+ include Backends::MailBackend
+ else
+ require 'backends/tmail_extensions'
+ require 'backends/tmail_backend'
+ 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
+
+end
+