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
|
2017-08-17 22:00:37 +05:30
|
|
|
ProcessingError = Class.new(StandardError)
|
|
|
|
EmailUnparsableError = Class.new(ProcessingError)
|
|
|
|
SentNotificationNotFoundError = Class.new(ProcessingError)
|
|
|
|
ProjectNotFound = Class.new(ProcessingError)
|
|
|
|
EmptyEmailError = Class.new(ProcessingError)
|
|
|
|
AutoGeneratedEmailError = Class.new(ProcessingError)
|
|
|
|
UserNotFoundError = Class.new(ProcessingError)
|
|
|
|
UserBlockedError = Class.new(ProcessingError)
|
|
|
|
UserNotAuthorizedError = Class.new(ProcessingError)
|
|
|
|
NoteableNotFoundError = Class.new(ProcessingError)
|
2018-03-17 18:26:18 +05:30
|
|
|
InvalidRecordError = Class.new(ProcessingError)
|
|
|
|
InvalidNoteError = Class.new(InvalidRecordError)
|
|
|
|
InvalidIssueError = Class.new(InvalidRecordError)
|
|
|
|
InvalidMergeRequestError = Class.new(InvalidRecordError)
|
2017-08-17 22:00:37 +05:30
|
|
|
UnknownIncomingEmail = Class.new(ProcessingError)
|
2018-12-13 13:39:08 +05:30
|
|
|
InvalidAttachment = Class.new(ProcessingError)
|
2015-09-25 12:07:36 +05:30
|
|
|
|
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
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
mail_key = extract_mail_key(mail)
|
|
|
|
handler = Handler.for(mail, mail_key)
|
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
|
|
|
|
|
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) ||
|
|
|
|
find_key_from_delivered_to_header(mail)
|
|
|
|
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
|
|
|
|
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
|