Extended getinfo for PtokaX with SQLite and LuaSQLite3 library
 

Extended getinfo for PtokaX with SQLite and LuaSQLite3 library

Started by PPK, 22 June, 2015, 14:23:06

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

PPK

This is script is extending getinfo commands. PtokaX contains inbuild !getinfo <nick> and !getipinfo <ip>. This script is adding !getdescriptioninfo, !gettaginfo and !getemailinfo.
This script require PtokaX 0.5.0.3 build 492 or higher. Script is compatible with Lua 5.1, 5.2 and 5.3. This script require LuaIconv and LuaSQLite3 libs to work.

Simply download http://www.ptokax.org/Scripts/getinfo-sqlite-ext.lua to PtokaX_dir\scripts directory.

Usage of this script is simple.
With command !help you can list all available commands:
QuoteAvailable commands:
   !getdescriptioninfo <description> - return user(s) with given description or description that match SQL Wildcards.
   !gettaginfo <tag> - return user(s) with given tag or tag that match SQL Wildcards.
   !getemailinfo <email> - return user(s) with given email or email that match SQL Wildcards.

In case of requests, bug reports or questions post them below ;)

Complete script:
Code: Lua
--[[
		PtokaX extended getinfo for sqlite script version 0.91. Script to get user information based on description, tag or email from sqlite database.

		Copyright (c) 2015 Petr Kozelka, PPK at PtokaX dot org

		This script is licensed under 
			Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
			See http://creativecommons.org/licenses/by-nc-nd/4.0/ for license details.
]]--

local iconv = require "iconv"
local sqlite3 = require "lsqlite3"

local utf8test = nil
local ansitoutf8 = nil

local uDB = nil

local sDBResult = nil
local sFirstNick = nil
local sFirstIP = nil

function OnStartup()
	-- test if we running on supported PtokaX version
	if Core.BuildNumber == nil or Core.BuildNumber < 492 then
		error("This script require PtokaX 0.5.0.3 build 492 or higher!")
	end

	-- test if PtokaX is compiled with SQLite support
	if SetMan.tBooleans.EnableDatabase == nil then
		error("This script require PtokaX with SQLite support!")
	end

	os.setlocale("")

	utf8test = iconv.new("utf8", "utf8")
	ansitoutf8 = iconv.new("utf8", SetMan.GetString(SetMan.tStrings.Encoding))

	uDB = sqlite3.open(Core.GetPtokaXPath().."cfg/users.sqlite")

	if uDB ~= nil then
		uDB:exec("PRAGMA synchronous = NORMAL;PRAGMA journal_mode = WAL;")
	end
end

function OnExit()
	if uDB ~= nil then
		uDB:close()
	end
end

function ChatArrival(tUser, sData)
	-- check if user is allowed to use getinfo
    if uDB == nil or tUser.iProfile == -1 or ProfMan.GetProfilePermissions(tUser.iProfile).bGetInfo == false then
        return false
    end

	-- check if received command is starting with chat command prefix
   if string.find(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), string.sub(sData, tUser.sNick:len()+4, tUser.sNick:len()+4), 1, true) == nil then
      return false
   end

	-- get command without '<nick> ' and ending pipe
	local sCmd = string.sub(sData, tUser.sNick:len()+5, -2)
	local sParam = nil

	-- try to find space -> command with param
	local nFound = string.find(sCmd, " ", 1, true)
	if nFound ~= nil then
		sParam = string.sub(sCmd, nFound+1)
		sCmd = string.sub(sCmd, 1, nFound-1):lower()
	else
		if sCmd:lower() == "help" then -- list available commands for this script
			local sCommandPrefix = string.sub(SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes), 1, 1)
			Core.SendToUser(tUser, string.format("<%s> Available commands:\n\t%sgetdescriptioninfo <description> - return user(s) with given description or description that match SQL Wildcards.\n\t%sgettaginfo <tag> - return user(s) with given tag or tag that match SQL Wildcards.\n\t%sgetemailinfo <email> - return user(s) with given email or email that match SQL Wildcards.|",
				Core.GetHubSecAlias(), sCommandPrefix, sCommandPrefix, sCommandPrefix))
			return false
		else
			-- we don't handle any commands without parameter
			return false
		end
	end

	if sCmd == "getdescriptioninfo" then
		sEscapedUtf = AnsiToUtfAndEscape(sParam)
		if sEscapedUtf == nil then
         Core.SendToUser(tUser, string.format("<%s> *** Syntax error in command %sgetdescriptioninfo <description>.|", Core.GetHubSecAlias(), SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes):sub(1, 1)))
			return true
		end

		DBExecute(string.format("SELECT nick, %s, ip_address, share, description, tag, connection, email FROM userinfo WHERE LOWER(description) LIKE LOWER(%s) ORDER BY last_updated DESC LIMIT 50;", "strftime('%s', last_updated)", sEscapedUtf), tUser, sParam)

		return true
	elseif sCmd == "gettaginfo" then
		sEscapedUtf = AnsiToUtfAndEscape(sParam)
		if sEscapedUtf == nil then
         Core.SendToUser(tUser, string.format("<%s> *** Syntax error in command %sgettaginfo <tag>.|", Core.GetHubSecAlias(), SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes):sub(1, 1)))
			return true
		end

		DBExecute(string.format("SELECT nick, %s, ip_address, share, description, tag, connection, email FROM userinfo WHERE LOWER(tag) LIKE LOWER(%s) ORDER BY last_updated DESC LIMIT 50;", "strftime('%s', last_updated)", sEscapedUtf), tUser, sParam)

		return true
	elseif sCmd == "getemailinfo" then
		sEscapedUtf = AnsiToUtfAndEscape(sParam)
		if sEscapedUtf == nil then
         Core.SendToUser(tUser, string.format("<%s> *** Syntax error in command %sgetemailinfo <email>.|", Core.GetHubSecAlias(), SetMan.GetString(SetMan.tStrings.ChatCommandsPrefixes):sub(1, 1)))
			return true
		end

		DBExecute(string.format("SELECT nick, %s, ip_address, share, description, tag, connection, email FROM userinfo WHERE LOWER(email) LIKE LOWER(%s) ORDER BY last_updated DESC LIMIT 50;", "strftime('%s', last_updated)", sEscapedUtf), tUser, sParam)

		return true
	end
