MrLubert

Follow Alert for Mirc

Dec 10th, 2014
3,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ON *:PART:#: {
  2.   if ($nick == $me) { unset %stream_auto_ [ $+ [ $chan ] ] | .timer[FOLLOWERS_CHECK_ [ $+ [ $chan ] ] off }
  3. }
  4.  
  5. ON *:KICK:#: {
  6.   if ($nick == $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 $chan) { msg $chan [ $+ $nick $+ ]: Error, You are NOT an channel operator! | return }
  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.   if ($1 == !setstreamchan) {
  39.     if ($nick !isop $chan) { msg $chan [ $+ $nick $+ ]: Error, You are NOT an channel operator! | return }
  40.     if (!$2) { msg $chan [ $+ $nick $+ ]: Error, Not enough parameters, try again and enter the Twitch Channel correctly! | return }
  41.     if (%stream_auto_ [ $+ [ $chan ] ]) { msg $chan [ $+ $nick $+ ]: Error, You must first disable the AUTO-FOLLOW-MESSAGE module and then change the stream channel! | return }
  42.     set %stream_chan_ [ $+ [ $chan ] ] $2
  43.     msg $chan [ $+ $nick $+ ]: The Twitch Channel has been set, now use !AutoFollow command to enable/disable the AUTO-FOLLOW-WELCOME module.
  44.   }
  45.   if ($1 == !streamchan) {
  46.     if ($nick !isop $chan) { msg $chan [ $+ $nick $+ ]: Error, You are NOT an channel operator! | return }
  47.     if (!%stream_chan_ [ $+ [ $chan ] ]) { msg $chan [ $+ $nick $+ ]: Error, There is NOT any Twitch Channel has been set! | return }
  48.     msg $chan [ $+ $nick $+ ]: The $chan Twitch Stream Channel is now:  $+ %stream_chan_ [ $+ [ $chan ] ] $+ 
  49.   }
  50.   if ($1 == !followed) {
  51.     if ($nick !isop $chan) { msg $chan [ $+ $nick $+ ]: Error, You are NOT an channel operator! | return }
  52.     if (!$2) { msg $chan [ $+ $nick $+ ]: Error, Not enough parameters, try again and enter the nickname correctly! | return }
  53.     var %stc = %stream_chan_ [ $+ [ $chan ] ]
  54.     var %h = ls_followers_ [ $+ [ %stc ] $+ ] _ [ $+ [ $chan ] ]
  55.     if (!$hget(%h)) { msg $chan [ $+ $nick $+ ]: There is NOT any nickname yet on my database! | return }
  56.     if (!$hfind(%h,$2)) { msg $chan [ $+ $nick $+ ]: Error, Cannot find, The $qt( $+ $2 $+ ) nickname is NOT on my database! | return }
  57.     var %fl = $hget(%h,$2)
  58.     var %timeago = $_timeago($calc($ctime - %fl)).all
  59.     msg $chan [ $+ $nick $+ ]: The $qt( $+ $2 $+ ) nickname followed the Twitch Channel on  $+ $date(%fl,ddd ddoo mmm yyyy HH:nn:ss) $+  $iif(%timeago,( $+ $v1 $+ ))
  60.   }
  61.   if ($1 == !lastfollow) {
  62.     if ($nick !isop $chan) { msg $chan [ $+ $nick $+ ]: Error, You are NOT an channel operator! | return }
  63.     var %stc = %stream_chan_ [ $+ [ $chan ] ]
  64.     var %h = ls_followers_last_ [ $+ [ %stc ] $+ ] _ [ $+ [ $chan ] ]
  65.     if (!$hget(%h)) { msg $chan [ $+ $nick $+ ]: There is NOT any nickname yet on my database! | return }
  66.     if (!$hget(%h,1).item) { msg $chan [ $+ $nick $+ ]: Error, Cannot find, The last followed nickname cannot be found on my database! | return }
  67.     var %fl = $hget(%h,1).data
  68.     var %fln = $hget(%h,1).item
  69.     var %timeago = $_timeago($calc($ctime - %fl)).all
  70.     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 $+ ))
  71.   }
  72. }
  73.  
  74. alias twitch_check_new {
  75.   if (!$1-) { return }
  76.   if (!%stream_auto_ [ $+ [ $1 ] ]) { .timer[FOLLOWERS_CHECK_ $+ $1 $+ ] off | return }
  77.   if ($me !ison $1) { .timer[FOLLOWERS_CHECK_ $+ $1 $+ ] off | return }
  78.   var %u = https://api.twitch.tv/kraken/channels/ $+ $2 $+ /follows?limit=1&nocache= $+ $ticks
  79.   var %v = twc_ $+ $ticks
  80.   JSONOpen -ud %v %u
  81.   if (%JSONError) { goto end | return }
  82.   var %followed = $json(%v,follows,0,created_at)
  83.   if (%followed) {
  84.     var %followed = $replace($remove(%followed,$chr(122)),$chr(116),$chr(32))
  85.     var %ctime = $ctime(%followed)
  86.     var %followed = $date(%ctime,ddd ddoo mmm yyyy HH:nn:ss)
  87.     var %followed_ago = $_timeago($calc($ctime - %ctime)).all
  88.   }
  89.   var %type = $json(%v,follows,0,user,type)
  90.   var %name = $json(%v,follows,0,user,display_name)
  91.   if (!%name) { var %name = $json(%v,follows,0,user,name) }
  92.   if (!%name) { goto end | return }
  93.   var %uu = https://api.twitch.tv/kraken/streams/ $+ $2
  94.   var %vv = twl_ $+ $ticks
  95.   JSONOpen -ud %vv %uu
  96.   if (%JSONError) { goto end | return }
  97.   var %id = $json(%vv,stream,_id)
  98.   if (!%id) {
  99.     msg # $1 The $qt( $+ $2 $+ ) stream channel is currently OFFLINE.
  100.     msg # $1 The AUTO-FOLLOW-WELCOME module is going OFF.
  101.     .timer[FOLLOWERS_CHECK_ $+ $1 $+ ] off
  102.     unset %stream_auto_ [ $+ [ $1 ] ]
  103.     goto end
  104.     return
  105.   }
  106.   if (!$hget(ls_followers_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ])) { hmake ls_followers_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ] 500 }
  107.   if ($hfind(ls_followers_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ],%name)) { goto end | return }
  108.   hadd -m ls_followers_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ] %name $ctime(%followed)
  109.   if ($hget(ls_followers_last_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ])) { hfree $v1 }
  110.   hadd -m ls_followers_last_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ] %name $ctime(%followed)
  111.   msg # $1 [ $+ %name $+ ]: Welcome & Thank you for following our channel!
  112.   :end
  113.   JSONClose %v
  114.   JSONClose %vv
  115. }
  116.  
  117. alias _timeago {
  118.   if (!$1) { return 0 }
  119.   if ($1) && ($1 !isnum) { return 0 }
  120.   if ($prop == all) {
  121.     var %s = $nduration($1)
  122.     var %s = $replacex(%s,wks,$chr(32) weeks.,wk,$chr(32) week.)
  123.     var %s = $replacex(%s,days,$chr(32) days.,day,$chr(32) day.)
  124.     if (*min* iswm %s) { var %s = $replacex(%s,hrs,$chr(32) hours.,hr,$chr(32) hour.) }
  125.     if (*min* !iswm %s) { var %s = $replacex(%s,hrs,$chr(32) hours,hr,$chr(32) hour) }
  126.     if (*sec* iswm %s) { var %s = $replacex(%s,mins,$chr(32) minutes.,min,$chr(32) minute.) }
  127.     if (*sec* !iswm %s) { var %s = $replacex(%s,mins,$chr(32) minutes,min,$chr(32) minute) }
  128.     var %s = $replacex(%s,secs,$chr(32) seconds,sec,$chr(32) second)
  129.     var %s = $replace(%s,$chr(46),$chr(44))
  130.     return $iif(%s,%s ago,0)
  131.   }
  132.   if ($1 <= 59) { return $1 $iif($1 == 1,second,seconds) ago }
  133.   if (($1 <= 3599) && ($1 > 59)) { return $floor($calc($1 / 60)) $iif($floor($calc($1 / 60)) == 1,minute,minutes) ago }
  134.   if (($1 <= 86399) && ($1 > 3599)) { return $round($calc($1 / 3600),0) $iif($round($calc($1 / 3600),0) == 1,hour,hours) ago }
  135.   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 }
  136.   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 }
  137.   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 }
  138. }
  139.  
  140. alias JSONOpen {
  141.   var %switches = -
  142.   if (-* iswm $1) {
  143.     %switches = $1
  144.     tokenize 32 $2-
  145.   }
  146.   var %com = JSONHandler:: $+ $1, %error, %file, %rem
  147.   var %init = function init(a,b){errortext="";data="";url=b ? b : "";method="GET";headers=[];parsedJSON={};status=a;fso=new ActiveXObject("Scripting.FileSystemObject")}
  148.   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()}
  149.   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");})})();
  150.   var %xhr = function xhr(){var a,b=['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"}}}}
  151.   if (!$regex(%switches, /^-[ufbwd]*$/)) { %error = Invalid switches specified }
  152.   elseif ($regex(%switches, /([ufbwd])\1/)) { %error = Duplicate switch specified ( $+ $regml(1) $+ ) }
  153.   elseif ($regex(%switches, /([ufb])/g) > 1) { %error = Conflicting switches specified (- $+ $regml(1) $+ , - $+ $regml(2) $+ $iif($regml(3), $chr(44) - $+ $v1) $+ ) }
  154.   elseif (w isin %switches && u !isin %switches) { %error = Invalid switch (-w is only for use with -u) }
  155.   elseif ($0 < 2) { %error = Required parameters missing }
  156.   elseif (!$regex($1,/^[a-z\d_.-]+$/i)) { %error = Invalid name specified (Can only contant letters, numbers, _, ., and -) }
  157.   elseif ($com(%com)) { %error = Specified name in use }
  158.   elseif (u isin %switches && $0 > 2) { %error = Invalid URL specified. (Contains Spaces: $2-) }
  159.   elseif (f isin %switches && !$isfile($2-)) { %error = File not found: $2- }
  160.   elseif (b isin %switches && &* !iswm $2) { %error = Invalid binary variable specified (No '&' Prefix) }
  161.   elseif (b isin %switches && $0 > 2) { %error = Invalid binary variable specified (Contains Spaces: $2-) }
  162.   elseif (b isin %switches && $bvar($2, 0) < 1) { %error = Invalid binary variable specified (No content) }
  163.   elseif (!$JSON::ComOpen(%com, MSScriptControl.ScriptControl) || $comerr) { %error = Unable to create an instance of MSScriptControl.ScriptControl }
  164.   elseif (!$com(%com, language, 4, bstr, jscript) || $comerr) { %error = Unable to set ScriptControl's language to Javascript }
  165.   elseif (!$com(%com, addcode, 1, bstr, %init) || $comerr) { %error = Unable to add init() to ScriptControl }
  166.   elseif (!$com(%com, addcode, 1, bstr, %xhr) || $comerr) { %error = Unable to add xhr() to ScriptControl }
  167.   elseif (!$com(%com, addcode, 1, bstr, %read) || $comerr) { %error = Unable to add readFile() to ScriptControl }
  168.   elseif (!$com(%com, ExecuteStatement, 1, bstr, %JSON) || $comerr) { %error = Unable to Initalize the JSON parser object }
  169.   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) { %error = Unable to initalize variables for ScriptControl }
  170.   else {
  171.     if (u isin %switches) {
  172.       if (w !isin %switches) {
  173.         var %i = $com(%com, ExecuteStatement, 1, bstr, $+(xhr,$chr(40),$chr(41)))
  174.         if (!%i || $comerr) { %error = Unable to retrieve data from $2 :: $com(%com).error :: $com(%com).errortext }
  175.         elseif (!$com(%com, eval, 1, bstr, errortext) || $com(%com).result) { %error = Unable to retrieve data $+ $iif($v1,: $v1,) }
  176.       }
  177.     }
  178.     else {
  179.       %file = $JSON::File($1)
  180.       if (f isin %switches) {
  181.         %file = $longfn($2)
  182.       }
  183.       elseif (b isin %switches) {
  184.         bwrite $qt(%file) 0 -1 $2
  185.         %rem = $true
  186.       }
  187.       else {
  188.         write -n $qt(%file) $2-
  189.         %rem = $true
  190.       }
  191.       if (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(readFile, $qt($JSON::Escape(%file)))) || $comerr) { %error = Unable to read contents of data-passing file }
  192.       elseif (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(parsedJSON=JSON.parse,data)) || $comerr) { %error = Unable to parse data into valid JSON }
  193.     }
  194.     if (d isin %switches) { $+(.timer,%com) 1 0 JSONClose $1 }
  195.   }
  196.   :error
  197.   %error = $iif($error, $v1, %error)
  198.   if (%rem && %file && $isfile(%file)) { .remove $qt(%file) }
  199.   if (%error) {
  200.     if ($timer(%com)) { $+(.timer,%com) off }
  201.     if ($com(%com)) {
  202.       set -eu0 $+(%,%com,::error) %error
  203.       .comclose $v1
  204.     }
  205.     else { set -eu0 %JSONError %error }
  206.   }
  207. }
  208. alias JSONURLOption {
  209.   var %com = JSONHandler:: $+ $1, %error, %head, %value, %x = 2
  210.  
  211.   if (!$com(%com)) { return }
  212.   unset % [ $+ [ %com ] $+ ] ::error
  213.   if ($0 < 3) { %error = Missing parameters }
  214.   elseif (!$com(%com, eval, 1, bstr, status) || $com(%com).result != XHR) { %error = HTTP Request already completed or wasn't specified }
  215.   elseif ($2 == method) {
  216.     if (!$regex($3-, /^(?:GET|POST|PUT|DELETE)$/i)) { %error = Invalid HTTP Request method Specified: $3- }
  217.     elseif (!$com(%com, ExecuteStatement, 1, bstr, method=" $+ $3 $+ ") || $comerr) { %error = Unable to set HTTP Request method }
  218.   }
  219.   else {
  220.     if (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(headers.push,[ $+ $qt($JSON::Escape($2)), $qt($JSON::Escape($3-)) $+ ])) || $comerr) { %error = Unable to set HTTP Header: %head $+ : $+ %value }
  221.   }
  222.   :error
  223.   %error = $iif($error, $v1, %error)
  224.   if (%error) {
  225.     reseterror
  226.     set -e $+(%,%com,::Error) %error
  227.   }
  228. }
  229. alias JSONGet {
  230.   var %switches -
  231.   if (-* iswm $1) {
  232.     %switches = $1
  233.     tokenize 32 $2-
  234.   }
  235.   var %com = JSONHandler:: $+ $1
  236.   var %file = $JSON::File($1)
  237.   var %error
  238.   var %rem
  239.   if ($com(%com)) {
  240.     if ($com(%com, eval, 1, bstr, status) && $com(%com).result !== XHR) { %error = HTTP Request already completed or wasn't specified }
  241.     elseif (!$regex(%switches, /^-[bf]*$/)) { %error = Invalid switches specified }
  242.     elseif ($regex(%switches, /([bf])\1/)) { %error = Duplicate switch specified ( $+ $regml(1) $+ ) }
  243.     elseif ($regex(%switches, /([bf])/g) > 1) { %error = Conflicting switches specified (- $+ $regml(1) $+ , - $+ $regml(2) $+ ) }
  244.     elseif (b isin %switches && &* !iswm $2) { %error = Invalid binary variable specified (No '&' Prefix) }
  245.     elseif (b isin %switches && $0 > 2) { %error = Invalid binary variable specified (Contains Spaces: $2-) }
  246.     elseif (f isin %switches && !$isfile($2-)) { %error = File not found: $2- }
  247.     else {
  248.       if (b isin %switches && $bvar($2,0)) {
  249.         bwrite $qt(%file) 0 -1 $2
  250.         %rem = $true
  251.       }
  252.       elseif (f isin %switches) { %file = $2- }      
  253.       elseif ($2-) {
  254.         write -n $qt(%file) $2-
  255.         %rem = $true
  256.       }
  257.       %file = $longfn(%file)
  258.       if ($isfile(%file) && (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(readFile,$qt($JSON::Escape(%file)))) || $comerr)) { %error = Unable to pass data to JSON Handler }
  259.       elseif (!$com(%com, ExecuteStatement, 1, bstr, xhr $+ $chr(40) $+ $chr(41)) || $comerr) { %error = Unable to retrieve data from specified URL :: $com(%com).error :: $com(%com).errortext }
  260.       elseif (!$com(%com, eval, 1, bstr, errortext) || $com(%com).result) { %error = $v1 }
  261.       if (%rem && $isfile(%file)) { .remove %file }
  262.     }
  263.   }
  264.   :error
  265.   %error = $iif($error, $v1, %error)
  266.   if (%error) {
  267.     reseterror
  268.     set -eu0 $+(%,%com,::Error) %error
  269.   }
  270. }
  271. alias JSON {
  272.   var %com, %x = 1, %i = 0, %get = parsedJSON, %tok, %res
  273.   if ($regex($1,/^\d+$/) && $0 === 1) {
  274.     while ($com(%x)) {
  275.       if (JSONHandler::* iswm $v1) {
  276.         %com = $v2
  277.         inc %i
  278.         if (%i === $1) { return %com }
  279.       }
  280.       inc %x
  281.     }
  282.     return $iif($1 == 0, %i)
  283.   }
  284.   elseif ($regex($1, /^JSONHandler::CHILD::([^:]+)::(.*)$/)) {
  285.     %com = $regml(1)
  286.     %get = $regml(2)
  287.   }
  288.   else { %com = JSONHandler:: $+ $1 }
  289.   if ($com(%com)) {
  290.     if ($0 == 1) {
  291.       if ($prop == isChild) { return $iif($regex($1, /^JSONHandler::CHILD::([^:]+)::(.*)$/), $true, $false) }
  292.       elseif ($prop == error) { return $iif($(,$+(%,%com,::error)), $true,$false) }
  293.       elseif ($prop == errortext) { return $(,$+(%,%com,::error)) }
  294.       elseif ($com($1)) { return $1 }
  295.     }
  296.     elseif (!$com(%com, eval, 1, bstr, status) || $com(%com).result != parsed) { set -eu0 $+(%,%com,::error) JSON Handler waiting for HTTP Request }
  297.     else {
  298.       %x = 2
  299.       while (%x <= $0) {
  300.         %tok = $(,$ $+ %x)
  301.         if (!$regex(%tok, /^\d+$/)) {
  302.           %tok = $qt($replace(%tok,\,\\,",\"))
  303.         }
  304.         %get = $+(%get,[,%tok,])
  305.         inc %x
  306.       }
  307.       if (!$com(%com, eval, 1, bstr, %get) || $comerr) { set -e $+(%,%com,::error) Invalid Item|index specified }
  308.       else {
  309.         %res = $com(%com).result
  310.         if (%res == [object]) { return JSONHandler::CHILD:: $+ $1 $+ :: $+ %get }
  311.         else { return %res }
  312.       }
  313.     }
  314.   }
  315. }
  316. alias JSONClose {
  317.   var %com = JSONHandler:: $+ $1
  318.   unset % [ $+ [ %com ] $+ ] ::*
  319.   if ($com(%com)) { .comclose $v1 }
  320.   if ($timer(%com)) { $+(.timer,%com) off }
  321. }
  322. alias JSONList {
  323.   var %x = 1, %i = 0
  324.   while ($com(%x)) {
  325.     if (JSONHandler::* iswm $v1) {
  326.       inc %i
  327.       echo $color(info text) -a * # $+ %i : $regsubex($v2, /^JSONHandler::/,)
  328.     }
  329.     inc %x
  330.   }
  331.   if (!%i) { echo $color(info text) -a * No active JSON handlers }  
  332. }
  333. alias JSON::ComOpen { .comopen $1- | if ($com($1) && !$comerr) { return $true } | :error | reseterror | if ($com($1)) { .comclose $1 } }
  334. alias JSON::Escape { return $replace($1,\,\\,",\") }
  335. alias JSON::File { var %a = 1 | while ($isfile(JSON $+ $1 $+ %a $+ .json)) { inc %a } | return $+(JSON, $1, %a, .json) }
  336. alias 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) }
Add Comment
Please, Sign In to add comment