PtokaX forum

Archive => Archived 4.0 boards => Help with Lua 4 scripts => Topic started by: TTB on 04 February, 2005, 11:45:58

Title: Saving file exists... [message]
Post by: TTB on 04 February, 2005, 11:45:58
Hi,

I have a question... I've made a script, with a function from chill's "save string".

It save's with a command text in a text file. It works fine, but I want an extra feature in it.

The extra feature would be, IF the textfile already exists, it won't write, just notify in the main chat to the current user it can't be saved because the file already exists... Here is a part of the script, so you know what I'm talking about...

function DataArrival(curUser,data)
if (strsub(data,1,1) == "<") then
local _,_,cmd,fileordir = strfind(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")
if cmd == (prefix.."faqsave") and curUser.bOperator then
local _,_,sdata = strfind(data,"^%b<>%s+%S+%s+%S+%s+(.*)|$")
if sdata then
      SaveFile(fileordir,sdata)
SendToAll(BotName, curUser.sName.." saved a new FAQ:  '"..fileordir.."'          Type:    "..prefix.."faq "..fileordir.."    in the MAIN CHAT to view this new FAQ!")
dir = (FAQFolder.."/")
execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")
else
  curUser:SendData(BotName, "Syntax error! Use: "..prefix.."faqsave to save your FAQ!|")
end
return 1


with it's function:

function SaveFile(filename,sdata)
local handle = openfile(FAQFolder.."/"..filename,"w")
write(handle,sdata)
closefile(handle)
end

That function should be different, like:

function SaveFile(filename,sdata,curUser)
  local blaat = openfile(FAQFolder.."/"..filename,"r")
           if blaat then
   closefile(blaat)
   curUser:SendData(BotName, "Faq already exists! Delete the FAQ before you save it!")
  else    
   local handle = openfile(FAQFolder.."/"..filename,"w")
   write(handle,sdata)
            closefile(handle)
   SendToAll(BotName, curUser.sName.." saved a new FAQ:  '"..filename.."'          Type:    "..prefix.."faq "..filename.."    in the MAIN CHAT to view this new FAQ!")
 end
end

I'm just started, and already came this far.  I don't get it work. I know it is wrong, but how to get this work??

Title:
Post by: TTB on 04 February, 2005, 12:07:56
Hi,

I think I already figured out... :)  I have tried it so many times, and *bling* ...

it should be this:

function SaveFile(filename,sdata,curUser)
  local f,sdata = openfile(FAQFolder.."/"..filename,"r")
           if f then
   curUser:SendData(BotName, "Faq already exists! Delete the FAQ before you save it!")
            closefile(f)
   return nil
  else
   local handle = openfile(FAQFolder.."/"..filename,"w")
   write(handle,sdata)
            closefile(handle)
   SendToAll(BotName, curUser.sName.." saved a new FAQ:  '"..filename.."'          Type:    "..prefix.."faq "..filename.."    in the MAIN CHAT to view this new FAQ!")
           end
   
end

If I do get probs with it, I will keep posting... :))
Title:
Post by: TTB on 07 February, 2005, 04:16:34
Hi,

Well. I do have another problem now...

I want to have this command also active in PM to the bot:

function DataArrival(curUser,data)
if (strsub(data,1,1) == "<") then
local _,_,cmd,fileordir = strfind(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")

--## COMMANDS WITH EXTRA TEXT ##--
if cmd == (prefix.."faqsave") and curUser.bOperator then
local _,_,sdata = strfind(data,"^%b<>%s+%S+%s+%S+%s+(.*)|$")
if sdata then
      SaveFile(fileordir,sdata,curUser)
dir = (FAQFolder.."/")
execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")
else
  curUser:SendData(BotName, "Syntax error! Use: "..prefix.."faqsave to save your FAQ!|")
end
return 1


The string thing could be very frustrating when it doesn't work the way I like... I have tried it with this:


elseif (strsub(data,1,5+strlen(BotName))=="$To: "..BotName) then
data = strsub(data,1,strlen(data)-1)
local s,e,cmd,fileordir = strfind(data, "^%b<>%s+%S+%s+%S+%s+(.*)")
if cmd == (prefix.."faqsave") and curUser.bOperator then
local s,e,sdata = strfind(data, "%b<>%s+%S+%s+(%S+)%s+(.*)")
if sdata then
      SaveFile(fileordir,data,curUser)
dir = (FAQFolder.."/")
execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")
else
  curUser:SendPM(BotName, "Syntax error! Use: "..prefix.."faqsave to save your FAQ!|")
end
return 1
end
end

I also tried some more off course... but for ppl who program a lot here... please give me the good patterns and help me with this... and what did I forget?

Thanx!
Title:
Post by: plop on 07 February, 2005, 19:37:21
a simple way 2 check if a file excists.
if readfrom(filename) then
   file excists
else
   file doesn't excists
end
and a example of how 2 make main chat and pm use the same functions/commands handling.
function savesomething(user, data)
   do something with the file
end

function DataArrival(user, data)
   data = strsub(data, 1, (strlen(data) - 1))
   if strsub(data, 1,1) == "<" then
savesomething(user, data)
   elseif strsub(data, 1, 4) == "$To:" then
local s,e,sData = strfind(data, "%$(%b<>.*)")-- this changes the data so mainchat and pm are the same.
savesomething(user, sData)
   end
end
for you 2 find out how 2 do the rest of it.

plop
Title:
Post by: TTB on 07 February, 2005, 23:59:30
Thanx Plop...

 :P