Jump to content

Module:Unsigned: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Protected "Module:Unsigned": High-risk Lua module: Request at WP:RFPP ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite))
Line 1: Line 1:
local p = {}
local p = {}
-- alternative module title: HouseBlaster attempts to get around Lua patterns being awful

-- There's probably a way to use strptime or some other more sophisticated way, but you're not supposed to be using a non-timestamp as input anyway.
-- There's probably a way to use strptime or some other more sophisticated way, but you're not supposed to be using a non-timestamp as input anyway.

local function endswith(String,End)
return End == '' or string.sub(String,-string.len(End)) == End
end


local function trim(s)
local function trim(s)
Line 11: Line 7:
end
end


local function addUtcToStringIfItDoesNotEndWithUtc(s)
function p.getTimestamp(s)
--gets the timestamp in the input
if s == "" or endswith(s, "~~~~") then return s end
--if nothing is found, returns the empty string
if not endswith(s, "(UTC)") then
--we search for time and date seperately to allow for a time-less addition of date
return s .. " (UTC)"
local currentTestString
--this flag tells us to return an error if we only have a time (no date)
--match() on a nil is a bad thing, so avoid that
local sOrBlank = s or ''
currentTestString = mw.ustring.match( sOrBlank, '%d%d? %u%l+ %d%d%d%d', 0 ) or ''
if currentTestString ~= '' then
-- we have a date, so process and return accordingly
local theTime = mw.ustring.match( sOrBlank, '%d%d:%d%d,', 0 ) or ''
if theTime ~= '' then
return theTime .. ' ' .. currentTestString .. ' (UTC)'
else
return currentTestString
end
else
return ''
end
end
return s
end
end


local function _main(args)
function p.getUsername(s)
--gets the username in the input
local hopefullyTimestamp = args[1] or os.date('%H:%M, %d %B %Y (%Z)')
--------WARNING--------
return addUtcToStringIfItDoesNotEndWithUtc(trim(hopefullyTimestamp))
--this method assumes that *everything* besides the timestamp is, in fact, part of the username
--it does no further username validation
--if you try putting random garbage in, you **will** get garbage out
--------YOU HAVE BEEN WARNED--------
local toReturn = s or ''
--remove the date
toReturn = mw.ustring.gsub(toReturn, '%d%d? %u%l+ %d%d%d%d', '', 1) or ''
--remove the time
toReturn = mw.ustring.gsub(toReturn, '%d%d:%d%d,?', '', 1) or ''
--remove (UTC)
toReturn = mw.ustring.gsub(toReturn, '%(UTC%)', '', 1) or ''
--concat the empty string to ensure we only return the string and not the number of matches
return trim(toReturn) .. ''
end

local function makeUnsigned(args)
local isIP
local username
local timestamp
local toReturn
if args[2] then
-- we have multiple parameters
-- so we can be less janky
timestamp = p.getTimestamp(args[2])
if timestamp ~= '' then
username = trim(args[1] or '')
else
username = trim(args[2] or '')
timestamp = args[1]
end
else
-- be very janky; this has a high probability of GIGO-ing
-- due to the way getUsername is implemented
username = p.getUsername(args[1])
timestamp = p.getTimestamp(args[1])
end
-- error if we cannot find a username
if username == '' then
return '<em class="error">Unable to detect username</em>'
end
-- check if we have an IP address or not
isIp = require('Module:IPAddress')._isIp(username)
if isIp then
return mw.getCurrentFrame():expandTemplate{ title = 'unsigned/unregistered', args = { username , timestamp } }
else
return mw.getCurrentFrame():expandTemplate{ title = 'unsigned/registered', args = { username , timestamp } }
end
end
end


Line 29: Line 92:
-- We're being called via #invoke. The args are passed through to the module
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
-- from the template page, so use the args that were passed into the template.
args = require('Module:Arguments').getArgs(frame)
args = frame.args
else
else
-- We're being called from another module or from the debug console, so assume
-- We're being called from another module or from the debug console, so assume
Line 35: Line 98:
args = frame
args = frame
end
end
return _main(args)
return makeUnsigned(args)
end
end



Revision as of 03:41, 25 May 2025

local p = {}
-- alternative module title: HouseBlaster attempts to get around Lua patterns being awful
-- There's probably a way to use strptime or some other more sophisticated way, but you're not supposed to be using a non-timestamp as input anyway.

local function trim(s)
	return s:gsub("^%s+", ""):gsub("%s+$", ""):gsub("\226\128\142", "")
end

function p.getTimestamp(s)
	--gets the timestamp in the input
	--if nothing is found, returns the empty string
	--we search for time and date seperately to allow for a time-less addition of date
	local currentTestString
	--this flag tells us to return an error if we only have a time (no date)
	--match() on a nil is a bad thing, so avoid that
	local sOrBlank = s or ''
	
	currentTestString = mw.ustring.match( sOrBlank, '%d%d? %u%l+ %d%d%d%d', 0 ) or ''
	
	if currentTestString ~= '' then
		-- we have a date, so process and return accordingly
		local theTime = mw.ustring.match( sOrBlank, '%d%d:%d%d,', 0 ) or ''
		if theTime ~= '' then
			return theTime .. ' ' .. currentTestString .. ' (UTC)'
		else
			return currentTestString
		end
	else
		return ''
	end
end

function p.getUsername(s)
	--gets the username in the input
	--------WARNING--------
	--this method assumes that *everything* besides the timestamp is, in fact, part of the username
	--it does no further username validation
	--if you try putting random garbage in, you **will** get garbage out
	--------YOU HAVE BEEN WARNED--------
	local toReturn = s or ''
	--remove the date
	toReturn = mw.ustring.gsub(toReturn, '%d%d? %u%l+ %d%d%d%d', '', 1) or ''
	--remove the time
	toReturn = mw.ustring.gsub(toReturn, '%d%d:%d%d,?', '', 1) or ''
	--remove (UTC)
	toReturn = mw.ustring.gsub(toReturn, '%(UTC%)', '', 1) or ''
	--concat the empty string to ensure we only return the string and not the number of matches
	return trim(toReturn) .. ''
end

local function makeUnsigned(args)
	local isIP
	local username
	local timestamp
	local toReturn
	
	if args[2] then
		-- we have multiple parameters
		-- so we can be less janky
		timestamp = p.getTimestamp(args[2])
		if timestamp ~= '' then
			username = trim(args[1] or '')
		else
			username = trim(args[2] or '')
			timestamp = args[1]
		end
	else
		-- be very janky; this has a high probability of GIGO-ing
		-- due to the way getUsername is implemented
		username = p.getUsername(args[1])
		timestamp = p.getTimestamp(args[1])
	end
	
	-- error if we cannot find a username
	if username == '' then
		return '<em class="error">Unable to detect username</em>'
	end
	
	-- check if we have an IP address or not
	isIp = require('Module:IPAddress')._isIp(username)
	
	if isIp then
		return mw.getCurrentFrame():expandTemplate{ title = 'unsigned/unregistered', args = { username , timestamp } } 
	else
		return mw.getCurrentFrame():expandTemplate{ title = 'unsigned/registered', args = { username , timestamp } }
	end
end

function p.main(frame)
	local args
	if type(frame.args) == 'table' then
		-- We're being called via #invoke. The args are passed through to the module
		-- from the template page, so use the args that were passed into the template.
		args = require('Module:Arguments').getArgs(frame)
	else
		-- We're being called from another module or from the debug console, so assume
		-- the args are passed in directly.
		args = frame
	end
	return makeUnsigned(args)
end

return p