Module: Sisimai::Lhost::DragonFly

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

Overview

Sisimai::Lhost::DragonFly decodes a bounce email which created by DMA: DragonFly Mail Agent www.dragonflybsd.org/handbook/mta/. Methods in the module are called from only Sisimai::Message.

Constant Summary collapse

Indicators =
Sisimai::Lhost.INDICATORS
Boundaries =
['Original message follows.', 'Message headers follow'].freeze
StartingOf =
{
  # https://github.com/corecode/dma/blob/ffad280aa40c242aa9a2cb9ca5b1b6e8efedd17e/mail.c#L84
  message: ['This is the DragonFly Mail Agent '],
}.freeze

Class Method Summary collapse

Class Method Details

.descriptionObject



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

def description; return 'DragonFly'; end

.inquire(mhead, mbody) ⇒ Hash, Nil

This method is abstract.

Decodes the bounce message from DMA: DragonFly Mail Agent

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



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/dragonfly.rb', line 23

def inquire(mhead, mbody)
  return nil if mhead['subject'].start_with?('Mail delivery failed') == false
  return nil if mhead['received'].none? { |a| a.include?(' (DragonFly Mail Agent') }

  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?

    # This is the DragonFly Mail Agent v0.13 at df.example.jp.
    #
    # There was an error delivering your mail to <kijitora@example.com>.
    #
    # email.example.jp [192.0.2.25] did not like our RCPT TO:
    # 552 5.2.2 <kijitora@example.com>: Recipient address rejected: Mailbox full
    #
    # Original message follows.
    v = dscontents[-1]

    if e.start_with?('There was an error delivering your mail to <')
      # email.example.jp [192.0.2.25] did not like our RCPT TO:
      # 552 5.2.2 <kijitora@example.com>: Recipient address rejected: Mailbox full
      if v["recipient"] != ""
        # There are multiple recipient addresses in the message body.
        dscontents << Sisimai::Lhost.DELIVERYSTATUS
        v = dscontents[-1]
      end
      v['recipient'] = Sisimai::Address.find(e, false)[0][:address]
      recipients += 1
    else
      # Pick the error message
      v['diagnosis'] += " #{e}"

      # Pick the remote hostname, and the SMTP command
      # net.c:500| snprintf(errmsg, sizeof(errmsg), "%s [%s] did not like our %s:\n%s",
      next if e.include?(' did not like our ') == false
      next if v['rhost'] != ""

      p = e.split(' ', 3)
      v['rhost']   = if p[0].include?('.') then p[0] else p[1] end
      v['command'] = Sisimai::SMTP::Command.find(e)
    end
  end
  return nil if recipients == 0
  return { 'ds' => dscontents, 'rfc822' => emailparts[1] }
end