Seen script
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Seen script

Started by Creative, 01 May, 2007, 13:11:14

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Creative

I have this Lastseen Script. I tried to convert in lua 5.1 using Mutors converter, but still it gave me lots of errors.
It works fine in ptokax 0.3.5.1 but doesnt works in 0.3.5.2

I am pasting the script below, please someone make this script work in 0.3.5.2.

-- Creator Leon
-- Started lua 5 conversion, damo
-- Fully converted to lua 5 by 6Marilyn6Manson6
---- Modded By Madman
-- Added save to file
-- Added HowToSend, request by Ubikk
-- Hidden the command, request by damo
-- Added OnError Msg
-- Rewrote the ChatArrival
-- Added postibilite so seen ips, Request by Ubikk

BotName = "Bot"

minLen = 2
maxLen = 20
HowToSend = 2 -- 1: Send to all, 2: Send to user, 3: PM to the user

require("seen.dat")

function Main()
	if HowToSend == 3 then
		frmHub:RegBot(BotName)
	end
end

function NewUserConnected(curUser)
	local boolDisc = 0
	if string.len(curUser.sName) < minLen then
		curUser:SendPM(BotName, "Your nick is to short, a minimum of "..minLen.." chars is required!")
		curUser:SendPM(BotName, "Disconnecting...")
		curUser:Disconnect()
		boolDisc = 1
	elseif string.len(curUser.sName) > maxLen then
		curUser:SendPM(BotName, "Your nick is to long, a maximum of "..maxLen.." chars is required!")
		curUser:SendPM(BotName, "Disconnecting...")
		curUser:Disconnect()
		boolDisc = 1
	elseif ( string.find (curUser.sName, " ", 1, 1) ) then
		curUser:SendPM(BotName, "Spaces not allowed in nicknames!")
		curUser:SendPM(BotName, "Disconnecting...")
		curUser:Disconnect()
		boolDisc = 1
	end
	if (boolDisc == 0) then
		if seenArray == nil then
			seenArray = {}
			seenArray[curUser.sName] = {}
			seenArray[curUser.sIP] = {}
			seenArray[curUser.sName] = os.date(curUser.sName.." connected at %d/%m/%Y %T and is still online.")
			seenArray[curUser.sIP] = os.date(curUser.sIP.." connected at %d/%m/%Y %T and is still online.")
			SaveToFile("Seen.dat", seenArray, "seenArray")
			SendToAll("Weee")
		else
			seenArray[curUser.sName] = os.date(curUser.sName.." connected at %d/%m/%Y %T and is still online.")
			seenArray[curUser.sIP] = os.date(curUser.sIP.." connected at %d/%m/%Y %T and is still online.")
			SaveToFile("Seen.dat", seenArray, "seenArray")
		end
	end
end

