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
|
2021-05-09 20:10:37 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2022-02-24 10:15:02 -05:00
|
|
|
RECEIVED_HEADER_REGEX = /for\s+\<(.+)\>/.freeze
|
|
|
|
|
2015-08-20 14:05:06 -04:00
|
|
|
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?
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
ignore_auto_reply!
|
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
|
2021-05-25 14:10:42 -04:00
|
|
|
Gitlab::Metrics::BackgroundTransaction.current&.add_event(handler.metrics_event, handler.metrics_params)
|
2019-09-13 09:26:31 -04:00
|
|
|
end
|
2021-07-01 17:08:38 -04:00
|
|
|
rescue StandardError => e
|
|
|
|
Gitlab::Metrics::BackgroundTransaction.current&.add_event('email_receiver_error', error: e.class.name)
|
|
|
|
raise e
|
2016-03-23 10:20:22 -04:00
|
|
|
end
|
2015-08-20 14:05:06 -04:00
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def mail_metadata
|
|
|
|
{
|
|
|
|
mail_uid: mail.message_id,
|
|
|
|
from_address: mail.from,
|
|
|
|
to_address: mail.to,
|
|
|
|
mail_key: mail_key,
|
|
|
|
references: Array(mail.references),
|
|
|
|
delivered_to: delivered_to.map(&:value),
|
|
|
|
envelope_to: envelope_to.map(&:value),
|
2021-07-01 17:08:38 -04:00
|
|
|
x_envelope_to: x_envelope_to.map(&:value),
|
2022-02-24 10:15:02 -05:00
|
|
|
# reduced down to what looks like an email in the received headers
|
|
|
|
received_recipients: recipients_from_received_headers,
|
2021-07-01 17:08:38 -04:00
|
|
|
meta: {
|
|
|
|
client_id: "email/#{mail.from.first}",
|
|
|
|
project: handler&.project&.full_path
|
|
|
|
}
|
2021-05-09 20:10:37 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-10-29 08:14:45 -04:00
|
|
|
def mail
|
|
|
|
strong_memoize(:mail) { build_mail }
|
|
|
|
end
|
|
|
|
|
2017-01-17 14:50:49 -05:00
|
|
|
private
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def handler
|
|
|
|
strong_memoize(:handler) { find_handler }
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_handler
|
2020-02-27 13:09:21 -05:00
|
|
|
Handler.for(mail, mail_key)
|
|
|
|
end
|
|
|
|
|
2016-05-20 20:03:39 -04:00
|
|
|
def build_mail
|
2022-06-02 05:09:17 -04:00
|
|
|
# See https://github.com/mikel/mail/blob/641060598f8f4be14d79bad8d703e9f2967e1cdb/spec/mail/message_spec.rb#L569
|
|
|
|
# for mail structure
|
2016-05-20 20:03:39 -04:00
|
|
|
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
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def mail_key
|
|
|
|
strong_memoize(:mail_key) do
|
|
|
|
key_from_to_header || key_from_additional_headers
|
|
|
|
end
|
2016-03-01 00:29:20 -05:00
|
|
|
end
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def key_from_to_header
|
2016-05-18 18:19:25 -04:00
|
|
|
mail.to.find do |address|
|
2021-12-08 04:13:01 -05:00
|
|
|
key = email_class.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
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def key_from_additional_headers
|
|
|
|
find_key_from_references ||
|
|
|
|
find_key_from_delivered_to_header ||
|
|
|
|
find_key_from_envelope_to_header ||
|
2022-02-24 10:15:02 -05:00
|
|
|
find_key_from_x_envelope_to_header ||
|
|
|
|
find_first_key_from_received_headers
|
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
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def find_key_from_references
|
2017-05-03 14:58:56 -04:00
|
|
|
ensure_references_array(mail.references).find do |mail_id|
|
2021-12-08 04:13:01 -05:00
|
|
|
key = email_class.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
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def delivered_to
|
|
|
|
Array(mail[:delivered_to])
|
|
|
|
end
|
|
|
|
|
|
|
|
def envelope_to
|
|
|
|
Array(mail[:envelope_to])
|
|
|
|
end
|
|
|
|
|
|
|
|
def x_envelope_to
|
|
|
|
Array(mail[:x_envelope_to])
|
|
|
|
end
|
|
|
|
|
2022-02-24 10:15:02 -05:00
|
|
|
def received
|
|
|
|
Array(mail[:received])
|
|
|
|
end
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def find_key_from_delivered_to_header
|
|
|
|
delivered_to.find do |header|
|
2021-12-08 04:13:01 -05:00
|
|
|
key = email_class.key_from_address(header.value)
|
2017-05-03 14:58:56 -04:00
|
|
|
break key if key
|
|
|
|
end
|
|
|
|
end
|
2017-08-03 07:29:18 -04:00
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def find_key_from_envelope_to_header
|
|
|
|
envelope_to.find do |header|
|
2021-12-08 04:13:01 -05:00
|
|
|
key = email_class.key_from_address(header.value)
|
2020-01-13 10:07:53 -05:00
|
|
|
break key if key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def find_key_from_x_envelope_to_header
|
|
|
|
x_envelope_to.find do |header|
|
2021-12-08 04:13:01 -05:00
|
|
|
key = email_class.key_from_address(header.value)
|
2020-08-21 11:10:03 -04:00
|
|
|
break key if key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-24 10:15:02 -05:00
|
|
|
def find_first_key_from_received_headers
|
2022-05-06 11:09:03 -04:00
|
|
|
return unless ::Feature.enabled?(:use_received_header_for_incoming_emails)
|
2022-02-24 10:15:02 -05:00
|
|
|
|
|
|
|
recipients_from_received_headers.find do |email|
|
|
|
|
key = email_class.key_from_address(email)
|
|
|
|
break key if key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def recipients_from_received_headers
|
|
|
|
strong_memoize :emails_from_received_headers do
|
|
|
|
received.map { |header| header.value[RECEIVED_HEADER_REGEX, 1] }.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def ignore_auto_reply!
|
|
|
|
if auto_submitted? || auto_replied?
|
2019-10-07 08:06:18 -04:00
|
|
|
raise AutoGeneratedEmailError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def auto_submitted?
|
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
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
def auto_replied?
|
2019-10-07 08:06:18 -04:00
|
|
|
autoreply = mail.header['X-Autoreply']&.value
|
|
|
|
|
|
|
|
autoreply && autoreply == 'yes'
|
2017-08-03 07:29:18 -04:00
|
|
|
end
|
2021-12-08 04:13:01 -05:00
|
|
|
|
|
|
|
def email_class
|
|
|
|
Gitlab::IncomingEmail
|
|
|
|
end
|
2015-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|