PtokaX forum

Archive => Archived 5.1 boards => Help with scripts => Topic started by: sphinx_spb on 04 June, 2007, 07:19:11

Title: Can't find error
Post by: sphinx_spb on 04 June, 2007, 07:19:11
I have a script, it's parsing rss feed and work fine, but today i add another command and this second command not work.
That is, !ps1 work fine, !ps2 not. Ptokax don't find syntax errors, and no errors after !ps2 - this command simply do nothing.
Please help me find the error!


-- options
bot = frmHub:GetHubBotName()
rHost = "some url"
SCmd = "ps1"
MCmd = "ps2"
rPort = 80


-- Init lib
libinit = package.loadlib("pxwsa_l51.dll", "_libinit")
libinit()

function Main()
  WSA.Init()
end

OpConnected = NewUserConnected

ChatArrival = function(user, data)
return RssParcer(user, data)
end

function RssParcer(user,data)
local data = string.sub(data,1,-2)
local _,_,cmd = string.find(data, "%b<>%s+%p(%w+)")
local difftime = os.difftime(os.time(), iLastTime)
    if (difftime > iExpireTime) then
    table.insert(tReqUsers, user)
if cmd and cmd==SCmd then
local i = 26063
local r = "/rss/"..i..".xml"
rFile = r
elseif cmd and cmd==MCmd then
local k = 27612
local s = "/rss/"..k..".xml"
rFile = s
end
user:SendPM(bot, "Please wait ...")
  sIncomingBuffer = ""
errorCode, errorStr, curSocket = WSA.NewSocket(0)
if errorCode then
user:SendPM(bot, "Error: "..errorCode.." "..errorStr)
else
errorCode, errorStr = WSA.Connect(curSocket, rHost, rPort)
if errorCode then
user:SendPM(bot, "Error: "..errorCode.." "..errorStr)
else
errorCode, errorStr = WSA.MarkNonBlocking(curSocket)
if errorCode then
user:SendPM(bot, "Error: "..errorCode.." "..errorStr)
else
local data = "GET "..rFile.." HTTP/1.1\r\nHost: "..rHost.."\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n"..string.char(13,10)
errorCode, errorStr, bytesSent = WSA.Send(curSocket, data)
if errorCode then
user:SendPM(bot, "Error: "..errorCode.." "..errorStr)
else
  SetTimer(1000) -- 1 sec
  StartTimer()
end
end
end
end

iLastTime = os.time()

end
return 1
end

OnExit = function()
local errorCode, errorStr = WSA.Dispose()
end

OnTimer = function()
GetResponse()
end

GetResponse = function ()

..some code..

end
Title: Re: Can't find error
Post by: CrazyGuy on 04 June, 2007, 13:13:51
Are you sure the file /rss/27612.xml exists ? Can you get it when using for example ftp ?
Title: Re: Can't find error
Post by: sphinx_spb on 04 June, 2007, 22:28:23
Yes, it exists for sure. The page is available, and if I replace 26063 with 27612 in script, it works. Something is wrong with script...
Title: Re: Can't find error
Post by: CrazyGuy on 04 June, 2007, 23:56:18
I take it the user does see the "Please wait...." line ?
So the problem might be in the GetResponse() function. Can you attach the full script and libs please ?
Title: Re: Can't find error
Post by: sphinx_spb on 05 June, 2007, 05:51:54
here it is
Title: Re: Can't find error
Post by: CrazyGuy on 05 June, 2007, 14:46:42
hmm, the only thing that may cause a problem without throwing an error is in function GetResponse() in this line

local _,_,i1,i2,i3,i4 = string.find(sIncomingBuffer,"<item>(.*)</item>.*<item>(.*)</item>.*<item>(.*)</item>.*<item>(.*)</item>")

The function checks if i1 is not nil, and likewise for the other i's, but there is no else in that function.
So in case of i1, i2,i3 and i4 all being nil, you will not see an error but you won't get an outcome either.

