Module: Sisimai::Lhost::IMailServer

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

Overview

Sisimai::Lhost::IMailServer decodes a bounce email which created by Progress iMail Server community.progress.com/s/products/imailserver. Methods in the module are called from only Sisimai::Message.

Constant Summary collapse

Boundaries =
['Original message follows.'].freeze
StartingOf =
{error: ['Body of message generated response:']}.freeze
MessagesOf =
{
  'userunknown' => ['Unknown user', 'Invalid final delivery userid'],
  'expired'     => ['Delivery failed '],
}.freeze

Class Method Summary collapse

Class Method Details

.descriptionObject



85
# File 'lib/sisimai/lhost/imailserver.rb', line 85

def description; return 'IPSWITCH IMail Server'; end

.inquire(mhead, mbody) ⇒ Hash, Nil

This method is abstract.

Decodes the bounce message from Progress iMail Server

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

def inquire(mhead, mbody)
  # X-Mailer: <SMTP32 v8.22>
  match  = 0
  match += 1 if mhead['subject'].start_with?('Undeliverable Mail ')
  match += 1 if mhead['x-mailer'].to_s.start_with?('<SMTP32 v')
  return nil if match == 0

  dscontents = [Sisimai::Lhost.DELIVERYSTATUS]; v = nil
  emailparts = Sisimai::RFC5322.part(mbody, Boundaries)
  bodyslices = emailparts[0].split("\n")
  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.

    # Unknown user: kijitora@example.com
    #
    # Original message follows.
    v = dscontents[-1]

    p0 = e.index(': ') || -1
    if (p0 > 8 && Sisimai::String.aligned(e, [': ', '@'])) || e.start_with?('undeliverable ')
      # Unknown user: kijitora@example.com
      # undeliverable to kijitora@example.com
      if v["recipient"] != ""
        # There are multiple recipient addresses in the message body.
        dscontents << Sisimai::Lhost.DELIVERYSTATUS
        v = dscontents[-1]
      end
      v['diagnosis'] = e
      v['recipient'] = Sisimai::Address.s3s4(e)
      recipients += 1
    else
      # Other error message text
      v['alterrors'] += " #{e}" if v['alterrors']
      v['alterrors']  = e if e.include?(StartingOf[:error][0])
    end
  end
  return nil if recipients == 0

  require 'sisimai/smtp/command'
  dscontents.each do |e|
    if e['alterrors'].nil? == false && e['alterrors'].empty? == false
      # Copy alternative error message
      e['diagnosis'] = if e['diagnosis']
                         "#{e['alterrors']} #{e['diagnosis']}"
                       else
                         e['alterrors']
                       end
      e.delete('alterrors')
    end
    e['command'] = Sisimai::SMTP::Command.find(e['diagnosis'])

    MessagesOf.each_key do |r|
      # Verify each regular expression of session errors
      next if MessagesOf[r].none? { |a| e['diagnosis'].include?(a) }
      e['reason'] = r
      break
    end
  end

  return {"ds" => dscontents, "rfc822" => emailparts[1]}
end