PtokaX forum

Development Section => Extensions for PtokaX Lua => Topic started by: Madman on 10 March, 2006, 11:51:15

Title: Get a txt file?
Post by: Madman on 10 March, 2006, 11:51:15
Is it possibole to get the content of an txt file from a webserver?
if so, how?
I havent tested WSA yet...  ::)
Title: Re: Get a txt file?
Post by: bastya_elvtars on 10 March, 2006, 12:15:00
You have send a HTTP GET command and receive.
Like:

    local GETHTML = "GET "..gfile.." HTTP/1.1\r\nHost: "..ghost.."\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n"
    err1,err2=WSA.BeginSendTo(sock,GETHTML..string.char(13,10),ghost,gport)


Be warned: this is for old PXWXSA, but the protocol command is what has importance here.
Title: Re: Get a txt file?
Post by: Madman on 10 March, 2006, 12:27:21
Could you give me a example script? I only tried a little, and so far i only succseded in connecting to a www...
Then i just crash PX... ;p
Title: Re: Get a txt file?
Post by: jiten on 10 March, 2006, 17:30:21
Quote from: Madman on 10 March, 2006, 12:27:21
Could you give me a example script? I only tried a little, and so far i only succseded in connecting to a www...
Then i just crash PX... ;p
You can use bluebear's latest PxWSA lib (it won't crash).
There's also an example script shipped in the archive.
If that one doesn't help, I'll make a small example for you.
Title: Re: Get a txt file?
Post by: jiten on 10 March, 2006, 20:15:35
Change Settings.sHost and Settings.sFile according to your needs:

--[[

PxWSA lib's example script by jiten (3/10/2006)

You should use the latest PxWSA lib available here: http://www.thewildplace.dk/#pxwsa

ATTENTION: It must be in your scripts' folder

]]--

