debian-mirror-gitlab/lib/gitlab/email/receiver.rb

168 lines
4.2 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
require_dependency 'gitlab/email/handler'
2015-09-25 12:07:36 +05:30
# Inspired in great part by Discourse's Email::Receiver
module Gitlab
module Email
2016-09-13 17:45:13 +05:30
class Receiver
2021-06-08 01:23:25 +05:30
include Gitlab::Utils::StrongMemoize
2015-09-25 12:07:36 +05:30
def initialize(raw)
@raw = raw
end
def execute
raise EmptyEmailError if @raw.blank?
2021-06-08 01:23:25 +05:30
ignore_auto_reply!
2015-09-25 12:07:36 +05:30
2016-09-13 17:45:13 +05:30
raise UnknownIncomingEmail unless handler
2015-09-25 12:07:36 +05:30
2019-12-04 20:38:33 +05:30
handler.execute.tap do
2021-09-04 01:27:46 +05:30
Gitlab::Metrics::BackgroundTransaction.current&.add_event(handler.metrics_event, handler.metrics_params)
2019-12-04 20:38:33 +05:30
end
2021-09-30 23:02:18 +05:30
rescue StandardError => e
Gitlab::Metrics::BackgroundTransaction.current&.add_event('email_receiver_error', error: e.class.name)
raise e
2015-09-25 12:07:36 +05:30
end
2021-06-08 01:23:25 +05:30
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-09-30 23:02:18 +05:30
x_envelope_to: x_envelope_to.map(&:value),
meta: {
client_id: "email/#{mail.from.first}",
project: handler&.project&.full_path
}
2021-06-08 01:23:25 +05:30
}
end
2021-12-11 22:18:48 +05:30
def mail
strong_memoize(:mail) { build_mail }
end
2017-08-17 22:00:37 +05:30
private
2021-06-08 01:23:25 +05:30
def handler
strong_memoize(:handler) { find_handler }
end
def find_handler
2020-04-08 14:13:33 +05:30
Handler.for(mail, mail_key)
end
2016-09-13 17:45:13 +05:30
def build_mail
Mail::Message.new(@raw)
rescue Encoding::UndefinedConversionError,
Encoding::InvalidByteSequenceError => e
2015-09-25 12:07:36 +05:30
raise EmailUnparsableError, e
end
2021-06-08 01:23:25 +05:30
def mail_key
strong_memoize(:mail_key) do
key_from_to_header || key_from_additional_headers
end
2016-06-02 11:05:42 +05:30
end
2021-06-08 01:23:25 +05:30
def key_from_to_header
2016-09-13 17:45:13 +05:30
mail.to.find do |address|
2022-01-26 12:08:38 +05:30
key = email_class.key_from_address(address)
2016-09-13 17:45:13 +05:30
break key if key
2016-06-02 11:05:42 +05:30
end
end
2021-06-08 01:23:25 +05:30
def key_from_additional_headers
find_key_from_references ||
find_key_from_delivered_to_header ||
find_key_from_envelope_to_header ||
find_key_from_x_envelope_to_header
2017-08-17 22:00:37 +05:30
end
def ensure_references_array(references)
case references
when Array
references
when String
# Handle emails from clients which append with commas,
# example clients are Microsoft exchange and iOS app
Gitlab::IncomingEmail.scan_fallback_references(references)
when nil
[]
end
end
2021-06-08 01:23:25 +05:30
def find_key_from_references
2017-08-17 22:00:37 +05:30
ensure_references_array(mail.references).find do |mail_id|
2022-01-26 12:08:38 +05:30
key = email_class.key_from_fallback_message_id(mail_id)
2016-09-13 17:45:13 +05:30
break key if key
2015-09-25 12:07:36 +05:30
end
end
2017-08-17 22:00:37 +05:30
2021-06-08 01:23:25 +05:30
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
def find_key_from_delivered_to_header
delivered_to.find do |header|
2022-01-26 12:08:38 +05:30
key = email_class.key_from_address(header.value)
2017-08-17 22:00:37 +05:30
break key if key
end
end
2017-09-10 17:25:29 +05:30
2021-06-08 01:23:25 +05:30
def find_key_from_envelope_to_header
envelope_to.find do |header|
2022-01-26 12:08:38 +05:30
key = email_class.key_from_address(header.value)
2020-03-13 15:44:24 +05:30
break key if key
end
end
2021-06-08 01:23:25 +05:30
def find_key_from_x_envelope_to_header
x_envelope_to.find do |header|
2022-01-26 12:08:38 +05:30
key = email_class.key_from_address(header.value)
2020-11-24 15:15:51 +05:30
break key if key
end
end
2021-06-08 01:23:25 +05:30
def ignore_auto_reply!
if auto_submitted? || auto_replied?
2019-12-21 20:55:43 +05:30
raise AutoGeneratedEmailError
end
end
2021-06-08 01:23:25 +05:30
def auto_submitted?
2017-09-10 17:25:29 +05:30
# Mail::Header#[] is case-insensitive
auto_submitted = mail.header['Auto-Submitted']&.value
# Mail::Field#value would strip leading and trailing whitespace
2019-12-21 20:55:43 +05:30
# See also https://tools.ietf.org/html/rfc3834
auto_submitted && auto_submitted != 'no'
end
2021-06-08 01:23:25 +05:30
def auto_replied?
2019-12-21 20:55:43 +05:30
autoreply = mail.header['X-Autoreply']&.value
autoreply && autoreply == 'yes'
2017-09-10 17:25:29 +05:30
end
2022-01-26 12:08:38 +05:30
def email_class
Gitlab::IncomingEmail
end
2015-09-25 12:07:36 +05:30
end
end
end