Author Topic: Get a txt file?  (Read 5390 times)

0 Members and 1 Guest are viewing this topic.

Offline Madman

  • Scripter
  • Emperor
  • ******
  • Posts: 1 316
  • Karma: +75/-1
Get a txt file?
« 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...  ::)
We suffer in silence, we lurk in the shadows, we kill in the night
Site currently down, ETA of returning online is 2099 ;p

PtokaX forum

Get a txt file?
« on: 10 March, 2006, 11:51:15 »

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 752
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: Get a txt file?
« Reply #1 on: 10 March, 2006, 12:15:00 »
You have send a HTTP GET command and receive.
Like:

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

Offline Madman

  • Scripter
  • Emperor
  • ******
  • Posts: 1 316
  • Karma: +75/-1
Re: Get a txt file?
« Reply #2 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
We suffer in silence, we lurk in the shadows, we kill in the night
Site currently down, ETA of returning online is 2099 ;p

Offline jiten

  • Scripter
  • Forum Legend
  • ******
  • Posts: 1 577
  • Karma: +71/-5
Re: Get a txt file?
« Reply #3 on: 10 March, 2006, 17:30: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.

Offline jiten

  • Scripter
  • Forum Legend
  • ******
  • Posts: 1 577
  • Karma: +71/-5
Re: Get a txt file?
« Reply #4 on: 10 March, 2006, 20:15:35 »
Change Settings.sHost and Settings.sFile according to your needs:

Code: [Select]
--[[

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

Offline Madman

  • Scripter
  • Emperor
  • ******
  • Posts: 1 316
  • Karma: +75/-1
Re: Get a txt file?
« Reply #5 on: 11 March, 2006, 15:45:46 »
Thanks m8. i was 2 drunk 2 try anything yesterday... ;P
Will try it later today....
We suffer in silence, we lurk in the shadows, we kill in the night
Site currently down, ETA of returning online is 2099 ;p

Offline Madman

  • Scripter
  • Emperor
  • ******
  • Posts: 1 316
  • Karma: +75/-1
Re: Get a txt file?
« Reply #6 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...

Code: [Select]
-- 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...
We suffer in silence, we lurk in the shadows, we kill in the night
Site currently down, ETA of returning online is 2099 ;p

Offline jiten

  • Scripter
  • Forum Legend
  • ******
  • Posts: 1 577
  • Karma: +71/-5
Re: Get a txt file?
« Reply #7 on: 11 March, 2006, 20:59:01 »
From my point of view, there's only one thing that must be changed in your code:

Code: [Select]
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:

Code: [Select]
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 ;)

Offline Madman

  • Scripter
  • Emperor
  • ******
  • Posts: 1 316
  • Karma: +75/-1
Re: Get a txt file?
« Reply #8 on: 11 March, 2006, 21:55:04 »
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...
Code: [Select]
" HTTP:/1.1\r\nHost: "
witch shoudl be
Code: [Select]
" HTTP/1.1\r\nHost: "
We suffer in silence, we lurk in the shadows, we kill in the night
Site currently down, ETA of returning online is 2099 ;p

Offline bluebear

  • Double Ace
  • *
  • Posts: 116
  • Karma: +12/-0
  • Life is a bitch - And then you marry one!
Re: Get a txt file?
« Reply #9 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 :)
Sincerely,
bluebear
--
http://www.thewildplace.dk/ is is closed - Use the following mirrors instead
http://bluebear.psycho-chihuahua.net
http://pxextension.piratez.dk/
[Lua extensions - Chat stats - YnHub PMSpy - DC Source code - and more]

Herodes

  • Guest
Re: Get a txt file?
« Reply #10 on: 21 March, 2006, 12:00:17 »
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 ?

Offline bluebear

  • Double Ace
  • *
  • Posts: 116
  • Karma: +12/-0
  • Life is a bitch - And then you marry one!
Re: Get a txt file?
« Reply #11 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.
Sincerely,
bluebear
--
http://www.thewildplace.dk/ is is closed - Use the following mirrors instead
http://bluebear.psycho-chihuahua.net
http://pxextension.piratez.dk/
[Lua extensions - Chat stats - YnHub PMSpy - DC Source code - and more]

Herodes

  • Guest
Re: Get a txt file?
« Reply #12 on: 22 March, 2006, 12:56:30 »
... But currently i dont have the time.
Yep time is an issue for all of us ... no time no time ..

Offline Psycho_Chihuahua

  • Systemspecialist IT
  • Emperor
  • **
  • Posts: 1 054
  • Karma: +112/-1
  • I am wherever i want to be
    • PtokaX Luaboard
Re: Get a txt file?
« Reply #13 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
PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

Offline bluebear

  • Double Ace
  • *
  • Posts: 116
  • Karma: +12/-0
  • Life is a bitch - And then you marry one!
Re: Get a txt file?
« Reply #14 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 :)
Sincerely,
bluebear
--
http://www.thewildplace.dk/ is is closed - Use the following mirrors instead
http://bluebear.psycho-chihuahua.net
http://pxextension.piratez.dk/
[Lua extensions - Chat stats - YnHub PMSpy - DC Source code - and more]

PtokaX forum

Re: Get a txt file?
« Reply #14 on: 31 March, 2006, 11:07:21 »