Settings = {
sBot = frmHub:GetHubBotName(), -- Default Bot Name or -- sBot = "custombot"
sProt = 0, -- 0 for TCP, 1 for UDP (mostly it's TCP)
sPort = 80, -- WSA lib default port
sHost = "www.jonsthoughtsoneverything.com", -- Host
sFile = "/feeds/newzbin/newzbin-apps.xml", -- File
}

fData = ""

-- Init WSA lib
libinit = loadlib("pxwsa.dll", "_libinit")
libinit()

-- Init sockets
WSA.Init()

Main = function()
SetTimer(1000)
end

ChatArrival = function(user,data)
local s,e,cmd = string.find(data, "^%b<>%s+%p(%S+).*|$")
if cmd and string.lower(cmd) == "pxwsa" then
ConnectToHost(); StartTimer()
return 1
end
end

OnExit = function()
WSA.Dispose()
end

ConnectToHost = function()
-- If not connected to any socket
if not bConnected then
-- Create a socket according to what we have above
s,e,sock = WSA.NewSocket(Settings.sProt)
-- Try connection to host
local errorCode, errorStr = WSA.Connect(sock,Settings.sHost,Settings.sPort)
-- Connection failed
if errorCode then
-- Connection Report
SendToAll(Settings.sBot,"*** Error: Connection to "..Settings.sHost.." failed!")
-- Mark as not connected
bConnected = false
else
SendToAll(Settings.sBot,"*** Connected")
-- Mark as connected
bConnected = true
-- Mark non-blocking socket
local sError, Str = WSA.MarkNonBlocking(sock)
-- Error
if sError then
-- Socket Error Marking Report
SendToAll(Settings.sBot,"*** Error: Could not mark non-blocking socket.")
else
SendToAll(Settings.sBot,"*** Socket Marked")
-- Send Request
local wCmd = "GET "..Settings.sFile.." HTTP/1.1\r\nHost: "..Settings.sHost.."\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n"..string.char(13,10)
-- Send the request
local _ErrorCode, _ErrorStr, bytesSent = WSA.Send(sock,wCmd)
-- Connection failed
if _ErrorCode then
-- Mark as not connected
bConnected = false
-- Report Error
SendToAll(Settings.sBot,"*** Error: Connection Failed - ".._ErrorStr)
-- Close existing socket
WSA.Close(sock)
else
-- Connection Report
SendToAll(Settings.sBot,"*** Request to "..Settings.sHost.." sent!")
end
end
end
end
end

-- PX WSA lib functions

-- Receive request
OnTimer = function()
if bConnected then
-- Wait for the request response
local errorCode, errorStr, sData, bytesRead = WSA.Receive(sock)
if errorCode then
-- Connection gracefully closed
if errorCode == 0 then
-- Close existing socket
WSA.Close(sock)
-- Mark as connected
bConnected = false
-- Send received buffer
SendToAll(fData)
-- Empty receive buffer
fData = ""
-- Non-critical error
elseif (errorCode == 10035) then
-- Receive failed
else
-- Close existing socket
WSA.Close(sock)
-- Mark as not connected
bConnected = false
-- Empty receive buffer
fData = ""
end
else
-- Merge to received buffer
fData = fData..sData
end
end
end
Title: Re: Get a txt file?
Post by: Madman on 11 March, 2006, 15:45:46
Thanks m8. i was 2 drunk 2 try anything yesterday... ;P
Will try it later today....
Title: Re: Get a txt file?
Post by: Madman on 11 March, 2006, 20:39:30
I have some prob... I tried jiten's version, witch works perfect...
In order to learn, i wrote a copy of it in my own scripting style...
But it for some reason don't work, and I can't see why...
Plese check and see if you can find the problem...


-- WSA test
-- Made by Madman

tConfig = {
Bot = frmHub:GetHubBotName(),
Protocol = 0,
Port = 80,
Host = "didde.myftp.org",
File = "/test.txt",
}

fData = ""

libinit = loadlib("pxwsa.dll", "_libinit")
libinit()

WSA.Init()

function Main()
SetTimer(1000)
end

function ChatArrival(curUser, data)
local data = string.sub(data, 1, -2)
local s,e,cmd = string.find(data, "%b<>%s+(%S+)")
if cmd == "!wsa" then
Connect() StartTimer() return 1
end
end

function Connect()
if not Connected then
s,e,Socket = WSA.NewSocket(tConfig.Protocol)
local errorCode, errorString = WSA.Connect(Socket, tConfig.Host, tConfig.Port)
if errorCode then
SendToAll(tConfig.Bot, "*** Error: Connection to " ..tConfig.Host.. " failed")
Connected = false
else
SendToAll(tConfig.Bot, "*** Connected")
Connected = true
local ErrorCode, ErrorString = WSA.MarkNonBlocking(Socket)
if ErrorCode then
SendToAll(tConfig.Bot, "*** Error: Could not mark non-blocking socket")
else
SendToAll(tConfig.Bot, "*** Socket marked as non-blocking")
local SendCmd = "GET " ..tConfig.File.. " HTTP:/1.1\r\nHost: " ..tConfig.Host.. "\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0, Windows NT 5.1)\r\n" ..string.char(13,10)
local _errorCode, _errorString, byteSent = WSA.Send(Socket, SendCmd)
if _errorCode then
Connected = false
SendToAll(tConfig.Bot, "*** Connection failed: " .._errorString)
WSA.Close(Socket)
else
SendToAll(tConfig.Bot, "*** Request to " ..tConfig.Host.. " sent")
Connected = true
end
end
end
end
end

function OnTimer()
if Connected then
local errorCode, errorString, sData, bytesRead = WSA.Receive(Socket)
if errorCode then
if errorCode == 0 then
WSA.Close(Socket)
Connected = false
SendToAll(fData)
fData = ""
elseif errorCode == 10035 then
else
WSA.Close(Socket)
Connected = false
fData = ""
end
else
fData = fData..sData
end
end
end

function OnExit()
WSA.Dispose()
end


I get this far with my version.
Quote
[20:32:52] <-=MadSecurity=-> *** Connected
[20:32:52] <-=MadSecurity=-> *** Socket marked as non-blocking
[20:32:52] <-=MadSecurity=-> *** Request to didde.myftp.org sent
but with jiten's i get
Quote
[20:33:00] <-=MadSecurity=-> *** Connected
[20:33:00] <-=MadSecurity=-> *** Socket Marked
[20:33:00] <-=MadSecurity=-> *** Request to didde.myftp.org sent!
[20:33:02] HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 9
Last-Modified: Fri, 10 Mar 2006 10:14:51 GMT
Connection: Close
Date: Sat, 11 Mar 2006 19:33:00 GMT
Server: Abyss/2.0.6-X1-Win32 AbyssLib/2.0.6

wwiiiieee

wwiiiieee is the conntent of the txt file...
Title: Re: Get a txt file?
Post by: jiten on 11 March, 2006, 20:59:01
From my point of view, there's only one thing that must be changed in your code:

local SendCmd = "GET " ..tConfig.File.. " HTTP:/1.1\r\nHost: " ..tConfig.Host.. "\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0, Windows NT 5.1)\r\n" ..string.char(13,10)

to:

local SendCmd = "GET "..tConfig.File.." HTTP/1.1\r\nHost: "..tConfig.Host.."\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n"..string.char(13,10)

As you can see, there a space between " and .. and vice-versa.
With that, you won't find the file ;)
Title: Re: Get a txt file?
Post by: Madman on 11 March, 2006, 21:55:04
Quote from: jiten on 11 March, 2006, 20:59:01
As you can see, there a space between " and .. and vice-versa.
With that, you won't find the file ;)
That dosent matter... becides.. that's just my scripting style.. ;)
But you helped found the problem anyway...
I had...

" HTTP:/1.1\r\nHost: "

witch shoudl be

" HTTP/1.1\r\nHost: "
Title: Re: Get a txt file?
Post by: bluebear on 15 March, 2006, 20:31:45
I havent read all posts in this thread.

I will soon release a http downloader lib.

Where u can add download tasks, then they will be downloaded in their own thread.
And your script can once in a while ask the lib if this download is completed or not.
Download will not block hub/script.

Files are downloaded as binary data rather then text. So it will be much better then downloading from a lua script :)
Title: Re: Get a txt file?
Post by: Herodes on 21 March, 2006, 12:00:17
Quote from: bluebear on 15 March, 2006, 20:31:45
I havent read all posts in this thread.

I will soon release a http downloader lib.

Where u can add download tasks, then they will be downloaded in their own thread.
And your script can once in a while ask the lib if this download is completed or not.
Download will not block hub/script.

Files are downloaded as binary data rather then text. So it will be much better then downloading from a lua script :)
hmm will it be available just for http ?
Title: Re: Get a txt file?
Post by: bluebear on 22 March, 2006, 12:41:25
Yes, it will be http only. I might add ftp and such as well.

Reason that im writing this lib, is that im making some new header files and such that will replace the original lua headers.
That will allow for usage of the same libary on both lua versions. So this http lib is only meant to test this.
Its almost finished though, but it have some small issues i need to look into... But currently i dont have the time.
Title: Re: Get a txt file?
Post by: Herodes on 22 March, 2006, 12:56:30
Quote from: bluebear on 22 March, 2006, 12:41:25
... But currently i dont have the time.
Yep time is an issue for all of us ... no time no time ..
Title: Re: Get a txt file?
Post by: Psycho_Chihuahua on 22 March, 2006, 20:33:17
bluebear, do you by any chance need beta testers for that script?

i would be happy to test it  ;D
Title: Re: Get a txt file?
Post by: bluebear on 31 March, 2006, 11:07:21
Sorry for the late reply, but i'm very busy atm.

Yes i have a beta, but it has some issues on script restart and script stop.

I'll try to put in online to night, but im not sure i will have time for it..
But if its not here monday afternoon that means i forgot. In that case throw me a pm in plops hub :)