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)
|
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
|
|
|
|
|
|
|
ignore_auto_submitted!(mail)
|
|
|
|
|
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
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
Gitlab::Metrics.add_event(:receive_email, handler.metrics_params)
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
handler.execute
|
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
|
|
|
|
|
|
|
def ignore_auto_submitted!(mail)
|
|
|
|
# Mail::Header#[] is case-insensitive
|
|
|
|
auto_submitted = mail.header['Auto-Submitted']&.value
|
|
|
|
|
|
|
|
# Mail::Field#value would strip leading and trailing whitespace
|
|
|
|
raise AutoGeneratedEmailError if
|
|
|
|
# See also https://tools.ietf.org/html/rfc3834
|
|
|
|
auto_submitted && auto_submitted != 'no'
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|