mIRC Home    About    Download    Register    News    Help

Print Thread
#11979 19/02/03 12:17 PM
Joined: Dec 2002
Posts: 68
P
Babel fish
OP Offline
Babel fish
P
Joined: Dec 2002
Posts: 68
I have an auto-update script. But, how could I make it so that it will (using sockets I assume) make sure the file is actually on the webserver before opening the dialog.

Usage: /download <URL> <Destanation dir> Example: /download http://www.mirc-help.de/mirc/mirc603.exe c:\

How could I make sure that mirc603.exe is actually there before doing it? Thanks.

#11980 19/02/03 12:24 PM
Joined: Feb 2003
Posts: 19
Pikka bird
Offline
Pikka bird
Joined: Feb 2003
Posts: 19
I think $sockerr should do the trick


trace me
#11981 19/02/03 12:29 PM
Joined: Jan 2003
Posts: 237
X
Fjord artisan
Offline
Fjord artisan
X
Joined: Jan 2003
Posts: 237
if ($sockerr > 0) return



;Check for Life

if (%life == $null) {
goto getlife
}
#11982 19/02/03 07:58 PM
Joined: Dec 2002
Posts: 1,321
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Dec 2002
Posts: 1,321
$sockerr is used if there is a problem with the socket, not if the destination file is not there. If you get a HTTP 404 - File not found, you'll still get the text of the 404 message, not a $sockerr.

If $sockerr is non-zero, that lets you know to check $sock($sockname).wserr and $sock($sockname).wsmsg to retrieve the error number and message, respectively. Those will be errors like Connection reset by peer, Network unreachable, etc.

In order to check to see if the file exists, you could try to GET the file -- if the file is there, you can download it. If it's not there, you will be able to determine that because you'll see the 404 message (sometimes a custom one, depending on the choices made by the web server administrator).
Code:

alias download {
  if !$isdir($mircdirSDownload) { mkdir $mircdirScriptedDownloads }
  set -u600 %«»FN $+(/,$gettok($remove($$1, http://), 2-, 47))
  set -u600 %«»Dir $iif($2, $2-, $mircdirScriptedDownloads\)
  if ($right(%«»Dir, 1) != \) set -u600 %«»Dir $+(%«»Dir, \)
  if !$isdir(%«»Dir) {
    var %i = 2, %levels = $numtok(%«»Dir, 92)
    while %i &lt;= %levels {
      if (!$isdir($gettok(%«»Dir, $+(1-,%i), 92))) mkdir $+(", $gettok(%«»Dir, $+(1-,%i), 92), ")
      inc %i
    }
  }
  var %sockname = $+(download, $ticks)
  sockopen %sockname $gettok($remove($$1, http://), 1, 47) 80
}
on *:SOCKOPEN:download*:{
  sockwrite -n $sockname GET %«»FN HTTP/1.0
  set -u600 %«»FN $+($mircdirScriptedDownloads\,$gettok(%«»FN, -1, 47))
  sockwrite $sockname $crlf
}
on *:SOCKREAD:download*:{
  if $sockerr &gt; 0 {
    echo $color(ctcp) -eabflirt Socket error: $+([, $sock($sockname).wserr, ]) $sock($sockname).wsmsg
    halt
  }
  if %«»Read != 1 {
    var %text
    while 1 {
      sockread %text
      if (!$sockbr) return
      if %text == HTTP/1.1 404 Not Found {
        echo $color(ctcp) -eabflirt * HTTP/1.1 404 File %«»Read.Filename not found!
        sockclose $sockname
        halt
      }
      elseif $regex(%text, /^Content-Length: (\d+)$/) == 1 { set -u600 %«»Bytes.To.Read $regml(1) }
      elseif (!%text) break
    }
    write -c %«»FN
    set %«»Ticks $ticks
  }
  set -u10 %«»Read 1
  while 1 {
    sockread &amp;code
    if (!$sockbr) return
    bwrite %«»FN -1 &amp;code
    if $file(%«»FN).size == %«»Bytes.To.Read {
      var %secs = $round($calc(($ticks - %«»Ticks) / 1000), 0)
      var %size = $file(%«»FN).size
      var %xfer = $+([, $bytes($round($calc(%size / %secs), 0), 3).suf, /sec])
      var %target = $+(", %«»Dir, $nopath(%«»FN), ")
      echo $color(info2) -eabflirt * Downloaded %target $bytes(%size, 3).suf in $gmt(%secs, n:ss) %xfer
      if $isfile(%target) &amp;&amp; $mircdirScriptedDownloads\ != %«»Dir { .rename %target $+(", $remove(%target,"), .old, ") }
      .rename %«»FN %target
      unset %«»* 
    }
  }
}

/download http[/b]://www.mirc-help.de/mirc/mirc603.exe C:\Program Files\mIRC\test

EDIT: Fixed 2 problems. Missing { } after an if condition and changed the script NOT to default to downloading into $mircdir, but into $mircdirScriptedDownloads instead...wouldn't want to /download http:[/b]//someplace.com/mirc.exe or http:[/b]//someplace.else.com/mirc.ini.

Last edited by Hammer; 24/08/03 06:28 PM.

DALnet: #HelpDesk and #m[color:#FF0000]IR[color:#EEEE00]C
#11983 24/08/03 01:50 PM
Joined: Aug 2003
Posts: 5
K
Nutrimatic drinks dispenser
Offline
Nutrimatic drinks dispenser
K
Joined: Aug 2003
Posts: 5
There is an Error in your Update addon Code please fix it becuase i wold like to use that code
-
* /set: invalid parameters (line 35, script1.ini)
-

Thank You grin


Mr.KanKouni

Link Copied to Clipboard