Advertisement
Guest User

Untitled

a guest
Jun 11th, 2015
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 5.30 KB | None | 0 0
  1. /*
  2.     $json
  3.     Written by Timi
  4.     http://timscripts.com/
  5.  
  6.     JSON, JavaScript Object Notation, is a popular format by which data is read and written.
  7.     Many sites and APIs, such as Google's AJAX APIs, use this format, as it easy to parse and
  8.     is widely supported.
  9.  
  10.     This snippet can read any value from valid JSON. It uses the MSScriptControl.ScriptControl object
  11.     to input the data where is can be parsed using JavaScript. Calls are made to this object to
  12.     retrieve the needed value.
  13.  
  14.     This script identifier also has the ability to get JSON data from a http://siteaddress.com
  15.     source. This can be done by specifying a URL beginning with http://. Files on your computer can
  16.     also be used. With URLs, the data will also be cached so multiple calls to the same data won't
  17.     require getting the data over again. The cache will be cleared every 5 mins, but it can be cleared
  18.     manually using /jsonclearcache. The URL must be encoded or else the query may not be completed correctly.
  19.  
  20.     Also included are two identifiers used for encoding URLs. One is for encoding an entire URL, and the other
  21.     is used for encoding a specific component of a url.
  22.  
  23.     Syntax:
  24.         $json(<valid JSON, file, or URL>,name/index,name/index2,...,name/indexN)[.count]
  25.         Note: When inputting JSON, it is recommended that a variable is used because JSON uses commas
  26.         to separate values. Also, URLs must begin with http://
  27.  
  28.         /clearjsoncache [URL]
  29.         Clears the JSON cache created when JSON is retrieved from URLs
  30.         If a URL is specified, the cache for that URL will be cleared.
  31.  
  32.         $json.enc(<URL>)
  33.         Encodes a URL. Uses JavaScript's encodeURI().
  34.  
  35.         $json.enccomponent(<URL Component>)
  36.         Encodes a URL component.
  37.  
  38.     Examples:
  39.         var %json = {"mydata":"myvalue","mynumbers":[1,2,3,5],"mystuff":{"otherdata":{"2":"4","6":"blah"}}}
  40.  
  41.         $json(%json,mydata) == myvalue
  42.         $json(%json,mynumbers,2) == 3 ;the array is indexed from 0
  43.         $json(%json,mystuff,otherdata,"6") == blah      ;if a name is a number, quotes must be used as to not confuse
  44.                                     ;it with an array.
  45.  
  46.         http://site.com/?component= $+ $json.enccomponent(&)
  47.         In the example, & is encoded because & usually separates other components. $json.enc would not
  48.         encode this.
  49.         ----------
  50.         Google Web Search example at end.
  51.  
  52.  
  53. */
  54.  
  55. alias json {
  56.   if ($isid) {
  57.     ;name of the com interface declared so I don't have to type it over and over again :D
  58.     var %c = jsonidentifier,%x = 2,%str,%p,%v,%addr
  59.  
  60.     ;filter out some characters in $1 (address)
  61.     if ($isfile($1)) { %addr = $qt($replace($1,\,\\,;,\u003b,",\u0022)) }
  62.     else { %addr = $qt($replace($1,;,\u003b,",\u0022)) }
  63.  
  64.     ;if the interface hasnt been open and initialized, do it.
  65.     json.comcheck
  66.     if (!$timer(jsonclearcache)) { .timerjsonclearcache -o 0 300 jsonclearcache }
  67.  
  68.     ;get the list of parameters
  69.     while (%x <= $0) {
  70.       %p = $($+($,%x),2)
  71.       if (%p == $null) { noop }
  72.       elseif (%p isnum || $qt($noqt(%p)) == %p) { %str = $+(%str,[,%p,]) }
  73.       else { %str = $+(%str,[",%p,"]) }
  74.       inc %x
  75.     }
  76.     if ($prop == count) { %str = %str $+ .length }
  77.  
  78.     ;check to see if source is file
  79.     if ($isfile($1)) {
  80.       if ($com(%c,eval,1,bstr,$+(str2json,$chr(40),filejson,$chr(40),%addr,$chr(41),$chr(41),%str))) { return $com(%c).result }
  81.     }
  82.     ;check to see if source is url
  83.     elseif (http://* iswm $1 || https://* iswm $1) {
  84.       ;if url is in cache, used cached data
  85.       if ($com(%c,eval,1,bstr,$+(str2json,$chr(40),urlcache[,%addr,],$chr(41),%str))) { return $com(%c).result }
  86.       ;otherwise, get data
  87.       elseif ($com(%c,eval,1,bstr,$+(urlcache[,%addr,]) = $+(httpjson,$chr(40),$qt($1),$chr(41)))) {
  88.         if ($com(%c,eval,1,bstr,$+(str2json,$chr(40),urlcache[,%addr,],$chr(41),%str))) { return $com(%c).result }
  89.       }
  90.     }
  91.     ;get data from inputted json
  92.     elseif ($com(%c,eval,1,bstr,$+(x=,$1,;,x,%str,;))) { return $com(%c).result }
  93.   }
  94. }
  95. alias jsonclearcache {
  96.   if ($com(jsonidentifier)) {
  97.     if (!$1) { noop $com(jsonidentifier,executestatement,1,bstr,urlcache = {}) }
  98.     else { echo -a $com(jsonidentifier,executestatement,1,bstr,urlcache[" $+ $1 $+ "] = "") }
  99.   }
  100. }
  101. alias json.enc {
  102.   json.comcheck
  103.   if ($com(jsonidentifier,eval,1,bstr,encodeURI(" $+ $1- $+ "))) { return $com(jsonidentifier).result }
  104. }
  105. alias json.enccomponent {
  106.   json.comcheck
  107.   if ($com(jsonidentifier,eval,1,bstr,encodeURIComponent(" $+ $1- $+ "))) { return $com(jsonidentifier).result }
  108. }
  109. alias -l json.comcheck {
  110.   var %c = jsonidentifier
  111.   if (!$com(%c)) {
  112.     .comopen %c MSScriptControl.ScriptControl
  113.     ;add two javascript functions for getting json from urls and files
  114.     noop $com(%c,language,4,bstr,jscript) $com(%c,addcode,1,bstr,function httpjson(url) $({,0) y=new ActiveXObject("Microsoft.XMLHTTP");y.open("GET",url,false);y.send();return y.responseText; $(},0))
  115.     noop $com(%c,addcode,1,bstr,function filejson (file) $({,0) x = new ActiveXObject("Scripting.FileSystemObject"); txt1 = x.OpenTextFile(file,1); txt2 = txt1.ReadAll(); txt1.Close(); return txt2; $(},0))
  116.     ;add function to securely evaluate json
  117.     noop $com(%c,addcode,1,bstr,function str2json (json) $({,0) return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(json.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + json + ')'); $(},0))
  118.     ;add a cache for urls
  119.     noop $com(%c,addcode,1,bstr,urlcache = {})
  120.   }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement