2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-09 02:48:23 -04:00
|
|
|
require_dependency 'gitlab/email/handler'
|
2016-05-18 18:19:25 -04:00
|
|
|
|
2015-08-20 14:05:06 -04:00
|
|
|
# Inspired in great part by Discourse's Email::Receiver
|
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
class Receiver
|
|
|
|
def initialize(raw)
|
2016-05-20 20:03:39 -04:00
|
|
|
@raw = raw
|
2015-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-05-20 20:03:39 -04:00
|
|
|
raise EmptyEmailError if @raw.blank?
|
|
|
|
|
|
|
|
mail = build_mail
|
2017-08-02 11:13:27 -04:00
|
|
|
|
2019-10-07 08:06:18 -04:00
|
|
|
ignore_auto_reply!(mail)
|
2017-08-02 11:13:27 -04:00
|
|
|
|
2020-02-27 13:09:21 -05:00
|
|
|
handler = find_handler(mail)
|
2015-08-20 14:05:06 -04:00
|
|
|
|
2016-06-15 04:23:41 -04:00
|
|
|
raise UnknownIncomingEmail unless handler
|
|
|
|
|
2019-09-13 09:26:31 -04:00
|
|
|
handler.execute.tap do
|
|
|
|
Gitlab::Metrics.add_event(handler.metrics_event, handler.metrics_params)
|
|
|
|
end
|
2016-03-23 10:20:22 -04:00
|
|
|
end
|
2015-08-20 14:05:06 -04:00
|
|
|
|
2017-01-17 14:50:49 -05:00
|
|
|
private
|
|
|
|
|
2020-02-27 13:09:21 -05:00
|
|
|
def find_handler(mail)
|
|
|
|
mail_key = extract_mail_key(mail)
|
|
|
|
Handler.for(mail, mail_key)
|
|
|
|
end
|
|
|
|
|
2016-05-20 20:03:39 -04:00
|
|
|
def build_mail
|
|
|
|
Mail::Message.new(@raw)
|
2016-05-18 18:19:25 -04:00
|
|
|
rescue Encoding::UndefinedConversionError,
|
|
|
|
Encoding::InvalidByteSequenceError => e
|
2015-08-20 15:17:59 -04:00
|
|
|
raise EmailUnparsableError, e
|
|
|
|
end
|
|
|
|
|
2016-05-20 20:03:39 -04:00
|
|
|
def extract_mail_key(mail)
|
|
|
|
key_from_to_header(mail) || key_from_additional_headers(mail)
|
2016-03-01 00:29:20 -05:00
|
|
|
end
|
|
|
|
|
2016-05-20 20:03:39 -04:00
|
|
|
def key_from_to_header(mail)
|
2016-05-18 18:19:25 -04:00
|
|
|
mail.to.find do |address|
|
2016-03-23 08:05:31 -04:00
|
|
|
key = Gitlab::IncomingEmail.key_from_address(address)
|
2016-05-18 18:19:25 -04:00
|
|
|
break key if key
|
2015-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-20 20:03:39 -04:00
|
|
|
def key_from_additional_headers(mail)
|
2017-05-03 14:58:56 -04:00
|
|
|
find_key_from_references(mail) ||
|
2020-01-13 10:07:53 -05:00
|
|
|
find_key_from_delivered_to_header(mail) ||
|
|
|
|
find_key_from_envelope_to_header(mail)
|
2017-01-17 14:50:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_references_array(references)
|
|
|
|
case references
|
|
|
|
when Array
|
|
|
|
references
|
2017-01-20 07:20:40 -05:00
|
|
|
when String
|
|
|
|
# Handle emails from clients which append with commas,
|
|
|
|
# example clients are Microsoft exchange and iOS app
|
2017-01-17 14:50:49 -05:00
|
|
|
Gitlab::IncomingEmail.scan_fallback_references(references)
|
2017-05-01 09:25:04 -04:00
|
|
|
when nil
|
|
|
|
[]
|
2017-01-17 14:50:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-03 14:58:56 -04:00
|
|
|
def find_key_from_references(mail)
|
|
|
|
ensure_references_array(mail.references).find do |mail_id|
|
2016-05-20 19:21:58 -04:00
|
|
|
key = Gitlab::IncomingEmail.key_from_fallback_message_id(mail_id)
|
2016-05-18 18:19:25 -04:00
|
|
|
break key if key
|
2016-03-01 00:29:20 -05:00
|
|
|
end
|
|
|
|
end
|
2017-05-03 14:58:56 -04:00
|
|
|
|
|
|
|
def find_key_from_delivered_to_header(mail)
|
|
|
|
Array(mail[:delivered_to]).find do |header|
|
|
|
|
key = Gitlab::IncomingEmail.key_from_address(header.value)
|
|
|
|
break key if key
|
|
|
|
end
|
|
|
|
end
|
2017-08-03 07:29:18 -04:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
def find_key_from_envelope_to_header(mail)
|
|
|
|
Array(mail[:envelope_to]).find do |header|
|
|
|
|
key = Gitlab::IncomingEmail.key_from_address(header.value)
|
|
|
|
break key if key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-07 08:06:18 -04:00
|
|
|
def ignore_auto_reply!(mail)
|
|
|
|
if auto_submitted?(mail) || auto_replied?(mail)
|
|
|
|
raise AutoGeneratedEmailError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_submitted?(mail)
|
2017-08-03 07:29:18 -04:00
|
|
|
# Mail::Header#[] is case-insensitive
|
|
|
|
auto_submitted = mail.header['Auto-Submitted']&.value
|
|
|
|
|
|
|
|
# Mail::Field#value would strip leading and trailing whitespace
|
2019-10-07 08:06:18 -04:00
|
|
|
# See also https://tools.ietf.org/html/rfc3834
|
|
|
|
auto_submitted && auto_submitted != 'no'
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_replied?(mail)
|
|
|
|
autoreply = mail.header['X-Autoreply']&.value
|
|
|
|
|
|
|
|
autoreply && autoreply == 'yes'
|
2017-08-03 07:29:18 -04:00
|
|
|
end
|
2015-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|