Configuration Saver [API 2]
 

Configuration Saver [API 2]

Started by ATAG, 02 September, 2008, 15:57:44

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

ATAG

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

11hh

#1
Very good this script, congratulate !
I recommend it to everybody !  :)

speedX

Thanking You,

speedX

?[-?Genius?-]?

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

ATAG

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.

?[-?Genius?-]?


Dreams

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

TiMeTrAVelleR

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

Dreams

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.

bastya_elvtars

#9
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
Everything could have been anything else and it would have just as much meaning.

Dreams

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

bastya_elvtars

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?
Everything could have been anything else and it would have just as much meaning.

Dreams

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

bastya_elvtars

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.
Everything could have been anything else and it would have just as much meaning.

Dreams

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?

bastya_elvtars

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.
Everything could have been anything else and it would have just as much meaning.

Dreams

Alright, thanks for the answers.. :) they have been most helpfull.

[NL]Daddy-Bear?

I like this script thanks Atag.
I fint it Useful.

Greats [NL]Daddy-Bear?

:D
???????????? Lets enjoy our Hobby ??????????

dchub://daddys-friends-association.no-ip.org:4106

take a peek you are welcome :D

SMF spam blocked by CleanTalk