BigW Consortium Gitlab

incoming_email.rb 1008 Bytes
Newer Older
Douwe Maan committed
1
module Gitlab
2
  module IncomingEmail
Douwe Maan committed
3
    class << self
4
      FALLBACK_MESSAGE_ID_REGEX = /\Areply\-(.+)@#{Gitlab.config.gitlab.host}\Z/.freeze
5

6 7
      def enabled?
        config.enabled && config.address
Douwe Maan committed
8 9
      end

10 11
      def reply_address(key)
        config.address.gsub('%{key}', key)
Douwe Maan committed
12 13
      end

14
      def key_from_address(address)
15 16
        regex = address_regex
        return unless regex
Douwe Maan committed
17

18
        match = address.match(regex)
Douwe Maan committed
19 20 21 22 23
        return unless match

        match[1]
      end

24 25
      def key_from_fallback_message_id(mail_id)
        match = mail_id.match(FALLBACK_MESSAGE_ID_REGEX)
26 27 28 29 30
        return unless match

        match[1]
      end

Douwe Maan committed
31
      def config
32
        Gitlab.config.incoming_email
Douwe Maan committed
33 34
      end

35 36
      private

Douwe Maan committed
37
      def address_regex
38 39 40 41
        wildcard_address = config.address
        return nil unless wildcard_address

        regex = Regexp.escape(wildcard_address)
42
        regex = regex.gsub(Regexp.escape('%{key}'), "(.+)")
43
        Regexp.new(regex).freeze
Douwe Maan committed
44 45 46 47
      end
    end
  end
end