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
|
2017-03-01 06:00:37 -05:00
|
|
|
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)
|
2017-08-24 02:20:36 -04:00
|
|
|
InvalidRecordError = Class.new(ProcessingError)
|
|
|
|
InvalidNoteError = Class.new(InvalidRecordError)
|
|
|
|
InvalidIssueError = Class.new(InvalidRecordError)
|
|
|
|
InvalidMergeRequestError = Class.new(InvalidRecordError)
|
2017-03-01 06:00:37 -05:00
|
|
|
UnknownIncomingEmail = Class.new(ProcessingError)
|
2018-10-24 12:01:44 -04:00
|
|
|
InvalidAttachment = Class.new(ProcessingError)
|
2016-05-18 18:19:25 -04:00
|
|
|
|
2015-08-20 14:05:06 -04:00
|
|
|
class Receiver
|
|
|
|
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?
|
|
|
|
|
|
|
|
mail = build_mail
|
2017-08-02 11:13:27 -04:00
|
|
|
|
2017-08-03 07:29:18 -04:00
|
|
|
ignore_auto_submitted!(mail)
|
2017-08-02 11:13:27 -04:00
|
|
|
|
2016-05-20 20:03:39 -04:00
|
|
|
mail_key = extract_mail_key(mail)
|
2016-06-15 04:18:40 -04:00
|
|
|
handler = Handler.for(mail, mail_key)
|
2015-08-20 14:05:06 -04:00
|
|
|
|
2016-06-15 04:23:41 -04:00
|
|
|
raise UnknownIncomingEmail unless handler
|
|
|
|
|
2017-04-21 11:11:21 -04:00
|
|
|
Gitlab::Metrics.add_event(:receive_email, handler.metrics_params)
|
2017-04-20 06:32:10 -04:00
|
|
|
|
2016-06-15 04:23:41 -04:00
|
|
|
handler.execute
|
2016-03-23 10:20:22 -04:00
|
|
|
end
|
2015-08-20 14:05:06 -04:00
|
|
|
|
2017-01-17 14:50:49 -05:00
|
|
|
private
|
|
|
|
|
2016-05-20 20:03:39 -04:00
|
|
|
def build_mail
|
|
|
|
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
|
|
|
|
|
2016-05-20 20:03:39 -04:00
|
|
|
def extract_mail_key(mail)
|
|
|
|
key_from_to_header(mail) || key_from_additional_headers(mail)
|
2016-03-01 00:29:20 -05:00
|
|
|
end
|
|
|
|
|
2016-05-20 20:03:39 -04:00
|
|
|
def key_from_to_header(mail)
|
2016-05-18 18:19:25 -04:00
|
|
|
mail.to.find do |address|
|
2016-03-23 08:05:31 -04:00
|
|
|
key = Gitlab::IncomingEmail.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
|
|
|
|
|
2016-05-20 20:03:39 -04:00
|
|
|
def key_from_additional_headers(mail)
|
2017-05-03 14:58:56 -04:00
|
|
|
find_key_from_references(mail) ||
|
|
|
|
find_key_from_delivered_to_header(mail)
|
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
|
|
|
|
|
2017-05-03 14:58:56 -04:00
|
|
|
def find_key_from_references(mail)
|
|
|
|
ensure_references_array(mail.references).find do |mail_id|
|
2016-05-20 19:21:58 -04:00
|
|
|
key = Gitlab::IncomingEmail.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
|
|
|
|
|
|
|
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-08-03 07:29:18 -04:00
|
|
|
|
|
|
|
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-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|