-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Sisimai::RFC791, Sisimai::String.ipv4 has been moved to Sis…
…imai::RFC791.find #319
- Loading branch information
1 parent
3d3b65b
commit 54901a7
Showing
6 changed files
with
88 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters