BigW Consortium Gitlab

receiver.rb 2.68 KB
Newer Older
1
require_dependency 'gitlab/email/handler'
2

3 4 5
# Inspired in great part by Discourse's Email::Receiver
module Gitlab
  module Email
6 7 8 9 10 11 12 13 14 15 16 17 18
    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)
    InvalidNoteError = Class.new(ProcessingError)
    InvalidIssueError = Class.new(ProcessingError)
    UnknownIncomingEmail = Class.new(ProcessingError)
19

20 21
    class Receiver
      def initialize(raw)
22
        @raw = raw
23 24 25
      end

      def execute
26 27 28 29
        raise EmptyEmailError if @raw.blank?

        mail = build_mail
        mail_key = extract_mail_key(mail)
Lin Jen-Shin committed
30
        handler = Handler.for(mail, mail_key)
31

32 33
        raise UnknownIncomingEmail unless handler

34
        Gitlab::Metrics.add_event(:receive_email, handler.metrics_params)
35

36
        handler.execute
37
      end
38

39 40
      private

41 42
      def build_mail
        Mail::Message.new(@raw)
43 44
      rescue Encoding::UndefinedConversionError,
             Encoding::InvalidByteSequenceError => e
45 46 47
        raise EmailUnparsableError, e
      end

48 49
      def extract_mail_key(mail)
        key_from_to_header(mail) || key_from_additional_headers(mail)
50 51
      end

52
      def key_from_to_header(mail)
53
        mail.to.find do |address|
54
          key = Gitlab::IncomingEmail.key_from_address(address)
55
          break key if key
56 57 58
        end
      end

59
      def key_from_additional_headers(mail)
60 61
        find_key_from_references(mail) ||
          find_key_from_delivered_to_header(mail)
62 63 64 65 66 67
      end

      def ensure_references_array(references)
        case references
        when Array
          references
68 69 70
        when String
          # Handle emails from clients which append with commas,
          # example clients are Microsoft exchange and iOS app
71
          Gitlab::IncomingEmail.scan_fallback_references(references)
72 73
        when nil
          []
74 75 76
        end
      end

77 78
      def find_key_from_references(mail)
        ensure_references_array(mail.references).find do |mail_id|
79
          key = Gitlab::IncomingEmail.key_from_fallback_message_id(mail_id)
80
          break key if key
81 82
        end
      end
83 84 85 86 87 88 89

      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
90 91 92
    end
  end
end