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

117 lines
3 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
2015-09-25 12:07:36 +05:30
def initialize(raw)
@raw = raw
end
def execute
raise EmptyEmailError if @raw.blank?
2016-09-13 17:45:13 +05:30
mail = build_mail
2017-09-10 17:25:29 +05:30
2019-12-21 20:55:43 +05:30
ignore_auto_reply!(mail)
2017-09-10 17:25:29 +05:30
2020-04-08 14:13:33 +05:30
handler = find_handler(mail)
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
Gitlab::Metrics.add_event(handler.metrics_event, handler.metrics_params)
end
2015-09-25 12:07:36 +05:30
end
2017-08-17 22:00:37 +05:30
private
2020-04-08 14:13:33 +05:30
def find_handler(mail)
mail_key = extract_mail_key(mail)
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
2016-09-13 17:45:13 +05:30
def extract_mail_key(mail)
key_from_to_header(mail) || key_from_additional_headers(mail)
2016-06-02 11:05:42 +05:30
end
2016-09-13 17:45:13 +05:30
def key_from_to_header(mail)
mail.to.find do |address|
2016-06-02 11:05:42 +05:30
key = Gitlab::IncomingEmail.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
2016-09-13 17:45:13 +05:30
def key_from_additional_headers(mail)
2017-08-17 22:00:37 +05:30
find_key_from_references(mail) ||
2020-03-13 15:44:24 +05:30
find_key_from_delivered_to_header(mail) ||
find_key_from_envelope_to_header(mail)
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
def find_key_from_references(mail)
ensure_references_array(mail.references).find do |mail_id|
2016-09-13 17:45:13 +05:30
key = Gitlab::IncomingEmail.key_from_fallback_message_id(mail_id)
break key if key
2015-09-25 12:07:36 +05:30
end
end
2017-08-17 22:00:37 +05:30
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-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
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-12-21 20:55:43 +05:30
def ignore_auto_reply!(mail)
if auto_submitted?(mail) || auto_replied?(mail)
raise AutoGeneratedEmailError
end
end
def auto_submitted?(mail)
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
def auto_replied?(mail)
autoreply = mail.header['X-Autoreply']&.value
autoreply && autoreply == 'yes'
2017-09-10 17:25:29 +05:30
end
2015-09-25 12:07:36 +05:30
end
end
end