mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Apr 2012
Posts: 11
D
Pikka bird
OP Offline
Pikka bird
D
Joined: Apr 2012
Posts: 11
Hi! I have an Alias that uses $comcall to reference another Alias that has a /return in it. How can I retrieve /return's value from the original Alias?

^^^That's basically the entire question. Please don't let the size of the code below scare you off, you don't have to read it.

It's just the code I'm using for those who want to see everything. Please don't feel like you have to read or understand any of it, as it is a heavily butchered version of this this script: http://www.hawkee.com/snippet/5397/

Code:
alias test {
  ;This is the test alias to see if I can get /returns
  echo $xml.document(http://api.imgur.com/2/image/D2ht4,imgurLoaded) this works fine
}

;This alias is the target of the $comcall
alias imgurLoaded {
  ;first check if document loaded correctly, $1 is a reference to the document
  ;echo imgurLoaded one < $1 > two < $2 > v1 < $v1 >
  
  if (!$xml.parseError($1)) {
    ;echo -a $xml.node($1,//original).text
    echo -a $iif($xml.selectNode($1,//original) && $v1 != $com($v1,text,2),$com($v1).result) $xml.destroy($v1)
  }
  noop $xml.destroy($1)
  
  return This is imgurLoaded's return! Success! Exactly what I'm looking for... except I can't get it :(
}

alias xml.document {
  ;echo xml.document one < $1 > two < $2 >
  var %document  = $xml._register(document),%error 
  .comopen %document MSXML.DOMDOCUMENT
  noop $com(%document,setProperty,3,bstr,SelectionLanguage,bstr,XPath)  $com(%document,async,5,bool,$iif($2,0,-1))
  
  ; Here is the $comcall I was talking about!
  noop testsuppie $comcall(%document,$2 %document,load,3,bstr,$1)
  
  ;echo Testing imgurLoaded's return: $imgurLoaded(%document) 
  
  return xml.document's return! Slightly successful :(
}

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; YOU CAN STOP READING HERE. The following is just support code that doesn't change anything important
; Just leaving it here so you can copy/paste to test if you want.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

alias imgur {
  ;Original primary alias from the hawkee link, I don't use it because it doesn't use /return
  noop $xml.document($+(http://api.imgur.com/2/image/, $1),imgurLoaded)
}

alias xml.loadXML if ($xml.instanceOf($1) == document) return $com($1,loadXML,3,bstr,$2-)
alias xml.selectNode return $iif($xml._register(node,$1) && $v1 != $com($1,selectSingleNode,3,bstr,$2,dispatch* $v1),$v1) 
alias xml.selectNodeSet return $iif($xml._register(nodeSet,$1) && $v1 != $com($1,selectNodes,3,bstr,$2,dispatch* $v1),$v1) 

alias xml.node {
  ;echo xml.node one < $1 > two < $2 > v1 < $v1 > prop < $prop >
  return $iif($xml.selectNode($1,$2) && $v1 != $com($v1,$prop,2),$com($v1).result) $xml.destroy($v1)
}
 
alias xml.setAttributes {
  if ($$2) && ($com($1)) {
    var %i 2
    while (%i <= $0) { 
      noop $com($1,setAttribute,3,bstr,$(,$ $+ %i),bstr,$(,$ $+ $calc(%i + 1)))
      inc %i 2
    }
  }
}
 
alias xml.destroy { 
  if ($xml.reference($$1)) {
    var %nodes = $com(_xmlCom,selectNodes,3,bstr,$+(//*[@id=',$1'],//@id),dispatch* nodes)
    while ($iif($com(nodes,nextNode,3,dispatch* ~xml) && $com(~xml),~xml)) {
      var %comObject = $iif($com(~xml,text,2),$com(~xml).result)
      if ($com(%comObject)) .comclose %comObject
      .comclose ~xml
    }
    .comclose nodes $com(_xmlCom,selectSingleNode,3,bstr,$+(//*[@id=',$1']),dispatch* $+(node,$1)) $xml.remove($+(node,$1),$true)
  }
  else echo -a $1
}
alias xml.remove {
  if ($xml.reference($1) || node* iswm $1) {
    noop $com($1,parentNode,3,dispatch* $+(parent,$1))
    noop $com($+(parent,$1),removeChild,3,dispatch,$1)
    .comclose $+(parent,$1)
  }
}

alias -l xml._register {
  if (!$hget(_xml,_constructed)) xml._constructor
  var %name = $xml._newName($1,$2)
  noop $com(_xmlCom,selectSingleNode,3,bstr,$iif($2,$+(//*[@id=',$2']),/*),dispatch* _parent)
  .comclose _parent $com(_xmlCom,createElement,3,bstr,$1,dispatch* _newNode) $com(_parent,appendChild,3,dispatch,_newNode)
  .comclose _newNode $com(_newNode,setAttribute,3,bstr,id,bstr,%name) 
  hadd -m _xml %name $1
  return %name
}
alias xml._newName {
  return $gettok($+($gettok($2,1,46),._xml,$ticks,$com(0)),1-,46)
}
alias xml.reference return $iif($hget(_xml,$1),$true,$false)
alias xml.instanceOf return $iif($hget(_xml,$1),$v1)
alias -l xml._ownerDocument return $gettok($1,1,46)

alias xml.resetAll {
  xml._destructor 
  xml._constructor
  echo -a removed all xml object references 
}
alias xml._constructor {
  .comopen _xmlCom MSXML.DOMDOCUMENT
  hadd -m _xml _constructed $com(_xmlCom,loadXML,3,bstr,<xmlComConnections />)
}
alias xml._destructor {
  if ($com(_xmlCom)) .comclose _xmlCom 
  hfree _xml 
  var %i = $com(0)
  while (%i > 0) { 
    if (_xml* iswm $com(%i)) .comclose $v2
    dec %i
  }
  hadd -m _xml _constructed 0
}


Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
Not sure why you complicated this with $com since it seems your question has nothing to do with them.

Quote:
How can I retrieve /return's value from the original Alias?
look at /help $result but here is an example:

Code:
alias a return b
alias c noop $a | echo -a $result


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Apr 2012
Posts: 11
D
Pikka bird
OP Offline
Pikka bird
D
Joined: Apr 2012
Posts: 11
Unfortunately, $result doesn't work here because it works instantly. If I have

Code:
noop $xml.document(http://api.imgur.com/2/image/D2ht4,imgurLoaded) | echo -a $result RESULTPRINT


I just get RESULTPRINT because the alias hasn't returned a result yet because $com is still thinking - it's going online and accessing an XML file, after all. (I think, please correct if I'm wrong.)

The only way I think I can get past this is to set the result to a global variable and make a while loop that checks to see if that variable is set, if not, wait .1 seconds and check again... but obviously this is really really ugly and I want to avoid doing that frown

Thanks for the suggestion though!

Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
Well, I didn't understand what "result" you were looking for..
$comcall is asynchronous, which is why you need an alias to know when it's done.
It means that indeed you cannot get the information from the original alias since nothing has been done (well you get the value of what $xml.document() returned in $result)
Of course, you can't have the asynchronous $comcall and still have the synchronous advantages, but you can't be willing to have both either, it doesn't make sense: you want to do something once you have the information, so just do what you want to do from that point with this result inside your callback alias.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Apr 2012
Posts: 11
D
Pikka bird
OP Offline
Pikka bird
D
Joined: Apr 2012
Posts: 11
(Sorry about breaking the screen width, some lines in the script are unfortunately long.)

I'll be honest, I barely understand you - synchronous advantages? Asynchronous? frown My education in programming/scripting is only hobby level, pretty weak on the jargon. Thanks very much anyway Wims! Your knowledge led me on some interesting Google searches! And I found solution, kinda.

I think Signals would have helped me with my original question: http://www.mircscripts.org/showdoc.php?type=tutorial&id=1305

However I steered away from that monstrous XML script and wrote my own from scratch, learning sockets and such along the way. The script takes i.imgur.com/XXXXX and turns it into i.imgur.com/XXXXX.jpg so you can load the image with one click by using this script: http://sephiroth.bounceme.net/board/viewtopic.php?t=104 I wanted to access the Imgur API instead of blindly appending jpg because the sephiroth link did not animate .gifs. Nearly every variable in the script is dynamically generated, so that way the lines should still load if multiple people post links but one is a bit slow on loading the API. You'll probably need to edit a few things about this script if you want to use it - it includes references to outside variables and channel specific stuff (%dharmaoptions and Destin(y)). Feel free to /query me at the information in my profile.

Code:
; dynamic variables inspired by http://forum.swiftirc.net/viewtopic.php?f=34&t=7969&view=next

;Inspired by BillGreen and http://stackoverflow.com/questions/4867035/how-do-i-change-the-color-of-links-in-mirc/6071100#6071100
;this is actually needed to bypass mIRC's parsing behavior of strtok(str, ":")
alias urlreg return /(\x03\d{1,2}(?:,\d{1,2})?|)(.*?)(((?:(?:https?://)|(?:www\.))(?!\.)\S+\.(?!\.)\S+)|(?:\S+\.(?:com|org|edu|gov|net|tv|uk|cc|xxx|mil|co|us|se|fm|me|de|kr|sv|fr|jp|tk|be|ly|info|biz|us|in|mobi)\S*))/ig
alias imgurReg return /(\x03\d{1,2}(?:,\d{1,2})?|)(.*?)(\S+i\.imgur\.com/[\w]{4,})(\s|$)/ig
alias idreg return /(.*?\S+i\.imgur\.com/)([\w]{4,})|(.*)/ig
alias urlcolor return $+($chr(3), 12, $chr(31), $1-, $chr(3), $chr(31))
alias selfurlcolor return $+($chr(3), 12, $chr(31), $1-, $chr(31), $chr(3), $color(own))

;trigger for the regex event only
on ^&$*:text:$($imgurReg):*:{
  inc %imgurID
  var %n = $calc( $regex($1-, $idreg) - 2 )
  set %imgur. [ $+ [ %imgurID ] $+ [ .Count ] ] %n
  
  ;if we are in a channel, turn nick into @nick if applicable
  set %imgur. [ $+ [ %imgurID ] $+ [ .Nick ] ] $iif($chan, $nick($chan, $nick).pnick, $nick)
  
  ;color all the linkes using the predefined alias above
  set %imgur. [ $+ [ %imgurID ] $+ [ .Msg ] ] $regsubex($1-, $urlreg,\1\2$urlcolor(\3)\1)
  set %imgur. [ $+ [ %imgurID ] $+ [ .Chan ] ] $chan
  set %imgur. [ $+ [ %imgurID ] $+ [ .Color ] ] $cnick($nick).color
  if ( $nick isop $chan ) {
    set %imgur. [ $+ [ %imgurID ] $+ [ .Color ] ] %DharmaOptions.opcolor
    if (destin isin %%imgurNick) {
      set %imgur. [ $+ [ %imgurID ] $+ [ .Color ] ] 4
    }
  }
  
  tokenize 3 $regsubex($1- , $idreg , \2 $chr(3))
  while (%n != 0) {
    set %imgur. [ $+ [ %imgurID ] $+ [ .Url. ] $+ [ %n ] ] [ $eval($($+($,%n)) , 2) ]
    noop $imgur $eval($($+($,%n)) , 2)
    ;inc %imgur. [ $+ [ %imgurID ] $+ [ .SockID. ] $+ [ %n ] ]
    sockopen $+( imgur. [ $+ [ %imgurID ] $+ [ .Url. ] $+ [ %n ] ] ) api.imgur.com 80
    dec %n
  }
  
  ;prevent mIRC's default echo
  haltdef
}

on *:sockopen:imgur.*: {
  sockwrite -nt $sockname GET /2/image/ $+ $eval( $+( %, $sockname) , 2 ) HTTP/1.1
  sockwrite -nt $sockname Host: api.imgur.com
  sockwrite -nt $sockname $crlf
}
on *:sockread:imgur.*: {
  var %read
  sockread %read
  if (*<original>* iswm %read ) {
    var %start = /((http|www)\S*)?imgur\.com/(
    var %mid = %imgur. [ $+ [ $gettok($sockname,2,46) ] $+ [ .Url. ] $+ [ $gettok($sockname,4,46) ] ]
    var %end = [^ \t\r\n\x31\x03]{0,5})/g
    set %imgur. [ $+ [ $gettok($sockname,2,46) ] $+ [ .Msg ] ] $regsubex( %imgur. [ $+ [ $gettok($sockname,2,46) ] $+ [ .Msg ] ] , $+( %start , %mid , %end ) , $regsubex(%read , /.*<original>|</original>.*/sig , \1 ) )
    
    inc %imgur. [ $+ [ $gettok($sockname,2,46) ] $+ [ .final ] ]
    if ( %imgur. [ $+ [ $gettok($sockname,2,46) ] $+ [ .final ] ] ==  %imgur. [ $+ [ $gettok($sockname,2,46) ] $+ [ .Count ] ] ) {
      ;print the message, default timestamp, highlighting options, and nick coloring
      echo -tcrl normal $iif( %imgur. [ $+ [ %imgurID ] $+ [ .Chan ] ] , $v1, %imgur. [ $+ [ %imgurID ] $+ [ .Nick ] ] ) $+(<, $chr(3), %imgur. [ $+ [ %imgurID ] $+ [ .Color ] ] , %imgur. [ $+ [ %imgurID ] $+ [ .Nick ] ] , $chr(3), >) %imgur. [ $+ [ $gettok($sockname,2,46) ] $+ [ .Msg ] ]
      unset [ $+( %, imgur. , $gettok($sockname,2,46) , * ) ]
    }
    sockclose $sockname
  }
  elseif (*error* iswm %read ) {
    inc %imgur. [ $+ [ $gettok($sockname,2,46) ] $+ [ .final ] ]
    if ( %imgur. [ $+ [ $gettok($sockname,2,46) ] $+ [ .final ] ] ==  %imgur. [ $+ [ $gettok($sockname,2,46) ] $+ [ .Count ] ] ) {
      ;print the message, default timestamp, highlighting options, and nick coloring
      echo -tcrl normal $iif( %imgur. [ $+ [ %imgurID ] $+ [ .Chan ] ] , $v1, %imgur. [ $+ [ %imgurID ] $+ [ .Nick ] ] ) $+(<, $chr(3), %imgur. [ $+ [ %imgurID ] $+ [ .Color ] ] , %imgur. [ $+ [ %imgurID ] $+ [ .Nick ] ] , $chr(3), >) %imgur. [ $+ [ $gettok($sockname,2,46) ] $+ [ .Msg ] ]
      unset [ $+( %, imgur. , $gettok($sockname,2,46) , * ) ]
    }
    sockclose $sockname
  }
  ;also need on a fail, like after 30 seconds, it prints the text regardless of sock success
}

Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
Asynchronous means that it doesn't "block" the execution of the code like a while loop would, $comcall will do its job and then call the alias you specified, because it's the only way to know when $comcall "did its job" (note: a /signal could be sent, that's basically the same idea).
You could use signal but it's not needed, what I was saying is that once the alias you specified is called, you have the information you want and you can go on from that point.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel

Link Copied to Clipboard