Module: Sisimai::Lhost::MFILTER

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

Overview

Sisimai::Lhost::mFILTER decodes a bounce email which created by Digital Arts m-FILTER www.daj.jp/bs/mf/. Methods in the module are called from only Sisimai::Message.

Constant Summary collapse

Indicators =
Sisimai::Lhost.INDICATORS
Boundaries =
['-------original message', '-------original mail info'].freeze
StartingOf =
{
  error:   ['-------server message'],
  command: ['-------SMTP command'],
}.freeze

Class Method Summary collapse

Class Method Details

.descriptionObject



117
# File 'lib/sisimai/lhost/mfilter.rb', line 117

def description; return 'Digital Arts m-FILTER'; end

.inquire(mhead, mbody) ⇒ Hash, Nil

This method is abstract.

Decodes the bounce message from Digital Arts m-FILTER

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

def inquire(mhead, mbody)
  # X-Mailer: m-FILTER
  proceedsto = false
  proceedsto = true if mhead['x-mailer'].to_s == 'm-FILTER'
  proceedsto = true if Boundaries.any? { |a| mbody.include?(a) }
  proceedsto = true if StartingOf[:error].any?   { |a| mbody.include?(a) }
  proceedsto = true if StartingOf[:command].any? { |a| mbody.include?(a) }
  return nil if proceedsto == false

  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
  markingset = {'diagnosis' => false, 'command' => false}

  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
      if e.include?('@') && e.include?(' ') == false && Sisimai::Address.is_emailaddress(e)
        readcursor |= Indicators[:deliverystatus]
      end
    end
    next if (readcursor & Indicators[:deliverystatus]) == 0 || e.empty?

    # このメールは「m-FILTER」が自動的に生成して送信しています。
    # メールサーバーとの通信中、下記の理由により
    # このメールは送信できませんでした。
    #
    # 以下のメールアドレスへの送信に失敗しました。
    # kijitora@example.jp
    #
    #
    # -------server message
    # 550 5.1.1 unknown user <kijitora@example.jp>
    #
    # -------SMTP command
    # DATA
    #
    # -------original message
    v = dscontents[-1]

    if e.include?('@') && e.include?(' ') == false
      # 以下のメールアドレスへの送信に失敗しました。
      # kijitora@example.jp
      if v["recipient"] != ""
        # There are multiple recipient addresses in the message body.
        dscontents << Sisimai::Lhost.DELIVERYSTATUS
        v = dscontents[-1]
      end
      v['recipient'] = e
      recipients += 1

    elsif e.size == 4 && e.index(' ').nil?
      # -------SMTP command
      # DATA
      next if v['command']
      v['command'] = e if markingset['command']

    else
      # Get error message and SMTP command
      if e == StartingOf[:error][0]
        # -------server message
        markingset['diagnosis'] = true

      elsif e == StartingOf[:command][0]
        # -------SMTP command
        markingset['command'] = true
      else
        # 550 5.1.1 unknown user <kijitora@example.jp>
        next if e.start_with?('-')
        next if v['diagnosis'] != ""
        v['diagnosis'] = e
      end
    end
  end
  return nil if recipients == 0

  dscontents.each do |e|
    e['agent'] = 'mFILTER'

    # Get localhost and remote host name from Received header.
    next if mhead['received'].empty?
    rheads = mhead['received']
    rhosts = Sisimai::RFC5322.received(rheads[-1])

    e['lhost'] = Sisimai::RFC5322.received(rheads[0]).shift if e["lhost"].empty?
    [rhosts[0], rhosts[1]].each do |ee|
      # Avoid "... by m-FILTER"
      next if ee.include?('.') == false
      e['rhost'] = ee
    end
  end
  return {"ds" => dscontents, "rfc822" => emailparts[1]}
end