BigW Consortium Gitlab

reply_parser.rb 1.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Inspired in great part by Discourse's Email::Receiver
module Gitlab
  module Email
    class ReplyParser
      attr_accessor :message

      def initialize(message)
        @message = message
      end

      def execute
        body = select_body(message)

        encoding = body.encoding

http://jneen.net/ committed
16 17 18
        body = EmailReplyTrimmer.trim(body)

        return '' unless body
19

20
        # not using /\s+$/ here because that deletes empty lines
21 22
        body = body.gsub(/[ \t]$/, '')

23 24 25
        # NOTE: We currently don't support empty quotes.
        # EmailReplyTrimmer allows this as a special case,
        # so we detect it manually here.
26 27
        return "" if body.lines.all? { |l| l.strip.empty? || l.start_with?('>') }

28 29 30 31 32 33
        body.force_encoding(encoding).encode("UTF-8")
      end

      private

      def select_body(message)
Douwe Maan committed
34 35 36 37 38 39
        part =
          if message.multipart?
            message.text_part || message.html_part || message
          else
            message
          end
40

41
        decoded = fix_charset(part)
42 43

        return "" unless decoded
44 45

        # Certain trigger phrases that means we didn't parse correctly
46
        if decoded =~ /(Content\-Type\:|multipart\/alternative|text\/plain)/
47 48 49
          return ""
        end

50
        if (part.content_type || '').include? 'text/html'
51 52 53 54
          HTMLParser.parse_reply(decoded)
        else
          decoded
        end
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
      end

      # Force encoding to UTF-8 on a Mail::Message or Mail::Part
      def fix_charset(object)
        return nil if object.nil?

        if object.charset
          object.body.decoded.force_encoding(object.charset.gsub(/utf8/i, "UTF-8")).encode("UTF-8").to_s
        else
          object.body.to_s
        end
      rescue
        nil
      end
    end
  end
end