2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
module Gitlab
|
|
|
|
module IncomingEmail
|
2019-12-04 20:38:33 +05:30
|
|
|
UNSUBSCRIBE_SUFFIX = '-unsubscribe'
|
|
|
|
UNSUBSCRIBE_SUFFIX_LEGACY = '+unsubscribe'
|
|
|
|
WILDCARD_PLACEHOLDER = '%{key}'
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
class << self
|
2016-06-02 11:05:42 +05:30
|
|
|
def enabled?
|
2020-10-24 23:57:45 +05:30
|
|
|
config.enabled && config.address.present?
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def supports_wildcard?
|
2020-10-24 23:57:45 +05:30
|
|
|
config.address.present? && config.address.include?(WILDCARD_PLACEHOLDER)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def supports_issue_creation?
|
|
|
|
enabled? && supports_wildcard?
|
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def reply_address(key)
|
2017-08-17 22:00:37 +05:30
|
|
|
config.address.sub(WILDCARD_PLACEHOLDER, key)
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
# example: incoming+1234567890abcdef1234567890abcdef-unsubscribe@incoming.gitlab.com
|
2017-08-17 22:00:37 +05:30
|
|
|
def unsubscribe_address(key)
|
|
|
|
config.address.sub(WILDCARD_PLACEHOLDER, "#{key}#{UNSUBSCRIBE_SUFFIX}")
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def key_from_address(address, wildcard_address: nil)
|
|
|
|
wildcard_address ||= config.address
|
|
|
|
regex = address_regex(wildcard_address)
|
2015-09-25 12:07:36 +05:30
|
|
|
return unless regex
|
|
|
|
|
|
|
|
match = address.match(regex)
|
|
|
|
return unless match
|
|
|
|
|
|
|
|
match[1]
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def key_from_fallback_message_id(mail_id)
|
2017-08-17 22:00:37 +05:30
|
|
|
message_id_regexp = /\Areply\-(.+)@#{Gitlab.config.gitlab.host}\z/
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
mail_id[message_id_regexp, 1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def scan_fallback_references(references)
|
|
|
|
# It's looking for each <...>
|
|
|
|
references.scan(/(?!<)[^<>]+(?=>)/)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def config
|
|
|
|
Gitlab.config.incoming_email
|
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
private
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def address_regex(wildcard_address)
|
2019-07-07 11:18:12 +05:30
|
|
|
return unless wildcard_address
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
regex = Regexp.escape(wildcard_address)
|
2017-08-17 22:00:37 +05:30
|
|
|
regex = regex.sub(Regexp.escape(WILDCARD_PLACEHOLDER), '(.+)')
|
2020-03-13 15:44:24 +05:30
|
|
|
Regexp.new(/\A<?#{regex}>?\z/).freeze
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|