end

function AnsiToUtfAndEscape(sData)
	local sUtfData = nil

	nstr, err = utf8test:iconv(sData)
	if err ~= nil then
		nstr, err = ansitoutf8:iconv(sData)
		if err == nil then
			sUtfData = nstr
		end
	else
		sUtfData = sData
	end

	if sUtfData == nil then
		return nil
	end

	sEscapedData = uDB:escape(sUtfData)
	if sEscapedData == nil then
		return nil
	end

	return sEscapedData
end

function DBExecute(sCommand, tUser, sParam)
	uDB:exec(sCommand, DBCallBack)

	if sDBResult ~= nil then
		Core.SendToUser(tUser, sDBResult)
		sDBResult = nil
		sFirstNick = nil
		sFirstIP = nil
	else
		Core.SendToUser(tUser, string.format("<%s> *** Error: %s not found.|", Core.GetHubSecAlias(), sParam))
	end
end

function DBCallBack(uData, iCount, sData, sNames)
	if iCount ~= 8 then
		return 1
	end

	if sData[1]:len() == 0 or sData[2]:len() == 0 or sData[3]:len() == 0 then
		return 1
	end

	if sDBResult == nil then
		sFirstNick = sData[1]
		sFirstIP = sData[3]

		sDBResult = string.format("<%s> \nNick: %s", Core.GetHubSecAlias(), sData[1])

		tReg = RegMan.GetReg(sData[1])
		if tReg ~= nil then
			sDBResult = sDBResult..string.format("\nProfile: %s", ProfMan.GetProfile(tReg.iProfile).sProfileName)
		end

		tUser = Core.GetUser(sData[1]) 
		if tUser ~= nil then
			sDBResult = sDBResult..string.format("\nStatus: Online from %s", os.date("%c", Core.GetUserValue(tUser, 25)))
		else
			sDBResult = sDBResult..string.format("\nStatus: Offline from %s", os.date("%c", sData[2]))
		end

		sDBResult = sDBResult..string.format("\nIP: %s\nShare size: %s", sData[3], sData[4])

		if sData[5]:len() ~= 0 then
			sDBResult = sDBResult..string.format("\nDescription: %s", sData[5])
		end

		if sData[6]:len() ~= 0 then
			sDBResult = sDBResult..string.format("\nTag: %s", sData[6])
		end

		if sData[7]:len() ~= 0 then
			sDBResult = sDBResult..string.format("\nConnection: %s", sData[7])
		end

		if sData[8]:len() ~= 0 then
			sDBResult = sDBResult..string.format("\nEmail: %s", sData[8])
		end

		sDBResult = sDBResult..string.format("\nCountry: %s", IP2Country.GetCountryCode(sData[3]))
	else
		if sFirstNick ~= nil then
			sDBResult = string.format("<%s> \nNick: %s\t\tIP: %s", Core.GetHubSecAlias(), sFirstNick, sFirstIP)
			sFirstNick = nil
			sFirstIP = nil
		end

		sDBResult = sDBResult..string.format("\nNick: %s\t\tIP: %s", sData[1], sData[3])
	end

	return 0
end
"Most of you are familiar with the virtues of a programmer. There are three, of course: laziness, impatience, and hubris." - Larry Wall

PPK

Script updated. Added help. Fixed blocking of hub commands without parameter (thx Alexey for report).
"Most of you are familiar with the virtues of a programmer. There are three, of course: laziness, impatience, and hubris." - Larry Wall

SMF spam blocked by CleanTalk