;---------------------------------------; ; HTTP Request Script by ramirez © 2005 ; ;---------------------------------------; ; Usage: /request signal url [info] ; ; - ; ; This script will send a HTTP request ; ; for the specified URL and then call ; ; your SIGNAL event for every line the ; ; HTTP request returns. ; ; ; ; The passed parameters to the signal ; ; are the following: ; ; ; ; $1 The status for the signal: ; ; 0: Error ; ; 1: Line Output ; ; 2: Start Output ; ; 3: End Output ; ; ; ; In case of Error: ; ; $2 Error number ; ; $3- Error message ; ; ; ; In case of Line Output: ; ; $2- The line of output from request ; ; ; ; NOTE. ; ; If you give the additional info ; ; parameter, $2-N will always be the ; ; info you want to send to the signal ; ; and the parameters listed above are ; ; pushed after the info parameters ; ; - ; ; See examples of usage below, ; ; in bottom of the file ; ;---------------------------------------; alias request { var %id = $1, %url = $2, %info = $3- if (!%id || !%url) { return } if (!$regex(%url, /^(http:\/\/)?([^:/]+)(:(\d+))?(/.*)?$/i)) { return } var %host, %port = 80, %path = /, %matches = $regml(0), %i = 1 if ($regml(%i) == http://) { inc %i } %host = $regml(%i) if (%matches > %i) { inc %i if ($left($regml(%i), 1) == /) { %path = $regml(%i) } else { inc %i %port = $regml(%i) if (%matches > %i) { inc %i %path = $regml(%i) } } } var %largest = 0, %total = $sock(req1123476767*, 0) %i = 1 while (%i <= %total) { var %conn = $right($sock(req1123476767*, %i).name, -13) if (%conn isnum) { if (%conn > %largest) { %largest = %conn } } inc %i } inc %largest var %name = req1123476767 $+ %largest hmake %name 5 hadd %name id %id hadd %name info %info hadd %name header $true hadd %name chunked $false hadd %name request $& GET %path HTTP/1.1 $+ $crlf $+ $& Host: %host $+ : $+ %port $+ $crlf $+ $& Connection: Close $+ $crlf $+ $crlf sockopen %name %host %port } on 1:sockopen:req1123476767*:{ var %id = $hget($sockname, id) if ($sockerr) { .signal %id 0 $hget($sockname, info) $sock($sockname).wserr $sock($sockname).wsmsg hfree $sockname return } .signal %id 2 $hget($sockname, info) sockwrite $sockname $hget($sockname, request) } on 1:sockread:req1123476767*:{ var %temp, %id = $hget($sockname, id), %chunked = $false, %read, %len if ($sockerr) { .signal %id 0 $hget($sockname, info) $sock($sockname).wserr $sock($sockname).wsmsg hfree -w $sockname return } while ($true) { sockread -f %temp if ($sockbr == 0) { break } if ($hget($sockname, header)) { if (%temp == Transfer-Encoding: chunked) { hadd -m $sockname chunked $true } elseif (%temp == $null) { hadd -m $sockname header $false } } elseif (%temp != $null) { if ($hget($sockname, chunked)) { if (%chunked) { inc %read $calc($sockbr + 2) if (%read >= %len) { %chunked = $false } .signal %id 1 $hget($sockname, info) %temp } else { %read = 0 %len = $base($gettok(%temp, 1, 32), 16, 10) %chunked = $true } } else { .signal %id 1 $hget($sockname, info) %temp } } } } on 1:sockclose:req1123476767*:{ var %id = $hget($sockname, id) .signal %id 3 $hget($sockname, info) hfree $sockname } ; EXAMPLES OF USAGE ; - ; Try with these to see some examples: ; - ; EXAMPLE #1 - Without Optional Info (echo's to active window) ; /request example1 http://ramirez.avalanchestudios.net/test.txt ; /request example1 http://ramirez.avalanchestudios.net/test.php ; - ; EXAMPLE #2 - With Optional Info (msg's to specified channel with specified color) ; /request example2 http://ramirez.avalanchestudios.net/test.txt #test 4 ; /request example2 http://ramirez.avalanchestudios.net/test.php #test 12 ; - ; NOTE. If you want to use the Example #2 examples above, join #test first! ; Otherwise change the #test to some other channel you are on! ; You can view the source of test.php at: ; http://ramirez.avalanchestudios.net/test.phps ; - ; Or with your own file! ; EXAMPLE #1 CODE ; - on *:SIGNAL:example1:{ var %status = $1 if (%status == 0) { ; ERROR! var %errno = $2, %errstr = $3- echo 4 -a ERROR ( $+ %errno $+ ): %errstr } if (%status == 1) { ; OUTPUTTING A LINE echo 2 -a $2- } if (%status == 2) { ; STARTING DATA OUTPUT echo 3 -a --- DATA START --- } if (%status == 3) { ; ENDING DATA OUTPUT echo 3 -a --- DATA END --- } } ; EXAMPLE #2 CODE ; - on *:SIGNAL:example2:{ var %status = $1, %chan = $2, %color = $3 if (%status == 0) { ; ERROR! var %errno = $4, %errstr = $5- echo 4 -a ERROR ( $+ %errno $+ ): %errstr } if (%status == 1) { ; OUTPUTTING A LINE msg %chan $+($chr(3), %color, $4-, $chr(3)) } ; You can also notice that start/end of output ; are optional to handle! }