J-Script — File Browser

J-Scriptv2.2 / J-Scriptv2.2 / ini / jsupdate.mrc

jsupdate.mrc — 4.83 KB — Download this file

; As you probably figured by the file name, this contains the
; coding that J-Script uses to update itself.  cool, no?
; True, I could have made this neater, but I didn't feel like it.

on 1:start:{ checkversion }
alias -l extractupdate { run -n $mircdirupdates\unzip.exe -qo $1- -d $mircdirupdates | .timer 1 10 installupdate }
alias -l installupdate { play -sc $mircdirupdates/update.cfg 250 }
alias checkversion { download $jscript(versionurl) $mircdirupdates }
alias -l compareversion { 
  var %version.this = $jscript(version)
  var %version.check = $read($mircdirupdates/$1, 1)
  var %update.file = $read($mircdirupdates/$1, 2)
  if (%version.check isnum) { 
    if (%version.check > %version.this) { jsset updatefile %update.file | asktoupdate }
  }
  remove -q $1
}
alias asktoupdate { doupdate $$input(There is an update for $j.s available. $crlf $crlf  Update now? $crlf $crlf You may update later if you wish.,vyi,$j.s Alert) }
alias -l doupdate { 
  if ($1 == $yes) { download $jscript(updatefile) | var %trash = $$input(Downloading update...,oi,$j.s Alert) }
  else { return }  ;silly user doesn't want to update!!
}
alias update play -sc $mircdirupdates\update.cfg 250

on *:SIGNAL:download_complete: {
  tokenize 32 $download($1).file
  if (.zip == $right($nopath($1),4)) { extractupdate $1 | return }
  if (.txt == $right($nopath($1),4)) { compareversion $nopath($1) | return }
}

; HTTP Downloader v1.0 by ^BeAsT^   amitai_86@nirel.org.il
alias download {
  if (!$isid) {
    if (!$isdir($2-)) {
      echo $color(info) -s $readini(mirc.ini,text,linesep)
      echo $color(info) -s * /download: insufficient parameters
      echo $color(info) -s $readini(mirc.ini,text,linesep)
      return
    }
    var %1 = download $+ $calc($remove($sock(download*,$sock(download*,0)),download) + 1)
    unset % [ $+ [ %1 $+ .* ] ]
    set % [ $+ [ %1 $+ .file ] ] $2- $+ $iif($right($2-,1) != $chr(92),$chr(92)) $+ $gettok($1,-1,47)
    set % [ $+ [ %1 $+ .url ] ] http:// $+ $remove($1,http://)
    set % [ $+ [ %1 $+ .ctime ] ] $ctime 0
    set % [ $+ [ %1 $+ .status ] ] Connecting
    sockopen %1 $gettok($remove($1,http://),1,47) 80
  }
  else {
    if ($1 == 0) { return $sock(download*,0) }
    if ($iif($1 isnum,$sock(download $+ $1),$sock($1))) {
      var %1 = $ifmatch
      var %2 = $download.var(%1,file)
      var %3 = $download.var(%1,size)
      var %4 = $file(%2).size
      if (!$prop) { return %1 }
      if ($prop == ip) { return $sock(%1).ip }
      if ($prop == status) { return $download.var(%1,status) }
      if ($prop == url) { return $download.var(%1,url) }
      if ($prop == file) { return %2 }
      if ($prop == size) { return %3 }
      if ($prop == type) { return $download.var(%1,type) }
      if ($prop == rcvd) { return %4 }
      if ($prop == cps) { return $int($calc(%4 / ($ctime - $download.var(%1,ctime,2)))) }
      if ($prop == pc) { return $int($calc($file(%2).size * 100 / %3)) }
      if ($prop == secs) { return $calc($ctime - $download.var(%1,ctime,1)) }
    }
  }
}
alias -l download.var { return $gettok(% [ $+ [ $+($1,.,$2) ] ],$iif(!$3,1-,$3),32) }
alias -l download.fail { .signal -n download_fail $1 $2- | close -d $sockname }
alias close {
  if ($1 == -d) {
    if ($sock($2)) {
      unset % [ $+ [ $2 $+ .* ] ]
      sockclose $2
    }
  }
  else { close $1- }
}

on *:sockopen:download*:{
  if ($sockerr) { download.fail $sockname Unable to Connect | return }
  set % [ $+ [ $sockname $+ .status ] ] Requesting File
  sockwrite -tn $sockname GET $download.var($sockname,url) HTTP/1.1
  sockwrite -tn $sockname Host: $sock($sockname).ip
  sockwrite -tn $sockname Accept: *.*, */*
  sockwrite -tn $sockname Connection: Close
  sockwrite -tn $sockname $crlf
}
on *:sockclose:download*:{ if ($download.var($sockname,status) != done) { download.fail $sockname Disconnected } }
on *:sockread:download*:{
  if ($sockerr) { download.fail $sockname Disconnected | return }
  if ($download.var($sockname,status) != downloading) {
    var %1 | sockread %1 | tokenize 32 %1
    if (HTTP/* iswm $1 && $2 != 200) { download.fail $sockname $3- }
    if ($1 == Content-Length:) { set % [ $+ [ $sockname $+ .size ] ] $2 }
    if ($1 == Content-Type:) { set % [ $+ [ $sockname $+ .type ] ] $2- }
    if (!$1) {
      .remove -q $+(",$download.var($sockname,file),")
      set % [ $+ [ $sockname $+ .ctime ] ] $download.var($sockname,ctime,1) $ctime
      set % [ $+ [ $sockname $+ .status ] ] Downloading
      return
    }
  }
  else {
    var %1 = $download.var($sockname,file)
    :sockread
    sockread &1
    if (!$sockbr) { return }
    bwrite $+(",%1,") -1 &1
    if ($file(%1).size >= $download.var($sockname,size)) {
      set % [ $+ [ $sockname $+ .status ] ] Done
      .signal -n download_complete $sockname
      close -d $sockname
      return
    }
    goto sockread
  }
}