2016-05-18 18:19:25 -04:00
|
|
|
|
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
|
2016-05-18 18:19:25 -04:00
|
|
|
class ProcessingError < StandardError; end
|
|
|
|
class EmailUnparsableError < ProcessingError; end
|
|
|
|
class SentNotificationNotFoundError < ProcessingError; end
|
|
|
|
class ProjectNotFound < ProcessingError; end
|
|
|
|
class EmptyEmailError < ProcessingError; end
|
|
|
|
class AutoGeneratedEmailError < ProcessingError; end
|
|
|
|
class UserNotFoundError < ProcessingError; end
|
|
|
|
class UserBlockedError < ProcessingError; end
|
|
|
|
class UserNotAuthorizedError < ProcessingError; end
|
|
|
|
class NoteableNotFoundError < ProcessingError; end
|
|
|
|
class InvalidNoteError < ProcessingError; end
|
|
|
|
class InvalidIssueError < ProcessingError; end
|
2016-05-23 13:19:20 -04:00
|
|
|
class UnknownIncomingEmail < ProcessingError; end
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
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-01-17 14:50:49 -05:00
|
|
|
find_key_from_references(ensure_references_array(mail.references))
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_references_array(references)
|
|
|
|
case references
|
|
|
|
when Array
|
|
|
|
references
|
|
|
|
when String # Handle emails from Microsoft exchange which uses commas
|
|
|
|
Gitlab::IncomingEmail.scan_fallback_references(references)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_key_from_references(references)
|
|
|
|
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
|
2015-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|