Advertisement
westor

Twitch Welcome New Follower (DEBUG) for VitruxPT v2.0

Jan 20th, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 22.76 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.     goto end
  86.     return
  87.   }
  88.   var %followed = $json(%v,follows,0,created_at)
  89.   if (%followed) {
  90.     var %followed = $replace($remove(%followed,$chr(122)),$chr(116),$chr(32))
  91.     var %ctime = $ctime(%followed)
  92.     var %followed = $date(%ctime,ddd ddoo mmm yyyy HH:nn:ss)
  93.     var %followed_ago = $_timeago($calc($ctime - %ctime)).all
  94.   }
  95.   var %type = $json(%v,follows,0,user,type)
  96.   var %name = $json(%v,follows,0,user,display_name)
  97.   if (!%name) { var %name = $json(%v,follows,0,user,name) }
  98.   if (!%name) { goto end | return }
  99.   var %uu = https://api.twitch.tv/kraken/streams/ $+ $2
  100.   var %vv = twl_ $+ $ticks
  101.   JSONOpen -ud %vv %uu
  102.   if (%JSONError) {
  103.     goto end
  104.     return
  105.   }
  106.   ; var %id = $json(%vv,stream,_id)
  107.   ; if (!%id) {
  108.   ;   .msg $1 The $qt( $+ $2 $+ ) stream channel is currently OFFLINE.
  109.   ;   .msg $1 The AUTO-FOLLOW-WELCOME module is going OFF.
  110.   ;   .timer[FOLLOWERS_CHECK_ $+ $1 $+ ] off
  111.   ;   unset %stream_auto_ [ $+ [ $1 ] ]
  112.   ;   goto end
  113.   ;   return
  114.   ; }
  115.   if (!$hget(ls_followers_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ])) { hmake ls_followers_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ] 500 }
  116.   if ($hfind(ls_followers_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ],%name)) { goto end | return }
  117.   hadd -m ls_followers_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ] %name $ctime(%followed)
  118.   if ($hget(ls_followers_last_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ])) { hfree $v1 }
  119.   hadd -m ls_followers_last_ [ $+ [ $2 ] $+ ] _ [ $+ [ $1 ] ] %name $ctime(%followed)
  120.   .msg $1 Welcome & Thank you for following our channel,( $+ %name $+ )! <3
  121.   :end
  122.   JSONClose %v
  123.   JSONClose %vv
  124. }
  125.  
  126. alias _timeago {
  127.   if (!$1) { return 0 }
  128.   if ($1) && ($1 !isnum) { return 0 }
  129.   if ($prop == all) {
  130.     var %s = $nduration($1)
  131.     var %s = $replacex(%s,wks,$chr(32) weeks.,wk,$chr(32) week.)
  132.     var %s = $replacex(%s,days,$chr(32) days.,day,$chr(32) day.)
  133.     if (*min* iswm %s) { var %s = $replacex(%s,hrs,$chr(32) hours.,hr,$chr(32) hour.) }
  134.     if (*min* !iswm %s) { var %s = $replacex(%s,hrs,$chr(32) hours,hr,$chr(32) hour) }
  135.     if (*sec* iswm %s) { var %s = $replacex(%s,mins,$chr(32) minutes.,min,$chr(32) minute.) }
  136.     if (*sec* !iswm %s) { var %s = $replacex(%s,mins,$chr(32) minutes,min,$chr(32) minute) }
  137.     var %s = $replacex(%s,secs,$chr(32) seconds,sec,$chr(32) second)
  138.     var %s = $replace(%s,$chr(46),$chr(44))
  139.     return $iif(%s,%s ago,0)
  140.   }
  141.   if ($1 <= 59) { return $1 $iif($1 == 1,second,seconds) ago }
  142.   if (($1 <= 3599) && ($1 > 59)) { return $floor($calc($1 / 60)) $iif($floor($calc($1 / 60)) == 1,minute,minutes) ago }
  143.   if (($1 <= 86399) && ($1 > 3599)) { return $round($calc($1 / 3600),0) $iif($round($calc($1 / 3600),0) == 1,hour,hours) ago }
  144.   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 }
  145.   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 }
  146.   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 }
  147. }
  148.  
  149. alias JSONOpen {
  150.   var %switches = -
  151.   if (-* iswm $1) {
  152.     %switches = $1
  153.     tokenize 32 $2-
  154.   }
  155.   var %close = $false
  156.  
  157.   var %com = JSONHandler:: $+ $1, %error, %file, %rem
  158.   var %init = function init(a,b){errortext="";data="";url=b ? b : "";method="GET";headers=[];parsedJSON={};status=a;fso=new ActiveXObject("Scripting.FileSystemObject")}
  159.   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()}
  160.   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");})})();
  161.   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"}}}}
  162.  
  163.   if (!$regex(%switches, /^-[ufbwd]*$/)) {
  164.     %error = Invalid switches specified
  165.   }
  166.   elseif ($regex(%switches, /([ufbwd])\1/)) {
  167.     %error = Duplicate switch specified ( $+ $regml(1) $+ )
  168.   }
  169.   elseif ($regex(%switches, /([ufb])/g) > 1) {
  170.     %error = Conflicting switches specified (- $+ $regml(1) $+ , - $+ $regml(2) $+ $iif($regml(3), $chr(44) - $+ $v1) $+ )
  171.   }
  172.   elseif (w isin %switches && u !isin %switches) {
  173.     %error = Invalid switch (-w is only for use with -u)
  174.   }
  175.   elseif ($0 < 2) {
  176.     %error = Required parameters missing
  177.   }
  178.   elseif (!$regex($1,/^[a-z\d_.-]+$/i)) {
  179.     %error = Invalid name specified (Can only contant letters, numbers, _, ., and -)
  180.   }
  181.   elseif ($com(%com)) {
  182.     %error = Specified name in use
  183.   }
  184.   elseif (u isin %switches && $0 > 2) {
  185.     %error = Invalid URL specified. (Contains Spaces: $2-)
  186.   }
  187.   elseif (f isin %switches && !$isfile($2-)) {
  188.     %error = File not found: $2-
  189.   }
  190.   elseif (b isin %switches && &* !iswm $2) {
  191.     %error = Invalid binary variable specified (No '&' Prefix)
  192.   }
  193.   elseif (b isin %switches && $0 > 2) {
  194.     %error = Invalid binary variable specified (Contains Spaces: $2-)
  195.   }
  196.   elseif (b isin %switches && $bvar($2, 0) < 1) {
  197.     %error = Invalid binary variable specified (No content)
  198.   }
  199.   else {
  200.     %close = $true
  201.     if (!$JSON::ComOpen(%com, MSScriptControl.ScriptControl) || $comerr) {
  202.       %error = Unable to create an instance of MSScriptControl.ScriptControl
  203.     }
  204.     elseif (!$com(%com, language, 4, bstr, jscript) || $comerr) {
  205.       %error = Unable to set ScriptControl's language to Javascript
  206.     }
  207.     elseif (!$com(%com, addcode, 1, bstr, %init) || $comerr) {
  208.       %error = Unable to add init() to ScriptControl
  209.     }
  210.     elseif (!$com(%com, addcode, 1, bstr, %xhr) || $comerr) {
  211.       %error = Unable to add xhr() to ScriptControl
  212.     }
  213.     elseif (!$com(%com, addcode, 1, bstr, %read) || $comerr) {
  214.       %error = Unable to add readFile() to ScriptControl
  215.     }
  216.     elseif (!$com(%com, ExecuteStatement, 1, bstr, %JSON) || $comerr) {
  217.       %error = Unable to Initalize the JSON parser object
  218.     }
  219.     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) {
  220.       %error = Unable to initalize variables for ScriptControl
  221.     }
  222.     else {
  223.       if (u isin %switches) {
  224.         if (w !isin %switches) {
  225.           var %i = $com(%com, ExecuteStatement, 1, bstr, $+(xhr,$chr(40),$chr(41)))
  226.           if (!%i || $comerr) {
  227.             %error = Unable to retrieve data from $2 :: $com(%com).error :: $com(%com).errortext
  228.           }
  229.           elseif (!$com(%com, eval, 1, bstr, errortext) || $com(%com).result) {
  230.             %error = Unable to retrieve data $+ $iif($v1,: $v1,)
  231.           }
  232.         }
  233.       }
  234.       else {
  235.         %file = $JSON::File($1)
  236.         if (f isin %switches) {
  237.           %file = $longfn($2)
  238.         }
  239.         elseif (b isin %switches) {
  240.           bwrite $qt(%file) 0 -1 $2
  241.           %rem = $true
  242.         }
  243.         else {
  244.           write -n $qt(%file) $2-
  245.           %rem = $true
  246.         }
  247.         if (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(readFile, $qt($JSON::Escape(%file)))) || $comerr) {
  248.           %error = Unable to read contents of data-passing file
  249.         }
  250.         elseif (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(parsedJSON=JSON.parse,data)) || $comerr) {
  251.           %error = Unable to parse data into valid JSON
  252.         }
  253.       }
  254.       if (d isin %switches) {
  255.         $+(.timer,%com) 1 0 JSONClose $1
  256.       }
  257.     }
  258.   }
  259.   :error
  260.   %error = $iif($error, $v1, %error)
  261.   if (%rem && %file && $isfile(%file)) {
  262.     .remove $qt(%file)
  263.   }
  264.   if (%error) {
  265.     if ($timer(%com)) { $+(.timer,%com) off }
  266.     if (%close && $com(%com)) {
  267.       set -eu0 $+(%,%com,::error) %error
  268.       set -eu0 %JSONError %error
  269.       .comclose %com
  270.     }
  271.     else {
  272.       set -eu0 %JSONError %error
  273.     }
  274.   }
  275.   else {
  276.     unset $+(%,%com,::error)
  277.     unset %JSONError
  278.   }
  279. }
  280. alias -l JSONError { return %JSONError }
  281.  
  282. alias JSONURLOption {
  283.   var %com = JSONHandler:: $+ $1, %error, %head, %value, %x = 2
  284.  
  285.   if (!$com(%com)) {
  286.     return
  287.   }
  288.   unset % [ $+ [ %com ] $+ ] ::error
  289.   if ($0 < 3) {
  290.     %error = Missing parameters
  291.   }
  292.   elseif (!$com(%com, eval, 1, bstr, status) || $com(%com).result != XHR) {
  293.     %error = HTTP Request already completed or wasn't specified
  294.   }
  295.   elseif ($2 == method) {
  296.     if (!$regex($3-, /^(?:GET|POST|PUT|DELETE)$/i)) {
  297.       %error = Invalid HTTP Request method Specified: $3-
  298.     }
  299.     elseif (!$com(%com, ExecuteStatement, 1, bstr, method=" $+ $3 $+ ") || $comerr) {
  300.       %error = Unable to set HTTP Request method
  301.     }
  302.   }
  303.   else {
  304.     if (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(headers.push,[ $+ $qt($JSON::Escape($2)), $qt($JSON::Escape($3-)) $+ ])) || $comerr) {
  305.       %error = Unable to set HTTP Header: %head $+ : $+ %value
  306.     }
  307.   }
  308.   :error
  309.   %error = $iif($error, $v1, %error)
  310.   if (%error) {
  311.     reseterror
  312.     set -e $+(%,%com,::Error) %error
  313.   }
  314. }
  315. alias JSONGet {
  316.   var %switches -
  317.   if (-* iswm $1) {
  318.     %switches = $1
  319.     tokenize 32 $2-
  320.   }
  321.   var %com = JSONHandler:: $+ $1
  322.   var %file = $JSON::File($1)
  323.   var %error
  324.   var %rem
  325.   if ($com(%com)) {
  326.     if ($com(%com, eval, 1, bstr, status) && $com(%com).result !== XHR) {
  327.       %error = HTTP Request already completed or wasn't specified
  328.     }
  329.     elseif (!$regex(%switches, /^-[bf]*$/)) {
  330.       %error = Invalid switches specified
  331.     }
  332.     elseif ($regex(%switches, /([bf])\1/)) {
  333.       %error = Duplicate switch specified ( $+ $regml(1) $+ )
  334.     }
  335.     elseif ($regex(%switches, /([bf])/g) > 1) {
  336.       %error = Conflicting switches specified (- $+ $regml(1) $+ , - $+ $regml(2) $+ )
  337.     }
  338.     elseif (b isin %switches && &* !iswm $2) {
  339.       %error = Invalid binary variable specified (No '&' Prefix)
  340.     }
  341.     elseif (b isin %switches && $0 > 2) {
  342.       %error = Invalid binary variable specified (Contains Spaces: $2-)
  343.     }
  344.     elseif (f isin %switches && !$isfile($2-)) {
  345.       %error = File not found: $2-
  346.     }
  347.     else {
  348.       if (b isin %switches && $bvar($2,0)) {
  349.         bwrite $qt(%file) 0 -1 $2
  350.         %rem = $true
  351.       }
  352.       elseif (f isin %switches) {
  353.         %file = $2-
  354.       }      
  355.       elseif ($2-) {
  356.         write -n $qt(%file) $2-
  357.         %rem = $true
  358.       }
  359.       %file = $longfn(%file)
  360.       if ($isfile(%file) && (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(readFile,$qt($JSON::Escape(%file)))) || $comerr)) {
  361.         %error = Unable to pass data to JSON Handler
  362.       }
  363.       elseif (!$com(%com, ExecuteStatement, 1, bstr, xhr $+ $chr(40) $+ $chr(41)) || $comerr) {
  364.         %error = Unable to retrieve data from specified URL :: $com(%com).error :: $com(%com).errortext    
  365.       }
  366.       elseif (!$com(%com, eval, 1, bstr, errortext) || $com(%com).result) {
  367.         %error = $v1
  368.       }
  369.       if (%rem && $isfile(%file)) { .remove %file }
  370.     }
  371.   }
  372.   :error
  373.   %error = $iif($error, $v1, %error)
  374.   if (%error) {
  375.     reseterror
  376.     set -eu0 $+(%,%com,::Error) %error
  377.   }
  378. }
  379. alias JSON {
  380.   var %com, %x = 1, %i = 0, %get = parsedJSON, %tok, %res, %LTLError
  381.   if ($regex($1,/^\d+$/) && $0 === 1) {
  382.     while ($com(%x)) {
  383.       if (JSONHandler::* iswm $v1) {
  384.         %com = $v2
  385.         inc %i
  386.         if (%i === $1) { return $mid(%com,14-) }
  387.       }
  388.       inc %x
  389.     }
  390.     return $iif($1 == 0, %i)
  391.   }
  392.   elseif ($regex($1, /^JSONHandler::CHILD::([^:]+)::(.*)$/)) {
  393.     %com = $regml(1)
  394.     %get = $regml(2)
  395.   }
  396.   else {
  397.     %com = JSONHandler:: $+ $1
  398.   }
  399.   if ($com(%com)) {
  400.     if ($0 == 1) {
  401.       if ($prop == isChild) {
  402.         return $iif($regex($1, /^JSONHandler::CHILD::([^:]+)::(.*)$/), $true, $false)
  403.       }
  404.       elseif ($prop == error || $prop === errormsg) {
  405.         return $(,$+(%,%com,::error))
  406.       }
  407.       elseif ($com($1)) {
  408.         return $1
  409.       }
  410.     }
  411.     elseif (!$com(%com, eval, 1, bstr, status) || $com(%com).result != parsed) {
  412.       set -eu0 $+(%,%com,::error) JSON Handler waiting for HTTP Request
  413.     }
  414.     else {
  415.       %x = 2
  416.       while (%x <= $0) {
  417.         %tok = $(,$ $+ %x)
  418.         if (!$regex(%tok, /^\d+$/)) {
  419.           %tok = $qt($replace(%tok,\,\\,",\"))
  420.         }
  421.         %get = $+(%get,[,%tok,])
  422.         inc %x
  423.       }
  424.       if (!$com(%com, eval, 1, bstr, %get) || $comerr) {
  425.         set -eu0 $+(%,%com,::error) Invalid Item|index specified
  426.       }
  427.       else {
  428.         %LLLError = $true
  429.         %res = $com(%com).result
  430.         %LLLError = $false
  431.         if (%res == [object]) {
  432.           return JSONHandler::CHILD:: $+ $1 $+ :: $+ %get
  433.         }
  434.         else {
  435.           return %res
  436.         }
  437.       }
  438.     }
  439.   }
  440.   return
  441.   :error
  442.   set -eu0 $+(%,%com,::error) $iif(%LLLError, Line to Long; truncated at 4000 bytes, $error)
  443.   reseterror
  444.   if (%LLLError) {
  445.     %res = $left($com(%com).result, 4000)
  446.   }
  447. }
  448. alias JSONClose {
  449.   var %com = JSONHandler:: $+ $1
  450.   var %file = $mircdirdata\JSON $+ $1 $+ 1.json
  451.   unset % [ $+ [ %com ] $+ ] ::*
  452.   if ($com(%com)) { .comclose $v1 }
  453.   if ($timer(%com)) { $+(.timer,%com) off }
  454.   if ($isfile(%file)) { .remove $qt(%file) }
  455. }
  456. alias JSONList {
  457.   var %x = 1, %i = 0
  458.   while ($com(%x)) {
  459.     if (JSONHandler::* iswm $v1) {
  460.       inc %i
  461.       echo $color(info text) -a * # $+ %i : $regsubex($v2, /^JSONHandler::/,)
  462.     }
  463.     inc %x
  464.   }
  465.   if (!%i) {
  466.     echo $color(info text) -a * No active JSON handlers
  467.   }  
  468. }
  469. alias -l JSON::ComOpen { .comopen $1- | if ($com($1) && !$comerr) { return $true } | :error | reseterror | if ($com($1)) { .comclose $1 } }
  470. alias -l JSON::Escape { return $replace($1,\,\\,",\") }
  471. alias -l JSON::File { var %a = 1 | while ($isfile($mircdirdata\ $+ JSON $+ $1 $+ %a $+ .json)) { inc %a } | return $mircdirdata\ $+ $+(JSON, $1, %a, .json) }
  472. 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