PtokaX forum

Archive => Archived 5.1 boards => Help with scripts => Topic started by: Creative on 01 May, 2007, 13:11:14

Title: Seen script
Post by: 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.

-- 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


Title: Re: Seen script
Post by: 6Marilyn6Manson6 on 01 May, 2007, 14:47:28
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?
Title: Re: Seen script
Post by: Creative on 01 May, 2007, 15:08:12
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'
Title: Re: Seen script
Post by: bastya_elvtars on 01 May, 2007, 15:16:53
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).
Title: Re: Seen script
Post by: 6Marilyn6Manson6 on 01 May, 2007, 15:18:27
Change line 18:

require("seen.dat")

to

dofile("seen.dat")

after in scripts directory made file seen.dat and restart scripts. Try  ;D
Title: Re: Seen script
Post by: bastya_elvtars on 01 May, 2007, 15:20:40
Oh, and the fixed script:
Code (lua) Select
-- 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
Title: Re: Seen script
Post by: Creative on 01 May, 2007, 17:03:27
Now im getting this error   :(

QuoteHyper hub\scripts\last2.lua:174: attempt to call a table value
Title: Re: Seen script
Post by: 6Marilyn6Manson6 on 01 May, 2007, 17:06:39
Quote from: Creative on 01 May, 2007, 17:03:27
Now im getting this error   :(


you have this error with bastya_elvtars version?
Title: Re: Seen script
Post by: bastya_elvtars on 01 May, 2007, 17:09:44
Fixed.
Title: Re: Seen script
Post by: 6Marilyn6Manson6 on 01 May, 2007, 17:15:52
Quote from: bastya_elvtars on 01 May, 2007, 17:09:44
Fixed.

Very nice bastya ;)
Title: Re: Seen script
Post by: Creative on 02 May, 2007, 09:17:49
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.
Title: Re: Seen script
Post by: bastya_elvtars on 02 May, 2007, 11:00:57
That requires rewriting the date format and deleting the old seenfile.
Title: Re: Seen script
Post by: Creative on 16 May, 2007, 10:26:03
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.
Title: Re: Seen script
Post by: bastya_elvtars on 16 May, 2007, 12:51:38
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.
Title: Re: Seen script
Post by: Creative on 16 May, 2007, 14:57:11
-- 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
Title: Re: Seen script
Post by: bastya_elvtars on 16 May, 2007, 15:05:04
-- 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
Title: Re: Seen script
Post by: Leun on 17 May, 2007, 11:52:31
nice touche bastya_elvtars   ;D

I think this is a good tutorial for learing deal with metatables
Title: Re: Seen script
Post by: Yahoo on 18 May, 2007, 08:01:29
sir can u make the script case insensitive for nick as suggested by creative
Title: Re: Seen script
Post by: bastya_elvtars on 18 May, 2007, 12:57:21
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.
Title: Re: Seen script
Post by: Yahoo on 18 May, 2007, 13:18:27
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
Title: Re: Seen script
Post by: achiever on 18 May, 2007, 13:42:22
what i think case insensitivity means is using a command in both cases? i.e +lastseen and also +LASTSEEN or am i wrong?
Title: Re: Seen script
Post by: bastya_elvtars on 18 May, 2007, 13:54:02
Code updated, your issues have been addressed.
Title: Re: Seen script
Post by: Yahoo on 18 May, 2007, 14:57:33
thanks for the script working fine
Title: Re: Seen script
Post by: Creative on 12 July, 2007, 07:34:04
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??
Title: Re: Seen script
Post by: Yahoo on 12 July, 2007, 19:40:43
i am also facing the same problem :(
Title: Re: Seen script
Post by: bastya_elvtars on 12 July, 2007, 21:30:13
I'll look into the issue.