alias download {
var %r = $(|,) return $false, %e = scon -r !echo $color(info) -a $!!download: Error -
if (!$isid) %e this snippet can only be called as an identifier. %r
if ($os isin 9598) %e this snippet requires Windows ME or higher. %r
if ($version < 6) %e this snippet requires mIRC version 6.0 or higher. %r
var %dir = $nofile($1), %file = $nopath($1), %method = $upper($2), %url = $3
var %bit = $4, %headers = $iif($2 == get,$5,$6), %postdata = " $+ $5", %res
if (* !iswm %file) %e you must specify a file to save the data to. %r
if (%file != $mkfn(%file)) %e file %file contains illegal characters. %r
if (* !iswm %dir) %dir = $mircdir
elseif (!$isdir(%dir)) %e no such folder %dir %r
if (!$istok(get head post,$2,32)) %e method can only be GET, HEAD or POST. %r
if (!$regex(%e,$3,/^\S+\.\S+\.\S+$/)) %e you didn't specify an url to download from. %r
if ($2 != head) {
if ($4 !isnum 1-3) %e bitmask should be a digit in range 1-3. %r
if ($2 == post) && (* !iswm $5) %e you didn't specify any post data. %r
if (%headers) && (!$regsub(%e,%headers,/(\S+?): (.+?)(?=\s?\n|$)/g,"\1" $chr(44) "\2",%headers)) {
%e bad header syntax. Correct -> Label: value seperated by $!!lf's %r
}
}
var %file = $+(",%dir,%file,"), %id = $+(@download,$ticks,$r(1111,9999),.vbs), %a = aline %id
if (http://* !iswm $3) %url = http:// $+ $3
.comopen %id wscript.shell
if ($comerr) %e could not open Wscript.Shell. %r
write -c %file
window -h %id
%a on error resume next
%a sub quit $lf set http = nothing : set ado = nothing : wscript.quit $lf end sub
%a sub errmsg
%a set fso = createobject("scripting.filesystemobject")
%a set file = fso.createtextfile( %file ,true)
%a file.write("Err number: " & err.number & " = " & err.description) : file.close
%a set fso = nothing
%a quit
%a end sub
%a arr = array("winhttp.winhttprequest.5.1","winhttp.winhttprequest","msxml2.serverxmlhttp","microsoft.xmlhttp")
%a i = 0 $lf while i < 4 and not isobject(http) : set http = createobject(arr(i)) : i = i + 1 : wend
%a if not isobject(http) then errmsg
%a err.clear
%a http.open $+(",%method,") , $+(",%url,") ,false
%a http.setrequestheader "User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"
if (%headers) { tokenize 10 %headers | scon -r %a http.setrequestheader $* }
if (%method == post) {
%a http.setrequestheader "Content-Type","application/x-www-form-urlencoded"
%a http.send %postdata
}
else %a http.send
%a if err then errmsg
%a set ado = createobject("adodb.stream")
%a if not isobject(ado) then errmsg
%a ado.open
if (%bit != 2) {
%a ado.type = 2 : ado.charset = "ascii"
%a ado.writetext "HTTP/1.1 " & http.status & " " & http.statustext,1
%a ado.writetext http.getallresponseheaders,1 : ado.position = 0
}
if (%bit != 1) %a ado.type = 1 : ado.read : ado.write http.responsebody
%a ado.savetofile %file ,2 : ado.close : quit
savebuf %id $+($nofile($mircexe),%id)
close -@ %id
.comclose %id $com(%id,run,1,bstr*,%id,uint,0,bool,true)
.remove $+($nofile($mircexe),%id)
%res = $read(%file,t,1)
if (Err number:*=* iswm %res) || (!$file(%file)) %e $iif(%res,%res,no data could be retrieved) - %url %r
return $true
:error
if ($com(%id)) .comclose %id
if ($isfile(%id)) .remove %id
if ($window(%id)) close -@ %id
return $false
}