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