; ------------------------------------------------------------------------------------------------------
; usage: $download(<link>)
; --
; properties:
; .owrite - When specified, if a file already exists with the same name as that of ebign downloaded,
; it is overwritten. When not specified, the user will be faces with a popup asking if they
; would like to overwrite the file.
; ------------------------------------------------------------------------------------------------------
alias download {
if (!$isid) || (!$1) {
/echo -s Invalid Usage: $ $+ download(link)[.owrite]
return
}
else {
var %link = $remove($$1, http://) $+ /
var %host = $gettok(%link, 1, $asc(/))
var %get = $iif($left($remove(%link, %host), -1), $ifmatch, /)
/inc %download.header
if ($sock(download)) /download.close
/sockopen download %host 80
/sockmark download %host %get
/set %download.info $rand(1,9999) $+ $chr(44) $+ $iif($prop && $prop == owrite, 1, 0)
return $gettok(%download.info, 1, 44)
}
}
alias download.close {
if ($sock(download)) /sockclose download
/unset %download.*
if ($isFile(download.dat)) .remove download.dat
}
on 1:SOCKOPEN:download: {
if ($sockerr) {
/echo -a Error: Could not connect to $gettok($sock($sockname).mark, 1, 32)
/download.close
}
else {
/sockwrite -n $sockname GET $gettok($sock($sockname).mark, 2-, 32) HTTP/1.1
/sockwrite -n $sockname Host: $gettok($sock($sockname).mark, 1, 32)
/sockwrite -n $sockname $crlf
}
}
on 1:SOCKREAD:download: {
/sockread 0f &data
if (%downlaod.header) {
var %data = $bvar(&data, 1, 100).text
if (%data == $null) /unset %download.header
}
else {
/bwrite download.dat -1 -1 &data
}
}
on 1:SOCKCLOSE:download: {
var %file = $gettok($nopath($gettok($sock($sockname).mark, 2-, 32)) $+ ?, 1, $asc(?))
if (%file == $null) %file = index.htm
var %owrite = $gettok(%download.info, 2, 44)
/echo -s File: %file
if ($isFile(%file)) {
if (%owrite) || ($$?!="Would you like to overwrite the previous file ( $+ %file $+ )?") .remove %file
}
.rename download.dat %file
.signal DOWNLOAD $gettok(%download.info, 1, 44) %file
/download.close
}
; ------------------------------------------------------------------------------------------------------
; example code
alias dlGoogle {
/echo -a Downloading File...
/set %dl.id $download(www.google.com)
}
on *:SIGNAL:DOWNLOAD: {
var %id = $1, %file = $2
; ----------------------
if (%id == %dl.id) {
/echo -a File Downloaded ( $+ %file $+ )
/run %file
}
}
; EOF
; ------------------------------------------------------------------------------------------------------