Advertisement
westor

Twitch Welcome New Follower (DEBUG) for VitruxPT

Jan 20th, 2015
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 23.01 KB | None | 0 0
  1. ON *:PART:#: {
  2.   if ($nick == $me) { unset %stream_auto_ [ $+ [ $chan ] ] | .timer[FOLLOWERS_CHECK_ [ $+ [ $chan ] ] off }
  3. }
  4.  
  5. ON *:KICK:#: {
  6.   if ($knick == $me) { unset %stream_auto_ [ $+ [ $chan ] ] | .timer[FOLLOWERS_CHECK_ [ $+ [ $chan ] ] off }
  7. }
  8.  
  9. ON *:JOIN:#: {
  10.   if ($nick == $me) && (%stream_chan_ [ $+ [ $chan ] ]) && (%stream_auto_ [ $+ [ $chan ] ]) { .timer[FOLLOWERS_CHECK_ $+ $chan $+ ] 0 5 twitch_check_new $chan %stream_chan_ [ $+ [ $chan ] ] }
  11. }
  12.  
  13. ON !*:TEXT:!*:#: {
  14.   if ($1 == !autofollow) {
  15.     if ($nick isop # || $nick == VitruxPT) {
  16.       if (!$2) { .msg $chan ( $+ $nick $+ ): Error, try again and use ON or OFF options! | return }
  17.       var %opt = $2
  18.       if (%opt !== ON) && (%opt !== OFF) { .msg $chan ( $+ $nick $+ ): Error, Incorrect parameters, try again and use ONLY the options ON or OFF. | return }
  19.       if (%opt == ON) {
  20.         if (!%stream_chan_ [ $+ [ $chan ] ]) { .msg $chan ( $+ $nick $+ ): Error, There is NOT any $qt(Stream Channel) set, Please use the !SetStreamChan command to set one! | return }
  21.         if (%stream_auto_ [ $+ [ $chan ] ]) { .msg $chan ( $+ $nick $+ ): Error, The AUTO-FOLLOW-WELCOME module is already running! }
  22.         else {
  23.           set %stream_auto_ [ $+ [ $chan ] ] $ctime
  24.           .timer[FOLLOWERS_CHECK_ $+ $chan $+ ] 0 5 twitch_check_new $chan %stream_chan_ [ $+ [ $chan ] ]
  25.           .msg $chan ( $+ $nick $+ ): The AUTO-FOLLOW-WELCOME module has been enabled. - (Twitch Channel:  $+ %stream_chan_ [ $+ [ $chan ] ] $+ )
  26.         }
  27.       }
  28.       if (%opt == OFF) {
  29.         if (!%stream_chan_ [ $+ [ $chan ] ]) { .msg $chan ( $+ $nick $+ ): Error, There is NOT any $qt(Stream Channel) set, Please use the !SetStreamChan command to set one! | return }
  30.         if (!%stream_auto_ [ $+ [ $chan ] ]) { .msg $chan ( $+ $nick $+ ): Error, The AUTO-FOLLOW-WELCOME module is already NOT running! }
  31.         else {
  32.           .timer[FOLLOWERS_CHECK_ $+ $chan $+ ] off
  33.           unset %stream_auto_ [ $+ [ $chan ] ]
  34.           .msg $chan ( $+ $nick $+ ): The AUTO-FOLLOW-WELCOME module has been disabled. - (Twitch Channel:  $+ %stream_chan_ [ $+ [ $chan ] ] $+ )
  35.         }
  36.       }
  37.     }
  38.   }
  39.   if ($1 == !setstreamchan) {
  40.     if ($nick isop # || $nick == VitruxPT) {
  41.       if (!$2) { .msg $chan ( $+ $nick $+ ): Error, Not enough parameters, try again and enter the Twitch Channel correctly! | return }
  42.       if (%stream_auto_ [ $+ [ $chan ] ]) { .msg $chan ( $+ $nick $+ ): Error, You must first disable the AUTO-FOLLOW-MESSAGE module and then change the stream channel! | return }
  43.       set %stream_chan_ [ $+ [ $chan ] ] $2
  44.       .msg $chan ( $+ $nick $+ ): The Twitch Channel has been set, now use !AutoFollow command to enable/disable the AUTO-FOLLOW-WELCOME module.
  45.     }
  46.     else { .msg $chan ( $+ $nick $+ ): Error, You are NOT an channel operator! | return }
  47.   }
  48.   if ($1 == !streamchan) {
  49.     if ($nick !isop $chan) { .msg $chan ( $+ $nick $+ ): Error, You are NOT an channel operator! | return }
  50.     if (!%stream_chan_ [ $+ [ $chan ] ]) { .msg $chan ( $+ $nick $+ ): Error, There is NOT any Twitch Channel has been set! | return }
  51.     .msg $chan ( $+ $nick $+ ): The $chan Twitch Stream Channel is now:  $+ %stream_chan_ [ $+ [ $chan ] ] $+
  52.   }
  53.   if ($1 == !followed) {
  54.     if ($nick !isop $chan) { .msg $chan ( $+ $nick $+ ): Error, You are NOT an channel operator! | return }
  55.     if (!$2) { .msg $chan ( $+ $nick $+ ): Error, Not enough parameters, try again and enter the nickname correctly! | return }
  56.     var %stc = %stream_chan_ [ $+ [ $chan ] ]
  57.     var %h = ls_followers_ [ $+ [ %stc ] $+ ] _ [ $+ [ $chan ] ]
  58.     if (!$hget(%h)) { .msg $chan ( $+ $nick $+ ): There is NOT any nickname yet on my database! | return }
  59.     if (!$hfind(%h,$2)) { .msg $chan ( $+ $nick $+ ): Error, Cannot find, The $qt( $+ $2 $+ ) nickname is NOT on my database! | return }
  60.     var %fl = $hget(%h,$2)
  61.     var %timeago = $_timeago($calc($ctime - %fl)).all
  62.     .msg $chan ( $+ $nick $+ ): The $qt( $+ $2 $+ ) nickname followed the Twitch Channel on  $+ $date(%fl,ddd ddoo mmm yyyy HH:nn:ss) $+  $iif(%timeago,( $+ $v1 $+ ))
  63.   }
  64.   if ($1 == !lastfollow) {
  65.     if ($nick !isop $chan) { .msg $chan ( $+ $nick $+ ): Error, You are NOT an channel operator! | return }
  66.     var %stc = %stream_chan_ [ $+ [ $chan ] ]
  67.     var %h = ls_followers_last_ [ $+ [ %stc ] $+ ] _ [ $+ [ $chan ] ]
  68.     if (!$hget(%h)) { .msg $chan ( $+ $nick $+ ): There is NOT any nickname yet on my database! | return }
  69.     if (!$hget(%h,1).item) { .msg $chan ( $+ $nick $+ ): Error, Cannot find, The last followed nickname cannot be found on my database! | return }
  70.     var %fl = $hget(%h,1).data
  71.     var %fln = $hget(%h,1).item
  72.     var %timeago = $_timeago($calc($ctime - %fl)).all
  73.     .msg $chan ( $+ $nick $+ ): The last follower nickname is $qt( $+ %fln $+ ) followed the Twitch Channel on  $+ $date(%fl,ddd ddoo mmm yyyy HH:nn:ss) $+  $iif(%timeago,( $+ $v1 $+ ))
  74.   }
  75. }
  76.  
  77. alias twitch_check_new {
  78.   if (!$1-) { return }
  79.   if (!%stream_auto_ [ $+ [ $1 ] ]) { .timer[FOLLOWERS_CHECK_ $+ $1 $+ ] off | return }
  80.   if ($me !ison $1) { .timer[FOLLOWERS_CHECK_ $+ $1 $+ ] off | return }
  81.   var %u = https://api.twitch.tv/kraken/channels/ $+ $2 $+ /follows?limit=1&nocache= $+ $ticks
  82.   var %v = twc_ $+ $ticks
  83.   JSONOpen -ud %v %u
  84.   if (%JSONError) {
  85.     .msg $1 ERROR 1: %JSONError -> $json(%v).error
  86.     .msg $1 SSL DETECTION 1: $sslready -> $sslversion -> $ssldll -> $ssllibdll
  87.     goto end
  88.     return
  89.   }
  90.   var %followed = $json(%v,follows,0,created_at)
  91.   if (%followed) {
  92.     var %followed = $replace($remove(%followed,$chr(122)),$chr(116),$chr(32))
  93.     var %ctime = $ctime(%followed)
  94.     var %followed = $date(%ctime,ddd ddoo mmm yyyy HH:nn:ss)
  95.     var %followed_ago = $_timeago($calc($ctime - %ctime)).all
  96.   }
  97.   var %type = $json(%v,follows,0,user,type)
  98.   var %name = $json(%v,follows,0,user,display_name)
  99.   if (!%name) { var %name = $json(%v,follows,0,user,name) }
  100.   if (!%name) { goto end | return }
  101.   var %uu = https://api.twitch.tv/kraken/streams/ $+ $2
  102.   var %vv = twl_ $+ $ticks
  103.   JSONOpen -ud %vv %uu
  104.   if (%JSONError) {
  105.     .msg $1 ERROR 2: %JSONError -> $json(%vv).error
  106.     .msg $1 SSL DETECTION 2: $sslready -> $sslversion -> $ssldll -> $ssllibdll
  107.     goto end
  108.     return
  109.   }
  110.   ; var %id = $json(%vv,stream,_id)
  111.   ; if (!%id) {
  112.   ;   .msg $1 The $qt( $+ $2 $+ ) stream channel is currently OFFLINE.
  113.   ;   .msg $1 The AUTO-FOLLOW-WELCOME module is going OFF.
  114.   ;   .timer[FOLLOWERS_CHECK_ $+ $1 $+ ] off
  115.   ;   unset %stream_auto_ [ $+ [ $1 ] ]
  116.   ;   goto end
  117.   ;   return
  118.   ; }
  119.   if (!$hget(ls_followers_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ])) { hmake ls_followers_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ] 500 }
  120.   if ($hfind(ls_followers_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ],%name)) { goto end | return }
  121.   hadd -m ls_followers_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ] %name $ctime(%followed)
  122.   if ($hget(ls_followers_last_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ])) { hfree $v1 }
  123.   hadd -m ls_followers_last_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ] %name $ctime(%followed)
  124.   .msg $1 Welcome & Thank you for following our channel,( $+ %name $+ )! <3
  125.   :end
  126.   JSONClose %v
  127.   JSONClose %vv
  128. }
  129.  
  130. alias _timeago {
  131.   if (!$1) { return 0 }
  132.   if ($1) && ($1 !isnum) { return 0 }
  133.   if ($prop == all) {
  134.     var %s = $nduration($1)
  135.     var %s = $replacex(%s,wks,$chr(32) weeks.,wk,$chr(32) week.)
  136.     var %s = $replacex(%s,days,$chr(32) days.,day,$chr(32) day.)
  137.     if (*min* iswm %s) { var %s = $replacex(%s,hrs,$chr(32) hours.,hr,$chr(32) hour.) }
  138.     if (*min* !iswm %s) { var %s = $replacex(%s,hrs,$chr(32) hours,hr,$chr(32) hour) }
  139.     if (*sec* iswm %s) { var %s = $replacex(%s,mins,$chr(32) minutes.,min,$chr(32) minute.) }
  140.     if (*sec* !iswm %s) { var %s = $replacex(%s,mins,$chr(32) minutes,min,$chr(32) minute) }
  141.     var %s = $replacex(%s,secs,$chr(32) seconds,sec,$chr(32) second)
  142.     var %s = $replace(%s,$chr(46),$chr(44))
  143.     return $iif(%s,%s ago,0)
  144.   }
  145.   if ($1 <= 59) { return $1 $iif($1 == 1,second,seconds) ago }
  146.   if (($1 <= 3599) && ($1 > 59)) { return $floor($calc($1 / 60)) $iif($floor($calc($1 / 60)) == 1,minute,minutes) ago }
  147.   if (($1 <= 86399) && ($1 > 3599)) { return $round($calc($1 / 3600),0) $iif($round($calc($1 / 3600),0) == 1,hour,hours) ago }
  148.   if (($1 <= 2592000) && ($1 > 86399)) { return $floor($calc($1 / 86400)) $iif($floor($calc($1 / 86400)) > 1,days,day) $+ , $round($calc($calc($1 - $calc($floor($calc($1 / 86400))*86400)) / 3600),0) $iif($round($calc($calc($1 - $calc($floor($calc($1 / 86400))*86400)) / 3600),0) > 1,hours,hour) ago }
  149.   if (($1 <= 31540000) && ($1 > 2592000)) { return $floor($calc($1 / 2592000)) $iif($floor($calc($1 / 2592000)) > 1,months,month) $+ , $round($calc($calc($1 - $calc($floor($calc($1 / 2592000))*2592000)) / 86400),0) $iif($round($calc($calc($1 - $calc($floor($calc($1 / 2592000))*2592000)) / 86400),0) > 1,days,day) ago }
  150.   if ($1 > 31104000) { return $floor($calc($1 / 31104000)) $iif($floor($calc($1 / 31104000)) > 1,years,year) $+ , $round($calc($calc($1 - $calc($floor($calc($1 / 31104000))*31104000)) / 2592000),0) $iif($round($calc($calc($1 - $calc($floor($calc($1 / 31104000))*31104000)) / 2592000),0) > 1,months,month) ago }
  151. }
  152.  
  153. alias JSONOpen {
  154.   var %switches = -
  155.   if (-* iswm $1) {
  156.     %switches = $1
  157.     tokenize 32 $2-
  158.   }
  159.   var %close = $false
  160.  
  161.   var %com = JSONHandler:: $+ $1, %error, %file, %rem
  162.   var %init = function init(a,b){errortext="";data="";url=b ? b : "";method="GET";headers=[];parsedJSON={};status=a;fso=new ActiveXObject("Scripting.FileSystemObject")}
  163.   var %read = function readFile(filename){var ado=new ActiveXObject("ADODB.Stream");ado.CharSet="utf-8";ado.Open();ado.LoadFromFile(filename);if(!ado.EOF){data=ado.ReadText();}ado.close()}
  164.   var %json = "object"!==typeof JSON&&(JSON={});(function(){function m(a){return 10>a?"0"+a:a}function t(a){p.lastIndex=0;return p.test(a)?'"'+a.replace(p,function(a){var c=u[a];return"string"===typeof c?c:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function q(a,l){var c,d,h,r,g=e,f,b=l[a];b&&"object"===typeof b&&"function"===typeof b.toJSON&&(b=b.toJSON(a));"function"===typeof k&&(b=k.call(l,a,b));switch(typeof b){case "string":return t(b);case "number":return isFinite(b)?String(b):"null";case "boolean":case "null":return String(b);case "object":if(!b)return"null";e+=n;f=[];if("[object Array]"===Object.prototype.toString.apply(b)){r=b.length;for(c=0;c<r;c+=1)f[c]=q(c,b)||"null";h=0===f.length?"[]":e?"[\n"+e+f.join(",\n"+e)+"\n"+g+"]":"["+f.join(",")+"]";e=g;return h}if(k&&"object"===typeof k)for(r=k.length,c=0;c<r;c+=1)"string"===typeof k[c]&&(d=k[c],(h=q(d,b))&&f.push(t(d)+(e?": ":":")+h));else for(d in b)Object.prototype.hasOwnProperty.call(b,d)&&(h=q(d,b))&&f.push(t(d)+(e?": ":":")+h);h=0===f.length?"{}":e?"{\n"+e+f.join(",\n"+e)+"\n"+g+"}":"{"+f.join(",")+"}";e=g;return h}}"function"!==typeof Date.prototype.toJSON&&(Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+m(this.getUTCMonth()+1)+"-"+m(this.getUTCDate())+"T"+m(this.getUTCHours())+":"+m(this.getUTCMinutes())+":"+m(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()});var s,p,e,n,u,k;"function"!==typeof JSON.stringify&&(p=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,u={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},JSON.stringify=function(a,l,c){var d;n=e="";if("number"===typeof c)for(d=0;d<c;d+=1)n+=" ";else"string"===typeof c&&(n=c);if((k=l)&&"function"!==typeof l&&("object"!==typeof l||"number"!==typeof l.length))throw Error("JSON.stringify");return q("",{"":a})});"function"!==typeof JSON.parse&&(s=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,JSON.parse=function(a,e){function c(a,d){var g,f,b=a[d];if(b&&"object"===typeof b)for(g in b)Object.prototype.hasOwnProperty.call(b,g)&&(f=c(b,g),void 0!==f?b[g]=f:delete b[g]);return e.call(a,d,b)}var d;a=String(a);s.lastIndex=0;s.test(a)&&(a=a.replace(s,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)}));if(/^[\],:{}\s]*$/.test(a.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return d=eval("("+a+")"),"function"===typeof e?c({"":d},""):d;throw new SyntaxError("JSON.parse");})})();
  165.   var %xhr = function xhr(){var a,b=['MSXML2.SERVERXMLHTTP.6.0','MSXML2.SERVERXMLHTTP.3.0','MSXML2.SERVERXMLHTTP','MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.3.0','Microsoft.XMLHTTP'],c;for(a=0;a<b.length;a++){try{c=new ActiveXObject(b[a]);break}catch(e){}}if(typeof c==="undefined"){errortext = "Unable to locate an XMLHTTP object for use"}else{c.open(method,url,false);for(a=0;a<headers.length;a++){c.setRequestHeader(headers[a][0],headers[a][1])}c.send(data);if(c.status!==200){errortext="Unable to retrieve data - Server Error: " + c.status}else{status="Parsed";try{parsedJSON=JSON.parse(c.responseText)}catch(ee){errortext="Unable to parse Retrieved Data"}}}}
  166.  
  167.   if (!$regex(%switches, /^-[ufbwd]*$/)) {
  168.     %error = Invalid switches specified
  169.   }
  170.   elseif ($regex(%switches, /([ufbwd])\1/)) {
  171.     %error = Duplicate switch specified ( $+ $regml(1) $+ )
  172.   }
  173.   elseif ($regex(%switches, /([ufb])/g) > 1) {
  174.     %error = Conflicting switches specified (- $+ $regml(1) $+ , - $+ $regml(2) $+ $iif($regml(3), $chr(44) - $+ $v1) $+ )
  175.   }
  176.   elseif (w isin %switches && u !isin %switches) {
  177.     %error = Invalid switch (-w is only for use with -u)
  178.   }
  179.   elseif ($0 < 2) {
  180.     %error = Required parameters missing
  181.   }
  182.   elseif (!$regex($1,/^[a-z\d_.-]+$/i)) {
  183.     %error = Invalid name specified (Can only contant letters, numbers, _, ., and -)
  184.   }
  185.   elseif ($com(%com)) {
  186.     %error = Specified name in use
  187.   }
  188.   elseif (u isin %switches && $0 > 2) {
  189.     %error = Invalid URL specified. (Contains Spaces: $2-)
  190.   }
  191.   elseif (f isin %switches && !$isfile($2-)) {
  192.     %error = File not found: $2-
  193.   }
  194.   elseif (b isin %switches && &* !iswm $2) {
  195.     %error = Invalid binary variable specified (No '&' Prefix)
  196.   }
  197.   elseif (b isin %switches && $0 > 2) {
  198.     %error = Invalid binary variable specified (Contains Spaces: $2-)
  199.   }
  200.   elseif (b isin %switches && $bvar($2, 0) < 1) {
  201.     %error = Invalid binary variable specified (No content)
  202.   }
  203.   else {
  204.     %close = $true
  205.     if (!$JSON::ComOpen(%com, MSScriptControl.ScriptControl) || $comerr) {
  206.       %error = Unable to create an instance of MSScriptControl.ScriptControl
  207.     }
  208.     elseif (!$com(%com, language, 4, bstr, jscript) || $comerr) {
  209.       %error = Unable to set ScriptControl's language to Javascript
  210.     }
  211.     elseif (!$com(%com, addcode, 1, bstr, %init) || $comerr) {
  212.       %error = Unable to add init() to ScriptControl
  213.     }
  214.     elseif (!$com(%com, addcode, 1, bstr, %xhr) || $comerr) {
  215.       %error = Unable to add xhr() to ScriptControl
  216.     }
  217.     elseif (!$com(%com, addcode, 1, bstr, %read) || $comerr) {
  218.       %error = Unable to add readFile() to ScriptControl
  219.     }
  220.     elseif (!$com(%com, ExecuteStatement, 1, bstr, %JSON) || $comerr) {
  221.       %error = Unable to Initalize the JSON parser object
  222.     }
  223.     elseif (!$com(%com, ExecuteStatement, 1, bstr, $iif(u isin %switches, $+(init,$chr(40), "XHR", $chr(44), $qt($JSON::Escape($2)),$chr(41)), $+(init,$chr(40), "Parsed", $chr(44), "", $chr(41)))) || $comerr) {
  224.       %error = Unable to initalize variables for ScriptControl
  225.     }
  226.     else {
  227.       if (u isin %switches) {
  228.         if (w !isin %switches) {
  229.           var %i = $com(%com, ExecuteStatement, 1, bstr, $+(xhr,$chr(40),$chr(41)))
  230.           if (!%i || $comerr) {
  231.             %error = Unable to retrieve data from $2 :: $com(%com).error :: $com(%com).errortext
  232.           }
  233.           elseif (!$com(%com, eval, 1, bstr, errortext) || $com(%com).result) {
  234.             %error = Unable to retrieve data $+ $iif($v1,: $v1,)
  235.           }
  236.         }
  237.       }
  238.       else {
  239.         %file = $JSON::File($1)
  240.         if (f isin %switches) {
  241.           %file = $longfn($2)
  242.         }
  243.         elseif (b isin %switches) {
  244.           bwrite $qt(%file) 0 -1 $2
  245.           %rem = $true
  246.         }
  247.         else {
  248.           write -n $qt(%file) $2-
  249.           %rem = $true
  250.         }
  251.         if (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(readFile, $qt($JSON::Escape(%file)))) || $comerr) {
  252.           %error = Unable to read contents of data-passing file
  253.         }
  254.         elseif (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(parsedJSON=JSON.parse,data)) || $comerr) {
  255.           %error = Unable to parse data into valid JSON
  256.         }
  257.       }
  258.       if (d isin %switches) {
  259.         $+(.timer,%com) 1 0 JSONClose $1
  260.       }
  261.     }
  262.   }
  263.   :error
  264.   %error = $iif($error, $v1, %error)
  265.   if (%rem && %file && $isfile(%file)) {
  266.     .remove $qt(%file)
  267.   }
  268.   if (%error) {
  269.     if ($timer(%com)) { $+(.timer,%com) off }
  270.     if (%close && $com(%com)) {
  271.       set -eu0 $+(%,%com,::error) %error
  272.       set -eu0 %JSONError %error
  273.       .comclose %com
  274.     }
  275.     else {
  276.       set -eu0 %JSONError %error
  277.     }
  278.   }
  279.   else {
  280.     unset $+(%,%com,::error)
  281.     unset %JSONError
  282.   }
  283. }
  284. alias -l JSONError { return %JSONError }
  285.  
  286. alias JSONURLOption {
  287.   var %com = JSONHandler:: $+ $1, %error, %head, %value, %x = 2
  288.  
  289.   if (!$com(%com)) {
  290.     return
  291.   }
  292.   unset % [ $+ [ %com ] $+ ] ::error
  293.   if ($0 < 3) {
  294.     %error = Missing parameters
  295.   }
  296.   elseif (!$com(%com, eval, 1, bstr, status) || $com(%com).result != XHR) {
  297.     %error = HTTP Request already completed or wasn't specified
  298.   }
  299.   elseif ($2 == method) {
  300.     if (!$regex($3-, /^(?:GET|POST|PUT|DELETE)$/i)) {
  301.       %error = Invalid HTTP Request method Specified: $3-
  302.     }
  303.     elseif (!$com(%com, ExecuteStatement, 1, bstr, method=" $+ $3 $+ ") || $comerr) {
  304.       %error = Unable to set HTTP Request method
  305.     }
  306.   }
  307.   else {
  308.     if (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(headers.push,[ $+ $qt($JSON::Escape($2)), $qt($JSON::Escape($3-)) $+ ])) || $comerr) {
  309.       %error = Unable to set HTTP Header: %head $+ : $+ %value
  310.     }
  311.   }
  312.   :error
  313.   %error = $iif($error, $v1, %error)
  314.   if (%error) {
  315.     reseterror
  316.     set -e $+(%,%com,::Error) %error
  317.   }
  318. }
  319. alias JSONGet {
  320.   var %switches -
  321.   if (-* iswm $1) {
  322.     %switches = $1
  323.     tokenize 32 $2-
  324.   }
  325.   var %com = JSONHandler:: $+ $1
  326.   var %file = $JSON::File($1)
  327.   var %error
  328.   var %rem
  329.   if ($com(%com)) {
  330.     if ($com(%com, eval, 1, bstr, status) && $com(%com).result !== XHR) {
  331.       %error = HTTP Request already completed or wasn't specified
  332.     }
  333.     elseif (!$regex(%switches, /^-[bf]*$/)) {
  334.       %error = Invalid switches specified
  335.     }
  336.     elseif ($regex(%switches, /([bf])\1/)) {
  337.       %error = Duplicate switch specified ( $+ $regml(1) $+ )
  338.     }
  339.     elseif ($regex(%switches, /([bf])/g) > 1) {
  340.       %error = Conflicting switches specified (- $+ $regml(1) $+ , - $+ $regml(2) $+ )
  341.     }
  342.     elseif (b isin %switches && &* !iswm $2) {
  343.       %error = Invalid binary variable specified (No '&' Prefix)
  344.     }
  345.     elseif (b isin %switches && $0 > 2) {
  346.       %error = Invalid binary variable specified (Contains Spaces: $2-)
  347.     }
  348.     elseif (f isin %switches && !$isfile($2-)) {
  349.       %error = File not found: $2-
  350.     }
  351.     else {
  352.       if (b isin %switches && $bvar($2,0)) {
  353.         bwrite $qt(%file) 0 -1 $2
  354.         %rem = $true
  355.       }
  356.       elseif (f isin %switches) {
  357.         %file = $2-
  358.       }      
  359.       elseif ($2-) {
  360.         write -n $qt(%file) $2-
  361.         %rem = $true
  362.       }
  363.       %file = $longfn(%file)
  364.       if ($isfile(%file) && (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(readFile,$qt($JSON::Escape(%file)))) || $comerr)) {
  365.         %error = Unable to pass data to JSON Handler
  366.       }
  367.       elseif (!$com(%com, ExecuteStatement, 1, bstr, xhr $+ $chr(40) $+ $chr(41)) || $comerr) {
  368.         %error = Unable to retrieve data from specified URL :: $com(%com).error :: $com(%com).errortext    
  369.       }
  370.       elseif (!$com(%com, eval, 1, bstr, errortext) || $com(%com).result) {
  371.         %error = $v1
  372.       }
  373.       if (%rem && $isfile(%file)) { .remove %file }
  374.     }
  375.   }
  376.   :error
  377.   %error = $iif($error, $v1, %error)
  378.   if (%error) {
  379.     reseterror
  380.     set -eu0 $+(%,%com,::Error) %error
  381.   }
  382. }
  383. alias JSON {
  384.   var %com, %x = 1, %i = 0, %get = parsedJSON, %tok, %res, %LTLError
  385.   if ($regex($1,/^\d+$/) && $0 === 1) {
  386.     while ($com(%x)) {
  387.       if (JSONHandler::* iswm $v1) {
  388.         %com = $v2
  389.         inc %i
  390.         if (%i === $1) { return $mid(%com,14-) }
  391.       }
  392.       inc %x
  393.     }
  394.     return $iif($1 == 0, %i)
  395.   }
  396.   elseif ($regex($1, /^JSONHandler::CHILD::([^:]+)::(.*)$/)) {
  397.     %com = $regml(1)
  398.     %get = $regml(2)
  399.   }
  400.   else {
  401.     %com = JSONHandler:: $+ $1
  402.   }
  403.   if ($com(%com)) {
  404.     if ($0 == 1) {
  405.       if ($prop == isChild) {
  406.         return $iif($regex($1, /^JSONHandler::CHILD::([^:]+)::(.*)$/), $true, $false)
  407.       }
  408.       elseif ($prop == error || $prop === errormsg) {
  409.         return $(,$+(%,%com,::error))
  410.       }
  411.       elseif ($com($1)) {
  412.         return $1
  413.       }
  414.     }
  415.     elseif (!$com(%com, eval, 1, bstr, status) || $com(%com).result != parsed) {
  416.       set -eu0 $+(%,%com,::error) JSON Handler waiting for HTTP Request
  417.     }
  418.     else {
  419.       %x = 2
  420.       while (%x <= $0) {
  421.         %tok = $(,$ $+ %x)
  422.         if (!$regex(%tok, /^\d+$/)) {
  423.           %tok = $qt($replace(%tok,\,\\,",\"))
  424.         }
  425.         %get = $+(%get,[,%tok,])
  426.         inc %x
  427.       }
  428.       if (!$com(%com, eval, 1, bstr, %get) || $comerr) {
  429.         set -eu0 $+(%,%com,::error) Invalid Item|index specified
  430.       }
  431.       else {
  432.         %LLLError = $true
  433.         %res = $com(%com).result
  434.         %LLLError = $false
  435.         if (%res == [object]) {
  436.           return JSONHandler::CHILD:: $+ $1 $+ :: $+ %get
  437.         }
  438.         else {
  439.           return %res
  440.         }
  441.       }
  442.     }
  443.   }
  444.   return
  445.   :error
  446.   set -eu0 $+(%,%com,::error) $iif(%LLLError, Line to Long; truncated at 4000 bytes, $error)
  447.   reseterror
  448.   if (%LLLError) {
  449.     %res = $left($com(%com).result, 4000)
  450.   }
  451. }
  452. alias JSONClose {
  453.   var %com = JSONHandler:: $+ $1
  454.   var %file = $mircdirdata\JSON $+ $1 $+ 1.json
  455.   unset % [ $+ [ %com ] $+ ] ::*
  456.   if ($com(%com)) { .comclose $v1 }
  457.   if ($timer(%com)) { $+(.timer,%com) off }
  458.   if ($isfile(%file)) { .remove $qt(%file) }
  459. }
  460. alias JSONList {
  461.   var %x = 1, %i = 0
  462.   while ($com(%x)) {
  463.     if (JSONHandler::* iswm $v1) {
  464.       inc %i
  465.       echo $color(info text) -a * # $+ %i : $regsubex($v2, /^JSONHandler::/,)
  466.     }
  467.     inc %x
  468.   }
  469.   if (!%i) {
  470.     echo $color(info text) -a * No active JSON handlers
  471.   }  
  472. }
  473. alias -l JSON::ComOpen { .comopen $1- | if ($com($1) && !$comerr) { return $true } | :error | reseterror | if ($com($1)) { .comclose $1 } }
  474. alias -l JSON::Escape { return $replace($1,\,\\,",\") }
  475. alias -l JSON::File { var %a = 1 | while ($isfile($mircdirdata\ $+ JSON $+ $1 $+ %a $+ .json)) { inc %a } | return $mircdirdata\ $+ $+(JSON, $1, %a, .json) }
  476. alias -l JSON::Funct { var %a = $1 $+ $chr(40), %i = 1 | while (%i < $0) { inc %i | %a = $+(%a,$iif(%i > 2,$chr(44)),$(, $ $+ %i)) } | return %a $+ $chr(41) }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement