aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorPeter Collingbourne <peter@pcc.me.uk>2010-03-10 01:25:12 +0000
committerPeter Collingbourne <peter@pcc.me.uk>2010-03-15 13:42:37 +0000
commit24f2217493a7f761d25b3a2c401f1eb3d10b6f6a (patch)
treec347eaf3a43e9126d9be70f825922f9fd12a3636 /lib
parenta84d2fa67c106cd537302b13631bc56c785dbb22 (diff)
Support for Outlook TNEF files
Diffstat (limited to 'lib')
-rw-r--r--lib/tnef.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/tnef.rb b/lib/tnef.rb
new file mode 100644
index 000000000..7b7ce965f
--- /dev/null
+++ b/lib/tnef.rb
@@ -0,0 +1,35 @@
+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("tnef -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
+ 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
+ end
+ end
+ end
+ main
+ end
+
+end