Module: Sisimai::Lhost::MailFoundry

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

Overview

Sisimai::Lhost::MailFoundry decodes a bounce email which created by MailFoundry www.barracuda.com/. Methods in the module are called from only Sisimai::Message.

Constant Summary collapse

Indicators =
Sisimai::Lhost.INDICATORS
Boundaries =
['Content-Type: message/rfc822'].freeze
StartingOf =
{
  message: ['Unable to deliver message to:'],
  error:   ['Delivery failed for the following reason:'],
}.freeze

Class Method Summary collapse

Class Method Details

.descriptionObject



70
# File 'lib/sisimai/lhost/mailfoundry.rb', line 70

def description; return 'MailFoundry'; end

.inquire(mhead, mbody) ⇒ Hash, Nil

This method is abstract.

Decodes the bounce message from MailFoundry

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



20
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
# File 'lib/sisimai/lhost/mailfoundry.rb', line 20

def inquire(mhead, mbody)
  return nil if mhead['subject'] != 'Message delivery has failed'
  return nil if mhead['received'].none? { |a| a.include?('(MAILFOUNDRY) id ') }

  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 e.start_with?(StartingOf[:message][0])
    end
    next if (readcursor & Indicators[:deliverystatus]) == 0 || e.empty?

    # Unable to deliver message to: <kijitora@example.org>
    # Delivery failed for the following reason:
    # Server mx22.example.org[192.0.2.222] failed with: 550 <kijitora@example.org> No such user here
    #
    # This has been a permanent failure. No further delivery attempts will be made.
    v = dscontents[-1]

    if e.start_with?('Unable to deliver message to: <') && e.index('@') > 1
      # Unable to deliver message to: <kijitora@example.org>
      if v["recipient"] != ""
        # There are multiple recipient addresses in the message body.
        dscontents << Sisimai::Lhost.DELIVERYSTATUS
        v = dscontents[-1]
      end
      v['recipient'] = e[e.index('<'), e.size]
      recipients += 1
    else
      # Error messages
      if e == StartingOf[:error][0]
        # Delivery failed for the following reason:
        v['diagnosis'] = e
      else
        # Detect error message
        next if v['diagnosis'].nil? || v['diagnosis'].empty? || e.start_with?('-')
        v['diagnosis'] += " #{e}"
      end
    end
  end
  return nil if recipients == 0
  return { 'ds' => dscontents, 'rfc822' => emailparts[1] }
end