HTTP File Downloader v1.0

By OrakuL on Apr 16, 2006

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
}

Comments

Sign in to comment.
sean   -  Apr 20, 2006

this is very useful :)

 Respond  
ch1zra   -  Apr 18, 2006

because there are people who do everything through mIRC :D

aka read mail, play games, share files, manipulate files, draw, read, write...

/me one of them :) (well, almost... i check my mail via ffox :P)

 Respond  
Daveoh   -  Apr 18, 2006

It\'s nice, got to say that.
I just don\'t think it\'s useful...
Don\'t think I\'m calling this crap cause I already said it\'s nice, it\'s just why would people rather download through mIRC than a normal web browser or any other way

 Respond  
ch1zra   -  Apr 18, 2006

also :

.timer
.remove

:)
it looks nicer without echoing to status.

 Respond  
ch1zra   -  Apr 18, 2006

works nice...
a little hint, if I may :)

use listbox under editbox, so you can have view of multiple downloaded files. it can still be limited to 1 dl at a time.
and also, it would be a nice touch to disable editbox while downloading :)

I like this code man.. I like it a lot :]

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.