Module: Sisimai::Lhost::MessagingServer

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

Overview

Sisimai::Lhost::MessagingServer decodes a bounce email which created by Oracle Communications Messaging Server and Sun Java System Messaging Server docs.oracle.com/en/industries/communications/messaging-server/index.html. Methods in the module are called from only Sisimai::Message.

Constant Summary collapse

Indicators =
Sisimai::Lhost.INDICATORS
Boundaries =
['Content-Type: message/rfc822', 'Return-path: '].freeze
StartingOf =
{message: ['This report relates to a message you sent with the following header fields:']}.freeze

Class Method Summary collapse

Class Method Details

.descriptionObject



140
# File 'lib/sisimai/lhost/messagingserver.rb', line 140

def description; return 'Oracle Communications Messaging Server'; end

.inquire(mhead, mbody) ⇒ Hash, Nil

This method is abstract.

Decodes the bounce message from MessagingServer

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



18
19
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sisimai/lhost/messagingserver.rb', line 18

def inquire(mhead, mbody)
  match  = 0
  match += 1 if mhead['content-type'].include?('Boundary_(ID_')
  match += 1 if mhead['subject'].start_with?('Delivery Notification: ')
  return nil if match == 0

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

    # --Boundary_(ID_0000000000000000000000)
    # Content-type: text/plain; charset=us-ascii
    # Content-language: en-US
    #
    # This report relates to a message you sent with the following header fields:
    #
    #   Message-id: <CD8C6134-C312-41D5-B083-366F7FA1D752@me.example.com>
    #   Date: Fri, 21 Nov 2014 23:34:45 +0900
    #   From: Shironeko <shironeko@me.example.com>
    #   To: kijitora@example.jp
    #   Subject: Nyaaaaaaaaaaaaaaaaaaaaaan
    #
    # Your message cannot be delivered to the following recipients:
    #
    #   Recipient address: kijitora@example.jp
    #   Reason: Remote SMTP server has rejected address
    #   Diagnostic code: smtp;550 5.1.1 <kijitora@example.jp>... User Unknown
    #   Remote system: dns;mx.example.jp (TCP|17.111.174.67|47323|192.0.2.225|25) (6jo.example.jp ESMTP SENDMAIL-VM)
    v = dscontents[-1]

    if Sisimai::String.aligned(e, ['  Recipient address: ', '@', '.']) ||
       Sisimai::String.aligned(e, ['  Original address: ',  '@', '.'])
      #   Recipient address: @smtp.example.net:kijitora@server
      #   Original address: kijitora@example.jp
      cv = Sisimai::Address.s3s4(e[e.rindex(' ') + 1, e.size])
      next if Sisimai::Address.is_emailaddress(cv) == false

      if v["recipient"] != "" && cv != v['recipient']
        # There are multiple recipient addresses in the message body.
        dscontents << Sisimai::Lhost.DELIVERYSTATUS
        v = dscontents[-1]
      end
      v['recipient'] = cv
      recipients += 1

    elsif e.start_with?('  Date: ')
      #   Date: Fri, 21 Nov 2014 23:34:45 +0900
      v['date'] = e[e.index(':') + 2, e.size]

    elsif e.start_with?('  Reason: ')
      #   Reason: Remote SMTP server has rejected address
      v['diagnosis'] = e[e.index(':') + 2, e.size]

    elsif e.start_with?('  Diagnostic code: ')
      #   Diagnostic code: smtp;550 5.1.1 <kijitora@example.jp>... User Unknown
      p1 = e.index(':')
      p2 = e.index(';')
      v['spec'] = e[p1 + 2, p2 - p1 - 2].upcase
      v['diagnosis'] = e[p2 + 1, e.size]

    elsif e.start_with?('  Remote system: ')
      #   Remote system: dns;mx.example.jp (TCP|17.111.174.67|47323|192.0.2.225|25)
      #     (6jo.example.jp ESMTP SENDMAIL-VM)
      p1 = e.index(';')
      p2 = e.index('(')
      remotehost = e[p1 + 1, p2 - p1 - 2]
      sessionlog = e[p2, e.size].split('|')
      v['rhost'] = remotehost

      # The value does not include ".", use IP address instead.
      # (TCP|17.111.174.67|47323|192.0.2.225|25)
      next if sessionlog[0] != '(TCP'
      v['lhost'] = sessionlog[1]
      v['rhost'] = sessionlog[3] if remotehost.index('.') < 2
    else
      # Original-envelope-id: 0NFC009FLKOUVMA0@mr21p30im-asmtp004.me.com
      # Reporting-MTA: dns;mr21p30im-asmtp004.me.com (tcp-daemon)
      # Arrival-date: Thu, 29 Apr 2014 23:34:45 +0000 (GMT)
      #
      # Original-recipient: rfc822;kijitora@example.jp
      # Final-recipient: rfc822;kijitora@example.jp
      # Action: failed
      # Status: 5.1.1 (Remote SMTP server has rejected address)
      # Remote-MTA: dns;mx.example.jp (TCP|17.111.174.67|47323|192.0.2.225|25)
      #  (6jo.example.jp ESMTP SENDMAIL-VM)
      # Diagnostic-code: smtp;550 5.1.1 <kijitora@example.jp>... User Unknown
      #
      case
      when e.start_with?('Status: ')
        # Status: 5.1.1 (Remote SMTP server has rejected address)
        p1 = e.index(':')
        p2 = e.index('('); next if p2.nil?
        v['status']    = e[p1 + 2, p2 - p1 - 3]
        v['diagnosis'] = e[p2 + 1, e[e.index(')') - p2 - 1]] if v["diagnosis"].empty?

      when e.start_with?('Arrival-Date: ')
        # Arrival-date: Thu, 29 Apr 2014 23:34:45 +0000 (GMT)
        v['date'] = e[e.index(':') + 2, e.size] if v["date"].empty?

      when e.start_with?('Reporting-MTA: ')
        # Reporting-MTA: dns;mr21p30im-asmtp004.me.com (tcp-daemon)
        localhost = e[e.index(';') + 1, e.size]
        v['lhost'] = localhost if v["lhost"].empty?
        v['lhost'] = localhost if v['lhost'].index('.') < 1
      end
    end
  end
  return nil if recipients == 0
  return {"ds" => dscontents, "rfc822" => emailparts[1]}
end