$findmdx() — File Browser

findmdxv1.0 / findmdxv1.0 / findmdx / findmdx.mrc

findmdx.mrc — 4.75 KB — Download this file

;$findmdx() by teh
;;An addon whose purpose is to minimize the size of addons which use MDX -- by allowing the author to use already present MDX files 
;;in $mircdir or download at runtime, if none are present.

;; Retrieves a file from a specified URL 
; usage: /getfile <URL> [port] 
; usage: $getfile(<sockname>,<item>) 
; usage: $getfile(N)[.host|.path|.file|.length + any $sock property] 
alias getfile { 
  if ($isid) { 
    var %sock = $iif($2,$1,$sock(getfile.*,$1)), %item = host path file length 
    var %value = $gettok($sock(%sock).mark,$findtok(%item,$iif($2,$2,$prop),1,32),32) 
    if (!$2) { 
      if (!$prop) return $sock(getfile.*,$1).status 
      elseif (!$istok(%item,$prop,32)) return $sock(getfile.*,$1) [ $+ . [ $+ [ $prop ] ] ] 
    } 
    return %value 
  } 
  var %socket = $+(getfile.,$ticks) 
  var %regex = $regex(url,$1-,m~http\:\/\/ ([^\/]+) (.*\/) ([\w\-\.]+\.\w{3,5})~xg) 
  sockopen %socket $regml(url,1) $iif($2,$2,80) 
  sockmark %socket $regml(url,1) $regml(url,2) $regml(url,3) 
} 

on *:sockopen:getfile.*: { 
  sockwrite -n $sockname GET $getfile($sockname,path) $+ $getfile($sockname,file) HTTP/1.1 
  sockwrite -n $sockname Accept: */* 
  sockwrite -n $sockname Accept-encoding: gzip/deflate 
  sockwrite -n $sockname Host: $getfile($sockname,host) 
  sockwrite -n $sockname Connection: close 
  sockwrite -n $sockname 
} 

on *:sockread:getfile.*: { 
  sockread 4096 &temp 
  if ($sock($sockname).rcvd <= 4096) { 
    if ($bfind(&temp,1,content-length:)) { 
      var %length_start = $calc($ifmatch + 15), %length_end = $calc($bfind(&temp,%length_start,13 10) - %length_start) 
      var %length = $bvar(&temp,%length_start,%length_end).text 
      sockmark $sockname $sock($sockname).mark %length 
    } 
    bcopy -c &temp 1 &temp $calc($bfind(&temp,1,10 13) + 3) -1 
  } 
  bwrite $+(",$scriptdir,$getfile($sockname,file),") -1 -1 &temp
} 

on *:sockclose:getfile.*: .signal getfile $getfile($sockname,file) 

on *:signal:getfile: {
  munzip Unzip -odS mdx $+($shortfn($scriptdir),$1) $+($shortfn($scriptdir),mdx)
  unset %MDX_sleep
  .remove $+(",$scriptdir,$1")
}

alias -l munzip dll $shortfn($scriptdirmUnzip.dll) $$1- 
alias -l mdx_download getfile http://members.aol.com/dragonzap1/mdx-beta-0.92.zip

alias sleep {
  var %a = $ticks $+ .wsf
  write %a <job id="js"><script language="jscript">WScript.Sleep( $+ $$1 $+ );</script></job>
  .comopen %a WScript.Shell
  if !$comerr { .comclose %a $com(%a,Run,3,bstr,%a,uint,0,bool,true) }
  .remove %a
}

;; Function: finds and/or downloads MDX files and 'installs' them
;; Usage: /findmdx <-as> [file1] [file2] [fileN]
;; Comments: -s searches for file(s) in your mIRC directory and moves it, if not found it downloads it. -a downloads all files.
alias findmdx {
  ; get parameters
  if (-* iswm $1) var %switches = $1, %files = $2-

  ; A switch
  if (a isletter %switches) mdx_download

  ; S switch
  elseif (s isletter %switches) {
    ; list of MDX files
    var %filelist = BARS.HLP BARS.MDX CTL_GEN.HLP CTL_GEN.MDX DIALOG.HLP dialog.mdx filecopy.avi mdx.cnt MDX.DLL MDX.HLP mdx.mrc mdxtut.mrc users.ico VIEWS.HLP VIEWS.MDX 
    ; loop through the specified files and search for them, download if missing
    var %i = 1
    while ($gettok(%files,%i,32)) {
      var %file = $ifmatch

      ; check if file is in list of MDX files
      if ($istok(%filelist,%file,32)) {
        var %foundfile = $findfile($scriptdir,%file,1,0)
        ; if file is found, copy file to script folder
        if (%foundfile) .copy $shortfn(%foundfile) $+($shortfn($scriptdir),%file)
        ; otherwise add the file to a list for later download
        else var %download_files = $addtok(%download_files,%file,32)
      }

      ; if specified file isn't in possible list of files, throw error and exit!
      else {
        echo -sc info * /findmdx: insufficient parameters - %file is not a valid MDX file
        return 
      }

      inc %i
    }
    ; if there are some files to download, download them
    if (%download_files) {
      mdx_download
      ; sleep until files are finished downloading (10 second timeout on download)
      inc -u10 %MDX_sleep
      while (%MDX_sleep) { sleep 10 }
      ; loop through the files and move the appropriate ones
      var %i = 1
      while ($gettok(%download_files,%i,32)) {
        .copy $+($shortfn($scriptdirmdx\),$ifmatch) $+($shortfn($scriptdir),$ifmatch)
        inc %i
      }
      ; remove the unwanted MDX files
      .echo -q $findfile($shortfn($scriptdirmdx),*.*,0,.remove $shortfn($1-))
      ; remove the MDX directory
      .rmdir $shortfn($scriptdirmdx)
    }
  }

  ; neither a nor s were specified as switches - throw error!
  else echo -sc info * /findmdx: insufficient parameters
}