Module:IPAddress: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
m (Changed protection level for "Module:IPAddress": integrating to local interface via MediaWiki:Newarticletext ([Edit=Require administrator access] (indefinite) [Move=Require administrator access] (indefinite)))
(update from Module:IPAddress/sandbox with new isIpOrRange function per Module talk:IPAddress#isIpOrRange)
Line 11: Line 11:


function _isIpV6( s )
function _isIpV6( s )
local dcolon, groups
local dcolon, groups
if type( s ) ~= "string"
if type( s ) ~= "string"
or s:len() == 0
or s:len() == 0
or s:find( "[^:%x]" ) -- only colon and hex digits are legal chars
or s:find( "[^:%x]" ) -- only colon and hex digits are legal chars
or s:find( "^:[^:]" ) -- can begin or end with :: but not with single :
or s:find( "^:[^:]" ) -- can begin or end with :: but not with single :
or s:find( "[^:]:$" )
or s:find( "[^:]:$" )
or s:find( ":::" )
or s:find( ":::" )
then
then
return false
return false
end
end
s, dcolon = s:gsub( "::", ":" )
s, dcolon = s:gsub( "::", ":" )
if dcolon > 1 then return false end -- at most one ::
if dcolon > 1 then return false end -- at most one ::
s = s:gsub( "^:?", ":" ) -- prepend : if needed, upper
s = s:gsub( "^:?", ":" ) -- prepend : if needed, upper
s, groups = s:gsub( ":%x%x?%x?%x?", "" ) -- remove valid groups, and count them
s, groups = s:gsub( ":%x%x?%x?%x?", "" ) -- remove valid groups, and count them
return ( ( dcolon == 1 and groups < 8 ) or ( dcolon == 0 and groups == 8 ) )
return ( ( dcolon == 1 and groups < 8 ) or ( dcolon == 0 and groups == 8 ) )
and ( s:len() == 0 or ( dcolon == 1 and s == ":" ) ) -- might be one dangling : if original ended with ::
and ( s:len() == 0 or ( dcolon == 1 and s == ":" ) ) -- might be one dangling : if original ended with ::
end
end


function _isIpV4( s )
function _isIpV4( s )
local function legal( n ) return ( tonumber( n ) or 256 ) < 256 and not n:match("^0%d") end-- in lua 0 is true!
local function legal( n ) return ( tonumber( n ) or 256 ) < 256 and not n:match("^0%d") end-- in lua 0 is true!

if type( s ) ~= "string" then return false end
if type( s ) ~= "string" then return false end
local p1, p2, p3, p4 = s:match( "^(%d+)%.(%d+)%.(%d+)%.(%d+)$" )
local p1, p2, p3, p4 = s:match( "^(%d+)%.(%d+)%.(%d+)%.(%d+)$" )
return legal( p1 ) and legal( p2 ) and legal( p3 ) and legal( p4 )
return legal( p1 ) and legal( p2 ) and legal( p3 ) and legal( p4 )
end
end


function _isIp( s )
function _isIp( s )
return _isIpV4( s ) and "4" or _isIpV6( s ) and "6"
return _isIpV4( s ) and "4" or _isIpV6( s ) and "6"
end
end


Line 46: Line 46:
function p.isIpV4(frame) return _isIpV4( frame.args[ 1 ] ) and "1" or "0" end
function p.isIpV4(frame) return _isIpV4( frame.args[ 1 ] ) and "1" or "0" end
function p.isIp(frame) return _isIp( frame.args[ 1 ] ) or "" end
function p.isIp(frame) return _isIp( frame.args[ 1 ] ) or "" end

function p.isIpOrRange(frame)
-- {{#invoke:IPAddress|isIpOrRange|x}} → 'ip' (IPv4/IPv6) or 'range' (CIDR IPv4/IPv6) or '' (invalid)
local modip = require('Module:IP')
local s = frame.args[1]
local success, ip = pcall(modip.IPAddress.new, s)
if success then
return 'ip'
end
success, ip = pcall(modip.Subnet.new, s)
if success then
return 'range'
end
return ''
end


return p
return p

Revision as of 09:53, December 14, 2020

Functions are not "local", so other modules can require this module and call them directly. We return an object with 3 small stub functions to call the real ones so that the functions can be called from templates also.

Only dotted decimal notation for IPv4 supported. Does not support dotted hexadecimal, dotted octal, or single-number formats (see IPv4#Address_representations).

Unit tests at Module:IPAddress/testcases


--[=[
Functions are not "local", so other modules can require this module and call them directly.
We return an object with 3 small stub functions to call the real ones so that the functions
can be called from templates also.

Only [[dotted decimal]] notation for IPv4 supported. Does not support
dotted hexadecimal, dotted octal, or single-number formats (see [[IPv4#Address_representations]]).

Unit tests at Module:IPAddress/tests
]=]

function _isIpV6( s )
	local dcolon, groups
	if type( s ) ~= "string"
		or s:len() == 0
		or s:find( "[^:%x]" ) -- only colon and hex digits are legal chars
		or s:find( "^:[^:]" ) -- can begin or end with :: but not with single :
		or s:find( "[^:]:$" )
		or s:find( ":::" )
	then
		return false
	end
	s, dcolon = s:gsub( "::", ":" )
	if dcolon > 1 then return false end -- at most one ::
	s = s:gsub( "^:?", ":" ) -- prepend : if needed, upper
	s, groups = s:gsub( ":%x%x?%x?%x?", "" ) -- remove valid groups, and count them
	return ( ( dcolon == 1 and groups < 8 ) or ( dcolon == 0 and groups == 8 ) )
		and ( s:len() == 0 or ( dcolon == 1 and s == ":" ) ) -- might be one dangling : if original ended with ::
end

function _isIpV4( s )
	local function legal( n ) return ( tonumber( n ) or 256 ) < 256  and not n:match("^0%d") end-- in lua 0 is true!

	if type( s ) ~= "string" then return false end
	local p1, p2, p3, p4 = s:match( "^(%d+)%.(%d+)%.(%d+)%.(%d+)$" )
	return legal( p1 ) and legal( p2 ) and legal( p3 ) and legal( p4 )
end

function _isIp( s )
	return _isIpV4( s ) and "4" or _isIpV6( s ) and "6"
end

local p = {}

function p.isIpV6(frame) return _isIpV6( frame.args[ 1 ] ) and "1" or "0" end
function p.isIpV4(frame) return _isIpV4( frame.args[ 1 ] ) and "1" or "0" end
function p.isIp(frame) return _isIp( frame.args[ 1 ] ) or "" end

function p.isIpOrRange(frame)
	-- {{#invoke:IPAddress|isIpOrRange|x}} → 'ip' (IPv4/IPv6) or 'range' (CIDR IPv4/IPv6) or '' (invalid)
	local modip = require('Module:IP')
	local s = frame.args[1]
	local success, ip = pcall(modip.IPAddress.new, s)
	if success then
		return 'ip'
	end
	success, ip = pcall(modip.Subnet.new, s)
	if success then
		return 'range'
	end
	return ''
end

return p