Module: Sisimai::Lhost::Verizon

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

Overview

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

Constant Summary collapse

Indicators =
Sisimai::Lhost.INDICATORS

Class Method Summary collapse

Class Method Details

.descriptionObject



150
# File 'lib/sisimai/lhost/verizon.rb', line 150

def description; return 'Verizon Wireless: https://www.verizonwireless.com'; end

.inquire(mhead, mbody) ⇒ Hash, Nil

This method is abstract.

Decodes the bounce message from Verizon

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



14
15
16
17
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
140
141
142
143
144
145
146
147
148
149
# File 'lib/sisimai/lhost/verizon.rb', line 14

def inquire(mhead, mbody)
  match = -1
  while true
    # Check the value of "From" header
    # :'subject' => %r/Undeliverable Message/,
    break if mhead['received'].none? { |a| a.include?('.vtext.com (') }
    match = 1 if mhead['from'] == 'post_master@vtext.com'
    match = 0 if Sisimai::String.aligned(mhead['from'], ['sysadmin@', '.vzwpix.com'])
    break
  end
  return nil if match < 0

  boundaries = []
  dscontents = [Sisimai::Lhost.DELIVERYSTATUS]; v = nil
  emailparts = []
  readcursor = 0      # (Integer) Points the current cursor position
  recipients = 0      # (Integer) The number of 'Final-Recipient' header
  senderaddr = ''     # (String) Sender address in the message body
  subjecttxt = ''     # (String) Subject of the original message
  messagesof = {}     # (Hash) Error message patterns

  if match == 1
    # vtext.com
    markingsof = {message: ['Error: ']}
    messagesof = {
      # The attempted recipient address does not exist.
      'userunknown' => ['550 - Requested action not taken: no such user here'],
    }
    boundaries = [Sisimai::RFC2045.boundary(mhead['content-type'], 1)]
    emailparts = Sisimai::RFC5322.part(mbody, boundaries)
    bodyslices = emailparts[0].split("\n")

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

      # Message details:
      #   Subject: Test message
      #   Sent date: Wed Jun 12 02:21:53 GMT 2013
      #   MAIL FROM: *******@hg.example.com
      #   RCPT TO: *****@vtext.com
      v = dscontents[-1]
      if e.start_with?('  RCPT TO: ')
        if v["recipient"] != ""
          # There are multiple recipient addresses in the message body.
          dscontents << Sisimai::Lhost.DELIVERYSTATUS
          v = dscontents[-1]
        end

        v['recipient'] = e[11, e.size]
        recipients += 1
        next

      elsif e.start_with?('  MAIL FROM: ')
        #   MAIL FROM: *******@hg.example.com
        senderaddr = e[13, e.size] if senderaddr.empty?

      elsif e.start_with?('  Subject: ')
        #   Subject:
        subjecttxt = e[11, e.size] if subjecttxt.empty?
      else
        # 550 - Requested action not taken: no such user here
        v['diagnosis'] = e if e.include?(' - ')
      end
    end
  else
    # vzwpix.com
    startingof = {message: ['Message could not be delivered to mobile']}
    messagesof = {'userunknown' => ['No valid recipients for this MM']}
    boundaries = [Sisimai::RFC2045.boundary(mhead['content-type'], 1)]
    emailparts = Sisimai::RFC5322.part(mbody, boundaries)
    bodyslices = emailparts[0].split("\n")

    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?

      # Original Message:
      # From: kijitora <kijitora@example.jp>
      # To: 0000000000@vzwpix.com
      # Subject: test for bounce
      # Date:  Wed, 20 Jun 2013 10:29:52 +0000
      v = dscontents[-1]
      if e.start_with?('To: ')
        if v["recipient"] != ""
          # There are multiple recipient addresses in the message body.
          dscontents << Sisimai::Lhost.DELIVERYSTATUS
          v = dscontents[-1]
        end
        v['recipient'] = Sisimai::Address.s3s4(e[4, e.size])
        recipients += 1
        next

      elsif e.start_with?('From: ')
        # From: kijitora <kijitora@example.jp>
        senderaddr = Sisimai::Address.s3s4(e[4, e.size]) if senderaddr.empty?

      elsif e.start_with?('Subject: ')
        #   Subject:
        subjecttxt = e[9, e.size] if subjecttxt.empty?
      else
        # Message could not be delivered to mobile.
        # Error: No valid recipients for this MM
        v['diagnosis'] = e[7, e.size] if e.start_with?('Error: ')
      end
    end
  end
  return nil if recipients == 0

  # Set the value of "MAIL FROM:" and "From:"
  emailparts[1] += "From: #{senderaddr}\n"    if emailparts[1].include?("\nFrom: ")    == false
  emailparts[1] += "Subject: #{subjecttxt}\n" if emailparts[1].include?("\nSubject: ") == false

  dscontents.each do |e|
    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