mIRC Homepage

Problem with fibreoptics com snippet

Posted By: pouncer

Problem with fibreoptics com snippet - 24/02/09 11:32 PM

since he isn't around much more im hoping someone else can help me. basically this is the snippet here:

http://www.mircscripts.org/comments.php?cid=2968

i havent pasted the whole code here because there are also comments in there which may be useful to some of you in fixing this! my problem is this:

Code:
//echo -q $download($mircdirfootball.jpg, GET, http://www.dorlingkindersley-uk.co.uk/static/clipart/uk/dk/football/image_football006.jpg, 2))


now that works fine if my mirc.ini is in the same folder as the mirc.exe but if my mirc.ini is in the Application Data/mIRC folder then i get this error:

$download: Error - no data could be retrieved - http://www.dorlingkindersley-uk.co.uk/static/clipart/uk/dk/football/image_football006.jpg
$false
Posted By: DJ_Sol

Re: Problem with fibreoptics com snippet - 25/02/09 12:15 AM

So put the ini file in the $mircdir.

Or, handle the fact that mirc's default root is the directory the ini file is in.
Posted By: starbucks_mafia

Re: Problem with fibreoptics com snippet - 25/02/09 12:41 AM

He is handling it. Something within the $download() identifier is failing. There doesn't appear to be anything wrong with the identifier's code though, only thing I can guess is there's something bizarre in the behaviour of ado.savetofile() in the VBScript it outputs. Someone with more specific knowledge would have to confirm.

pouncer:
The comment before the code snippet lists fibreoptics' email address, I assumed you've tried emailing him?
Posted By: Horstl

Re: Problem with fibreoptics com snippet - 25/02/09 01:17 AM

I never fiddled with data objects... Maybe the "fso.createtextfile()" won't accept spaces in the path too (the way it's currently done; spaces seem to be the problem). But then, the file gets created in the directory (just remains empty)...
Posted By: DJ_Sol

Re: Problem with fibreoptics com snippet - 25/02/09 05:05 AM

$mircdir will return the dir the default mirc.ini is in. If you don't want to put mirc.ini in your mirc directory, use:

$nofile($mircexe)

to return your root directory.
Posted By: Horstl

Re: Problem with fibreoptics com snippet - 25/02/09 06:03 AM

Sorry, but you didn't get the point: There's a problem with this [nice] snippet - it is the thing to fix, and that's what he is asking for.

The snippet writes a short VBscript to a custom window, safebufs and runs this script via com call: it's using an ADO-"stream" to download binary data (creating an (empty) file, buffering the data in memory, dumping the downloaded data into the file).
This procedure seem to fail if the output filepath contains spaces (?) - and it's not unlikely for $mircdir to be a filepath with spaces.

As $mircdir points to a directory where read/write access may be taken for granted, and because it's a directory often used by mIRC scripts as point of reference for data storage (e.g. at subdirectories of $mircdir), neither a hardcoded, nonspaced directory will make up a true workaround, nor will changing the mircdir itself do - you cannot demand this of "end users" - and $nofile($mircexe) *may* contain spaces as well.
Posted By: pouncer

Re: Problem with fibreoptics com snippet - 25/02/09 09:50 AM

Yes that email address isn't valid i can't get in touch with him.. i wonder if hixxy has seen this as he told me to post this here!!
Posted By: hixxy

Re: Problem with fibreoptics com snippet - 27/02/09 12:40 AM

I've taken a look at it, but I can't find out where the error is. The script actually writes a function to the VBscript so that when there is an error, the script displays the specific error.
What you're seeing is basically the snippets way of displaying an "unknown error" - when everything should work, but for some reason it hasn't.
FiberOPtics may be able to tell what's up with it, but he doesn't really script much these days so it's unlikely you'll catch him.

If you just want to download that file, you may want to try a different download snippet. Here's one I wrote which uses sockets:

