PtokaX forum

Archive => Archived 4.0 boards => Request for Lua 4 scripts => Topic started by: D-J Valhala on 24 April, 2004, 17:09:07

Title: a stand alone Away script?
Post by: D-J Valhala on 24 April, 2004, 17:09:07
can someone post me a stand alone Away script?
10x :D
Title:
Post by: Corayzon on 24 April, 2004, 17:38:42
sBot = "Away-Bot"

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Away\Back Settings
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Messages
awaymsg1 = "#u is now away"
awaymsg2 = "#u is now away because: #e"
backmsg = "#u is now back #e"
awaylistmsg = "#n#n#tA w a y   L i s t#n#n"
awaylistformat = "#t#t#u#t[#e]#n"

-- Commands
cmdaway = "away"
cmdawaylevel = 12
cmdback = "back"
cmdbacklevel = 12
cmdawaylist = "awaylist"
cmdawaylistlevel =12

function DataArrival(user, data)

data=strsub(data,1,strlen(data)-1)
_,_,cmd=strfind(data, "%b<>%s+(%S+)")
if cmd ~= nil then cmd = strlower(cmd) end

-- away.lua commands
------------------------------------------
if cmd == cmdaway or
cmd == cmdback or
cmd == cmdawaylist then
if doAwayCommand(user, data, cmd) then return 1 end
end
end

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- cSlave 0.3.0 :: away.lua :: writin by Corayzon :: Complete!
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function doAwayCommand(user, data, cmd)
if cmd == cmdaway then
setAway(user, data)
return 1

elseif cmd == cmdback then
setBack(user, data)
return 1

elseif cmd == cmdawaylist then
user:SendPM(sBot, getAwayList(user).."\r\n")
return 0
end
end

function setAway(user, data)
_,_,cmd,reason = strfind(data, "%b<>%s+(%S+)%s+(.+)")
if reason==nil then
reason = "none"
SendToAll(sBot, translateString(awaymsg1, user.sName, reason))
saveSetting(sysPath.."data/awaylist.txt", "<"..user.sName..">", "none", 1)
else
SendToAll(sBot, translateString(awaymsg2, user.sName, reason))
saveSetting(sysPath.."data/awaylist.txt", "<"..user.sName..">", reason, 1)
end
end

function setBack(user, data)
_,_,cmd,reason = strfind(data, "%b<>%s+(%S+)%s+(.+)")
if reason==nil then
if getSetting(sysPath.."data/awaylist.txt", "<"..user.sName..">") ~= nil then
SendToAll(sBot, translateString(backmsg, user.sName, ""))
deleteSetting(sysPath.."data/awaylist.txt", "<"..user.sName..">")
end
else
if getSetting(sysPath.."data/awaylist.txt", "<"..user.sName..">") ~= nil then
SendToAll(sBot, translateString(backmsg, user.sName, reason))
deleteSetting(sysPath.."data/awaylist.txt", "<"..user.sName..">")
end
end
end

function getAwayList(user)
local sOut = translateString(awaylistmsg, user.sName, setting)
local iCount = 1
local done = 1

while 1 do
sSetting = getSettingByLine(sysPath.."data/awaylist.txt", iCount)
if sSetting == nil then
break
else
local _,_, username, reason = strfind(sSetting, "<(%S+)>%s+(.*)")
sOut = sOut..translateString(awaylistformat, username, reason)
end
iCount = iCount + 1
end
return sOut
end

function getAwayReason(sUser)
local sData = getSetting(sysPath.."data/awaylist.txt", "<"..sUser..">")
if  sData ~= nil then
local _,_, username, reason = strfind(sData, "<(%S+)>%s+(.*)")
if reason ~= nil then
return reason
end
end
end


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- cSlave 0.3.0 :: system.lua :: writin by Corayzon :: Complete!
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function translateString(sString, sUserName, e, x, r, a, s1, s2, s3, s4, s5, s6, s7, s8)

