Module:IPAddress/sandbox

local p = {}

function p._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 p._isIpV4(s)
	local function legal(n) return (tonumber(n) or 256) < 256  and not n:match("^0%d") end
	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 p._isIp(s)
	return p._isIpV4(s) and "4" or p._isIpV6(s) and "6"
end

function p._isIpV4Range(s)
	return p._isIpV4(s:gsub("/%d+$", "")) and (p._isIpOrRange(s) == 'range')
end

function p._isIpV6Range(s)
	return p._isIpV6(s:gsub("/%d+$", "")) and (p._isIpOrRange(s) == 'range')
end

function p._isIpOrRange(s)
	local modip = require('Module:IP')
	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

function p._isUser(s)
	if p._isIpOrRange(s) ~= '' then return '' end
	local isTempUser = p._isTempUser(s)
	if isTempUser then return 'temp'
	else return 'perm' end
end

function p._isTempUser(s)
	if p._isIpOrRange(s) ~= '' then return false end
	return s:match('^~%d+%-%d+%-%d+$')
end

function p._isPermUser(s)
	if p._isIpOrRange(s) ~= '' then return false end
	return not p._isTempUser(s)
end

function p._main(s)
	local isIpOrRange = p._isIpOrRange(s)
	local isUser = p._isUser(s)
	return isIpOrRange ~= '' and isIpOrRange or isUser
end

local function input(frame)
	-- Return input parameter after replacing any of following directional markers.
	-- LRM : LEFT-TO-RIGHT MARK (U+200E)         : hex e2 80 8e = 226 128 142
	-- LRE : LEFT-TO-RIGHT EMBEDDING (U+202A)    : hex e2 80 aa = 226 128 170
	-- PDF : POP DIRECTIONAL FORMATTING (U+202C) : hex e2 80 ac = 226 128 172
	-- This is required for MediaWiki:Blockedtext message.
	return (frame.args[1] or ''):gsub('\226\128[\142\170\172]', ' ')  -- replace LRM, LRE, PDF with space delimiter
end

function p.isIpV6(frame) return p._isIpV6(input(frame)) and "1" or "0" end
function p.isIpV4(frame) return p._isIpV4(input(frame)) and "1" or "0" end
function p.isIpV6Range(frame) return p._isIpV6Range(input(frame)) and "1" or "0" end 
function p.isIpV4Range(frame) return p._isIpV4Range(input(frame)) and "1" or "0" end 
function p.isIp(frame) return p._isIp(input(frame)) or "" end

function p.isIpOrRange(frame)
	-- {{#invoke:IPAddress|isIpOrRange|x}} → 'ip' (IPv4/IPv6) or 'range' (CIDR IPv4/IPv6) or '' (invalid)
	return p._isIpOrRange(input(frame))
end

function p.isTempUser(frame) return p._isTempUser(input(frame)) and "1" or "0" end
function p.isPermUser(frame) return p._isPermUser(input(frame)) and "1" or "0" end
function p.isUser(frame) return p._isUser(input(frame)) or "" end

function p.main(frame)
	local args = require("Module:Arguments").getArgs(frame)
	local sanitizeInput = input(frame)
	local result = p._main(sanitizeInput)
	if args[result] then
		return args[result]
	else
		return result
	end
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.