2015-08-18 20:02:26 -04:00
|
|
|
module Gitlab
|
2015-09-21 03:46:47 -04:00
|
|
|
module IncomingEmail
|
2016-10-12 13:07:36 -04:00
|
|
|
UNSUBSCRIBE_SUFFIX = '+unsubscribe'.freeze
|
2016-10-18 14:03:31 -04:00
|
|
|
WILDCARD_PLACEHOLDER = '%{key}'.freeze
|
|
|
|
|
2015-08-18 20:02:26 -04:00
|
|
|
class << self
|
2016-03-17 15:03:51 -04:00
|
|
|
def enabled?
|
|
|
|
config.enabled && config.address
|
2015-08-18 20:02:26 -04:00
|
|
|
end
|
|
|
|
|
2016-10-18 14:03:31 -04:00
|
|
|
def supports_wildcard?
|
|
|
|
config.address && config.address.include?(WILDCARD_PLACEHOLDER)
|
|
|
|
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
|
|
|
|
|
|
|
|
def unsubscribe_address(key)
|
|
|
|
config.address.sub(WILDCARD_PLACEHOLDER, "#{key}#{UNSUBSCRIBE_SUFFIX}")
|
2015-08-18 20:02:26 -04:00
|
|
|
end
|
|
|
|
|
2015-09-21 03:46:47 -04:00
|
|
|
def key_from_address(address)
|
2015-08-20 19:44:44 -04:00
|
|
|
regex = address_regex
|
|
|
|
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
|
|
|
|
|
2015-08-18 20:02:26 -04:00
|
|
|
def address_regex
|
2015-08-20 19:21:31 -04:00
|
|
|
wildcard_address = config.address
|
|
|
|
return nil unless wildcard_address
|
|
|
|
|
|
|
|
regex = Regexp.escape(wildcard_address)
|
2016-10-12 13:07:36 -04:00
|
|
|
regex = regex.sub(Regexp.escape(WILDCARD_PLACEHOLDER), '(.+)')
|
2015-08-20 19:21:31 -04:00
|
|
|
Regexp.new(regex).freeze
|
2015-08-18 20:02:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|