Skip to content

Commit

Permalink
Implement Sisimai::RFC791.is_ipv4address #319, imported from https://…
Browse files Browse the repository at this point in the history
  • Loading branch information
azumakuniyuki committed Dec 22, 2024
1 parent 54901a7 commit 16e443d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/sisimai/fact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Sisimai
# Sisimai::Fact generate the list of decoded bounce data
class Fact
require 'sisimai/message'
require 'sisimai/rfc791'
require 'sisimai/rfc1123'
require 'sisimai/rfc1894'
require 'sisimai/rfc5322'
Expand Down Expand Up @@ -246,7 +247,7 @@ def self.rise(**argvs)
ee.each do |w|
# get a hostname from the string like "127.0.0.1 x109-20.example.com 192.0.2.20"
# or "mx.sp.example.jp 192.0.2.135"
next if w =~ /\A\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}\z/; # Skip if it is an IPv4 address
next if Sisimai::RFC791.is_ipv4address(w)
piece[v] = w
break
end
Expand Down
16 changes: 16 additions & 0 deletions lib/sisimai/rfc791.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ module Sisimai
# Sisimai::RFC791 is a class related to the Internet host
module RFC791
class << self
# Returns 1 if the argument is an IPv4 address
# @param [String] argv1 IPv4 address like "192.0.2.25"
# @return [Bool] 1: is an IPv4 address
# @since v5.2.0
def is_ipv4address(argv0)
return false if argv0.nil? || argv0.size < 7
octet = argv0.split(/[.]/); return false if octet.size != 4

octet.each do |e|
# Check each octet is between 0 and 255
return false unless e =~ /\A[0-9]{1,3}\z/
v = e.to_i
return false if v < 0 || v > 255
end
return true
end
# Find an IPv4 address from the given string
# @param [String] argv1 String including an IPv4 address
# @return [Array] List of IPv4 addresses
Expand Down
16 changes: 14 additions & 2 deletions test/public/rfc791-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@
require 'sisimai/rfc791'

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

def test_ipv4
def test_is_upv4address
addr0 = ["123.456.78.9"]
addr1 = ["192.0.2.22"]

addr0.each do |e|
assert_equal false, Sisimai::RFC791.is_ipv4address(e)
end
addr1.each do |e|
assert_equal true, Sisimai::RFC791.is_ipv4address(e)
end
end

def test_find
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'],
Expand Down

0 comments on commit 16e443d

Please sign in to comment.