Module:IPAddress: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(include changes from sandbox to remove global functions (undesirable and don't appear to be needed) and related to style; then can focus on proposed changes in sandbox)
(clean input to remove directional markers per MediaWiki talk:Blockedtext#Not detecting IPs properly)
Line 21: Line 21:


function p._isIpV4(s)
function p._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
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+)$")
Line 31: Line 31:
end
end


local function input(frame)
function p.isIpV6(frame) return p._isIpV6(frame.args[1]) and "1" or "0" end
-- Return input parameter after replacing any of following directional markers.
function p.isIpV4(frame) return p._isIpV4(frame.args[1]) and "1" or "0" end
-- LRM : LEFT-TO-RIGHT MARK (U+200E) : hex e2 80 8e = 226 128 142
function p.isIp(frame) return p._isIp(frame.args[1]) or "" end
-- 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.isIp(frame) return p._isIp(input(frame)) or "" end


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