Try this function instead to see if any of these conditions is true.


GetResponse = function ()
errorCode, errorStr, sData, bytesRead = WSA.Receive(curSocket)
if errorCode == nil then
    -- Receive done - but there can still be data on the socket you need to read.
sIncomingBuffer = sIncomingBuffer..sData
elseif errorCode == 0 then
  -- the connection has been gracefully closed by remotehost.
        sMsg = "===============================??????? ??????================================\n"
        local _,_,i1,i2,i3,i4 = string.find(sIncomingBuffer,"<item>(.*)</item>.*<item>(.*)</item>.*<item>(.*)</item>.*<item>(.*)</item>")
        if (i1 ~= nil) then
            local _,_,t1,d1 = string.find(i1,"<title>(.*)</title>.*<description>(.*)</description>")
            if (t1 ~= nil and d1 ~= nil) then
                sMsg = sMsg..t1.."\t"..d1.."\n"
            end
else
SendToAll(frmHub:GetHubBotName(), "i1 is nil")
        end
        sMsg = sMsg.."-----------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
        if (i2 ~= nil) then
            local _,_,t2,d2 = string.find(i2,"<title>(.*)</title>.*<description>(.*)</description>")
            if (t2 ~= nil and d2 ~= nil) then
                sMsg = sMsg..t2.."\t"..d2.."\n"
            end
else
SendToAll(frmHub:GetHubBotName(), "i2 is nil")
        end
        sMsg = sMsg.."-----------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
        if (i3 ~= nil) then
            local _,_,t3,d3 = string.find(i3,"<title>(.*)</title>.*<description>(.*)</description>")
            if (t3 ~= nil and d3 ~= nil) then
                sMsg = sMsg..t3.."\t"..d3.."\n"
            end
else
SendToAll(frmHub:GetHubBotName(), "i3 is nil")
        end
        sMsg = sMsg.."-----------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
        if (i4 ~= nil) then
            local _,_,t4,d4 = string.find(i4,"<title>(.*)</title>.*<description>(.*)</description>")
            if (t4 ~= nil and d4 ~= nil) then
                sMsg = sMsg..t4.."\t"..d4.."\n"
            end
else
SendToAll(frmHub:GetHubBotName(), "i1 is nil")
        end
        sMsg = sMsg.."=================================================================================\n"
        sMsg = sMsg.."??... ?????? ?? ??????, ?? ??? ??? :))\n??????????: ??? ????? ????????????? ???????? ???????? ???? http://www.gismeteo.ru/towns/"..City..".htm\n"
for i, user in pairs(tReqUsers) do
user:SendPM(bot, "\n"..sMsg)
        end
        WSA.Close(curSocket)
        StopTimer()
        tReqUsers = {}
elseif errorCode == 10035 then
        -- Receive call is blocked. Do nothing and wait.
else
-- error
        for i, user in pairs(tReqUsers) do
user:SendPM(bot, "??????: "..errorCode.." "..errorStr)
        end
        WSA.Close(curSocket)
        StopTimer()
        tReqUsers = {}
end
end


Edit: due to unknown chars, this paste looks crap. Download the attached modded lua file instead.

Title: Re: Can't find error
Post by: sphinx_spb on 05 June, 2007, 20:51:13
Well, now the error appears:

Quote[22:39] Syntax C:\ptokax\scripts\Weather2.lua:61: attempt to concatenate global 'rFile' (a nil value)

but command !ps1 still works, and !ps2 still nothing...
Title: Re: Can't find error
Post by: CrazyGuy on 06 June, 2007, 11:53:41
Quote from: sphinx_spb on 05 June, 2007, 20:51:13
Well, now the error appears:

but command !ps1 still works, and !ps2 still nothing...

That is funny, because I never changed something about that  :-\ Only thing I added was a message in mainchat if parts of the downloaded xml were not present.
Will check some more.....