Sha256: 997a476f09beb45e3613e8bf2d85c6236058aa4d5894088d46e46829a9ec9e56

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module Mail
  module Jdec
    module MessagePatch
      def decoded
        decoded = super

        if Jdec.enabled? && MessagePatch.autodetect?(self)
          detected = Detector.detect(decoded)
          if detected && detected[:type] == :text
            charset = detected[:encoding].downcase
            decoded = Mail::Encodings.transcode_charset(decoded.dup.force_encoding(charset), charset, 'utf-8')
            header[:content_type] = 'text/plain' unless has_content_type?
            header[:content_type].parameters[:charset] = charset unless has_charset?
          else
            decoded = Mail::Encodings.transcode_charset(decoded, decoded.encoding, 'utf-8')
          end
        end

        decoded
      end

      class << self
        def autodetect?(message)
          !message.has_content_type? ||
            (mime_types_for_autodetect?(message.mime_type) && !message.has_charset? && !message.attachment? && !message.multipart?)
        end

        def mime_types_for_autodetect?(mime_type)
          Jdec.mime_types_for_autodetect.any? do |type|
            if type.is_a?(Regexp)
              type.match?(mime_type)
            else
              type == mime_type
            end
          end
        end
      end
    end
  end
end

unless Mail::Message.included_modules.include?(Mail::Jdec::MessagePatch)
  Mail::Message.prepend Mail::Jdec::MessagePatch
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mail-jdec-1.1.2 lib/mail/jdec/message_patch.rb
mail-jdec-1.1.1 lib/mail/jdec/message_patch.rb