Naar inhoud springen

Module:CheckIfUserNameIsIP

Uit Wikipedia, de vrije encyclopedie

--[=[
Functies zijn niet "lokaal", andere modules kunnen deze module nodig hebben en deze direct aanroepen.
Voor IPv4-adressen wordt "4" teruggegeven
Voor IPv6-adressen wordt "6" teruggegeven
Alles wat geen IPv4 of IPv6 is geeft een "0" terug.
Voor oorspronkelijke code zie https://en.wikipedia.org/w/index.php?title=Module:IPAddress&oldid=542839577
]=]

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 _testip( s )
    return _isIpV4( s ) and "4" or _isIpV6( s ) and "6" or "0"
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.testip(frame) return _testip( frame.args[ 1 ] ) or "" end
 
return p