Module: Sisimai::Lhost::X2

Defined in:
lib/sisimai/lhost/x2.rb

Overview

Sisimai::Lhost::X2 decodes a bounce email which created by Unknown MTA #2. Methods in the module are called from only Sisimai::Message.

Constant Summary collapse

Indicators =
Sisimai::Lhost.INDICATORS
Boundaries =
['--- Original message follows.'].freeze
StartingOf =
{
message: [
  "Unable to deliver message to the following address",
  "This Delivery Status Notification is sent from MTA",
]}.freeze

Class Method Summary collapse

Class Method Details

.descriptionObject



93
# File 'lib/sisimai/lhost/x2.rb', line 93

def description; return 'Unknown MTA #2'; end

.inquire(mhead, mbody) ⇒ Hash, Nil

This method is abstract.

Decodes the bounce message from Unknown MTA #2

Parameters:

  • mhead (Hash)

    Message headers of a bounce email

  • mbody (String)

    Message body of a bounce email

Returns:

  • (Hash)

    Bounce data list and message/rfc822 part

  • (Nil)

    it failed to decode or the arguments are missing



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sisimai/lhost/x2.rb', line 21

def inquire(mhead, mbody)
  match   = false
  match ||= true if mhead['from'].include?('MAILER-DAEMON@')
  match ||= true if mhead['subject'].start_with?('Delivery failure')
  match ||= true if mhead['subject'].start_with?('failure delivery')
  match ||= true if mhead['subject'].start_with?('failed delivery')
  return nil if match == false

  dscontents = [Sisimai::Lhost.DELIVERYSTATUS]; v = nil
  emailparts = Sisimai::RFC5322.part(mbody, Boundaries)
  bodyslices = emailparts[0].split("\n")
  readcursor = 0      # (Integer) Points the current cursor position
  recipients = 0      # (Integer) The number of 'Final-Recipient' header

  while e = bodyslices.shift do
    # Read error messages and delivery status lines from the head of the email to the previous
    # line of the beginning of the original message.
    if readcursor == 0
      # Beginning of the bounce message or delivery status part
      readcursor |= Indicators[:deliverystatus] if StartingOf[:message].any? { |a| e.start_with?(a) }
      next
    end
    next if (readcursor & Indicators[:deliverystatus]) == 0 || e.empty?

    # Message from example.com.
    # Unable to deliver message to the following address(es).
    #
    # <kijitora@example.com>:
    # This user doesn't have a example.com account (kijitora@example.com) [0]
    # 
    # --- OR ---
    # 
    # This Delivery Status Notification is sent from MTA...
    # 
    # Your delivery to the following address has been failed.
    # Please refer to the below for details.
    # ------------------------------------------------------
    # 
    # Delivery failed: kijitora@example.co.jp
    # 192.0.2.25 does not like recipient.
    # Remote host said[Response Message]: 550 5.1.1 <kijitora@example.co.jp>:
    #   Recipient address rejected: User unknown in local recipient table
    # Giving up on 192.0.2.25.
    # STEP: RCPT TO
    v = dscontents[-1]

    if e.start_with?('<')                 && Sisimai::String.aligned(e, ['<', '@', '>:']) ||
       e.start_with?('Delivery failed: ') && Sisimai::String.aligned(e, ['failed: ', '@'])
      # <kijitora@example.com>:
      # Delivery failed: kijitora@example.co.jp
      if v["recipient"] != ""
        # There are multiple recipient addresses in the message body.
        dscontents << Sisimai::Lhost.DELIVERYSTATUS
        v = dscontents[-1]
      end
      v['recipient'] = e[1, e.size - 3] if e.start_with?('<')
      v['recipient'] = e[e.index(': ') + 2, e.size - 1] if v['recipient'].empty?
      recipients += 1

    elsif e.start_with?('STEP: ')
      # STEP: RCPT TO
      # STEP: DATA SEND
      v['command'] = Sisimai::SMTP::Command.find(e)

    elsif e.start_with?('-----') == false
      # This user doesn't have a example.com account (kijitora@example.com) [0]
      v['diagnosis'] += " #{e}"
    end
  end
  return nil if recipients == 0
  return {"ds" => dscontents, "rfc822" => emailparts[1]}
end