Snippet that downloads files from the web via sockets. Right mouse click on channel > File Downloader > enter link > download. Long and useless script but works fine. :)
; HTTP File Downloader v1.0
; coded by OrakuL
; death09@inbox.lv
; #wolfbrothers @ irc.lv
menu channel {
File Downloader:/dlmanager
}
dialog dl {
title "HTTP File Downloader v1.0"
size -1 -1 192 45
option dbu
box "URL:", 1, 1 0 190 21
edit "", 2, 5 7 183 10, return autohs
button "Download", 4, 1 23 45 12 ok
button "Open", 6, 49 23 45 12, disable
button "Remove", 7, 97 23 45 12, disable
text "Status:", 3, 4 37 24 8
text "Waiting", 8, 24 37 119 8
button "Cancel", 9, 145 23 45 12, disable cancel
}
alias dlmanager {
if (!$dialog(dl)) { dialog -m dl dl }
}
alias check_link {
if ($nopath($1) != $null) {
set %dl_file $nopath($1)
if (http:// isin $1) {
set %dl_link $remove($1,http://)
}
else {
set %dl_link $1
}
var %i 1
while (%i <= $len(%dl_link)) {
if ($mid(%dl_link,%i,1) == /) {
set %dl_host $left(%dl_link,$calc(%i - 1))
set %dl_path $mid(%dl_link,%i,99999)
set %dl_port 80
if (: isin %dl_host) {
var %p 1
while (%p <= $len(%dl_host)) {
if ($mid(%dl_host,%p,1) == :) {
set %dl_port $mid(%dl_host,$calc(%p + 1),10)
set %dl_host $left(%dl_host,$calc(%p - 1))
goto end
}
inc %p
}
}
goto end
}
inc %i
}
:end
return true
}
}
on *:dialog:dl:sclick:4,6,7,9: {
if ($did == 4) {
if ($did(dl,2).text != $null) {
did -a dl 8 Connecting
did -b dl 4
if ($check_link($did(dl,2).text) == true) {
dl_download
}
}
}
if ($did == 6) { run download\ $+ %dl_file }
if ($did == 7) { remove_file | did -a dl 8 File deleted! }
if ($did == 9) { stop_download }
}
alias dl_download {
if ($sock(dl_manager)) {
did -e dl 4
return
}
if (%dl_host != $null) && (%dl_port != $null) {
sockopen dl_manager %dl_host %dl_port
did -a dl 4 0 kb/s
}
else {
unset %dl_host %dl_path %dl_file %dl_link %dl_port %downloadready %downloadoffset %dl_total %speed %downloadlength
did -a dl 8 Cannot connect!
did -e dl 4
did -a dl 4 Download
timerdl off
}
}
alias speed_test {
set %dl_total $calc(%dl_total + %speed)
did -a dl 4 $speed(%speed,1)
set %speed 0
}
alias speed {
var %dl_bytes $1
if ($2 == 1) {
if (%dl_bytes == 0) {
return 0 kb/s
}
if (%dl_bytes >= $calc(1024 * 1024)) {
return $round($calc(%dl_bytes / 1024),2) mb/s
}
if (%dl_bytes >= 1024) {
return $round($calc(%dl_bytes / 1024),2) kb/s
}
if (%dl_bytes < 1024) {
return %dl_bytes b/s
}
}
if ($2 == 2) {
if (%dl_bytes >= $calc($calc(1024 * 1024) * 1024)) {
return $round($calc($calc($calc(%dl_bytes / 1024) / 1024) / 1024),2) gb
}
if (%dl_bytes >= $calc(1024 * 1024)) {
return $round($calc($calc(%dl_bytes / 1024) / 1025),2) mb
}
if (%dl_bytes >= 1024) {
return $round($calc(%dl_bytes / 1024),2) kb
}
if (%dl_bytes < 1024) {
return %dl_bytes b
}
}
}
alias stop_download {
sockclose dl_manager
remove download\ $+ %dl_file
unset %dl_host %dl_path %dl_file %dl_link %dl_port %downloadready %downloadoffset %dl_total %speed %downloadlength
timerdl off
}
alias remove_file {
sockclose dl_manager
remove download\ $+ %dl_file
unset %dl_host %dl_path %dl_file %dl_link %dl_port %downloadready %downloadoffset %dl_total %speed %downloadlength
/did -b dl 6
/did -b dl 7
timerdl off
}
on *:dialog:dl:close:*: {
sockclose dl_manager
timerdl off
unset %dl_host %dl_path %dl_file %dl_link %dl_port %downloadready %downloadoffset %dl_total %speed %downloadlength
}
on *:sockopen:dl_manager:{
if ($sockerr) {
/did -a dl 8 Cannot connect!
/did -e dl 4
/did -a dl 4 Download
return
}
/did -e dl 9
/did -b dl 4
/did -a dl 8 Downloading
write -c download\ $+ %dl_file
unset %downloadlength %downloadready
sockwrite -n $sockname GET %dl_path HTTP/1.0
sockwrite -n $sockname Accept: */*
sockwrite -n $sockname Host: %dl_host
sockwrite -n $sockname
timerdl 0 1 /speed_test
set %dl_total 0
}
on *:sockread:dl_manager:{
if (%downloadready != 1) {
var %header
sockread %header
if (not found isin %header) {
stop_download
did -a dl 8 File not found!
did -a dl 4 Download
did -e dl 4
did -b dl 6
did -b dl 7
halt
}
while ($sockbr) {
if (Content-length: * iswm %header) {
%downloadlength = $gettok(%header,2,32)
}
elseif (* !iswm %header) {
%downloadready = 1
%downloadoffset = $sock($sockname).rcvd
break
}
sockread %header
}
}
sockread 4096 &d
while ($sockbr) {
set %speed $calc(%speed + $sockbr)
bwrite download\ $+ %dl_file -1 -1 &d
sockread 4096 &d
}
var %per $ceil($round( $calc(100 * ($sock($sockname).rcvd - %downloadoffset) / %downloadlength) ,1))
/did -a dl 8 %per $+ % ( $+ $speed(%dl_total,2) of $speed(%downloadlength,2) $+ )
}
on *:sockclose:dl_manager:{
/did -a dl 4 Download
/did -a dl 8 Download complete!
/did -e dl 7
/did -e dl 6
/did -e dl 4
timerdl off
unset %dl_host %dl_path %dl_link %dl_port %downloadready %downloadoffset %dl_total %speed %downloadlength
}