PtokaX forum

Lua 5.3/5.2/5.1 Scripts (for PtokaX 0.4.0.0 and newer) => Finished Scripts => Topic started by: ATAG on 02 September, 2008, 15:57:44

Title: Configuration Saver [API 2]
Post by: ATAG on 02 September, 2008, 15:57:44
PtokaX often forget to save changed configuration so here is the cure :)

Saves in evrey 5 minutes or you can use !save command (in main or PM to hub bot).

-- automatic configuration saver
-- tested on PtokaX 0.4.1.1
-- v 0.2
-- by ATAG
-- 2008.09.02.
-- idea by 11hh

function OnStartup()
tmr = TmrMan.AddTimer(300*1000)

-- create backup from Profiles.xml
local cur = io.open(Core.GetPtokaXPath().."cfg/Profiles.xml")
if not cur then return end

local back = io.open(Core.GetPtokaXPath().."cfg/Profiles.bak","w+")
if back then
back:write(cur:read("*a"))
back:flush()
back:close()
end
cur:close()
end

function OnTimer(tmr)
SetMan.Save()
RegMan.Save()
BanMan.Save()
ScriptMan.Refresh()
  SaveProfiles()
end

function ChatArrival(tUser,sData)
if not Core.GetUserValue(tUser,11) then return false end
if not sData:find("%b<> %psave|") then return false end
OnTimer()
Core.SendToUser(tUser,"<"..SetMan.GetHubBot().sNick.."> Settings saved. OK|")
return true
end

function ToArrival(tUser,sData)
if not Core.GetUserValue(tUser,11) then return false end
local _,_,to = sData:find("%$To: (.-) From:.-%$%b<> %psave|")
if not to then return false end

if to == SetMan.GetHubBot().sNick then
OnTimer()
Core.SendPmToUser(tUser,SetMan.GetHubBot().sNick, "Settings saved. OK|")
return true
end
end

function SaveProfiles()
local f = io.open( Core.GetPtokaXPath().."cfg/Profiles.xml","w+" )

local buf = {'<?xml version="1.0" encoding="windows-1252" standalone="yes" ?>','<Profiles>'}

local profiles = ProfMan.GetProfiles()
for j in ipairs(profiles) do

table.insert(buf,'\t<Profile>')
table.insert(buf,'\t\t<Name>'..profiles[j].sProfileName..'</Name>')


local profilenumber = profiles[j].iProfileNumber
local permissions = ""

for i = 0, 55 do
permissions = permissions.. (ProfMan.GetProfilePermission(profilenumber,i) and '1' or '0')
end

permissions = permissions..string.rep('0', 200) -- 255 - 55


table.insert(buf,'\t\t<Permissions>'..permissions..'</Permissions>')
table.insert(buf,'\t</Profile>')

end
table.insert(buf,'</Profiles>')

f:write(table.concat(buf,"\n")..'\n')
f:flush()
f:close()
end



PS: Thx for report and idea to 11hh :)
Title: Re: Configuration Saver [API 2]
Post by: 11hh on 02 September, 2008, 17:58:19
Very good this script, congratulate ! (http://www.hallmark.com/wcsstore/HallmarkStore/images/products/ecards/nfg2687.swf)
I recommend it to everybody !  :)
Title: Re: Configuration Saver [API 2]
Post by: speedX on 09 December, 2008, 06:50:10
Very nice script. Thanx ATAG
Title: Re: Configuration Saver [API 2]
Post by: ?[-?Genius?-]? on 22 February, 2009, 08:51:59
Hello Would it be possible that the bot is sending information in a PM to the operators where the settings are saved?

I like the idea of the script: D

Sory my bad inglish
Title: Re: Configuration Saver [API 2]
Post by: ATAG on 22 February, 2009, 13:15:38
Settings will be saved mostly by PtokaX, you can find all configuration files in the cfg folder. There's no special backup location to report.
Title: Re: Configuration Saver [API 2]
Post by: ?[-?Genius?-]? on 22 February, 2009, 19:27:05
Ok m8.

Tankx for fast reply.
Title: Re: Configuration Saver [API 2]
Post by: Dreams on 17 March, 2010, 16:07:01
My thought about this is, what happens if the ptokax craches and reboots, cause the script saves on script start, and if some regs have been lost on that crash, they will be lost anyway if this backup script saves on script start, Or maybe not, may differ on the situation, but i would feel much safer with the backup if you could remove save on script start. =) or a on/off switch on that...
Title: Re: Configuration Saver [API 2]
Post by: TiMeTrAVelleR on 17 March, 2010, 18:10:12
Saves in evrey 5 minutes or you can use !save command (in main or PM to hub bot).
Title: Re: Configuration Saver [API 2]
Post by: Dreams on 19 March, 2010, 09:41:05
Yes i know =) but it saves on start too, and that is what im triying to say that it would be nice if that could be removed.
Title: Re: Configuration Saver [API 2]
Post by: bastya_elvtars on 20 March, 2010, 23:53:54
Untested...