-- Hub Info Strings
if strfind(sString, "#hd") then sString, _ = gsub(sString, "#hd",frmHub:GetHubDescr()) end
if strfind(sString, "#uc") then sString, _ = gsub(sString, "#uc", frmHub:GetUsersCount()) end
if strfind(sString, "#hb") then sString, _ = gsub(sString, "#hb", frmHub:GetHubBotName()) end
if strfind(sString, "#mu") then sString, _ = gsub(sString, "#mu", frmHub:GetMaxUsers()) end
if strfind(sString, "#ms") then sString, _ = gsub(sString, "#ms", frmHub:GetMinShare()) end
if strfind(sString, "#op") then sString, _ = gsub(sString, "#op", frmHub:GetOpChatName()) end
if strfind(sString, "#ra") then sString, _ = gsub(sString, "#ra", frmHub:GetRedirectAddress()) end
if strfind(sString, "#hs") then sString, _ = gsub(sString, "#hs", frmHub:GetCurrentShareAmount()) end
if strfind(sString, "#h") then sString, _ = gsub(sString, "#h", frmHub:GetHubName()) end

-- System Strings
if strfind(sString, "#n") then sString, _ = gsub(sString, "#n", "\r\n") end
if strfind(sString, "#t") then sString, _ = gsub(sString, "#t", "\t") end

