Skip to content

Commit

Permalink
Implement Sisimai::RFC791, Sisimai::String.ipv4 has been moved to Sis…
Browse files Browse the repository at this point in the history
…imai::RFC791.find #319
  • Loading branch information
azumakuniyuki committed Dec 22, 2024
1 parent 3d3b65b commit 54901a7
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 73 deletions.
6 changes: 3 additions & 3 deletions lib/sisimai/rfc5322.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Sisimai
# Sisimai::RFC5322 provide methods for checking email address.
module RFC5322
class << self
require 'sisimai/string'
require 'sisimai/rfc791'
require 'sisimai/address'
HeaderTable = {
:messageid => %w[message-id],
Expand Down Expand Up @@ -119,7 +119,7 @@ def received(argv1)
next if token[e].nil?
next if token[e].empty?
next unless token[e].start_with?('[')
token[e] = Sisimai::String.ipv4(token[e]).shift || ''
token[e] = Sisimai::RFC791.find(token[e]).shift || ''
end
token['from'] ||= ''

Expand All @@ -128,7 +128,7 @@ def received(argv1)
break if token['from'] == 'localhost'
break if token['from'] == 'localhost.localdomain'
break unless token['from'].include?('.') # A hostname without a domain name
break unless Sisimai::String.ipv4(token['from']).empty?
break unless Sisimai::RFC791.find(token['from']).empty?

# No need to rewrite token['from']
right = true
Expand Down
53 changes: 53 additions & 0 deletions lib/sisimai/rfc791.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Sisimai
# Sisimai::RFC791 is a class related to the Internet host
module RFC791
class << self
# Find an IPv4 address from the given string
# @param [String] argv1 String including an IPv4 address
# @return [Array] List of IPv4 addresses
# @since v5.0.0
def find(argv0)
return nil if argv0.to_s.empty?
return [] if argv0.size < 7

ipv4a = []
%w|( ) [ ] ,|.each do |e|
# Rewrite: "mx.example.jp[192.0.2.1]" => "mx.example.jp 192.0.2.1"
p0 = argv0.index(e); next unless p0
argv0[p0, 1] = ' '
end

argv0.split(' ').each do |e|
# Find string including an IPv4 address
next unless e.index('.') # IPv4 address must include "." character

lx = e.size; next if lx < 7 || lx > 17 # 0.0.0.0 = 7, [255.255.255.255] = 17
cu = 0 # Cursor for seeking each octet of an IPv4 address
as = '' # ASCII Code of each character
eo = '' # Buffer of each octet of IPv4 Address

while cu < lx
# Check whether each character is a number or "." or not
as = e[cu, 1].ord
cu += 1

if as < 48 || as > 57
# The character is not a number(0-9)
break if as != 46 # The character is not "."
next if eo == '' # The current buffer is empty
break if eo.to_i > 255 # The current buffer is greater than 255
eo = ''
next
end
eo << as.chr
break if eo.to_i > 255
end
ipv4a << e if eo.size > 0 && eo.to_i < 256
end
return ipv4a
end

end
end
end

4 changes: 2 additions & 2 deletions lib/sisimai/smtp/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ module SMTP
# reason from D.S.N. value, and getting D.S.N. from the text including D.S.N.
module Status
class << self
require "sisimai/string"
require "sisimai/rfc791"

CodePatterns = [
%r/[ ]?[(][#]([45][.]\d[.]\d+)[)]?[ ]?/, # #5.5.1
Expand Down Expand Up @@ -733,7 +733,7 @@ def find(argv1 = nil, argv2 = '0')
esmtperror = ' ' + argv1 + ' '
lookingfor = []

Sisimai::String.ipv4(esmtperror).each do |e|
Sisimai::RFC791.find(esmtperror).each do |e|
# Rewrite an IPv4 address in the given string(argv1) with '***.***.***.***'
p0 = esmtperror.index(e) || next
esmtperror[p0, e.size] = '***.***.***.***'
Expand Down
46 changes: 0 additions & 46 deletions lib/sisimai/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,52 +75,6 @@ def aligned(argv1, argv2)
return false
end

# Find an IPv4 address from the given string
# @param [String] argv1 String including an IPv4 address
# @return [Array] List of IPv4 addresses
# @since v5.0.0
def ipv4(argv0)
return nil if argv0.to_s.empty?
return [] if argv0.size < 7

ipv4a = []
%w|( ) [ ] ,|.each do |e|
# Rewrite: "mx.example.jp[192.0.2.1]" => "mx.example.jp 192.0.2.1"
p0 = argv0.index(e); next unless p0
argv0[p0, 1] = ' '
end

argv0.split(' ').each do |e|
# Find string including an IPv4 address
next unless e.index('.') # IPv4 address must include "." character

lx = e.size; next if lx < 7 || lx > 17 # 0.0.0.0 = 7, [255.255.255.255] = 17
cu = 0 # Cursor for seeking each octet of an IPv4 address
as = '' # ASCII Code of each character
eo = '' # Buffer of each octet of IPv4 Address

while cu < lx
# Check whether each character is a number or "." or not
as = e[cu, 1].ord
cu += 1

if as < 48 || as > 57
# The character is not a number(0-9)
break if as != 46 # The character is not "."
next if eo == '' # The current buffer is empty
break if eo.to_i > 255 # The current buffer is greater than 255
eo = ''
next
end
eo << as.chr
break if eo.to_i > 255
end
ipv4a << e if eo.size > 0 && eo.to_i < 256
end

return ipv4a
end

# Convert given HTML text to plain text
# @param [String] argv1 HTML text
# @param [Boolean] loose Loose check flag
Expand Down
29 changes: 29 additions & 0 deletions test/public/rfc791-test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'minitest/autorun'
require 'sisimai/rfc791'

class RFC791Test < Minitest::Test
Methods = { class: %w[find] }

def test_ipv4
ip4address = [
['host smtp.example.jp 127.0.0.4 SMTP error from remote mail server', '127.0.0.4'],
['mx.example.jp (192.0.2.2) reason: 550 5.2.0 Mail rejete.', '192.0.2.2'],
['Client host [192.0.2.49] blocked using cbl.abuseat.org (state 13).', '192.0.2.49'],
['127.0.0.1', '127.0.0.1'],
['365.31.7.1', ''],
['a.b.c.d', ''],
]
ip4address.each do |e|
assert_equal e[1], Sisimai::RFC791.find(e[0]).shift.to_s
end
assert_nil Sisimai::RFC791.find('')
assert_instance_of Array, Sisimai::RFC791.find('3.14')

ce = assert_raises ArgumentError do
Sisimai::RFC791.find()
Sisimai::RFC791.find("nekochan", nil)
end
end

end

23 changes: 1 addition & 22 deletions test/public/string-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'sisimai/string'

class StringTest < Minitest::Test
Methods = { class: %w[token is_8bit sweep aligned ipv4 to_plain to_utf8] }
Methods = { class: %w[token is_8bit sweep aligned to_plain to_utf8] }

def test_methods
Methods[:class].each { |e| assert_respond_to Sisimai::String, e }
Expand Down Expand Up @@ -69,27 +69,6 @@ def test_aligned
end
end

def test_ipv4
ip4address = [
['host smtp.example.jp 127.0.0.4 SMTP error from remote mail server', '127.0.0.4'],
['mx.example.jp (192.0.2.2) reason: 550 5.2.0 Mail rejete.', '192.0.2.2'],
['Client host [192.0.2.49] blocked using cbl.abuseat.org (state 13).', '192.0.2.49'],
['127.0.0.1', '127.0.0.1'],
['365.31.7.1', ''],
['a.b.c.d', ''],
]
ip4address.each do |e|
assert_equal e[1], Sisimai::String.ipv4(e[0]).shift.to_s
end
assert_nil Sisimai::String.ipv4('')
assert_instance_of Array, Sisimai::String.ipv4('3.14')

ce = assert_raises ArgumentError do
Sisimai::String.ipv4()
Sisimai::String.ipv4("nekochan", nil)
end
end

Ht1 = '
<html>
<head>
Expand Down

0 comments on commit 54901a7

Please sign in to comment.