aboutsummaryrefslogtreecommitdiffstats
path: root/lib/tnef.rb
blob: 1c941f8b087a50cdd34bfb21ab2e1036dac77071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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