Downloading File
Filename: Download Alias
; Download alias v0.3 created by Chessnut of SwiftIRC/Undernet. ; ; Usage: ; /download URL, or /.download URL to suppress echoes ; ; A signal will be triggered once the download fails or succeeds. The signal is named "download". ; ; In the signal: ; $1 contains $true or $false depending if the download fails or succeeds ; $2- contains the short file name (i.e. rfc1945.txt). ; ; Alias to convert all hexadecimal elements in the filename to plain text, such as %20 for a space alias -l urldecode return $regsubex($1-,/%([\da-f]{2})/ig,$chr($base(\1,16,10))) alias download { ; Check for unique socket name var %checkID 1 while ($sock(dload2. $+ %checkID)) inc %checkID var %url $remove($1,http://,https://), $& %hostAndPort $gettok(%url,1,47), $& %host $gettok(%hostAndPort,1,58), $& %port $gettok(%hostAndPort,2,58), $& %pageAndFile $replace(/ $+ $gettok(%url,2-,47),$chr(32),/), $& %file $urldecode($gettok($gettok(%pageAndFile,-1,47),1,63)), $& %ID dload2. $+ %checkID, $& %isFile $isfile(%file) ; Horribly long regex statement to check for an invalid URL. 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)) { if ($show) echo 4 Invalid download file. .signal download $false %file } elseif (%isFile) { if ($show) echo 4 %file already exists in your mIRC directory. .signal download $false %file } elseif (!%isFile) { ; Check if an SSL connection needs to be made if (https://* iswm $1) { if ($sslready) sockopen -e %ID %host $iif(%port,$v1,443) else { if ($show) echo 4 You need SSL support to download this file. .signal download $false %file } } else sockopen %ID %host $iif(%port,$v1,80) if ($sock(%ID)) sockmark %ID $show %pageAndFile } } on *:sockopen:dload2.*:{ ; The sockmark contains a boolean value for whether the script should echo and the page of the document tokenize 32 $sock($sockname).mark if ($sockerr) .signal download $false $urldecode($gettok($gettok($2-,-1,47),1,63)) if ($1 && $sockerr == 3) echo 4 Failure establishing socket connection: $sock($sockname).wserr elseif ($1 && $sockerr == 4) echo 4 Error resolving hostname: $sock($sockname).addr elseif ($1 && $sockerr > 0) echo 4 Unknown socket error, error code: $v1 elseif (!$sockerr) { sockwrite -nt $sockname GET $2 HTTP/1.0 sockwrite $sockname $+(Host: $sock($sockname).addr,:,$sock($sockname).port,$crlf,$crlf) } } on *:sockread:dload2.*:{ tokenize 32 $sock($sockname).mark var %file $urldecode($gettok($gettok($2-,-1,47),1,63)) if ($sockerr) .signal download $false %file if ($1 && $sockerr == 3) echo 4 Error reading socket: $sock($sockname).wserr elseif ($1 && $sockerr > 0) echo 4 Unknown error reading socket, error code: $v1 elseif (!$v1 && !$sockerr) { if ($0 == 2) { var %length 0,%header,%redirect $false sockread %header ; Check the HTTP Status to see if the resource has moved if ($regex(%header,/^HTTP/1\.[01] (3\d\d .+)/)) { if ($1) echo 6 Document4 %file has moved. HTTP Status: $regml(1) var %redirect $true } ; Check the HTTP Status to see if the resource could not be found elseif ($regex(%header,/^HTTP/1\.[01] ([45]\d\d .+)/)) { if ($1) echo 4 HTTP Error downloading4 %file ( $+ $regml(1) $+ ) .signal download $false $3- sockclose $sockname halt } if ($regex(%header,/^HTTP/1\.[01] 200/) || %redirect) { while (%header) { sockread %header if (Content-Length: * iswm %header) var %length $gettok(%header,2,32) ; If the resource has moved, download the file from the new location. elseif (Location: * iswm %header) { if ($1) echo 6 Document has moved to4 $gettok(%header,2-,32) $+ . Downloading from new location... download $gettok(%header,2-,32) sockclose $sockname halt } } ; If the resource has moved, but no new location is specified, it will halt here if (%redirect) { if ($1) echo 4 Error downloading file (No new path specified) .signal download $false %file sockclose $sockname halt } sockmark $sockname $1 %length %file tokenize 32 $sock($sockname).mark if ($1) echo 6 Downloading4 $3- ( $+ $iif($2,$bytes($2,3).suf,Unknown size) $+ ) } else { if ($1) echo 4 Unknown error occurred: %header .signal download $false $3- sockclose $sockname halt } } ; If everything went smoothly, download the file sockread &content bwrite $qt($3-) -1 -1 &content ; If the file has finished downloading, output the message. if ($file($3-).size >= $2) { if ($1) echo 3 File4 $3- downloaded successfully (Elapsed time: $duration($sock($sockname).to) $+ ) .signal download $true $3- sockclose $sockname } } } on *:sockclose:dload2.*:{ tokenize 32 $sock($sockname).mark if ($sockerr == 3) echo 4 An error occurred while receiving data, or a SSL error occurred: $sock($sockname).wserr elseif ($sockerr == 5) echo 4 An SSL error occurred on sockopen elseif ($v1) echo 4 Socket unexpectedly closed, error code: $v1 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) $+ ) .signal download $iif($sockerr,$false,$true) $3- }