Class: Attachments::Extract

Inherits:
Object
  • Object
show all
Defined in:
lib/attachments/extract.rb

Overview

Extract attachments of specific types from emails and save them in files

Constant Summary collapse

UNCERTAIN_TYPES =
[ "application/octet-stream" ].to_set

Instance Method Summary collapse

Constructor Details

#initialize(include_types = [ "text/plain" ]) ⇒ Extract

Returns a new instance of Extract.



22
23
24
25
# File 'lib/attachments/extract.rb', line 22

def initialize include_types = [ "text/plain" ]
  @include_types = include_types
  reset
end

Instance Method Details

#closeObject



27
28
29
30
31
32
# File 'lib/attachments/extract.rb', line 27

def close
  files.each do |f|
    FileUtils::rm(f[:tmpfile])
  end
  reset
end

#filesObject



131
132
133
# File 'lib/attachments/extract.rb', line 131

def files
  @files
end

#fromObject



84
85
86
# File 'lib/attachments/extract.rb', line 84

def from
  (@mail && @mail.from) || nil
end

#html_bodyObject



113
114
115
116
117
118
119
120
121
# File 'lib/attachments/extract.rb', line 113

def html_body
  if(@mail && @mail.text_part && @mail.text_part.body)
    m = @mail.text_part.body.decoded
    charset = @mail.text_part.charset
    charset ? Iconv.conv("utf-8", charset, m) : m
  else
    nil
  end
end

#last_parsedObject



135
136
137
# File 'lib/attachments/extract.rb', line 135

def last_parsed
  @last_parsed
end

#mailObject



123
124
125
# File 'lib/attachments/extract.rb', line 123

def mail
  @mail
end

#nameObject



127
128
129
# File 'lib/attachments/extract.rb', line 127

def name
  @name
end

#parse(filename_or_hash) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/attachments/extract.rb', line 34

def parse filename_or_hash
  hash = case
         when filename_or_hash.is_a?(String) then
           { :filename => filename_or_hash }
         when filename_or_hash.is_a?(Hash) then
           filename_or_hash
         else
           {}
         end

  content = case
            when hash[:filename] then
              read_file(hash[:filename])
            when hash[:content] then
              hash[:content]
            when hash[:stream] then
              hash[:stream].read()
            else
              nil
            end

  if content
    parse_data content 
  elsif hash[:mail]
    parse_mail hash[:mail]
  end
end

#parse_data(raw_mail_data) ⇒ Object



68
69
70
71
# File 'lib/attachments/extract.rb', line 68

def parse_data raw_mail_data
  mail = Mail.new(raw_mail_data)
  parse_mail mail
end

#parse_file(filename) ⇒ Object



62
63
64
65
66
# File 'lib/attachments/extract.rb', line 62

def parse_file filename
  @last_parsed = filename
  content = read_file filename
  parse_data content
end

#parse_mail(mail) ⇒ Object



73
74
75
76
77
78
# File 'lib/attachments/extract.rb', line 73

def parse_mail mail
  @mail = mail
  # Parse parts recursively until it is not multipart
  # Ignore types that are not suited for forwarding
  parse_part @mail
end

#subjectObject



88
89
90
91
92
93
94
95
# File 'lib/attachments/extract.rb', line 88

def subject
  s = (@mail && @mail.subject) || nil
  if s && s.respond_to?(:encode)
    s.encode("utf-8")
  else
    s
  end
end

#text_bodyObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/attachments/extract.rb', line 97

def text_body
  if(@mail && @mail.text_part && @mail.text_part.body)
    m = @mail.text_part.body.decoded
    charset = @mail.text_part.charset
    text = charset ? Iconv.conv("utf-8", charset, m) : m
    (text.respond_to? :force_encoding) ? text.force_encoding("utf-8") : text
  elsif(@mail && @mail.body && @mail.content_type.to_s.include?("text/plain"))
    m = @mail.body.decoded
    charset = @mail.charset
    text = charset ? Iconv.conv("utf-8", charset, m) : m
    (text.respond_to? :force_encoding) ? text.force_encoding("utf-8") : text
  else
    nil
  end
end

#toObject



80
81
82
# File 'lib/attachments/extract.rb', line 80

def to 
  (@mail && @mail.to) || nil
end