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-02-13 22:33:31 +05:30
|
|
|
UNSUBSCRIBE_SUFFIX = '-unsubscribe'.freeze
|
|
|
|
UNSUBSCRIBE_SUFFIX_LEGACY = '+unsubscribe'.freeze
|
|
|
|
WILDCARD_PLACEHOLDER = '%{key}'.freeze
|
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?
|
|
|
|
config.enabled && config.address
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def supports_wildcard?
|
|
|
|
config.address && config.address.include?(WILDCARD_PLACEHOLDER)
|
|
|
|
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-13 22:33:31 +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
|
|
|
|
|
|
|
|
def key_from_address(address)
|
|
|
|
regex = address_regex
|
|
|
|
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
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def address_regex
|
|
|
|
wildcard_address = config.address
|
|
|
|
return nil unless wildcard_address
|
|
|
|
|
|
|
|
regex = Regexp.escape(wildcard_address)
|
2017-08-17 22:00:37 +05:30
|
|
|
regex = regex.sub(Regexp.escape(WILDCARD_PLACEHOLDER), '(.+)')
|
2018-11-08 19:23:39 +05:30
|
|
|
Regexp.new(/\A#{regex}\z/).freeze
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|