function OpConnected(curUser)
	if seenArray == nil then
		seenArray = {}
		seenArray[curUser.sName] = {}
		seenArray[curUser.sIP] = {}
		seenArray[curUser.sName] = os.date(curUser.sName.." connected at %d/%m/%Y %T and is still online.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." connected at %d/%m/%Y %T and is still online.")
		SaveToFile("Seen.dat", seenArray, "seenArray")
	else
		seenArray[curUser.sName] = os.date(curUser.sName.." connected at %d/%m/%Y %T and is still online.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." connected at %d/%m/%Y %T and is still online.")
		SaveToFile("Seen.dat", seenArray, "seenArray")
	end
end

function OpDisconnected(curUser)
	if seenArray == nil then
		seenArray = {}
		seenArray[curUser.sName] = {}
		seenArray[curUser.sIP] = {}
		seenArray[curUser.sName] = os.date(curUser.sName.." disconnected at %d/%m/%Y %T and has not been seen since.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." disconnected at %d/%m/%Y %T and has not been seen since.")
		SaveToFile("Seen.dat", seenArray, "seenArray")
	else
		seenArray[curUser.sName] = os.date(curUser.sName.." disconnected at %d/%m/%Y %T and has not been seen since.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." disconnected at %d/%m/%Y %T and has not been seen since.")
		SaveToFile("Seen.dat", seenArray, "seenArray")
	end
end

UserDisconnected = OpDisconnected

function OnError(ErrorMsg)
	SendToOps(ErrorMsg)
end

function ChatArrival(curUser, data)
	if seenArray == nil then
		seenArray = {}
		seenArray[curUser.sName] = {}
		seenArray[curUser.sIP] = {}
		seenArray[curUser.sName] = os.date(curUser.sName.." was last seen at %d/%m/%Y %T writing things.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." was last seen at %d/%m/%Y %T writing things.")
		SaveToFile("Seen.dat", seenArray, "seenArray")
	else
		seenArray[curUser.sName] = os.date(curUser.sName.." was last seen at %d/%m/%Y %T writing things.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." was last seen at %d/%m/%Y %T writing things.")
		SaveToFile("Seen.dat", seenArray, "seenArray")
	end
	local data = string.sub(data, 1, -2)
	local s,e,cmd = string.find(data, "%b<>%s+[%!%+%?%#](%S+)")
	if cmd then
		local tCmds = {
		["lastseen"] = function(curUser, data)
			local s,e,arg = string.find(data, "%b<>%s+%S+%s+(%S+)")
			if not arg then
				if HowToSend == 1 then
					SendToAll(BotName, "Syntax is: !" ..cmd.. " <nick/ip>")
				elseif HowToSend == 2 then
					curUser:SendData(BotName, "Syntax is: !" ..cmd.. " <nick/ip>")
				elseif HowToSend == 3 then
					curUser:SendPM(BotName, "Syntax is: !" ..cmd.. " <nick/ip>")
				end
				return 0
			end
			if (arg == curUser.sName) or (arg == curUser.sIP) then
				if HowToSend == 1 then
					SendToAll(BotName, "You should not do seen's for yourself " ..curUser.sName.. " ;)" )
				elseif HowToSend == 2 then
					curUser:SendData(BotName, "You should not do seen's for yourself " ..curUser.sName.. " ;)" )
				elseif HowToSend == 3 then
					curUser:SendPM(BotName, "You should not do seen's for yourself " ..curUser.sName.. " ;)" )
				end
				return 0
			end
			if seenArray[arg] == nil then
				if HowToSend == 1 then
					SendToAll(BotName, "I have not seen "..arg.."!" )
				elseif HowToSend == 2 then
					curUser:SendData(BotName, "I have not seen "..arg.."!" )
				elseif HowToSend == 3 then
					curUser:SendPM(BotName, "I have not seen "..arg.."!" )
				end
				return 0
			end
			if HowToSend == 1 then
				SendToAll(BotName, "User: "..seenArray[arg] )
			elseif HowToSend == 2 then
				curUser:SendData(BotName, "User: "..seenArray[arg] )
			elseif HowToSend == 3 then
				curUser:SendPM(BotName, "User: "..seenArray[arg] )
			end
			return 0
		end,
		}
		if tCmds[cmd] then
			return tCmds[cmd](curUser, data)
		end
	end
end

function Serialize(tTable, sTableName, sTab)
	assert(tTable, "tTable equals nil");
	assert(sTableName, "sTableName equals nil");
	assert(type(tTable) == "table", "tTable must be a table!");
	assert(type(sTableName) == "string", "sTableName must be a string!");
	sTab = sTab or "";
	sTmp = ""
	sTmp = sTmp..sTab..sTableName.." = {\n"
	for key, value in tTable do
		local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
		if(type(value) == "table") then
			sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
		else
			local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
			sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
		end
		sTmp = sTmp..",\n"
	end
	sTmp = sTmp..sTab.."}"
	return sTmp
end

function SaveToFile(file , table , tablename)
	local handle = io.open(file,"w+")
	handle:write(Serialize(table, tablename))
	handle:flush()
	handle:close()
end

function LoadFromFile(file)
	local handle = io.open(file,"r")
	if (handle ~= nil) then
		loadstring(handle:read("*all"))
		handle:flush()
		handle:close()
	end
end



6Marilyn6Manson6

Quote from: Creative on 01 May, 2007, 13:11:14
I have this Lastseen Script. I tried to convert in lua 5.1 using Mutors converter, but still it gave me lots of errors.
It works fine in ptokax 0.3.5.1 but doesnt works in 0.3.5.2

I am pasting the script below, please someone make this script work in 0.3.5.2.

Post error of scripts (you can see it in script editor from ptokax) and when you have it?

Creative

QuoteSyntax ...creative\Desktop\Hyper hub\scripts\last2.lua:18: module 'seen.dat' not found:
   no field package.preload['seen.dat']
   no file 'C:\Documents and Settings\creative\Desktop\Hyper hub\seen\dat.lua'
   no file 'C:\Documents and Settings\creativeDesktop\Hyper hub\libs\seen\dat.lua'
   no file 'C:\Documents and Settings\creative\Desktop\Hyper Hub\scripts\libs\seen\dat.lua'
   no file 'C:\Documents and Settings\creative\Desktop\Hyper Hub\seen\dat.dll'
   no file 'C:\Documents and Settings\creative\Desktop\Hyper Hub\libs\seen\dat.dll'
   no file 'C:\Documents and Settings\creative\Desktop\Hyper Hub\scripts\libs\seen\dat.dll'
   no file 'C:\Documents and Settings\creative\Desktop\Hyper Hub\seen.dll'
   no file 'C:\Documents and Settings\creative\Desktop\Hyper Hub\libs\seen.dll'
   no file 'C:\Documents and Settings\creative\Desktop\Hyper Hub\scripts\libs\seen.dll'

bastya_elvtars

Some suggestions:
- require() is for libraries, not for a table serialized to a file. dofile() is better.
- I keep failing to understand why one loads a proper lua file with loadstring(f_read("*a")). It's unnecessary, dofile() Just Works(TM).
Everything could have been anything else and it would have just as much meaning.

6Marilyn6Manson6

Change line 18:

require("seen.dat")


to

dofile("seen.dat")


after in scripts directory made file seen.dat and restart scripts. Try  ;D

bastya_elvtars

#5
Oh, and the fixed script:
Code: lua
-- Creator Leon
-- Started lua 5 conversion, damo
-- Fully converted to lua 5 by 6Marilyn6Manson6
---- Modded By Madman
-- Added save to file
-- Added HowToSend, request by Ubikk
-- Hidden the command, request by damo
-- Added OnError Msg
-- Rewrote the ChatArrival
-- Added postibilite so seen ips, Request by Ubikk
 
-- bastya_elvtars, 07-05-01:
--- Added a PROPER loading method at last - no require(), no loadstring()
-- Removed unused LoadFromFile() function
-- 5.1-compatible
 
BotName = "Bot"
 
minLen = 2
maxLen = 20
HowToSend = 2 -- 1: Send to all, 2: Send to user, 3: PM to the user
 
if loadfile("seen.dat") then dofile("seen.dat") end 
 
function Main()
	if HowToSend == 3 then
		frmHub:RegBot(BotName)
	end
end
 
function NewUserConnected(curUser)
	local boolDisc = 0
	if string.len(curUser.sName) < minLen then
		curUser:SendPM(BotName, "Your nick is to short, a minimum of "..minLen.." chars is required!")
		curUser:SendPM(BotName, "Disconnecting...")
		curUser:Disconnect()
		boolDisc = 1
	elseif string.len(curUser.sName) > maxLen then
		curUser:SendPM(BotName, "Your nick is to long, a maximum of "..maxLen.." chars is required!")
		curUser:SendPM(BotName, "Disconnecting...")
		curUser:Disconnect()
		boolDisc = 1
	elseif ( string.find (curUser.sName, " ", 1, 1) ) then
		curUser:SendPM(BotName, "Spaces not allowed in nicknames!")
		curUser:SendPM(BotName, "Disconnecting...")
		curUser:Disconnect()
		boolDisc = 1
	end
	if (boolDisc == 0) then
		if seenArray == nil then
			seenArray = {}
			seenArray[curUser.sName] = {}
			seenArray[curUser.sIP] = {}
			seenArray[curUser.sName] = os.date(curUser.sName.." connected at %d/%m/%Y %T and is still online.")
			seenArray[curUser.sIP] = os.date(curUser.sIP.." connected at %d/%m/%Y %T and is still online.")
			SaveToFile("Seen.dat", seenArray, "seenArray")
			SendToAll("Weee")
		else
			seenArray[curUser.sName] = os.date(curUser.sName.." connected at %d/%m/%Y %T and is still online.")
			seenArray[curUser.sIP] = os.date(curUser.sIP.." connected at %d/%m/%Y %T and is still online.")
			SaveToFile("Seen.dat", seenArray, "seenArray")
		end
	end
end
 
function OpConnected(curUser)
	if seenArray == nil then
		seenArray = {}
		seenArray[curUser.sName] = {}
		seenArray[curUser.sIP] = {}
		seenArray[curUser.sName] = os.date(curUser.sName.." connected at %d/%m/%Y %T and is still online.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." connected at %d/%m/%Y %T and is still online.")
		SaveToFile("Seen.dat", seenArray, "seenArray")
	else
		seenArray[curUser.sName] = os.date(curUser.sName.." connected at %d/%m/%Y %T and is still online.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." connected at %d/%m/%Y %T and is still online.")
		SaveToFile("Seen.dat", seenArray, "seenArray")
	end
end
 
function OpDisconnected(curUser)
	if seenArray == nil then
		seenArray = {}
		seenArray[curUser.sName] = {}
		seenArray[curUser.sIP] = {}
		seenArray[curUser.sName] = os.date(curUser.sName.." disconnected at %d/%m/%Y %T and has not been seen since.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." disconnected at %d/%m/%Y %T and has not been seen since.")
		SaveToFile("Seen.dat", seenArray, "seenArray")
	else
		seenArray[curUser.sName] = os.date(curUser.sName.." disconnected at %d/%m/%Y %T and has not been seen since.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." disconnected at %d/%m/%Y %T and has not been seen since.")
		SaveToFile("Seen.dat", seenArray, "seenArray")
	end
end
 
UserDisconnected = OpDisconnected
 
function OnError(ErrorMsg)
	SendToOps(ErrorMsg)
end
 
function ChatArrival(curUser, data)
	if seenArray == nil then
		seenArray = {}
		seenArray[curUser.sName] = {}
		seenArray[curUser.sIP] = {}
		seenArray[curUser.sName] = os.date(curUser.sName.." was last seen at %d/%m/%Y %T writing things.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." was last seen at %d/%m/%Y %T writing things.")
		SaveToFile("Seen.dat", seenArray, "seenArray")
	else
		seenArray[curUser.sName] = os.date(curUser.sName.." was last seen at %d/%m/%Y %T writing things.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." was last seen at %d/%m/%Y %T writing things.")
		SaveToFile("Seen.dat", seenArray, "seenArray")
	end
	local data = string.sub(data, 1, -2)
	local s,e,cmd = string.find(data, "%b<>%s+[%!%+%?%#](%S+)")
	if cmd then
		local tCmds = {
		["lastseen"] = function(curUser, data)
			local s,e,arg = string.find(data, "%b<>%s+%S+%s+(%S+)")
			if not arg then
				if HowToSend == 1 then
					SendToAll(BotName, "Syntax is: !" ..cmd.. " <nick/ip>")
				elseif HowToSend == 2 then
					curUser:SendData(BotName, "Syntax is: !" ..cmd.. " <nick/ip>")
				elseif HowToSend == 3 then
					curUser:SendPM(BotName, "Syntax is: !" ..cmd.. " <nick/ip>")
				end
				return 0
			end
			if (arg == curUser.sName) or (arg == curUser.sIP) then
				if HowToSend == 1 then
					SendToAll(BotName, "You should not do seen's for yourself " ..curUser.sName.. " ;)" )
				elseif HowToSend == 2 then
					curUser:SendData(BotName, "You should not do seen's for yourself " ..curUser.sName.. " ;)" )
				elseif HowToSend == 3 then
					curUser:SendPM(BotName, "You should not do seen's for yourself " ..curUser.sName.. " ;)" )
				end
				return 0
			end
			if seenArray[arg] == nil then
				if HowToSend == 1 then
					SendToAll(BotName, "I have not seen "..arg.."!" )
				elseif HowToSend == 2 then
					curUser:SendData(BotName, "I have not seen "..arg.."!" )
				elseif HowToSend == 3 then
					curUser:SendPM(BotName, "I have not seen "..arg.."!" )
				end
				return 0
			end
			if HowToSend == 1 then
				SendToAll(BotName, "User: "..seenArray[arg] )
			elseif HowToSend == 2 then
				curUser:SendData(BotName, "User: "..seenArray[arg] )
			elseif HowToSend == 3 then
				curUser:SendPM(BotName, "User: "..seenArray[arg] )
			end
			return 0
		end,
		}
		if tCmds[cmd] then
			return tCmds[cmd](curUser, data)
		end
	end
end
 
function Serialize(tTable, sTableName, sTab)
	assert(tTable, "tTable equals nil");
	assert(sTableName, "sTableName equals nil");
	assert(type(tTable) == "table", "tTable must be a table!");
	assert(type(sTableName) == "string", "sTableName must be a string!");
	sTab = sTab or "";
	sTmp = ""
	sTmp = sTmp..sTab..sTableName.." = {\n"
	for key, value in pairs(tTable) do
		local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
		if(type(value) == "table") then
			sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
		else
			local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
			sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
		end
		sTmp = sTmp..",\n"
	end
	sTmp = sTmp..sTab.."}"
	return sTmp
end
 
function SaveToFile(file , tbl , tablename)
	local handle = io.open(file,"w+")
	handle:write(Serialize(tbl, tablename))
	handle:flush()
	handle:close()
end
Everything could have been anything else and it would have just as much meaning.

Creative

Now im getting this error   :(

QuoteHyper hub\scripts\last2.lua:174: attempt to call a table value

6Marilyn6Manson6

Quote from: Creative on 01 May, 2007, 17:03:27
Now im getting this error   :(


you have this error with bastya_elvtars version?

bastya_elvtars

Everything could have been anything else and it would have just as much meaning.

6Marilyn6Manson6


Creative

Thank you bastya. Now it works fine :)

I would like to make some small changes in looks.

Now the result looks like,
Quote<BOT> User: adam disconnected at 02/05/2007 12:29:15 and has not been seen since.

but can u make the result to be seen as,
Quote<[Bot]> adam was last seen on Wednesday, May 02, 2007 15:53.

bastya_elvtars

That requires rewriting the date format and deleting the old seenfile.
Everything could have been anything else and it would have just as much meaning.

Creative

Hello bastya_elvtars,
                            Now the script is working fine. Can u make it case insensitive, for example if i want to check hubtime of user: DaeMON then even if i type +lastseen daemon it should show the result.

bastya_elvtars

Hey Creative,
I have rewritten this script into a less memory-consuming format and would like you to harvest that script, since I cannot find it. If you post it here, I'll solve the case sensitivity bug in no time.
Everything could have been anything else and it would have just as much meaning.

Creative

-- Creator Leon
-- Started lua 5 conversion, damo
-- Fully converted to lua 5 by 6Marilyn6Manson6
---- Modded By Madman
-- Added save to file
-- Added HowToSend, request by Ubikk
-- Hidden the command, request by damo
-- Added OnError Msg
-- Rewrote the ChatArrival
-- Added postibilite so seen ips, Request by Ubikk
 
-- bastya_elvtars, 07-05-01:
--- Added a PROPER loading method at last - no require(), no loadstring()
-- Removed unused LoadFromFile() function
-- 5.1-compatible

--[[
Again touched by bastya_elvtars, 07-05-07

  I really do not se the need for serializing such simple data structures, custom flatfile formats
make it possible to NOT loop on every user (dis-)connection. This is just stupid.
Serialisation is unnecessary in 95% of the cases, but it's indeed easier to copy/paste. :-P

Let's see what I've done:
- Files contain lines in the format: xxxx|yyyyy
- As they get opened, xxxx becomes the table key, yyyyy becomey the value.
- If new data gets added to the table, a metamethod saves it, but not by saving the
whole table, just appends the data to the end of the file. When the file gets fully parsed the next time,
always the last matching keys get back to the table. Thus, no frequent loops and lengthy disk writes occur,
but the script startup takes longer because of the larger file and the fact that the file gets compacted on
startup, meaning that the whole table is iterated and the file is saved properly. The other advantage of this
method is the 100% data persistence, in theory the chances for losing data are very low.
This idea is originating from plop
For further information, see http://wiki.ptokax.ath.cx
Note that I have not tidied up the code, just made it more resource-friendly.
Also added a file converter chunk, converts from the old format.
]]

BotName = "NYPD"
 
minLen = 2
maxLen = 20
HowToSend = 2 -- 1: Send to all, 2: Send to user, 3: PM to the user
 
seenArray,seenArray2={},{}
_seenArray=
  {
    __newindex=function(tbl,k,v) -- triggered when a new entry gets into the table,
      seenArray2[k]=v -- it is triggered always, since seenArray is always empty, we add items to seenArray2
      local f=io.open("seen.dat","a+") -- append the new entry to the file
      if f then
        f:write(k.."|"..v.."\n")
        f:close()
      end
    end
  }
  
if loadfile("seen.dat") then
  dofile("seen.dat")
  local f=io.open("seen.dat","w+")
  for k,v in pairs(seenArray) do
    f:write(k.."|"..v.."\n")
  end
  f:close()
  seenArray=nil
  collectgarbage("collect"); io.flush()
  seenArray={}
end

function Main()
  setmetatable(seenArray,_seenArray)
  local f=io.open("seen.dat","r")
  if f then
    for line in f:lines() do
      local nick_ip,jabber=string.match(line,"([^%|]+)%|(.+)")
      if nick_ip then
        seenArray2[nick_ip]=jabber
      end
    end
    f:close()
  end
  f=io.open("seen.dat","w+")
  for k,v in pairs(seenArray2) do
    f:write(k.."|"..v.."\n")
  end
  f:close()  
	if HowToSend == 3 then
		frmHub:RegBot(BotName)
	end
end
 
function NewUserConnected(curUser)
	local boolDisc = 0
	if string.len(curUser.sName) < minLen then
		curUser:SendPM(BotName, "Your nick is to short, a minimum of "..minLen.." chars is required!")
		curUser:SendPM(BotName, "Disconnecting...")
		curUser:Disconnect()
		boolDisc = 1
	elseif string.len(curUser.sName) > maxLen then
		curUser:SendPM(BotName, "Your nick is to long, a maximum of "..maxLen.." chars is required!")
		curUser:SendPM(BotName, "Disconnecting...")
		curUser:Disconnect()
		boolDisc = 1
	elseif ( string.find (curUser.sName, " ", 1, 1) ) then
		curUser:SendPM(BotName, "Spaces not allowed in nicknames!")
		curUser:SendPM(BotName, "Disconnecting...")
		curUser:Disconnect()
		boolDisc = 1
	end
	if (boolDisc == 0) then
		if seenArray == nil then
			seenArray = {}
			seenArray[curUser.sName] = os.date(curUser.sName.." is online.")
			seenArray[curUser.sIP] = os.date(curUser.sIP.." is online.")
-- 			SaveToFile("Seen.dat", seenArray, "seenArray")
-- 			SendToAll("Weee")
		else
			seenArray[curUser.sName] = os.date(curUser.sName.." is online.")
			seenArray[curUser.sIP] = os.date(curUser.sIP.." is online.")
-- 			SaveToFile("Seen.dat", seenArray, "seenArray")
		end
	end
end
 
function OpConnected(curUser)
	if seenArray == nil then
		seenArray = {}
		seenArray[curUser.sName] = os.date(curUser.sName.." is online.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." is online.")
-- 		SaveToFile("Seen.dat", seenArray, "seenArray")
	else
		seenArray[curUser.sName] = os.date(curUser.sName.." is online.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." is online.")
-- 		SaveToFile("Seen.dat", seenArray, "seenArray")
	end
end
 
function OpDisconnected(curUser)
	if seenArray == nil then
		seenArray = {}
		seenArray[curUser.sName] = os.date(curUser.sName.." was lastseen on %d/%m/%Y at %T .")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." was lastseen on %d/%m/%Y at %T .")
-- 		SaveToFile("Seen.dat", seenArray, "seenArray")
	else
		seenArray[curUser.sName] = os.date(curUser.sName.." was lastseen on %d/%m/%Y at %T .")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." was lastseen on %d/%m/%Y at %T .")
-- 		SaveToFile("Seen.dat", seenArray, "seenArray")
	end
end
 
UserDisconnected = OpDisconnected
 
function OnError(ErrorMsg)
	SendToOps(ErrorMsg)
end
 
function ChatArrival(curUser, data)
	if seenArray == nil then
		seenArray = {}
		seenArray[curUser.sName] = os.date(curUser.sName.." is online.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." is online.")
-- 		SaveToFile("Seen.dat", seenArray, "seenArray")
	else
		seenArray[curUser.sName] = os.date(curUser.sName.." is online.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." is online.")
-- 		SaveToFile("Seen.dat", seenArray, "seenArray")
	end
	local data = string.sub(data, 1, -2)
	local s,e,cmd = string.find(data, "%b<>%s+[%!%+%?%#](%S+)")
	if cmd then
		local tCmds = {
		["lastseen"] = function(curUser, data)
			local s,e,arg = string.find(data, "%b<>%s+%S+%s+(%S+)")
			if not arg then
				if HowToSend == 1 then
					SendToAll(BotName, "Syntax is: !" ..cmd.. " <nick/ip>")
				elseif HowToSend == 2 then
					curUser:SendData(BotName, "Syntax is: !" ..cmd.. " <nick/ip>")
				elseif HowToSend == 3 then
					curUser:SendPM(BotName, "Syntax is: !" ..cmd.. " <nick/ip>")
				end
				return 0
			end
			if (arg == curUser.sName) or (arg == curUser.sIP) then
				if HowToSend == 1 then
					SendToAll(BotName, "You should not do seen's for yourself " ..curUser.sName.. " ;)" )
				elseif HowToSend == 2 then
					curUser:SendData(BotName, "You should not do seen's for yourself " ..curUser.sName.. " ;)" )
				elseif HowToSend == 3 then
					curUser:SendPM(BotName, "You should not do seen's for yourself " ..curUser.sName.. " ;)" )
				end
				return 0
			end
			if seenArray2[arg] == nil then
				if HowToSend == 1 then
					SendToAll(BotName, "I have not seen "..arg.."!" )
				elseif HowToSend == 2 then
					curUser:SendData(BotName, "I have not seen "..arg.."!" )
				elseif HowToSend == 3 then
					curUser:SendPM(BotName, "I have not seen "..arg.."!" )
				end
				return 0
			end
			if HowToSend == 1 then
				SendToAll(BotName, "User: "..seenArray2[arg] )
			elseif HowToSend == 2 then
				curUser:SendData(BotName, "User: "..seenArray2[arg] )
			elseif HowToSend == 3 then
				curUser:SendPM(BotName, "User: "..seenArray2[arg] )
			end
			return 0
		end,
		}
		if tCmds[cmd] then
			return tCmds[cmd](curUser, data)
		end
	end
end
 
-- function Serialize(tTable, sTableName, sTab)
-- 	assert(tTable, "tTable equals nil");
-- 	assert(sTableName, "sTableName equals nil");
-- 	assert(type(tTable) == "table", "tTable must be a table!");
-- 	assert(type(sTableName) == "string", "sTableName must be a string!");
-- 	sTab = sTab or "";
-- 	sTmp = ""
-- 	sTmp = sTmp..sTab..sTableName.." = {\n"
-- 	for key, value in pairs(tTable) do
-- 		local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
-- 		if(type(value) == "table") then
-- 			sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
-- 		else
-- 			local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
-- 			sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
-- 		end
-- 		sTmp = sTmp..",\n"
-- 	end
-- 	sTmp = sTmp..sTab.."}"
-- 	return sTmp
-- end
--  
-- function SaveToFile(file , tbl , tablename)
-- 	local handle = io.open(file,"w+")
-- 	handle:write(Serialize(tbl, tablename))
-- 	handle:flush()
-- 	handle:close()
-- end

bastya_elvtars

#15
-- Creator Leon
-- Started lua 5 conversion, damo
-- Fully converted to lua 5 by 6Marilyn6Manson6
---- Modded By Madman
-- Added save to file
-- Added HowToSend, request by Ubikk
-- Hidden the command, request by damo
-- Added OnError Msg
-- Rewrote the ChatArrival
-- Added postibilite so seen ips, Request by Ubikk
 
-- bastya_elvtars, 07-05-01:
--- Added a PROPER loading method at last - no require(), no loadstring()
-- Removed unused LoadFromFile() function
-- 5.1-compatible

--[[
Again touched by bastya_elvtars, 07-05-07

  I really do not se the need for serializing such simple data structures, custom flatfile formats
make it possible to NOT loop on every user (dis-)connection. This is just stupid.
Serialisation is unnecessary in 95% of the cases, but it's indeed easier to copy/paste. :-P

Let's see what I've done:
- Files contain lines in the format: xxxx|yyyyy
- As they get opened, xxxx becomes the table key, yyyyy becomey the value.
- If new data gets added to the table, a metamethod saves it, but not by saving the
whole table, just appends the data to the end of the file. When the file gets fully parsed the next time,
always the last matching keys get back to the table. Thus, no frequent loops and lengthy disk writes occur,
but the script startup takes longer because of the larger file and the fact that the file gets compacted on
startup, meaning that the whole table is iterated and the file is saved properly. The other advantage of this
method is the 100% data persistence, in theory the chances for losing data are very low.
This idea is originating from plop
For further information, see http://wiki.ptokax.ath.cx
Note that I have not tidied up the code, just made it more resource-friendly.
Also added a file converter chunk, converts from the old format.
]]

BotName = "NYPD"
 
minLen = 2
maxLen = 20
HowToSend = 2 -- 1: Send to all, 2: Send to user, 3: PM to the user
 
seenArray,seenArray2={},{}
_seenArray=
  {
    __newindex=function(tbl,k,v) -- triggered when a new entry gets into the table,
      seenArray2[string.lower(k)]=v -- it is triggered always, since seenArray is always empty, we add items to seenArray2
      local f=io.open("seen.dat","a+") -- append the new entry to the file
      if f then
        f:write(string.lower(k).."|"..v.."\n")
        f:close()
      end
    end
  }
  
if loadfile("seen.dat") then
  dofile("seen.dat")
  local f=io.open("seen.dat","w+")
  for k,v in pairs(seenArray) do
    f:write(string.lower(k).."|"..v.."\n")
  end
  f:close()
  seenArray=nil
  collectgarbage("collect"); io.flush()
  seenArray={}
end

function Main()
  setmetatable(seenArray,_seenArray)
  local f=io.open("seen.dat","r")
  if f then
    for line in f:lines() do
      local nick_ip,jabber=string.match(line,"([^%|]+)%|(.+)")
      if nick_ip then
        seenArray2[nick_ip]=jabber
      end
    end
    f:close()
  end
  f=io.open("seen.dat","w+")
  for k,v in pairs(seenArray2) do
    f:write(string.lower(k).."|"..v.."\n")
  end
  f:close()  
	if HowToSend == 3 then
		frmHub:RegBot(BotName)
	end
end
 
function NewUserConnected(curUser)
	local boolDisc = 0
	if string.len(curUser.sName) < minLen then
		curUser:SendPM(BotName, "Your nick is to short, a minimum of "..minLen.." chars is required!")
		curUser:SendPM(BotName, "Disconnecting...")
		curUser:Disconnect()
		boolDisc = 1
	elseif string.len(curUser.sName) > maxLen then
		curUser:SendPM(BotName, "Your nick is to long, a maximum of "..maxLen.." chars is required!")
		curUser:SendPM(BotName, "Disconnecting...")
		curUser:Disconnect()
		boolDisc = 1
	elseif ( string.find (curUser.sName, " ", 1, 1) ) then
		curUser:SendPM(BotName, "Spaces not allowed in nicknames!")
		curUser:SendPM(BotName, "Disconnecting...")
		curUser:Disconnect()
		boolDisc = 1
	end
	if (boolDisc == 0) then
		if seenArray == nil then
			seenArray = {}
			seenArray[curUser.sName] = os.date(curUser.sName.." is online.")
			seenArray[curUser.sIP] = os.date(curUser.sIP.." is online.")
-- 			SaveToFile("Seen.dat", seenArray, "seenArray")
-- 			SendToAll("Weee")
		else
			seenArray[curUser.sName] = os.date(curUser.sName.." is online.")
			seenArray[curUser.sIP] = os.date(curUser.sIP.." is online.")
-- 			SaveToFile("Seen.dat", seenArray, "seenArray")
		end
	end
end
 
function OpConnected(curUser)
	if seenArray == nil then
		seenArray = {}
		seenArray[curUser.sName] = os.date(curUser.sName.." is online.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." is online.")
-- 		SaveToFile("Seen.dat", seenArray, "seenArray")
	else
		seenArray[curUser.sName] = os.date(curUser.sName.." is online.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." is online.")
-- 		SaveToFile("Seen.dat", seenArray, "seenArray")
	end
end
 
function OpDisconnected(curUser)
	if seenArray == nil then
		seenArray = {}
		seenArray[curUser.sName] = os.date(curUser.sName.." was lastseen on %d/%m/%Y at %T .")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." was lastseen on %d/%m/%Y at %T .")
-- 		SaveToFile("Seen.dat", seenArray, "seenArray")
	else
		seenArray[curUser.sName] = os.date(curUser.sName.." was lastseen on %d/%m/%Y at %T .")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." was lastseen on %d/%m/%Y at %T .")
-- 		SaveToFile("Seen.dat", seenArray, "seenArray")
	end
end
 
UserDisconnected = OpDisconnected
 
function OnError(ErrorMsg)
	SendToOps(ErrorMsg)
end
 
function ChatArrival(curUser, data)
	if seenArray == nil then
		seenArray = {}
		seenArray[curUser.sName] = os.date(curUser.sName.." is online.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." is online.")
-- 		SaveToFile("Seen.dat", seenArray, "seenArray")
	else
		seenArray[curUser.sName] = os.date(curUser.sName.." is online.")
		seenArray[curUser.sIP] = os.date(curUser.sIP.." is online.")
-- 		SaveToFile("Seen.dat", seenArray, "seenArray")
	end
	local data = string.sub(data, 1, -2)
	local s,e,cmd = string.find(data, "%b<>%s+[%!%+%?%#](%S+)")
	if cmd then
		local tCmds = {
		["lastseen"] = function(curUser, data)
			local s,e,arg = string.find(data, "%b<>%s+%S+%s+(%S+)")
			if not arg then
				if HowToSend == 1 then
					SendToAll(BotName, "Syntax is: !" ..cmd.. " <nick/ip>")
				elseif HowToSend == 2 then
					curUser:SendData(BotName, "Syntax is: !" ..cmd.. " <nick/ip>")
				elseif HowToSend == 3 then
					curUser:SendPM(BotName, "Syntax is: !" ..cmd.. " <nick/ip>")
				end
				return 0
			end
			if (arg == curUser.sName) or (arg == curUser.sIP) then
				if HowToSend == 1 then
					SendToAll(BotName, "You should not do seen's for yourself " ..curUser.sName.. " ;)" )
				elseif HowToSend == 2 then
					curUser:SendData(BotName, "You should not do seen's for yourself " ..curUser.sName.. " ;)" )
				elseif HowToSend == 3 then
					curUser:SendPM(BotName, "You should not do seen's for yourself " ..curUser.sName.. " ;)" )
				end
				return 0
			end
			if seenArray2[string.lower(arg)] == nil then
				if HowToSend == 1 then
					SendToAll(BotName, "I have not seen "..arg.."!" )
				elseif HowToSend == 2 then
					curUser:SendData(BotName, "I have not seen "..arg.."!" )
				elseif HowToSend == 3 then
					curUser:SendPM(BotName, "I have not seen "..arg.."!" )
				end
				return 0
			end
			if HowToSend == 1 then
				SendToAll(BotName, "User: "..seenArray2[string.lower(arg)] )
			elseif HowToSend == 2 then
				curUser:SendData(BotName, "User: "..seenArray2[string.lower(arg)] )
			elseif HowToSend == 3 then
				curUser:SendPM(BotName, "User: "..seenArray2[string.lower(arg)] )
			end
			return 0
		end,
		}
		if tCmds[string.lower(cmd)] then
			return tCmds[string.lower(cmd)](curUser, data)
		end
	end
end
Everything could have been anything else and it would have just as much meaning.

Leun

nice touche bastya_elvtars   ;D

I think this is a good tutorial for learing deal with metatables

Yahoo

sir can u make the script case insensitive for nick as suggested by creative
"BoRN FIGhTEr"

bastya_elvtars

Quote from: Yahoo on 18 May, 2007, 08:01:29
sir can u make the script case insensitive for nick as suggested by creative

It already is.
Everything could have been anything else and it would have just as much meaning.

Yahoo

sir not working
QuoteNYPD> The valmiki was last seen on Friday, May 18, 2007 11:29 .
[PoTtEr:16:40:40] <potter> +lastseen valmiki
[PoTtEr:16:40:47] *** Server command: +lastseen VALMIKI
[PoTtEr:16:40:48] <NYPD> I have not seen VALMIKI!
[PoTtEr:16:40:48] <potter> +lastseen VALMIKI
"BoRN FIGhTEr"

achiever

what i think case insensitivity means is using a command in both cases? i.e +lastseen and also +LASTSEEN or am i wrong?
thks,
achiever.

bastya_elvtars

Code updated, your issues have been addressed.
Everything could have been anything else and it would have just as much meaning.

Yahoo

thanks for the script working fine
"BoRN FIGhTEr"

Creative

Hello bastya...
                    Script is working fine... but sometimes it shows, "<HYPER> User: Nick is online." even if the user is not offline........
and this isnt happening only for 1 user, it shows user is online for many users....
can u solve the problem??

Yahoo

i am also facing the same problem :(
"BoRN FIGhTEr"

SMF spam blocked by CleanTalk