Skip to content

Commit

Permalink
Merge pull request #132 from sisimai/follow-up-pr-131
Browse files Browse the repository at this point in the history
Follow up Pull-Request #131
  • Loading branch information
azumakuniyuki committed Oct 20, 2018
2 parents 40c9655 + 7c94831 commit 0ec709f
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 157 deletions.
5 changes: 3 additions & 2 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ RELEASE NOTES for Ruby version of Sisimai
- releases: "https://github.com/sisimai/rb-Sisimai/releases"
- download: "https://rubygems.org/gems/sisimai"

v4.23.0p1
v4.23.0p2
--------------------------------------------------------------------------------
- release: ""
- version: "4.23.0p1"
- version: ""
- changes:
- Variable improvement (remove redundant substitution)
- Remove Sisimai::RFC2606 (Unused module)
Expand All @@ -15,6 +15,7 @@ v4.23.0p1
- Implement Sisimai::MIME.breaksup
- Call Sisimai::MIME.makeflat at Sisimai::Message::Email.parse
- Other related updates in Sisimai::Bite::Email::*
- Tiny improvement in Sisimai::String.to_plain

v4.23.0
--------------------------------------------------------------------------------
Expand Down
4 changes: 1 addition & 3 deletions lib/sisimai/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,7 @@ def self.expand_alias(email)

local = email.split('@')
value = ''
if cv = local[0].match(/\A([-_\w]+?)[+].+\z/)
value = cv[1] + '@' + local[1]
end
if cv = local[0].match(/\A([-_\w]+?)[+].+\z/) then value = cv[1] + '@' + local[1] end
return value
end

Expand Down
6 changes: 2 additions & 4 deletions lib/sisimai/bite/email/amazonses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,8 @@ def scan(mhead, mbody)
# Get other D.S.N. value from the error message
errormessage = e['diagnosis']

if cv = e['diagnosis'].match(/["'](\d[.]\d[.]\d.+)['"]/)
# 5.1.0 - Unknown address error 550-'5.7.1 ...
errormessage = cv[1]
end
# 5.1.0 - Unknown address error 550-'5.7.1 ...
if cv = e['diagnosis'].match(/["'](\d[.]\d[.]\d.+)['"]/) then errormessage = cv[1] end
pseudostatus = Sisimai::SMTP::Status.find(errormessage)
e['status'] = pseudostatus unless pseudostatus.empty?
end
Expand Down
14 changes: 5 additions & 9 deletions lib/sisimai/bite/email/amazonworkmail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,16 @@ def scan(mhead, mbody)
# Get other D.S.N. value from the error message
errormessage = e['diagnosis']

if cv = e['diagnosis'].match(/["'](\d[.]\d[.]\d.+)['"]/)
# 5.1.0 - Unknown address error 550-'5.7.1 ...
errormessage = cv[1]
end
# 5.1.0 - Unknown address error 550-'5.7.1 ...
if cv = e['diagnosis'].match(/["'](\d[.]\d[.]\d.+)['"]/) then errormessage = cv[1] end

pseudostatus = Sisimai::SMTP::Status.find(errormessage)
e['status'] = pseudostatus unless pseudostatus.empty?
end

if cv = e['diagnosis'].match(/[<]([245]\d\d)[ ].+[>]/)
# 554 4.4.7 Message expired: unable to deliver in 840 minutes.
# <421 4.4.2 Connection timed out>
e['replycode'] = cv[1]
end
# 554 4.4.7 Message expired: unable to deliver in 840 minutes.
# <421 4.4.2 Connection timed out>
if cv = e['diagnosis'].match(/[<]([245]\d\d)[ ].+[>]/) then e['replycode'] = cv[1] end

e['reason'] ||= Sisimai::SMTP::Status.name(e['status'])
e['agent'] = self.smtpagent
Expand Down
10 changes: 3 additions & 7 deletions lib/sisimai/bite/email/exim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,7 @@ def scan(mhead, mbody)
unless mhead['received'].empty?
# Get the name of local MTA
# Received: from marutamachi.example.org (c192128.example.net [192.0.2.128])
if cv = mhead['received'][-1].match(/from[ \t]([^ ]+)/)
localhost0 = cv[1]
end
if cv = mhead['received'][-1].match(/from[ \t]([^ ]+)/) then localhost0 = cv[1] end
end

