tglogo.png

Downloading File

Filename: Download Alias
  1. ; Download alias v0.3 created by Chessnut of SwiftIRC/Undernet.
  2. ;
  3. ; Usage:
  4. ; /download URL, or /.download URL to suppress echoes
  5. ;
  6. ; A signal will be triggered once the download fails or succeeds. The signal is named "download".
  7. ;
  8. ; In the signal:
  9. ; $1 contains $true or $false depending if the download fails or succeeds
  10. ; $2- contains the short file name (i.e. rfc1945.txt).
  11. ;

  12. ; Alias to convert all hexadecimal elements in the filename to plain text, such as %20 for a space
  13. alias -l urldecode return $regsubex($1-,/%([\da-f]{2})/ig,$chr($base(\1,16,10)))

  14. alias download {
  15.   ; Check for unique socket name
  16.   var %checkID 1
  17.   while ($sock(dload2. $+ %checkID)) inc %checkID

  18.   var %url $remove($1,http://,https://), $&
  19.     %hostAndPort $gettok(%url,1,47), $&
  20.     %host $gettok(%hostAndPort,1,58), $&
  21.     %port $gettok(%hostAndPort,2,58), $&
  22.     %pageAndFile $replace(/ $+ $gettok(%url,2-,47),$chr(32),/), $&
  23.     %file $urldecode($gettok($gettok(%pageAndFile,-1,47),1,63)), $&
  24.     %ID dload2. $+ %checkID, $&
  25.     %isFile $isfile(%file)

  26.   ; Horribly long regex statement to check for an invalid URL.
  27.   if (!$regex($1,/^(?:(?:https?://)?(?:[-a-z\d.]+\.[a-z]{2,}|localhost|(?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\b\.){3} (?:\b25[0-5]|2[0-4]\d|[01]?\d?\d\b))(?::\d+)?(?:/[^\\|/?><:*^]+)+)(\?[^&]*(&[^&]*)*)?$/ix)) {
  28.     if ($show) echo 4 Invalid download file.
  29.     .signal download $false %file
  30.   }
  31.   elseif (%isFile) {
  32.     if ($show) echo 4 %file already exists in your mIRC directory.
  33.     .signal download $false %file
  34.   }
  35.   elseif (!%isFile) {
  36.     ; Check if an SSL connection needs to be made
  37.     if (https://* iswm $1) {
  38.       if ($sslready) sockopen -e %ID %host $iif(%port,$v1,443)
  39.       else {
  40.         if ($show) echo 4 You need SSL support to download this file.
  41.         .signal download $false %file
  42.       }
  43.     }
  44.     else sockopen %ID %host $iif(%port,$v1,80)
  45.     if ($sock(%ID)) sockmark %ID $show %pageAndFile
  46.   }
  47. }

  48. on *:sockopen:dload2.*:{
  49.   ; The sockmark contains a boolean value for whether the script should echo and the page of the document
  50.   tokenize 32 $sock($sockname).mark
  51.   if ($sockerr) .signal download $false $urldecode($gettok($gettok($2-,-1,47),1,63))
  52.   if ($1 && $sockerr == 3) echo 4 Failure establishing socket connection: $sock($sockname).wserr
  53.   elseif ($1 && $sockerr == 4) echo 4 Error resolving hostname: $sock($sockname).addr
  54.   elseif ($1 && $sockerr > 0) echo 4 Unknown socket error, error code: $v1
  55.   elseif (!$sockerr) {
  56.     sockwrite -nt $sockname GET $2 HTTP/1.0
  57.     sockwrite $sockname $+(Host: $sock($sockname).addr,:,$sock($sockname).port,$crlf,$crlf)
  58.   }
  59. }

  60. on *:sockread:dload2.*:{
  61.   tokenize 32 $sock($sockname).mark
  62.   var %file $urldecode($gettok($gettok($2-,-1,47),1,63))
  63.   if ($sockerr) .signal download $false %file
  64.   if ($1 && $sockerr == 3) echo 4 Error reading socket: $sock($sockname).wserr
  65.   elseif ($1 && $sockerr > 0) echo 4 Unknown error reading socket, error code: $v1
  66.   elseif (!$v1 && !$sockerr) {
  67.     if ($0 == 2) {
  68.       var %length 0,%header,%redirect $false
  69.       sockread %header
  70.       ; Check the HTTP Status to see if the resource has moved
  71.       if ($regex(%header,/^HTTP/1\.[01] (3\d\d .+)/)) {
  72.         if ($1) echo 6 Document4 %file has moved. HTTP Status: $regml(1)
  73.         var %redirect $true
  74.       }
  75.       ; Check the HTTP Status to see if the resource could not be found
  76.       elseif ($regex(%header,/^HTTP/1\.[01] ([45]\d\d .+)/)) {
  77.         if ($1) echo 4 HTTP Error downloading4 %file ( $+ $regml(1) $+ )
  78.         .signal download $false $3-
  79.         sockclose $sockname
  80.         halt
  81.       }
  82.       if ($regex(%header,/^HTTP/1\.[01] 200/) || %redirect) {
  83.         while (%header) {
  84.           sockread %header
  85.           if (Content-Length: * iswm %header) var %length $gettok(%header,2,32)
  86.           ; If the resource has moved, download the file from the new location.
  87.           elseif (Location: * iswm %header) {
  88.             if ($1) echo 6 Document has moved to4 $gettok(%header,2-,32) $+ . Downloading from new location...
  89.             download $gettok(%header,2-,32)
  90.             sockclose $sockname
  91.             halt
  92.           }
  93.         }
  94.         ; If the resource has moved, but no new location is specified, it will halt here
  95.         if (%redirect) {
  96.           if ($1) echo 4 Error downloading file (No new path specified)
  97.           .signal download $false %file
  98.           sockclose $sockname
  99.           halt
  100.         }
  101.         sockmark $sockname $1 %length %file
  102.         tokenize 32 $sock($sockname).mark
  103.         if ($1) echo 6 Downloading4 $3- ( $+ $iif($2,$bytes($2,3).suf,Unknown size) $+ )
  104.       }
  105.       else {
  106.         if ($1) echo 4 Unknown error occurred: %header
  107.         .signal download $false $3-
  108.         sockclose $sockname
  109.         halt
  110.       }
  111.     }
  112.     ; If everything went smoothly, download the file
  113.     sockread &content
  114.     bwrite $qt($3-) -1 -1 &content
  115.     ; If the file has finished downloading, output the message.
  116.     if ($file($3-).size >= $2) {
  117.       if ($1) echo 3 File4 $3- downloaded successfully (Elapsed time: $duration($sock($sockname).to) $+ )
  118.       .signal download $true $3-
  119.       sockclose $sockname
  120.     }
  121.   }
  122. }

  123. on *:sockclose:dload2.*:{
  124.   tokenize 32 $sock($sockname).mark
  125.   if ($sockerr == 3) echo 4 An error occurred while receiving data, or a SSL error occurred: $sock($sockname).wserr
  126.   elseif ($sockerr == 5) echo 4 An SSL error occurred on sockopen
  127.   elseif ($v1) echo 4 Socket unexpectedly closed, error code: $v1
  128.   else echo 3 Either the file4 $gettok($sock($sockname).mark,3-,32) downloaded successfully, or the connection was closed prematurely. (Elapsed time: $duration($sock($sockname).to) $+ )
  129.   .signal download $iif($sockerr,$false,$true) $3-
  130. }

Related Files


Please note that on our website we use cookies necessary for the functioning of our website, cookies that optimize the performance. To learn more about our cookies, how we use them and their benefits, please read our Cookie Policy.
I Understand