Class: Mail::Gpg::InlineDecryptedMessage

Inherits:
Message
  • Object
show all
Defined in:
lib/mail/gpg/inline_decrypted_message.rb

Instance Method Summary collapse

Constructor Details

#initialize(cipher_mail, options = {}) ⇒ InlineDecryptedMessage

options are:

:verify: decrypt and verify



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
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mail/gpg/inline_decrypted_message.rb', line 13

def initialize(cipher_mail, options = {})
  if cipher_mail.multipart?
    super() do
      cipher_mail.header.fields.each do |field|
        header[field.name] = field.value
      end
      cipher_mail.parts.each do |part|
        part Mail::Part.new do |p|
          if part.has_content_type? && /application\/(?:octet-stream|pgp-encrypted)/ =~ part.mime_type
            # encrypted attachment, we set the content_type to the generic 'application/octet-stream'
            # and remove the .pgp/gpg/asc from name/filename in header fields
            decrypted = GpgmeHelper.decrypt(part.decoded, options)
            p.content_type part.content_type.sub(/application\/(?:octet-stream|pgp-encrypted)/, 'application/octet-stream')
              .sub(/name=(?:"')?(.*)\.(?:pgp|gpg|asc)(?:"')?/, 'name="\1"')
            p.content_disposition part.content_disposition.sub(/filename=(?:"')?(.*)\.(?:pgp|gpg|asc)(?:"')?/, 'filename="\1"')
            p.content_transfer_encoding Mail::Encodings::Base64
            p.body Mail::Encodings::Base64::encode(decrypted.to_s)
          else
            if part.body.include?('-----BEGIN PGP MESSAGE-----')
              p.body GpgmeHelper.decrypt(part.decoded, options).to_s
            else
              p.content_type part.content_type
              p.content_transfer_encoding part.content_transfer_encoding
              p.body part.body.to_s
            end
          end
        end
      end
    end # of multipart
  else
    decrypted = cipher_mail.body.empty? ? '' : GpgmeHelper.decrypt(cipher_mail.body.decoded, options)
    super() do
      cipher_mail.header.fields.each do |field|
        header[field.name] = field.value
      end
      body decrypted.to_s
    end
  end
end