Allows you to download a file and perform actions on it when it's done. I was surprised I couldn't find the snippet on the site, and because of a topic on the site somewhere (somebody asked if it is possible to perform $readini commands on a file online [--well now its possible]) i made the script. ENJOY
/*
Format:
/download <http://path/to/file> <SaveAs-Filename> [/commands when file is done]
Features:
- Allows simultanious transfers of multiple files
- Allows (optional!) execution of commands when file is done
- Binary read + write for fast transfers and no lag
- Url correction
Examples:
;Downloads the index page from mirc.com and echoes when its done:
/download www.mirc.com bla.html /echo -a The script has finished downloading
;Downloads the mirc install and run it:
/download http://mirc.arcor-online.net/mirc616.exe mirc616.exe run mirc616.exe
;Another mirc install example:
/download ftp://ftp.snt.utwente.nl/pub/software/mirc/mirc616.exe mirc616.exe run mirc616.exe
;As you can see, also allows downloading through ftp
;Downloads the index page from hawkee.com and runs it:
/download www.hawkee.com/ hawkee.html run hawkee.html
The execution of the commands when the file is done is optional
*/
alias download {
;Detect host
var %hostn = $iif(*: !iswm $geturl($1,1),1,2), %sock = $+(download,$1)
;Error checks
if ($sock(%sock)) {
erroutput Script is already downloading $1
return
}
if ($file($2)) {
erroutput File $2 already exists
return
}
;Everything ready to go
sockopen %sock $geturl($1,%hostn) 80
sockmark %sock $false $geturl($1,%hostn) $iif($geturl($1,$+($calc(%hostn + 1),-)),$+(/,$v1),/) $2-
}
on *:SOCKOPEN:download*:{
var %a = sockwrite -n $sockname
;Nessecary requests
%a GET $getmark($sockname,3) HTTP/1.1
%a Host: $getmark($sockname,2)
;Connection: close to force the sockclose event
%a Connection: close
;Just-to-make-sure requests:
%a Accept: */*
%a Accept-Charset: *
%a Accept-Encoding: *
%a Accept-Language: *
%a Content-Type: application/octet-stream
%a User-Agent: Mozilla/5.0
%a $crlf
}
on *:SOCKREAD:download*:{
;First read the headers, and if the header is send, binary write to the specified file
var %header, &binvar
if ($getmark($sockname,1)) {
sockread -fn &binvar
bwrite $getmark($sockname,4) -1 -1 &binvar
}
else sockread %header
$iif((!%header) && (!$getmark($sockname,1)),sockmark $sockname $true $getmark($sockname,2-))
}
on *:SOCKCLOSE:download*:$iif($getmark($sockname,5-),$v1)
;Local aliases
alias -l geturl return $gettok($1,$$2,47)
alias -l getmark return $gettok($sock($1).mark,$$2,32)
alias -l erroutput echo $color(info) -ae * /download error: $$1-
/*
I edited this so that it\'ll support quotations around the filename. It seems to work fine. I hope you don\'t mind. I\'m also unsure about the policies about posting edited versions of code on this site.. but anyway, for anyone who wants it: (tested on mIRC 6.3 using Hawkee\'s noprofile.gif image, and some image of a fuzzy cat eating a plant)
For the noprofile.gif image:
/download http://www.hawkee.com/images/noprofile.gif \"no profile.gif\" echo -a Completed download. Check the mIRC directory.
*/
alias download {
;Detect host
var %hostn = $iif(*: !iswm $geturl($1,1),1,2), %sock = $+(download,$1)
;Error checks
if ($sock(%sock)) {
erroutput Script is already downloading $1
return
}
var %2 = $iif($left($2,1) == \",$gettok($2-,1,34),$2)
var %3- = $iif($left($2,1) == \",$gettok($2-,2-,34),$3-)
if ($file(%2)) {
erroutput File %2 already exists
return
}
;Everything ready to go
sockopen %sock $geturl($1,%hostn) 80
sockmark %sock $false $geturl($1,%hostn) $iif($geturl($1,$+($calc(%hostn + 1),-)),$+(/,$v1),/) $replace(%2,$chr(32),^_^) %3-
}
on :SOCKOPEN:download:{
var %a = sockwrite -n $sockname
;Nessecary requests
%a GET $getmark($sockname,3) HTTP/1.1
%a Host: $getmark($sockname,2)
;Connection: close to force the sockclose event
%a Connection: close
;Just-to-make-sure requests:
%a Accept: /
%a Accept-Charset:
%a Accept-Encoding:
%a Accept-Language: *
%a Content-Type: application/octet-stream
%a User-Agent: Mozilla/5.0
%a $crlf
}
on :SOCKREAD:download:{
;First read the headers, and if the header is send, binary write to the specified file
var %header, &binvar
if ($getmark($sockname,1)) {
sockread &binvar
bwrite $qt($replace($getmark($sockname,4),^_^,$chr(32))) -1 -1 &binvar
}
else sockread %header
$iif((!%header) && (!$getmark($sockname,1)),sockmark $sockname $true $getmark($sockname,2-))
}
on :SOCKCLOSE:download:$iif($getmark($sockname,5-),$v1)
;Local aliases
alias -l geturl return $gettok($1,$$2,47)
alias -l getmark return $gettok($sock($1).mark,$$2,32)
alias -l erroutput echo $color(info) -ae * /download error: $$1-
reason it no longer works is due to a correction made as of 6.2:
\"82.Fixed /sockread -nf bug, switches were not being parsed correctly\"
not sure why the author even tried to read a line at a time (as -fn is supposed to do, when this script was made -fn with a &var was simply ignored and treated as if no switches were passed) which is a recipe for disaster :P much less efficient, and reading with -n to a &var causes the trailing CRLF to be turned into 2 null bytes
so in short, anyone still wanting to use this snippet should remove \"-fn\" from \"sockread -fn &binvar\"