dscontents.each do |e|
Expand Down Expand Up @@ -418,10 +416,8 @@ def scan(mhead, mbody)

unless e['rhost']
# Get the remote host name
if cv = e['diagnosis'].match(/host[ \t]+([^ \t]+)[ \t]\[.+\]:[ \t]/)
# host neko.example.jp [192.0.2.222]: 550 5.1.1 <kijitora@example.jp>... User Unknown
e['rhost'] = cv[1]
end
# host neko.example.jp [192.0.2.222]: 550 5.1.1 <kijitora@example.jp>... User Unknown
if cv = e['diagnosis'].match(/host[ \t]+([^ \t]+)[ \t]\[.+\]:[ \t]/) then e['rhost'] = cv[1] end

unless e['rhost']
# Get localhost and remote host name from Received header.
Expand Down
4 changes: 1 addition & 3 deletions lib/sisimai/bite/email/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,7 @@ def scan(mhead, mbody)
end
end

if cv = e['diagnosis'].match(/[(]state[ ](\d+)[)][.]/)
statecode0 = cv[1]
end
if cv = e['diagnosis'].match(/[(]state[ ](\d+)[)][.]/) then statecode0 = cv[1] end
if StateTable[statecode0]
# (state *)
e['reason'] = StateTable[statecode0]['reason']
Expand Down
10 changes: 3 additions & 7 deletions lib/sisimai/bite/email/mailru.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ def scan(mhead, mbody)
unless mhead['received'].empty?
# Get the name of local MTA
# Received: from marutamachi.example.org (c192128.example.net [192.0.2.128])
if cv = mhead['received'][-1].match(/from[ \t]([^ ]+)/)
localhost0 = cv[1]
end
if cv = mhead['received'][-1].match(/from[ \t]([^ ]+)/) then localhost0 = cv[1] end
end

dscontents.each do |e|
Expand All @@ -205,10 +203,8 @@ def scan(mhead, mbody)

unless e['rhost']
# Get the remote host name
if cv = e['diagnosis'].match(/host[ ]+([^ \t]+)[ ]\[.+\]:[ ]/)
# host neko.example.jp [192.0.2.222]: 550 5.1.1 <kijitora@example.jp>... User Unknown
e['rhost'] = cv[1]
end
# host neko.example.jp [192.0.2.222]: 550 5.1.1 <kijitora@example.jp>... User Unknown
if cv = e['diagnosis'].match(/host[ ]+([^ \t]+)[ ]\[.+\]:[ ]/) then e['rhost'] = cv[1] end

unless e['rhost']
# Get localhost and remote host name from Received header.
Expand Down
10 changes: 3 additions & 7 deletions lib/sisimai/bite/email/mxlogic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ def scan(mhead, mbody)
unless mhead['received'].empty?
# Get the name of local MTA
# Received: from marutamachi.example.org (c192128.example.net [192.0.2.128])
if cv = mhead['received'][-1].match(/from[ ]([^ ]+) /)
localhost0 = cv[1]
end
if cv = mhead['received'][-1].match(/from[ ]([^ ]+) /) then localhost0 = cv[1] end
end

dscontents.each do |e|
Expand All @@ -192,10 +190,8 @@ def scan(mhead, mbody)

unless e['rhost']
# Get the remote host name
if cv = e['diagnosis'].match(/host[ ]+([^ \t]+)[ ]\[.+\]:[ ]/)
# host neko.example.jp [192.0.2.222]: 550 5.1.1 <kijitora@example.jp>... User Unknown
e['rhost'] = cv[1]
end
# host neko.example.jp [192.0.2.222]: 550 5.1.1 <kijitora@example.jp>... User Unknown
if cv = e['diagnosis'].match(/host[ ]+([^ \t]+)[ ]\[.+\]:[ ]/) then e['rhost'] = cv[1] end

unless e['rhost']
# Get localhost and remote host name from Received header.
Expand Down
9 changes: 3 additions & 6 deletions lib/sisimai/bite/email/qmail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ def scan(mhead, mbody)
v['alterrors'] = e if e.start_with?(StartingOf[:error][0])

