Module: Sisimai::Lhost::Sendmail
- Defined in:
- lib/sisimai/lhost/sendmail.rb
Overview
Sisimai::Lhost::Sendmail decodes a bounce email which created by Sendmail Open Source sendmail.org/. Methods in the module are called from only Sisimai::Message.
Constant Summary collapse
- Indicators =
Sisimai::Lhost.INDICATORS
- Boundaries =
['Content-Type: message/rfc822', 'Content-Type: text/rfc822-headers'].freeze
- StartingOf =
{ # Error text regular expressions which defined in sendmail/savemail.c # savemail.c:1040|if (printheader && !putline(" ----- Transcript of session follows -----\n", # savemail.c:1041| mci)) # savemail.c:1042| goto writeerr; # savemail.c:1360|if (!putline( # savemail.c:1361| sendbody # savemail.c:1362| ? " ----- Original message follows -----\n" # savemail.c:1363| : " ----- Message header follows -----\n", message: [' ----- Transcript of session follows -----'], error: ['... while talking to '], }.freeze
Class Method Summary collapse
Class Method Details
.description ⇒ Object
213 |
# File 'lib/sisimai/lhost/sendmail.rb', line 213 def description; return 'V8Sendmail: /usr/sbin/sendmail'; end |
.inquire(mhead, mbody) ⇒ Hash, Nil
This method is abstract.
Decodes the bounce message from Sendmail Open Source
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/sisimai/lhost/sendmail.rb', line 32 def inquire(mhead, mbody) return nil if mhead['x-aol-ip'] match = false match ||= true if mhead['subject'].end_with?('see transcript for details') match ||= true if mhead['subject'].start_with?('Warning: ') return nil if match == false fieldtable = Sisimai::RFC1894.FIELDTABLE = {} # (Hash) Store values of each Per-Message field dscontents = [Sisimai::Lhost.DELIVERYSTATUS]; v = nil emailparts = Sisimai::RFC5322.part(mbody, Boundaries) bodyslices = emailparts[0].split("\n") readslices = [''] readcursor = 0 # (Integer) Points the current cursor position recipients = 0 # (Integer) The number of 'Final-Recipient' header thecommand = '' # (String) SMTP Command name begin with the string '>>>' esmtpreply = [] # (Array) Reply from remote server on SMTP session sessionerr = false # (Boolean) Flag, "true" if it is SMTP session error anotherset = {} # Another error information 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. readslices << e # Save the current line for the next loop if readcursor == 0 # Beginning of the bounce message or the message/delivery-status part readcursor |= Indicators[:deliverystatus] if e.start_with?(StartingOf[:message][0]) next end next if (readcursor & Indicators[:deliverystatus]) == 0 || e.empty? f = Sisimai::RFC1894.match(e) if f > 0 # "e" matched with any field defined in RFC3464 o = Sisimai::RFC1894.field(e) || next v = dscontents[-1] case o[3] when "addr" # Final-Recipient: rfc822; kijitora@example.jp # X-Actual-Recipient: rfc822; kijitora@example.co.jp if o[0] == 'final-recipient' # Final-Recipient: rfc822; 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'] = o[2] recipients += 1 else # X-Actual-Recipient: rfc822; kijitora@example.co.jp v['alias'] = o[2] end when "code" # Diagnostic-Code: SMTP; 550 5.1.1 <userunknown@example.jp>... User Unknown v['spec'] = o[1] v['diagnosis'] = o[2] else # Other DSN fields defined in RFC3464 next if fieldtable[o[0]].nil? next if o[3] == "host" && Sisimai::RFC1123.is_internethost(o[2]) == false v[fieldtable[o[0]]] = o[2] next if f != 1 [fieldtable[o[0]]] = o[2] end else # The line does not begin with a DSN field defined in RFC3464 # # ----- Transcript of session follows ----- # ... while talking to mta.example.org.: # >>> DATA # <<< 550 Unknown user recipient@example.jp # 554 5.0.0 Service unavailable # ... # Reporting-MTA: dns; mx.example.jp # Received-From-MTA: DNS; x1x2x3x4.dhcp.example.ne.jp # Arrival-Date: Wed, 29 Apr 2009 16:03:18 +0900 if e.start_with?(' ') == false if e.start_with?('>>> ') # >>> DATA (Client Command) thecommand = Sisimai::SMTP::Command.find(e) if thecommand.empty? elsif e.start_with?('<<< ') # <<< Response from the SMTP server cv = e[4, e.size - 4] esmtpreply << cv if esmtpreply.index(cv).nil? else # Detect an SMTP session error or a connection error next if sessionerr if e.start_with?(StartingOf[:error][0]) # ----- Transcript of session follows ----- # ... while talking to mta.example.org.: sessionerr = true next end if e.start_with?('<') && Sisimai::String.aligned(e, ['@', '>.', ' ']) # <kijitora@example.co.jp>... Deferred: Name server: example.co.jp.: host name lookup failure anotherset['recipient'] = Sisimai::Address.s3s4(e[0, e.index('>')]) anotherset['diagnosis'] = e[e.index(' ') + 1, e.size] else # ----- Transcript of session follows ----- # Message could not be delivered for too long # Message will be deleted from queue cr = Sisimai::SMTP::Reply.find(e) cs = Sisimai::SMTP::Status.find(e) if cr.size + cs.size > 7 # 550 5.1.2 <kijitora@example.org>... Message # # DBI connect('dbname=...') # 554 5.3.0 unknown mailer error 255 anotherset['status'] = cs anotherset['diagnosis'] ||= '' anotherset['diagnosis'] += " #{e}" elsif e.start_with?('Message ', 'Warning: ') # Message could not be delivered for too long # Warning: message still undelivered after 4 hours anotherset['diagnosis'] ||= '' anotherset['diagnosis'] += " #{e}" end end end else # Continued line of the value of Diagnostic-Code field next if readslices[-2].start_with?('Diagnostic-Code:') == false next if e.start_with?(' ') == false v['diagnosis'] += " " + e readslices[-1] = "Diagnostic-Code: #{e}" end end end # End of message/delivery-status return nil if recipients == 0 dscontents.each do |e| # Set default values if each value is empty. .each_key { |a| e[a] ||= [a] || '' } if anotherset['diagnosis'] # Copy alternative error message e['diagnosis'] = anotherset['diagnosis'] if e['diagnosis'].start_with?(' ') e['diagnosis'] = anotherset['diagnosis'] if e['diagnosis'] =~ /\A\d+\z/ e['diagnosis'] = anotherset['diagnosis'] if e['diagnosis'].empty? end while true # Replace or append the error message in "diagnosis" with the ESMTP Reply Code when the # following conditions have matched break if esmtpreply.empty? || recipients != 1 e['diagnosis'] = sprintf("%s %s", esmtpreply.join(' '), e['diagnosis']) break end e["command"] = thecommand if e["command"].empty? e["command"] = Sisimai::SMTP::Command.find(e['diagnosis']) if e["command"].empty? e["command"] = "EHLO" if e["command"].empty? && esmtpreply.size > 0 while true # Check alternative status code and override it break if anotherset.has_key?('status') == false break if anotherset['status'].size == 0 break if Sisimai::SMTP::Status.test(e['status']) e['status'] = anotherset['status'] break end # @example.jp, no local part # # Get email address from the value of Diagnostic-Code field next if e['recipient'].start_with?('@') == false cv = Sisimai::Address.find(e['diagnosis'], true) || [] e['recipient'] = cv[0][:address] if cv.size > 0 end return { 'ds' => dscontents, 'rfc822' => emailparts[1] } end |