-- Input Strings
sString, _ = gsub(sString, "#u", sUserName)
if e ~= nil then sString, _ = gsub(sString, "#e", e) end
if x ~= nil then sString, _ = gsub(sString, "#x", x) end
if r ~= nil then sString, _ = gsub(sString, "#r", r) end
if a ~= nil then sString, _ = gsub(sString, "#a", a) end
if s1 ~= nil then sString, _ = gsub(sString, "#1", s1) end
if s2 ~= nil then sString, _ = gsub(sString, "#2", s2) end
if s3 ~= nil then sString, _ = gsub(sString, "#3", s3) end
if s4 ~= nil then sString, _ = gsub(sString, "#4", s4) end
if s5 ~= nil then sString, _ = gsub(sString, "#5", s5) end
if s6 ~= nil then sString, _ = gsub(sString, "#6", s6) end
if s7 ~= nil then sString, _ = gsub(sString, "#7", s7) end
if s8 ~= nil then sString, _ = gsub(sString, "#8", s8) end
return sString
end
Title:
Post by: D-J Valhala on 24 April, 2004, 17:54:27
thank you man i knew that you help me :)))
if you need somthing ANYTHING ask and i will help you as much as i can !!! :)
the script work fine but back is not working,,,
Syntax Error: attempt to concat global `sysPath' (a nil value)
:\
sorry to bother you m8
Title:
Post by: Corayzon on 24 April, 2004, 18:07:03
add the following string to the top of the script

sysPath = ""

and in the scripts folder make a folder called data and inside this folder make a text file called 'awaylist.txt'

this should solve the problem dude ;)
Title:
Post by: D-J Valhala on 24 April, 2004, 20:04:21
Syntax Error: attempt to call global `saveSetting' (a nil value)
Syntax Error: attempt to call global `getSetting' (a nil value)
Title:
Post by: Corayzon on 25 April, 2004, 08:15:27
Add this to cSlave.lua

-- cSlave 0.3.0 :: database.lua :: writin by Corayzon
-------------------------------------------------------------------------------------
function getSetting(sFile, sSettingName)
readfrom(sFile)
while 1 do
local sLine = read()
if sLine == nil or sLine == "" then
readfrom()
return nil
else
if strsub(sLine,1, strlen(sSettingName)) == sSettingName then
readfrom()
return sLine
end
end
end
end

function getSettingByLine(sFile, iLineNumber)
local iCount = 0
readfrom(sFile)
while iCount < iLineNumber do
sLine = read()
iCount = iCount + 1
if sLine == nil or sLine == "" then
readfrom()
return nil
else
if iCount == iLineNumber then
readfrom()
return sLine
end
end
end
end

function saveSetting(sFile, sSettingName, sSetting, bOverwrite)
if bOverwrite == 1 then
replaceSetting(sFile, sSettingName, sSetting)
return 1
end
if getSetting(sFile, sSettingName) then
return
end
appendto(sFile)
write(sSettingName.." "..sSetting.."\r\n")
writeto()
return 1
end

function replaceSetting(sFile, sSettingName, sSetting)
readfrom(sFile)
local sOut = ""
local saved = 0
while 1 do
local sLine = read()
if sLine ~= nil or sLine == "" then
if strlower(strsub(sLine,1, strlen(sSettingName))) == strlower(sSettingName) then
if saved ~= 1 then
sOut = sOut .. sSettingName .. " " .. sSetting .. "\r\n"
saved = 1
end
else
sOut = sOut .. sLine .. "\r\n"
end
else
if saved ~= 1 then sOut = sOut .. sSettingName .. " " .. sSetting .. "\r\n" end
readfrom()
writeto(sFile)
write(sOut)
writeto()
return 1
end
end
end

function deleteSetting(sFile, sSettingName)
readfrom(sFile)
local sOut = ""
while 1 do
local sLine = read()
if sLine ~= nil or sLine == "" then
if strsub(sLine,1, strlen(sSettingName)) ~= sSettingName then
sOut = sOut..sLine.."\r\n"
end
else
readfrom()
writeto(sFile)
write (sOut)
writeto()
return 1
end
end
end

function getSettingsCount(sFile)
local iCount = 0
readfrom(sFile)
while 1 do
local sLine = read()
iCount = iCount + 1
if sLine == nil or line == "" then
readfrom()
return iCount - 1
end
end
end

function getSettingName(sFile, sSetting)
readfrom(sFile)
while 1 do
local sLine = read()
if sLine == nil or sLine == "" then
readfrom()
return nil
else
local _,_,sSettingName, style, release = strfind(sLine, "(%S+)%s+(%S+)%s+(.*)")
if strlower(sSetting) == strlower(release) then
readfrom()
return sSettingName
end
end
end
end
Title:
Post by: D-J Valhala on 29 April, 2004, 23:48:28
can you please post the finished script all of it?
10x
Title:
Post by: Corayzon on 30 April, 2004, 03:52:28
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Away\Back Settings
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Messages
awaymsg1 = "#u is now away"
awaymsg2 = "#u is now away because: #e"
backmsg = "#u is now back #e"
awaylistmsg = "#n#n#tA w a y   L i s t#n#n"
awaylistformat = "#t#t#u#t[#e]#n"

-- Commands
cmdaway = "away"
cmdback = "back"
cmdawaylist = "awaylist"

function DataArrival(user, data)

data=strsub(data,1,strlen(data)-1)
_,_,cmd=strfind(data, "%b<>%s+(%S+)")
if cmd ~= nil then cmd = strlower(cmd) end

-- away.lua commands
------------------------------------------
if cmd == cmdaway or
cmd == cmdback or
cmd == cmdawaylist then
if doAwayCommand(user, data, cmd) then return 1 end
end
end

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- cSlave 0.3.0 :: away.lua :: writin by Corayzon :: Complete!
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function doAwayCommand(user, data, cmd)
if cmd == cmdaway then
setAway(user, data)
return 1

elseif cmd == cmdback then
setBack(user, data)
return 1

elseif cmd == cmdawaylist then
user:SendPM(sBot, getAwayList(user).."\r\n")
return 0
end
end

function setAway(user, data)
_,_,cmd,reason = strfind(data, "%b<>%s+(%S+)%s+(.+)")
if reason==nil then
reason = "none"
SendToAll(sBot, translateString(awaymsg1, user.sName, reason))
saveSetting("data/awaylist.txt", "<"..user.sName..">", "none", 1)
else
SendToAll(sBot, translateString(awaymsg2, user.sName, reason))
saveSetting("data/awaylist.txt", "<"..user.sName..">", reason, 1)
end
end

function setBack(user, data)
_,_,cmd,reason = strfind(data, "%b<>%s+(%S+)%s+(.+)")
if reason==nil then
if getSetting(sysPath.."data/awaylist.txt", "<"..user.sName..">") ~= nil then
SendToAll(sBot, translateString(backmsg, user.sName, ""))
deleteSetting(sysPath.."data/awaylist.txt", "<"..user.sName..">")
end
else
if getSetting(sysPath.."data/awaylist.txt", "<" .. user.sName .. ">") ~= nil then
SendToAll(sBot, translateString(backmsg, user.sName, reason))
deleteSetting("data/awaylist.txt", "<" .. user.sName .. ">")
end
end
end

function getAwayList(user)
local sOut = translateString(awaylistmsg, user.sName, setting)
local iCount = 1
local done = 1

while 1 do
sSetting = getSettingByLine("data/awaylist.txt", iCount)
if sSetting == nil then
break
else
local _,_, username, reason = strfind(sSetting, "<(%S+)>%s+(.*)")
sOut = sOut..translateString(awaylistformat, username, reason)
end
iCount = iCount + 1
end
return sOut
end

function getAwayReason(sUser)
local sData = getSetting("data/awaylist.txt", "<"..sUser..">")
if  sData ~= nil then
local _,_, username, reason = strfind(sData, "<(%S+)>%s+(.*)")
if reason ~= nil then
return reason
end
end
end

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- cSlave 0.3.0 :: system.lua :: writin by Corayzon :: Complete!
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function translateString(sString, sUserName, e, x, r, a, s1, s2, s3, s4, s5, s6, s7, s8)

-- Hub Info Strings
if strfind(sString, "#hd") then sString, _ = gsub(sString, "#hd",frmHub:GetHubDescr()) end
if strfind(sString, "#uc") then sString, _ = gsub(sString, "#uc", frmHub:GetUsersCount()) end
if strfind(sString, "#hb") then sString, _ = gsub(sString, "#hb", frmHub:GetHubBotName()) end
if strfind(sString, "#mu") then sString, _ = gsub(sString, "#mu", frmHub:GetMaxUsers()) end
if strfind(sString, "#ms") then sString, _ = gsub(sString, "#ms", frmHub:GetMinShare()) end
if strfind(sString, "#op") then sString, _ = gsub(sString, "#op", frmHub:GetOpChatName()) end
if strfind(sString, "#ra") then sString, _ = gsub(sString, "#ra", frmHub:GetRedirectAddress()) end
if strfind(sString, "#hs") then sString, _ = gsub(sString, "#hs", frmHub:GetCurrentShareAmount()) end
if strfind(sString, "#h") then sString, _ = gsub(sString, "#h", frmHub:GetHubName()) end

-- System Strings
if strfind(sString, "#n") then sString, _ = gsub(sString, "#n", "\r\n") end
if strfind(sString, "#t") then sString, _ = gsub(sString, "#t", "\t") end

-- Input Strings
sString, _ = gsub(sString, "#u", sUserName)
if e ~= nil then sString, _ = gsub(sString, "#e", e) end
if x ~= nil then sString, _ = gsub(sString, "#x", x) end
if r ~= nil then sString, _ = gsub(sString, "#r", r) end
if a ~= nil then sString, _ = gsub(sString, "#a", a) end
if s1 ~= nil then sString, _ = gsub(sString, "#1", s1) end
if s2 ~= nil then sString, _ = gsub(sString, "#2", s2) end
if s3 ~= nil then sString, _ = gsub(sString, "#3", s3) end
if s4 ~= nil then sString, _ = gsub(sString, "#4", s4) end
if s5 ~= nil then sString, _ = gsub(sString, "#5", s5) end
if s6 ~= nil then sString, _ = gsub(sString, "#6", s6) end
if s7 ~= nil then sString, _ = gsub(sString, "#7", s7) end
if s8 ~= nil then sString, _ = gsub(sString, "#8", s8) end
return sString
end

----------------------------------------------------------------------------------
 cSlave 0.3.0 :: database.lua :: writin by Corayzon
-------------------------------------------------------------------------------------
function getSetting(sFile, sSettingName)
readfrom(sFile)
while 1 do
local sLine = read()
if sLine == nil or sLine == "" then
readfrom()
return nil
else
if strsub(sLine,1, strlen(sSettingName)) == sSettingName then
readfrom()
return sLine
end
end
end
end

function getSettingByLine(sFile, iLineNumber)
local iCount = 0
readfrom(sFile)
while iCount < iLineNumber do
sLine = read()
iCount = iCount + 1
if sLine == nil or sLine == "" then
readfrom()
return nil
else
if iCount == iLineNumber then
readfrom()
return sLine
end
end
end
end

function saveSetting(sFile, sSettingName, sSetting, bOverwrite)
if bOverwrite == 1 then
replaceSetting(sFile, sSettingName, sSetting)
return 1
end
if getSetting(sFile, sSettingName) then
return
end
appendto(sFile)
write(sSettingName.." "..sSetting.."\r\n")
writeto()
return 1
end

function replaceSetting(sFile, sSettingName, sSetting)
readfrom(sFile)
local sOut = ""
local saved = 0
while 1 do
local sLine = read()
if sLine ~= nil or sLine == "" then
if strlower(strsub(sLine,1, strlen(sSettingName))) == strlower(sSettingName) then
if saved ~= 1 then
sOut = sOut .. sSettingName .. " " .. sSetting .. "\r\n"
saved = 1
end
else
sOut = sOut .. sLine .. "\r\n"
end
else
if saved ~= 1 then sOut = sOut .. sSettingName .. " " .. sSetting .. "\r\n" end
readfrom()
writeto(sFile)
write(sOut)
writeto()
return 1
end
end
end

function deleteSetting(sFile, sSettingName)
readfrom(sFile)
local sOut = ""
while 1 do
local sLine = read()
if sLine ~= nil or sLine == "" then
if strsub(sLine,1, strlen(sSettingName)) ~= sSettingName then
sOut = sOut..sLine.."\r\n"
end
else
readfrom()
writeto(sFile)
write (sOut)
writeto()
return 1
end
end
end

function getSettingsCount(sFile)
local iCount = 0
readfrom(sFile)
while 1 do
local sLine = read()
iCount = iCount + 1
if sLine == nil or line == "" then
readfrom()
return iCount - 1
end
end
end

function getSettingName(sFile, sSetting)
readfrom(sFile)
while 1 do
local sLine = read()
if sLine == nil or sLine == "" then
readfrom()
return nil
else
local _,_,sSettingName, style, release = strfind(sLine, "(%S+)%s+(%S+)%s+(.*)")
if strlower(sSetting) == strlower(release) then
readfrom()
return sSettingName
end
end
end
end
Title:
Post by: Corayzon on 30 April, 2004, 03:54:49
Also...

Make a folder called data in the ptokax folder and make a text file called awaylist.txt in it ;)
Title:
Post by: D-J Valhala on 30 April, 2004, 09:32:43
Syntax Error: attempt to concat global `sysPath' (a nil value)
Syntax Error: attempt to concat global `sysPath' (a nil value)
Title:
Post by: Corayzon on 30 April, 2004, 14:49:50
function setAway(user, data)
_,_,cmd,reason = strfind(data, "%b<>%s+(%S+)%s+(.+)")
if reason==nil then
reason = "none"
SendToAll(sBot, translateString(awaymsg1, user.sName, reason))
saveSetting(sysPath.."data/awaylist.txt", "<"..user.sName..">", "none", 1)
else
SendToAll(sBot, translateString(awaymsg2, user.sName, reason))
saveSetting("data/awaylist.txt", "<"..user.sName..">", reason, 1)
end
end

saveSetting(sysPath.."data/awaylist.txt", "<"..user.sName..">", "none", 1) is ment to be saveSetting("data/awaylist.txt", "<"..user.sName..">", "none", 1)

code in above post is fixed!
Title:
Post by: nErBoS on 01 May, 2004, 02:06:16
Hi,

Here is the script, but please don?t ask for scripts in PM use the forum, thats is objective...

--Requested by D-J Valhala
--Made by nErBoS

sBot = "Away-Bot"

away = {}
awaysv = "away.dat"
changed = 0

function Main()
frmHub:RegBot(sBot)
end

function OnExit()
Refresh()
SaveToFile(awaysv , away , "away")
end

function DataArrival(user, data)
if (strlen(data,1,1) == "<" or strlen(data,1,5+strlen(sBot)) == "$To: "..sBot) then
data = strsub(dat,1,strlen(data)-1)
s,e,cmd = strfind(data, "%b<>%s+(%S+)")
if (cmd == "+away") then
Refresh()
if (away[user.sName] ~= nil) then
user:SendPM(sBot, "You are already in away.")
else
local s,e,msg = strfind(data, "%b<>%s+%S+%s+(.*)")
away[user.sName] = msg
SendToAll("** The User "..user.sName.." is now away: \""..msg.."\"")
end
return 1
elseif (cmd == "+back") then
Refresh()
if (away[user.sName] == nil) then
user:SendPM(sBot, "You aren?t away.")
else
SendToAll("** The User "..user.sName.." is back from away: \""..away[user.sName].."\"")
away[user.sName] = nil
end
return 1
end
end
end

function Refresh()
if (changed == 0 and readfrom(awaysv) ~= nil) then
LoadFromFile(awaysv)
changed = 1
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 format("[%q]",key) or format("[%d]",key);

if(type(value) == "table") then
Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and 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)
writeto(file)
write(Serialize(table, tablename))
writeto()
end

function LoadFromFile(file)
if (readfrom(file) ~= nil) then
readfrom(file)
dostring(read("*all"))
readfrom()
end
end

Best regards, nErBoS