next if v['rhost']
if cv = e.match(ReHost)
v['rhost'] = cv[1]
end
next unless cv = e.match(ReHost)
v['rhost'] = cv[1]
end
end
end
Expand All @@ -210,9 +209,7 @@ def scan(mhead, mbody)

unless e['command']
# Verify each regular expression of patches
if cv = e['diagnosis'].match(ReCommands)
e['command'] = cv[1].upcase
end
if cv = e['diagnosis'].match(ReCommands) then e['command'] = cv[1].upcase end
e['command'] ||= ''
end
end
Expand Down
9 changes: 3 additions & 6 deletions lib/sisimai/bite/email/x4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,8 @@ def scan(mhead, mbody)
v['alterrors'] = e if e.start_with?(StartingOf[:error][0])

next if v['rhost']
if cv = e.match(ReHost)
v['rhost'] = cv[1]
end
next unless cv = e.match(ReHost)
v['rhost'] = cv[1]
end
end
end
Expand All @@ -230,9 +229,7 @@ def scan(mhead, mbody)

unless e['command']
# Verify each regular expression of patches
if cv = e['diagnosis'].match(ReCommands)
e['command'] = cv[1].upcase
end
if cv = e['diagnosis'].match(ReCommands) then e['command'] = cv[1].upcase end
e['command'] ||= ''
end
end
Expand Down
6 changes: 2 additions & 4 deletions lib/sisimai/bite/email/yahoo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ def scan(mhead, mbody)
# Remote host said: 550 5.1.1 <kijitora@example.org>... User Unknown [RCPT_TO]
v['diagnosis'] = e

if cv = e.match(/\[([A-Z]{4}).*\]\z/)
# Get SMTP command from the value of "Remote host said:"
v['command'] = cv[1]
end
# Get SMTP command from the value of "Remote host said:"
if cv = e.match(/\[([A-Z]{4}).*\]\z/) then v['command'] = cv[1] end
else
# <mailboxfull@example.jp>:
# Remote host said:
Expand Down
6 changes: 2 additions & 4 deletions lib/sisimai/bite/json/amazonses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,8 @@ def adapt(argvs)
v['diagnosis'] = e['diagnosticCode']
end

if cv = o['reportingMTA'].match(/\Adsn;[ ](.+)\z/)
# 'reportingMTA' => 'dsn; a27-23.smtp-out.us-west-2.amazonses.com',
v['lhost'] = cv[1]
end
# 'reportingMTA' => 'dsn; a27-23.smtp-out.us-west-2.amazonses.com',
if cv = o['reportingMTA'].match(/\Adsn;[ ](.+)\z/) then v['lhost'] = cv[1] end

if BounceType.key?(o['bounceType'].to_sym) &&
BounceType[o['bounceType'].to_sym].key?(o['bounceSubType'].to_sym)
Expand Down
33 changes: 12 additions & 21 deletions lib/sisimai/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,12 @@ def self.make(data: nil, **argvs)
break if datestring
end

if datestring
if datestring && cv = datestring.match(/\A(.+)[ ]+([-+]\d{4})\z/)
# Get the value of timezone offset from datestring
if cv = datestring.match(/\A(.+)[ ]+([-+]\d{4})\z/)
# Wed, 26 Feb 2014 06:05:48 -0500
datestring = cv[1]
zoneoffset = Sisimai::DateTime.tz2second(cv[2])
p['timezoneoffset'] = cv[2]
end
# Wed, 26 Feb 2014 06:05:48 -0500
datestring = cv[1]
zoneoffset = Sisimai::DateTime.tz2second(cv[2])
p['timezoneoffset'] = cv[2]
end

begin
Expand Down Expand Up @@ -243,23 +241,18 @@ def self.make(data: nil, **argvs)
# The value of "List-Id" header
p['listid'] = rfc822data['list-id'] || ''
unless p['listid'].empty?
# Get the value of List-Id header
if cv = p['listid'].match(/\A.*([<].+[>]).*\z/)
# List name <list-id@example.org>
p['listid'] = cv[1]
end
# Get the value of List-Id header like "List name <list-id@example.org>"
if cv = p['listid'].match(/\A.*([<].+[>]).*\z/) then p['listid'] = cv[1] end
p['listid'] = p['listid'].delete('<>').chomp("\r")
p['listid'] = '' if p['listid'].include?(' ')
end

