File: extract-attachements.rb

package info (click to toggle)
libtmail-ruby 0.10.8-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 856 kB
  • ctags: 1,461
  • sloc: ruby: 8,406; ansic: 678; objc: 584; yacc: 305; makefile: 149
file content (33 lines) | stat: -rw-r--r-- 555 bytes parent folder | download | duplicates (3)
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
#
# extract-attachments.rb  -- Extracts attachment(s) from the message.
#
# Usage: ruby extract-attachments.rb mail mail...
#

require 'tmail'

def main
  idx = 1
  ARGV.each do |fname|
    TMail::Mail.load(fname).parts.each do |m|
      m.base64_decode
      File.open("#{idx}.#{ext(m)}", 'w') {|f|
        f.write m.body
      }
      idx += 1
    end
  end
end

CTYPE_TO_EXT = {
  'image/jpeg' => 'jpeg',
  'image/gif'  => 'gif',
  'image/png'  => 'png',
  'image/tiff' => 'tiff'
}

def ext( mail )
  CTYPE_TO_EXT[mail.content_type] || 'txt'
end

main