Write/Read Functions
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Write/Read Functions

Started by nEgativE, 15 March, 2005, 13:38:32

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

nEgativE

Hi, i need more help :)

Could someone check if there is something wrong with the function ReadRelease ? i'm able to write, but the Read function does not return correct.. tks in advance.

------------------------------------------------------
function WriteRelease(user, data, cmd)

	arg = GetArgs(data)
	if arg == nil then
	arg = "Sem coment?rios no Hub."
	user:SendData(sBot, arg)
	return 1
	end

	local handle = io.open("Data/CLP.GuestBook.dat","a")
	timedate = os.date() 
	handle.write(handle,timedate.." - "..user.sName..": "..arg.."?")
	SendPmToOps(sChatBot,"DC:CLP - Novo coment?rio no GuestBook do Hub por: "..user.sName.."")
	SendToAll(sBot,"DC:CLP - Novo coment?rio no GuestBook do Hub, digite: !livro para ler o(s) coment?rio(s).")
	handle:close()
end

function ReadRelease(user, data, cmd)

	user:SendPM(sBot, ":.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:")
	handle = io.open("Data/CLP.GuestBook.dat","r")
	if (handle) then 
		line = handle.read(handle,"*a") 
		line=string.sub(line,1,string.len(line)-1) 
		linearray=tokenize(line,"?") 
		for i=1,linearray.n do 
			user:SendPM(sBot,linearray[i])
		end
		handle:close()
	user:SendPM(sBot, ":.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:")
	end
end
------------------------------------------------------

Jelf

Change this
handle.write(handle,timedate.." - "..user.sName..": "..arg.."?")
to this
handle:write(handle,timedate.." - "..user.sName..": "..arg.."?")
And this
line = handle.read(handle,"*a")
to this
line = handle:read()
Then should be ok :)

jiten

darn, some minutes late  ;)

nEgativE

Hehe jiten, still get some syntax errors on this one !

Write function --> script.lua:1199: bad argument #1 to `write' (string expected, got userdata)

Read function --> script.lua:1213: `for' limit must be a number

jiten

Can u post the whole script so that I can have a better look at it?

nEgativE

sBot = "?per"
sChatBot ="Moita"

function tokenize (inString,token) 

	_WORDS = {} 
	local matcher = "([^?"..token.."]+)" 
	string.gsub(inString, matcher, function (w) table.insert(_WORDS,w) end) 
	return _WORDS 
end

function ChatArrival(user, data) 

	data=string.sub(data,1,string.len(data)-1)
	_,_,cmd=string.find(data, "%b<>%s+(%S+)")

	if (cmd=="!livro") then
	ReadRelease(user, data, cmd)
	return 1
	else
	if (cmd=="!escrever") then
	WriteRelease(user, data, cmd)
	return 1
	end
end
end

function GetArgs(data)
	s,e,arg = string.find( data, "%b<>%s+%S+%s+(.+)" )
	return arg
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
function WriteRelease(user, data, cmd)

	arg = GetArgs(data)
	if arg == nil then
	arg = "Sem coment?rios no Hub."
	user:SendData(sBot, arg)
	return 1
	end

	handle = io.open("Data/CLP.GuestBook.dat","a")
	timedate = os.date() 
	handle:write(handle,timedate.." - "..user.sName..": "..arg.."?")
	SendPmToOps(sChatBot,"DC:CLP - Novo coment?rio no GuestBook do Hub por: "..user.sName.."")
	SendToAll(sBot,"DC:CLP - Novo coment?rio no GuestBook do Hub, digite: !livro para ler o(s) coment?rio(s).")
	handle:close()
end

function ReadRelease(user, data, cmd)

	user:SendPM(sBot, ":.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:")
	handle = io.open("Data/CLP.GuestBook.dat","r")
	if (handle) then 
		line = handle:read()
		line=string.sub(line,1,string.len(line)-1) 
		linearray=tokenize(line,"?") 
		for i=1,linearray.n do 
			user:SendPM(sBot,linearray[i])
		end
		handle:close()
	user:SendPM(sBot, ":.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:")
	end
end

jiten

#6
Here you go:

--Converted to LUA 5 by jiten
sBot = "?per"
sChatBot ="Moita"

function tokenize (inString,token) 

	_WORDS = {} 
	local matcher = "([^?"..token.."]+)" 
	string.gsub(inString, matcher, function (w) table.insert(_WORDS,w) end) 
	return _WORDS 
end

function ChatArrival(user, data) 

	data=string.sub(data,1,string.len(data)-1)
	_,_,cmd=string.find(data, "%b<>%s+(%S+)")

	if (cmd=="!livro") then
	ReadRelease(user, data, cmd)
	return 1
	else
	if (cmd=="!escrever") then
	WriteRelease(user, data, cmd)
	return 1
	end
end
end

function GetArgs(data)
	s,e,arg = string.find( data, "%b<>%s+%S+%s+(.+)" )
	return arg
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
function WriteRelease(user, data, cmd)

	arg = GetArgs(data)
	if arg == nil then
	arg = "Sem coment?rios no Hub."
	user:SendData(sBot, arg)
	return 1
	end

	local handle = io.open("Data/CLP.GuestBook.dat","a+")
	assert(handle, "Data/CLP.GuestBook.dat")
	timedate = os.date() 
	handle:write(timedate.." - "..user.sName..": "..arg.."?")
	SendPmToOps(sChatBot,"DC:CLP - Novo coment?rio no GuestBook do Hub por: "..user.sName.."")
	SendToAll(sBot,"DC:CLP - Novo coment?rio no GuestBook do Hub, digite: !livro para ler o(s) coment?rio(s).")
	handle:close()
end

function ReadRelease(user, data, cmd)

	user:SendPM(sBot, ":.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:")
	handle = io.open("Data/CLP.GuestBook.dat","r")
	if (handle) then 
		line = handle:read()
		line=string.sub(line,1,string.len(line)-1) 
		linearray=tokenize(line,"?") 
		for i=1,table.getn(linearray) do 
			user:SendPM(sBot,linearray[i])
		end
		handle:close()
	user:SendPM(sBot, ":.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:")
	end
end

nEgativE

running perfect :) tks again []'s

jiten


SMF spam blocked by CleanTalk