-- automatic configuration saver
-- tested on PtokaX 0.4.1.1
-- v 0.2
-- by ATAG
-- 2008.09.02.
-- idea by 11hh

-- Save on startup? (true/false)
SaveonStartup = true

function OnStartup()
tmr = TmrMan.AddTimer(300*1000)

-- create backup from Profiles.xml
local cur = io.open(Core.GetPtokaXPath().."cfg/Profiles.xml")
if not cur then return end
if SaveonStartup then
  local back = io.open(Core.GetPtokaXPath().."cfg/Profiles.bak","w+")
  if back then
  back:write(cur:read("*a"))
  back:flush()
  back:close()
  end
       end
cur:close()
end

function OnTimer(tmr)
SetMan.Save()
RegMan.Save()
BanMan.Save()
       SaveProfiles()
end

function ChatArrival(tUser,sData)
if not Core.GetUserValue(tUser,11) then return false end
if not sData:find("%b<> %psave|") then return false end
OnTimer()
Core.SendToUser(tUser,"<"..SetMan.GetHubBot().sNick.."> Settings saved. OK|")
return true
end

function ToArrival(tUser,sData)
if not Core.GetUserValue(tUser,11) then return false end
local _,_,to = sData:find("%$To: (.-) From:.-%$%b<> %psave|")
if not to then return false end

if to == SetMan.GetHubBot().sNick then
OnTimer()
Core.SendPmToUser(tUser,SetMan.GetHubBot().sNick, "Settings saved. OK|")
return true
end
end

function SaveProfiles()
local f = io.open( Core.GetPtokaXPath().."cfg/Profiles.xml","w+" )

local buf = {'<?xml version="1.0" encoding="windows-1252" standalone="yes" ?>','<Profiles>'}

local profiles = ProfMan.GetProfiles()
for j in ipairs(profiles) do

table.insert(buf,'\t<Profile>')
table.insert(buf,'\t\t<Name>'..profiles[j].sProfileName..'</Name>')


local profilenumber = profiles[j].iProfileNumber
local permissions = ""

for i = 0, 55 do
permissions = permissions.. (ProfMan.GetProfilePermission(profilenumber,i) and '1' or '0')
end

permissions = permissions..string.rep('0', 200) -- 255 - 55


table.insert(buf,'\t\t<Permissions>'..permissions..'</Permissions>')
table.insert(buf,'\t</Profile>')

end
table.insert(buf,'</Profiles>')

f:write(table.concat(buf,"\n")..'\n')
f:flush()
f:close()
end
Title: Re: Configuration Saver [API 2]
Post by: Dreams on 23 March, 2010, 13:36:41
It doesnt save at script start, that is good =)
and it doesnt save at all, so thats not good.

So that is the thing that has to be fixed now.

Regards
Title: Re: Configuration Saver [API 2]
Post by: bastya_elvtars on 23 March, 2010, 17:53:08
Edited my above post, try again. If it does not work, I'll rewrite it later this evening.
BTW I have not done any changes that coul make the saving every X minutes stop. Are you sure that worked before?
Title: Re: Configuration Saver [API 2]
Post by: Dreams on 23 March, 2010, 20:43:16
Im sure it worked before, i even did set it to save every second and it didnt save.
But when i changed to true on scrupt start it made a backup.
I will try the updated script in the mornin, dont have the time right now.
But thanks for the reply and changes

edit: some minutes later...

Okay so i couldnt keep myself from trying this script, and this is what i could come up with

When setting the Save on startup to true, the script saves on start but nothing more then that.
When setting the Save on startup to false, the script doesnt save on startup and nothing more either.

Regards// Dreams
Title: Re: Configuration Saver [API 2]
Post by: bastya_elvtars on 23 March, 2010, 21:09:59
Wait. The script does make that .bak file _only_ on startup. Otherwise it writes right into the xml files, of which you ought to check the timestamp regularly.
Title: Re: Configuration Saver [API 2]
Post by: Dreams on 23 March, 2010, 23:14:13
So what you are sayin is that when Save on startup is set to False, it creates the backup right into the RegisteredUsers.xml every 5 minutes?
And there aint a way to make a .bak file if Save on startup isnt set to true? or am i getting it wrong?
Title: Re: Configuration Saver [API 2]
Post by: bastya_elvtars on 24 March, 2010, 16:53:41
Save on startup backs up Profiles.xml. The 5-minute backup thing overwrites the existing XMLs. So you won't see a .bak when you have the thing set to false, but the profiles will be saved. The .bak is just a safety solution, the real saving is done right into the XML files.
Title: Re: Configuration Saver [API 2]
Post by: Dreams on 24 March, 2010, 23:05:18
Alright, thanks for the answers.. :) they have been most helpfull.
Title: Re: Configuration Saver [API 2]
Post by: [NL]Daddy-Bear? on 25 March, 2010, 21:04:07
I like this script thanks Atag.
I fint it Useful.

Greats [NL]Daddy-Bear?

:D