Code:
/*
**
** /download by hixxy (hixxy@mirc.net) for mIRC 6.17 or above.
**
**
** What the script does
** ????????????????????
**
** This script will let you download files from the internet using mIRC.
**
** To use some one of the script's features you will need to install SSL for mIRC.
** For help installing OpenSSL please take a look at this page: http://www.mirc.co.uk/ssl.html
**
** How to load
** ???????????
**
** You can load this script by typing /load -rs <path to download.mrc>. If you saved the file in your mIRC directory you can just do /load -rs download.mrc
** You can also just copy/paste it into remote from a website and then hit the OK button.
**
** How to use
** ??????????
**
** NOTE: You can use this to download from either HTTP or FTP servers.
**
** The syntax is one of the following.
** /download <-cpr> <name>
** /download [-eo] <name> <url> <file>
**
** The -cpr switches stand for cancel, pause and resume respectively. You can only use these on existing downloads.
**
** The -e switch means you want to create an SSL socket.
** The -o switch means you want to overwrite <file> if it exists rather than spitting out an error.
**
** The script files off various signals as it is downloading the file. These are all sent to the 'download' signal with parameters being:
**
** begin - when the download begins.
** cancel - when the download is cancelled.
** end - when the download finishes.
** error <error code> <error message> - when there's a socket error.
** notfound <file> - could not find the requested file.
** pause - when the download is paused.
** progress <size> - when the snippet has downloaded another chunk of the file. <size> is the amount of bytes downloaded.
** resume - when the download is resumed.
** size <size> - the size of the file has been acquired.
**
** FTP only signals:
**
** cannotassignaddress - the server sent a 425 error.
** requireslogin - the server does not allow anonymous access. 
**
** Contact
** ???????
**
** If you have a feature suggestion, bug report or question, please send an email to hixxy@mirc.net
** If you got the script from a scripting website and I (hixxy) submitted the code, you may also leave feedback on the script's comment page.
**
*/

alias -l raisemessage {
  if ($line($active,0)) && (($show) || ($1 == -e)) { linesep -a }
  echo $colour(info) -a $+ $iif($1 != -e,q $1-,$chr(32) $2-)
  if ($show) || ($1 == -e) { linesep -a }
  halt
}

alias download {
  var %overwrite, %ssl
  if ($isid) { raisemessage -e * /download: Should be called as a command }
  if ($0 < 3) && (!$istok(-c -p -r,$1,32)) { raisemessage -e * /download: Insufficient parameters }
  if (-* iswm $1) {
    if ((c isincs $1) || (p isincs $1) || (r isincs $1)) && (!$sock(download_ $+ $2)) { raisemessage -e * /download: No such download ' $+ $2' }
    if (c isincs $1) {
      if ($isfile($gettok($sock(download_ $+ $2).mark,3-,32))) { .remove $qt($gettok($sock(download_ $+ $2).mark,3-,32)) }
      if (http:* iswm $gettok($sock(download_ $+ $2).mark,2,32)) {
        sockclose download_ $+ $2
        .signal download $2 cancel
        raisemessage * /download: Cancelled download ' $+ $2'
      }
      elseif (ftp:* iswm $gettok($sock(download_ $+ $2).mark,2,32)) {
        sockclose download_ $+ $2
        sockclose ftp_download_ $+ $2
        sockclose ftpc_download_ $+ $2
        .signal download $2 cancel
        raisemessage * /download: Cancelled download ' $+ $2'
      }
    }
    elseif (p isincs $1) {
      if (http:* iswm $gettok($sock(download_ $+ $2).mark,2,32)) && (!$sock(download_ $+ $2).pause) {
        sockpause download_ $+ $2
        .signal download $2 pause
        raisemessage * /download: Paused download ' $+ $2'
      }
      elseif (ftp:* iswm $gettok($sock(download_ $+ $2).mark,2,32)) && (!$sock(download_ $+ $2).pause) {
        sockpause download_ $+ $2
        if ($sock(ftpc_download_ $+ $2)) { sockpause ftpc_download_ $+ $2 }
        .signal download $2 pause
        raisemessage * /download: Paused download ' $+ $2'
      }
    }
    elseif (r isincs $1) {
      if (http:* iswm $gettok($sock(download_ $+ $2).mark,2,32)) && ($sock(download_ $+ $2).pause) {
        sockpause -r download_ $+ $2
        .signal download $2 resume
        raisemessage * /download: Resumed download ' $+ $2'
      }
      elseif (ftp:* iswm $gettok($sock(download_ $+ $2).mark,2,32)) && ($sock(download_ $+ $2).pause) {
        sockpause -r download_ $+ $2
        if ($sock(ftpc_download_ $+ $2)) { sockpause -r ftpc_download_ $+ $2 }
        .signal download $2 resume
        raisemessage * /download: Resumed download ' $+ $2'
      }
    }
    if (e isincs $1) { var %ssl = $true }
    if (o isincs $1) { var %overwrite = $true }
    tokenize 32 $2-
  }
  if ($sock(download_ $+ $1)) { raisemessage -e * /download: Download exists ' $+ $1' }
  if ($isfile($3-)) && (!%overwrite) { raisemessage -e * /download: File exists ' $+ $3-' }
  if ($nopath($3-) == $null) { raisemessage -e * /download: No filename supplied ' $+ $3-' }
  if ($nofile($3-) $+ $mkfn($nopath($3-)) != $3-) { raisemessage -e * /download: Filename not valid $+(',$nopath($3-),') }
  if (%ssl) && (!$sslready) { raisemessage -e * /download: SSL not ready }
  if (http:* !iswm $2) && (ftp:* !iswm $2) { raisemessage -e * /download: Invalid URL }
  var %port = $iif(http:* iswm $2,80,21)
  while (!$portfree(%port)) { inc %port }
  sockopen $iif(%ssl == $true,-e) download_ $+ $1 $gettok($2,2,47) %port
  sockmark download_ $+ $1 $false $2-
  raisemessage * /download: Downloading $2 to $3-
}

