Module: Sisimai::Lhost::TrendMicro

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

Overview

Sisimai::Lhost::TrendMicro decodes a bounce email which created by TREND VISION ONE: Email and Collaboration Security: www.trendmicro.com/en_us/business/products/email-and-collaboration.html Methods in the module are called from only Sisimai::Message.

Constant Summary collapse

Boundaries =
['Content-Type: message/rfc822'].freeze

Class Method Summary collapse

Class Method Details

.descriptionObject



80
# File 'lib/sisimai/lhost/trendmicro.rb', line 80

def description; return 'TREND VISION ONE(Email and Collaboration Security)'; end

.inquire(mhead, mbody) ⇒ Hash, Nil

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



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

def inquire(mhead, mbody)
  # :received => %r/[ ][(]InterScanMSS[)][ ]with[ ]/,
  match = 0
  tryto = [
    'Mail could not be delivered',
    'メッセージを配信できません。',
    'メール配信に失敗しました',
  ]
  match += 1 if mhead['from'].start_with?('"InterScan MSS"')
  match += 1 if mhead['from'].start_with?('"InterScan Notification"')
  match += 1 if tryto.any? { |a| mhead['subject'] == a }
  return nil if match == 0

  require 'sisimai/smtp/command'
  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.
    next if e.empty?

    v  = dscontents[-1]
    p1 = e.index(' <<< ') || -1 # Sent <<< ...
    p2 = e.index(' >>> ') || -1 # Received >>> ...
    if e.include?('@') && e.include?(' <') && ( p1 > 1 || p2 > 1 || e.include?('Unable to deliver ') )
      # Sent <<< RCPT TO:<kijitora@example.co.jp>
      # Received >>> 550 5.1.1 <kijitora@example.co.jp>... user unknown
      # Unable to deliver message to <kijitora@neko.example.jp>
      cr = e[e.rindex('<') + 1, e.rindex('>') - e.rindex('<') - 1]

      if v["recipient"] != "" && cr != v['recipient']
        # There are multiple recipient addresses in the message body.
        dscontents << Sisimai::Lhost.DELIVERYSTATUS
        v = dscontents[-1]
      end
      v['recipient'] = cr
      v['diagnosis'] = e if e.include?('Unable to deliver ')
      recipients = dscontents.size
    end

    case
    when e.start_with?('Sent <<< ')
      # Sent <<< RCPT TO:<kijitora@example.co.jp>
      v['command'] = Sisimai::SMTP::Command.find(e)

    when e.start_with?('Received >>> ')
      # Received >>> 550 5.1.1 <kijitora@example.co.jp>... user unknown
      v['diagnosis'] = e[e.index(' >>> ') + 4, e.size]
    else
      # Error message in non-English
      v['command'] = Sisimai::SMTP::Command.find(e) if e.include?(' >>> ')
      p3 = e.index(' <<< '); next if p3.nil?
      v['diagnosis'] = e[p3 + 4, e.size]
    end
  end
  return nil if recipients == 0

  dscontents.each do |e|
    e['reason'] = 'userunknown' if e['diagnosis'].include?('Unable to deliver')
  end
  return { 'ds' => dscontents, 'rfc822' => emailparts[1] }
end