# The value of "Message-Id" header
p['messageid'] = rfc822data['message-id'] || ''
unless p['messageid'].empty?
# Remove angle brackets
if cv = p['messageid'].match(/\A([^ ]+)[ ].*/)
p['messageid'] = cv[1]
end
p['messageid'] = p['messageid'].delete('<>').chomp("\r")
# Leave only string inside of angle brackets(<>)
if cv = p['messageid'].match(/\A([^ ]+)[ ].*/) then p['messageid'] = cv[1] end
if cv = p['messageid'].match(/[<]([^ ]+?)[>]/) then p['messageid'] = cv[1] end
end

# CHECK_DELIVERY_STATUS_VALUE:
Expand Down Expand Up @@ -306,10 +299,8 @@ def self.make(data: nil, **argvs)

# Check the value of "action"
if p['action'].size > 0
if cv = p['action'].match(/\A(.+?) .+/)
# Action: expanded (to multi-recipient alias)
p['action'] = cv[1]
end
# Action: expanded (to multi-recipient alias)
if cv = p['action'].match(/\A(.+?) .+/) then p['action'] = cv[1] end

unless %w[failed delayed delivered relayed expanded].index(p['action'])
# The value of "action" is not in the following values:
Expand Down
45 changes: 20 additions & 25 deletions lib/sisimai/message/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -391,22 +391,34 @@ def self.parse(argvs)
mailheader['subject'] ||= ''
mailheader['content-type'] ||= ''

if hookmethod.is_a? Proc
# Call the hook method
begin
p = {
'datasrc' => 'email',
'headers' => mailheader,
'message' => bodystring,
'bounces' => nil
}
havecaught = hookmethod.call(p)
rescue StandardError => ce
warn ' ***warning: Something is wrong in hook method :' << ce.to_s
end
end

# Decode BASE64 Encoded message body, rewrite.
mesgformat = (mailheader['content-type'] || '').downcase
ctencoding = (mailheader['content-transfer-encoding'] || '').downcase

if mesgformat.start_with?('text/plain', 'text/html')
# Content-Type: text/plain; charset=UTF-8
if ctencoding == 'base64' || ctencoding == 'quoted-printable'
if ctencoding == 'base64'
# Content-Transfer-Encoding: base64
bodystring = Sisimai::MIME.base64d(bodystring)

elsif ctencoding == 'quoted-printable'
# Content-Transfer-Encoding: quoted-printable
bodystring = if ctencoding == 'base64'
# Content-Transfer-Encoding: base64
Sisimai::MIME.base64d(bodystring)
else
# Content-Transfer-Encoding: quoted-printable
Sisimai::MIME.qprintd(bodystring)
end
bodystring = Sisimai::MIME.qprintd(bodystring)
end

if mesgformat.start_with?('text/html;')
Expand All @@ -422,22 +434,6 @@ def self.parse(argvs)
end
end

# Call the hook method
if hookmethod.is_a? Proc
# Execute hook method
begin
p = {
'datasrc' => 'email',
'headers' => mailheader,
'message' => bodystring,
'bounces' => nil
}
havecaught = hookmethod.call(p)
rescue StandardError => ce
warn ' ***warning: Something is wrong in hook method :' << ce.to_s
end
end

# EXPAND_FORWARDED_MESSAGE:
# Check whether or not the message is a bounce mail.
# Pre-Process email body if it is a forwarded bounce message.
Expand All @@ -459,7 +455,6 @@ def self.parse(argvs)
# 4. Sisimai::Bite::Email::*
# 5. Sisimai::RFC3464
# 6. Sisimai::RFC3834
#
if Sisimai::ARF.is_arf(mailheader)
# Feedback Loop message
scannedset = Sisimai::ARF.scan(mailheader, bodystring)
Expand Down
Loading

0 comments on commit 0ec709f

Please sign in to comment.