on *:sockopen:download_*:{
  if ($sockerr) {
    .signal download $mid($sockname,10) error $sock($sockname).wserr $sock($sockname).wsmsg
    return
  }
  if (http:* iswm $gettok($sock($sockname).mark,2,32)) {
    sockwrite -n $sockname GET / $+ $gettok($gettok($sock($sockname).mark,2,32),3-,47) HTTP/1.1
    sockwrite -n $sockname Host: $gettok($gettok($sock($sockname).mark,2,32),2,47)
    sockwrite -n $sockname Accept: */*
    sockwrite -n $sockname Connection: close
    sockwrite -n $sockname User-Agent: mIRC/ $+ $version
    sockwrite -n $sockname
  }
}
on *:sockread:download_*:{
  if (http:* iswm $gettok($sock($sockname).mark,2,32)) {
    if ($gettok($sock($sockname).mark,1,32) == $false) {
      var %data
      sockread %data
      if (*404 not found* iswm %data) {
        .signal download $mid($sockname,10) notfound / $+ $gettok($gettok($sock($sockname).mark,2,32),3-,47)
        sockclose $sockname
        return
      }
      if (%data == $null) {
        sockmark $sockname $true $gettok($sock($sockname).mark,2-,32)
        .signal download $mid($sockname,10) begin
      }
      elseif (content-length: * iswm %data) { .signal download $mid($sockname,10) size $gettok(%data,2,32) }
    }
    else {
      sockread -f &data
      bwrite $qt($gettok($sock($sockname).mark,3-,32)) -1 -1 &data
      .signal download $mid($sockname,10) progress $sockbr
    }
  }
  else {
    var %data
    sockread %data
    tokenize 32 %data
    if ($1 == 150) { .signal download $mid($sockname,10) size $mid($gettok($1-,-2,32),2,-1) }
    elseif ($1-2 == 200 PORT) { sockwrite -n $sockname TYPE I }
    elseif ($1-2 == 200 TYPE) { sockwrite -n $sockname RETR $gettok($gettok($sock($sockname).mark,2,32),-1,47) }
    elseif ($1 == 220) { sockwrite -n $sockname USER anonymous }
    elseif ($1 == 226) {
      .signal download $mid($sockname,10) end
      sockclose $sockname
      sockclose ftp_ $+ $sockname
      sockclose ftpc_ $+ $sockname
    }
    elseif ($1 == 230) {
      if ($deltok($gettok($gettok($sock($sockname).mark,2,32),3-,47),-1,47) != $null) { sockwrite -n $sockname CWD / $+ $v1 }
    }
    elseif ($1 == 250) {
      socklisten ftp_download_ $+ $mid($sockname,10) 3762
      sockmark ftp_download_ $+ $mid($sockname,10) $sock($sockname).mark
      sockwrite -n $sockname PORT $replace($sock(ftp_download_ $+ $mid($sockname,10)).ip,.,$chr(44)) $+ ,14,178
    }
    elseif ($1 == 331) { sockwrite -n $sockname PASS ftpd }
    elseif ($1 == 425) {
      .signal download $mid($sockname,10) cannotassignaddress
      sockclose $sockname
    }
    elseif ($1 == 530) {
      .signal download $mid($sockname,10) requireslogin
      sockclose $sockname
    }
    elseif ($1 == 550) {
      .signal download $mid($sockname,10) notfound / $+ $gettok($gettok($sock($sockname).mark,2,32),3-,47)
      sockclose $sockname
    }
  }
}
on *:socklisten:ftp_download_*:{
  sockaccept ftpc_ $+ $mid($sockname,5)
  sockmark ftpc_ $+ $mid($sockname,5) $sock($sockname).mark
  .signal download $mid($sockname,14) begin
}
on *:sockclose:download_*:{ .signal download $mid($sockname,10) end }
on *:sockread:ftpc_*:{
  sockread -f &data
  while ($sockbr) {
    bwrite $qt($gettok($sock($sockname).mark,3-,32)) -1 -1 &data
    .signal download $mid($sockname,15) progress $sockbr
    sockread -f &data
  }
}

on *:load:{
  if ($version < 6.17) {
    !.echo -q $input(This script was created for mIRC version 6.17 or later $+ $chr(44) you are using $version,dho,Invalid version)
    .unload -rs $script
  }
}


Unfortunately it's a bit bloated for your needs because it supports downloading from FTP servers too, but it does the job!

Here is a script which I used to download and then open football.jpg when it had finished:

Code:
alias test download -o football http://www.dorlingkindersley-uk.co.uk/static/clipart/uk/dk/football/image_football006.jpg football.jpg

on *:signal:download:{
  if ($1 == football) && ($2 == end) run football.jpg
}


There are many different signals and things that my script fires off, which you can use if you need to. Just read the comments.

One more thing... if you don't want the /download: downloading .. message to appear, just call /.download instead of /download.
Posted By: FiberOPtics

Re: Problem with fibreoptics com snippet - 15/05/09 09:34 PM

Originally Posted By: pouncer
Yes that email address isn't valid i can't get in touch with him.. i wonder if hixxy has seen this as he told me to post this here!!


Hey, sorry, that email address has been deleted a long time ago.

I'm not into scripting anymore since a few years, but I'm sure a few of the regulars here can try to fix it.

Actually with this snippet I was trying to do something different than the usual socket stuff, only requiring a single alias. However it will work perfectly with sockets so I would opt for hixxy's code.

Either way, happy scripting!

Dieter
Posted By: MeStinkBAD

Re: Problem with fibreoptics com snippet - 13/10/09 02:22 PM

Originally Posted By: Horstl
Sorry, but you didn't get the point: There's a problem with this [nice] snippet - it is the thing to fix, and that's what he is asking for.

The snippet writes a short VBscript to a custom window, safebufs and runs this script via com call: it's using an ADO-"stream" to download binary data (creating an (empty) file, buffering the data in memory, dumping the downloaded data into the file).
This procedure seem to fail if the output filepath contains spaces (?) - and it's not unlikely for $mircdir to be a filepath with spaces.

As $mircdir points to a directory where read/write access may be taken for granted, and because it's a directory often used by mIRC scripts as point of reference for data storage (e.g. at subdirectories of $mircdir), neither a hardcoded, nonspaced directory will make up a true workaround, nor will changing the mircdir itself do - you cannot demand this of "end users" - and $nofile($mircexe) *may* contain spaces as well.


The vb script has to be in the same directory as the directory mIRC is told to start in. It has nothing to do w/ spaces.

Here try this...

Code:
alias download {
  var %r = $(|,) return $false, %e = scon -r !echo $color(info) -a $!!download: Error -
  if (!$isid) %e this snippet can only be called as an identifier. %r
  if ($os isin 9598) %e this snippet requires Windows ME or higher. %r
  if ($version < 6) %e this snippet requires mIRC version 6.0 or higher. %r
  var %dir = $nofile($1), %file = $nopath($1), %method = $upper($2), %url = $3
  var %bit = $4, %headers = $iif($2 == get,$5,$6), %postdata = " $+ $5", %res
  if (* !iswm %file) %e you must specify a file to save the data to. %r
  if (%file != $mkfn(%file)) %e file %file contains illegal characters. %r
  if (* !iswm %dir) %dir = $mircdir
  elseif (!$isdir(%dir)) %e no such folder %dir %r
  if (!$istok(get head post,$2,32)) %e method can only be GET, HEAD or POST. %r
  if (!$regex(%e,$3,/^\S+\.\S+\.\S+$/)) %e you didn't specify an url to download from. %r
  if ($2 != head) {
    if ($4 !isnum 1-3) %e bitmask should be a digit in range 1-3. %r
    if ($2 == post) && (* !iswm $5) %e you didn't specify any post data. %r
    if (%headers) && (!$regsub(%e,%headers,/(\S+?): (.+?)(?=\s?\n|$)/g,"\1" $chr(44) "\2",%headers)) {
      %e bad header syntax. Correct -> Label: value seperated by $!!lf's %r
    }
  }
  var %file = $+(",%dir,%file,"), %id = $+(@download,$ticks,$r(1111,9999),.vbs), %a = aline %id
  if (http://* !iswm $3) %url = http:// $+ $3
  .comopen %id wscript.shell
  if ($comerr) %e could not open Wscript.Shell. %r
  write -c %file
  window -h %id
  %a on error resume next 
  %a sub quit $lf set http = nothing : set ado = nothing : wscript.quit $lf end sub
  %a sub errmsg
  %a set fso = createobject("scripting.filesystemobject")
  %a set file = fso.createtextfile( %file ,true)
  %a file.write("Err number: " & err.number & " = " & err.description) : file.close
  %a set fso = nothing
  %a quit
  %a end sub
  %a arr = array("winhttp.winhttprequest.5.1","winhttp.winhttprequest","msxml2.serverxmlhttp","microsoft.xmlhttp")
  %a i = 0 $lf while i < 4 and not isobject(http) : set http = createobject(arr(i)) : i = i + 1 : wend
  %a if not isobject(http) then errmsg
  %a err.clear
  %a http.open $+(",%method,") , $+(",%url,") ,false
  %a http.setrequestheader "User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"
  if (%headers) { tokenize 10 %headers | scon -r %a http.setrequestheader $* }
  if (%method == post) {
    %a http.setrequestheader "Content-Type","application/x-www-form-urlencoded"
    %a http.send %postdata
  }
  else %a http.send
  %a if err then errmsg
  %a set ado = createobject("adodb.stream")
  %a if not isobject(ado) then errmsg
  %a ado.open
  if (%bit != 2) {
    %a ado.type = 2 : ado.charset = "ascii"
    %a ado.writetext "HTTP/1.1 " & http.status & " " & http.statustext,1
    %a ado.writetext http.getallresponseheaders,1 : ado.position = 0
  }
  if (%bit != 1) %a ado.type = 1 : ado.read : ado.write http.responsebody
  %a ado.savetofile %file ,2 : ado.close : quit
  savebuf %id $+($nofile($mircexe),%id)
  close -@ %id
  .comclose %id $com(%id,run,1,bstr*,%id,uint,0,bool,true)
  .remove $+($nofile($mircexe),%id)
  %res = $read(%file,t,1)
  if (Err number:*=* iswm %res) || (!$file(%file)) %e $iif(%res,%res,no data could be retrieved) - %url %r
  return $true
  :error
  if ($com(%id)) .comclose %id
  if ($isfile(%id)) .remove %id
  if ($window(%id)) close -@ %id
  return $false
}

Posted By: Horstl

Re: Problem with fibreoptics com snippet - 13/10/09 04:53 PM

Nice spot smile
The .vbs is created at $mircdir (until another dir is specified - like in your edit), and the final $com-call obviously has to call it there.
One issue remains with your edit though: you can assume r/w rights at $mircdir, but you cannot take them for granted at $nofile($mircexe). And (side note only) you'd need to quote $+($nofile($mircexe),%id) (for the /savebuf and /remove commands, as $nofile($mircexe) may contain spaces.

I suggest the following change (based on the original snippet which savebuffs the .vbs "safely" at $mircdir):
Code:
 .comclose %id $com(%id,run,1,bstr*,$qt($mircdir $+ %id),uint,0,bool,true)
instead of the original
Code:
 .comclose %id $com(%id,run,1,bstr*,%id,uint,0,bool,true)

Posted By: Lpfix5

Re: Problem with fibreoptics com snippet - 13/10/09 10:06 PM

MeStinkBAD I think it's time we introduce you to consolidation variables and if elseif and else statements with || and &&

:S Holy wow no offense.
Posted By: Corey_

Re: Problem with fibreoptics com snippet - 27/10/09 09:44 PM

Thanks for this, hixxy! It does just what I want.
© 2022 mIRC Discussion Forums