Advertisement
westor

Twitch Giveaway (AUTO) v1.0 for Draven

Jun 19th, 2015
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 26.42 KB | None | 0 0
  1. ON !*:TEXT:*:#: { ; by westor 2015
  2.   if (%giveawaystart_ [ $+ [ $chan ] ]) {
  3.     if ($1 == %giveawaymsg_ [ $+ [ $chan ] ]) {
  4.       if ($read(Raffle_ $+ $chan $+ .txt,nw,$nick)) { msg # ( $+ $nick $+ ): You are already into the list! | return }
  5.       write Raffle_ $+ $chan $+ .txt $nick
  6.       msg # ( $+ $nick $+ ): You have been added to the list.
  7.     }
  8.   }
  9.   if ($1 == !giveawaystart) {
  10.     if ($nick isop #) {
  11.       if ($2) {
  12.         if (%giveawaystart_ [ $+ [ $chan ] ]) { msg # ( $+ $nick $+ ): There is an other giveaway at the moment! | return }
  13.         set -e %giveawaystart_ [ $+ [ $chan ] ] On
  14.         set -e %giveawaymsg_ [ $+ [ $chan ] ] $2
  15.         set -e %giveawaynick_ [ $+ [ $chan ] ] $nick
  16.         msg # ( $+ $nick $+ ): Giveaway started and will expire in 5 minutes, the word/phrase to join is $qt($2)
  17.         .timer[GIVEAWAY_SAY] 0 60 giveaway_say $chan
  18.         .timer[GIVEAWAY] 1 300 giveaway_end $chan
  19.         if ($isfile(Raffle_ $+ $chan $+ .txt)) { .remove $qt(Raffle_ $+ $chan $+ .txt) }
  20.       }
  21.       elseif (!$2) { msg # ( $+ $nick $+ ): Please enter a word for the giveaway trigger. }
  22.     }
  23.     elseif ($nick !isop #) { msg # ( $+ $nick $+ ): You are NOT an channel operator! }
  24.   }
  25. }
  26.  
  27. alias giveaway_say { ; by westor 2015
  28.   if (!$1) { .timer[GIVEAWAY_SAY] off | return }
  29.   if ($me !ison $1) { .timer[GIVEAWAY_SAY] off | return }
  30.   if (!%giveawaystart_ [ $+ [ $1 ] ]) { .timer[GIVEAWAY_SAY] off | return }
  31.   var %l = $timer([GIVEAWAY]).secs
  32.   .msg $1 (GIVEAWAY): The GiveAway will expire in:  $+ $duration(%l) $+ 
  33. }
  34.  
  35. alias giveaway_end { ; by westor 2015
  36.   .timer[GIVEAWAY*] off
  37.   if (!$1) { return }
  38.   if (!%giveawaystart_ [ $+ [ $1 ] ]) { return }
  39.   var %user = $read(Raffle $+ _ $+ $1 $+ .txt,n)
  40.   unset %giveawaystart_ [ $+ [ $1 ] ]
  41.   unset %giveawaymsg_ [ $+ [ $1 ] ]
  42.   unset %giveawaynick_ [ $+ [ $1 ] ]
  43.   if (%user) { tw_check_follow_auto $remove($1,$chr(35)) %user $1 }
  44.   else { .msg $1 (GIVEAWAY): There is NOT any winner available. }
  45.   if ($isfile(Raffle_ $+ $1 $+ .txt)) { .remove $qt(Raffle_ $+ $1 $+ .txt) }
  46. }
  47.  
  48. alias tw_check_follow_auto { ; by westor 2015
  49.   if (!$1) { return }
  50.   var %u = https://api.twitch.tv/kraken/users/ $+ $2 $+ /follows/channels/ $+ $1 $+ ?limit=1&nocache= $+ $ticks
  51.   var %v = twc_ $+ $ticks
  52.   JSONOpen -ud %v %u
  53.   if (%JSONError) { var %error = 1 | goto end | return }
  54.   var %fc = $json(%v,error)
  55.   if (%fc) {
  56.     var %msg = $json(%v,message)
  57.     if (is not following isin %msg) { var %status = 0 }
  58.   }
  59.   :end
  60.   var %status = $json(%v,created_at)
  61.   if (%error) { msg $3 (GIVEAWAY): Giveaway has ended, the winner is: $2 ( $+ $2 is NOT following the channel) | goto end2 }
  62.   if (!%status) { msg $3 (GIVEAWAY): Giveaway has ended, the winner is: $2 ( $+ $2 is NOT following the channel) }
  63.   elseif (%status) { msg $3 (GIVEAWAY): Giveaway has ended, the winner is: $2 ( $+ $2 is following the channel) }
  64.   :end2
  65.   JSONClose %v
  66. }
  67.  
  68. ; -----
  69.  
  70. alias JSONVersion {
  71.   if ($isid) {
  72.     return $iif($1 != short,JSONForMirc v,v) $+ 0.2.2
  73.   }
  74. }
  75. alias JSONError {
  76.   if ($isid) {
  77.     return %JSONError
  78.   }
  79. }
  80. alias JSONOpen {
  81.   if ($isid) return
  82.   unset %JSONError
  83.   debugger -i 0 Calling /JSONOpen $1-
  84.   var %switches = -, %error, %com, %file
  85.   if (-* iswm $1) {
  86.     %switches = $1
  87.     tokenize 32 $2-
  88.   }
  89.   if ($regex(%switches, ([^dbfuw\-]))) {
  90.     %error = Invalid switches specified: $regml(1)
  91.   }
  92.   elseif ($regex(%switches, ([dbfuw]).*?\1)) {
  93.     %error = Duplicate switch specified: $regml(1)
  94.   }
  95.   elseif ($regex(%switches, /([bfu])/g) > 1) {
  96.     %error = Conflicting switches: $regml(1) $+ , $regml(2)
  97.   }
  98.   elseif (u !isin %switches && w isin %switches) {
  99.     %error = -w switch can only be used with -u
  100.   }
  101.   elseif ($0 < 2) {
  102.     %error = Missing Parameters
  103.   }
  104.   elseif (!$regex($1, /^[a-z][a-z\d_.-]+$/i)) {
  105.     %error = Invalid handler name: Must start with a letter and contain only letters numbers _ . and -
  106.   }
  107.   elseif ($com(JSONHandler:: $+ $1)) {
  108.     %error = Name in use
  109.   }
  110.   elseif (b isin %switches && $0 != 2) {
  111.     %error = Invalid parameter: Binary variable names cannot contain spaces
  112.   }
  113.   elseif (b isin %switches && &* !iswm $2) {
  114.     %error = Invalid parameters: Binary variable names start with &
  115.   }
  116.   elseif (b isin %switches && !$bvar($2, 0)) {
  117.     %error = Invalid parameters: Binary variable is empty
  118.   }
  119.   elseif (f isin %switches && !$isfile($2-)) {
  120.     %error = Invalid parameters: File doesn't exist
  121.   }
  122.   elseif (f isin %switches && !$file($2-).size) {
  123.     %error = Invalid parameters: File is empty
  124.   }
  125.   elseif (u isin %switches && $0 != 2) {
  126.     %error = Invalid parameters: URLs cannot contain spaces
  127.   }
  128.   else {
  129.     .comopen JSONHandler:: $+ $1 MSScriptControl.ScriptControl
  130.     if (!$com(JSONHandler:: $+ $1) || $comerr) {
  131.       %error = Unable to create an instance of MSScriptControl.ScriptControl
  132.     }
  133.     else {
  134.       %com = JSONHandler:: $+ $1
  135.       if (!$com(%com, language, 4, bstr, jscript) || $comerr) {
  136.         %error = Unable to set ScriptControl's language to Javascript
  137.       }
  138.       elseif (!$com(%com, timeout, 4, bstr, 60000) || $comerr) {
  139.         %error = Unable to set ScriptControl's timeout to 60seconds
  140.       }
  141.       elseif (!$com(%com, ExecuteStatement, 1, bstr, $JScript) || $comerr) {
  142.         %error = Unable to add required javascript to the ScriptControl instance
  143.       }
  144.       elseif (u isincs %switches) {
  145.         if (1 OK != $jstry(%com, $jscript(urlInit), $escape($2-).quote)) {
  146.           %error = $gettok($v2, 2-, 32)
  147.         }
  148.         elseif (w !isincs %switches && 0 ?* iswm $jsTry(%com, $jscript(urlParse), status="error").withError) {
  149.           %error = $gettok($v2, 2-, 32)
  150.         }
  151.       }
  152.       elseif (f isincs %switches) {
  153.         if (1 OK != $jstry(%com, $jscript(fileParse), $escape($longfn($2-)).quote)) {
  154.           %error = $gettok($v2, 2-, 32)
  155.         }
  156.       }
  157.       elseif (b isincs %switches) {
  158.         %file = $tempfile
  159.         bwrite $qt(%file) -1 -1 $2
  160.         debugger %com Wrote $2 to $qt(%file)
  161.         if (0 ?* iswm $jstry(%com, $jscript(fileParse), $escape(%file).quote)) {
  162.           %error = $gettok($v2, 2-, 32)
  163.         }
  164.       }
  165.       else {
  166.         %file = $tempfile
  167.         write -n $qt(%file) $2-
  168.         debugger %com Wrote $2- to $qt(%file)
  169.         if (0 ?* iswm $jstry(%com, $jscript(fileParse), $escape(%file).quote)) {
  170.           %error = $gettok($v2, 2-, 32)
  171.         }
  172.       }
  173.       if (!%error) {
  174.         if (d isin %switches) {
  175.           $+(.timer, %com) -o 1 0 JSONClose $1
  176.         }
  177.         Debugger -s %com Successfully created
  178.       }
  179.     }
  180.   }
  181.   :error
  182.   %error = $iif($error, $error, %error)
  183.   reseterror
  184.   if (%file && $isfile(%file)) {
  185.     .remove $qt(%file)
  186.     debugger %com Removed $qt(%file)
  187.   }
  188.   if (%error) {
  189.     if (%com && $com(%com)) {
  190.       .comclose %com
  191.     }
  192.     set -eu0 %JSONError %error
  193.     Debugger -e 0 /JSONOpen %switches $1- --RAISED-- %error
  194.   }
  195. }
  196. alias JSONUrlMethod {
  197.   if ($isid) return
  198.   unset %JSONError
  199.   debugger -i 0 Calling /JSONUrlMethod $1-
  200.   var %error, %com
  201.   if ($0 < 2) {
  202.     %error = Missing parameters
  203.   }
  204.   elseif ($0 > 2) {
  205.     %error = Too many parameters specified
  206.   }
  207.   elseif (!$regex($1, /^[a-z][a-z\d_.\-]+$/i)) {
  208.     %error = Invalid handler name: Must start with a letter and contain only letters numbers _ . and -
  209.   }
  210.   elseif (!$com(JSONHandler:: $+ $1)) {
  211.     %error = Invalid handler name: JSON handler does not exist
  212.   }
  213.   elseif (!$regex($2, /^(?:GET|POST|PUT|DEL)$/i)) {
  214.     %error = Invalid request method: Must be GET, POST, PUT, or DEL
  215.   }
  216.   else {
  217.     var %com = JSONHandler:: $+ $1
  218.     if (1 OK != $jsTry(%com, $JScript(UrlMethod), status="error", $qt($upper($2))).withError) {
  219.       %error = $gettok($v2, 2-, 32)
  220.     }
  221.     else {
  222.       Debugger -s $+(%com,>JSONUrlMethod) Method set to $upper($2)
  223.     }
  224.   }
  225.   :error
  226.   %error = $iif($error, $v1, %error)
  227.   reseterror
  228.   if (%error) {
  229.     set -eu0 %JSONError %error
  230.     if (%com) set -eu0 % [ $+ [ %com ] $+ ] ::Error %error
  231.     Debugger -e $iif(%com, $v1, 0) /JSONUrlMethod %switches $1- --RAISED-- %error
  232.   }
  233. }
  234. alias JSONUrlHeader {
  235.   if ($isid) return
  236.   unset %JSONError
  237.   debugger -i 0 Calling /JSONUrlHeader $1-
  238.   var %error, %com
  239.   if ($0 < 3) {
  240.     %error = Missing parameters
  241.   }
  242.   elseif (!$regex($1, /^[a-z][a-z\d_.\-]+$/i)) {
  243.     %error = Invalid handler name: Must start with a letter and contain only letters numbers _ . and -
  244.   }
  245.   elseif (!$com(JSONHandler:: $+ $1)) {
  246.     %error = Invalid handler name: JSON handler does not exist
  247.   }
  248.   elseif (!$regex($2, /^[a-z_-]+:?$/i)) {
  249.     %error = Invalid header name: Header names can only contain letters, _ and -
  250.   }
  251.   else {
  252.     %com = JSONHandler:: $+ $1
  253.     if (1 OK !== $jsTry(%com, $JScript(UrlHeader), status="error", $escape($regsubex($2, :+$, )).quote, $escape($3-).quote).withError) {
  254.       %error = $gettok($v2, 2-, 32)
  255.     }
  256.     else {
  257.       Debugger -s $+(%com,>JSONUrlHeader) Header $+(',$2,') set to $3-
  258.     }
  259.   }
  260.   :error
  261.   %error = $iif($error, $v1, %error)
  262.   reseterror
  263.   if (%error) {
  264.     set -eu0 %JSONError %error
  265.     if (%com) set -eu0 % [ $+ [ %com ] $+ ] ::Error %error
  266.     Debugger -e $iif(%com, $v1, 0) /JSONUrlMethod %switches $1- --RAISED-- %error
  267.   }
  268. }
  269. alias JSONUrlOption {
  270.   if ($isid) return
  271.   unset %JSONError
  272.   Debugger -i 0 /JSONUrlOption is depreciated and will be removed. Please use /JSONUrlMethod and /JSONUrlHeader
  273.   if ($2 == method) {
  274.     JSONUrlMethod $1 $3-
  275.   }
  276.   else {
  277.     JSONUrlHeader $1-
  278.   }
  279. }
  280. alias JSONUrlGet {
  281.   if ($isid) return
  282.   unset %JSONError
  283.   Debugger -i 0 Calling /JSONUrlGet $1-
  284.   var %switches = -, %error, %com, %file
  285.   if (-* iswm $1) {
  286.     %switches = $1
  287.     tokenize 32 $2-
  288.   }
  289.   if (!$0 || (%switches != - && $0 < 2)) {
  290.     %error = Missing parameters
  291.   }
  292.   elseif (!$regex(%switches, ^-[bf]?$)) {
  293.     %error = Invalid switch(es) specified
  294.   }
  295.   elseif (!$regex($1, /^[a-z][a-z\d_.\-]+$/i)) {
  296.     %error = Invalid handler name: Must start with a letter and contain only letters numbers _ . and -
  297.   }
  298.   elseif (!$com(JSONHandler:: $+ $1)) {
  299.     %error = Specified handler does not exist
  300.   }
  301.   elseif (b isincs %switches && &* !iswm $2) {
  302.     %error = Invalid bvar name: bvars start with &
  303.   }
  304.   elseif (b isincs %switches && $0 > 2) {
  305.     %error = Invalid bvar name: Contains spaces: $2-
  306.   }
  307.   elseif (f isincs %switches && !$isfile($2-)) {
  308.     %error = Specified file does not exist: $longfn($2-)
  309.   }
  310.   else {
  311.     %com = JSONHandler:: $+ $1
  312.     if ($0 > 1) {
  313.       if (f isincs %switches) {
  314.         if (0 ?* iswm $jsTry(%com, $JScript(UrlData), status="error", $escape($longfn($2-)).quote).withError) {
  315.           %error = $gettok($v2, 2-, 32)
  316.         }
  317.         else {
  318.           Debugger -s $+(%com,>JSONUrlGet) Stored $longfn($2-) as data to send with HTTP Request
  319.         }
  320.       }
  321.       else {
  322.         %file = $tempfile
  323.         if (b isincs %switches) {
  324.           bwrite $qt(%file) -1 -1 $2
  325.         }
  326.         else {
  327.           write -n $qt(%file) $2-
  328.         }
  329.         Debugger -s $+(%com,>JSONUrlGet) Wrote specified data to %file
  330.         if (0 ?* iswm $jsTry(%com, $JScript(UrlData), status="error", $escape(%file).quote).withError) {
  331.           %error = $gettok($v2, 2-, 32)
  332.         }
  333.         else {
  334.           Debugger -s $+(%Com,>JSONUrlGet) Stored $2- as data to send with HTTP Request
  335.         }
  336.         .remove $qt(%file)
  337.       }
  338.     }
  339.     if (!%error) {
  340.       if (0 ?* iswm $jsTry(%com, $JScript(URLParse), status="error").withError) {
  341.         %error = $gettok($v2, 2-, 32)
  342.       }
  343.       else {
  344.         Debugger -s $+(%com,>JSONUrlGet) Request finished
  345.       }
  346.     }
  347.   }
  348.   :error
  349.   %error = $iif($error, $v1, %error)
  350.   reseterror
  351.   if (%error) {
  352.     set -eu0 %JSONError %error
  353.     if (%com) set -eu0 % [ $+ [ %com ] $+ ] ::Error %error
  354.     Debugger -e $iif(%com, $v1, 0) /JSONUrlGet %switches $1- --RAISED-- %error
  355.   }
  356. }
  357. alias JSONGet {
  358.   if ($isid) return
  359.   unset %JSONError
  360.   debugger -i 0 /JSONGet is depreciated and will be removed. Please use /JSONUrlGet
  361.   JSONUrlGet $1-
  362. }
  363. alias JSONClose {
  364.   if ($isid) return
  365.   unset %JSONError
  366.   Debugger -i 0 /JSONClose $1-
  367.   var %switches = -, %error, %com, %x
  368.   if (-* iswm $1) {
  369.     %switches = $1
  370.     tokenize 32 $2-
  371.   }
  372.   if ($0 < 1) {
  373.     %error = Missing parameters
  374.   }
  375.   elseif ($0 > 1) {
  376.     %error = Too many parameters specified.
  377.   }
  378.   elseif (%switches !== - && %switches != -w) {
  379.     %error = Unknown switches specified
  380.   }
  381.   elseif (%switches == -) {
  382.     %com = JSONHandler:: $+ $1
  383.     if ($com(%com)) { .comclose %com }
  384.     if ($timer(%com)) { $+(.timer,%com) off }
  385.     unset % [ $+ [ %com ] $+ ] ::Error
  386.     Debugger -i %com Closed
  387.   }
  388.   else {
  389.     %com = JSONHandler:: $+ $1
  390.     %x = 1
  391.     while (%x <= $com(0)) {
  392.       if (%com iswm $com(%x)) {
  393.         .comclose $v1
  394.         $+(.timer,$v1) off
  395.         unset % [ $+ [ $v1 ] $+ ] ::*
  396.         Debugger -i %com Closed
  397.       }
  398.       else {
  399.         inc %x
  400.       }
  401.     }
  402.   }
  403.   :error
  404.   %error = $iif($error, $v1, %error)
  405.   reseterror
  406.   if (%error) {
  407.     set -eu0 %JSONError %error
  408.   }
  409. }
  410. alias JSONList {
  411.   if ($isid) return
  412.   Debugger -i 0 Calling /JSONList $1-
  413.   var %x = 1, %i = 0
  414.   while ($com(%x)) {
  415.     if (JSONHandler::* iswm $v1) {
  416.       inc %i
  417.       echo $color(info) -a * # $+ %i : $regsubex($v2, /^JSONHandler::/, )
  418.     }
  419.     inc %x
  420.   }
  421.   if (!%i) {
  422.     echo $color(info) -a * No active JSON handlers
  423.   }
  424. }
  425. alias JSON {
  426.   if (!$isid) {
  427.     return
  428.   }
  429.   var %x, %calling, %i = 0, %com, %get = json, %ref = $false, %error, %file
  430.   if ($JSONDebug) {
  431.     %x = 0
  432.     while (%x < $0) {
  433.       inc %x
  434.       %calling = %calling $+ $iif(%calling,$chr(44)) $($ $+ %x,2)
  435.     }
  436.     debugger -i 0 Calling $!JSON( $+ %calling $+ $chr(41) $+ $iif($prop,. $+ $prop)
  437.   }
  438.   if (!$0) {
  439.     return
  440.   }
  441.   if ($regex($1, ^\d+$)) {
  442.     %x = 1
  443.     while ($com(%x)) {
  444.       if (JSONHandler::* iswm $v1) {
  445.         inc %i
  446.         if (%i == $1) {
  447.           %com = $com(%x)
  448.           break
  449.         }
  450.       }
  451.       inc %x
  452.     }
  453.     if ($0 == 1 && $1 == 0) {
  454.       return %i
  455.     }
  456.   }
  457.   elseif ($regex($1, /^[a-z][a-z\d_.-]+$/i)) {
  458.     %com = JSONHandler:: $+ $1
  459.   }
  460.   elseif ($regex($1, /^(JSONHandler::[a-z][a-z\d_.-]+)::(.+)$/i)) {
  461.     %com = $regml(1)
  462.     %get = json $+ $regml(2)
  463.     %ref = $true
  464.   }
  465.   if (!%com) {
  466.     %error = Invalid name specified
  467.   }
  468.   elseif (!$com(%com)) {
  469.     %error = Handler doesn't exist
  470.   }
  471.   elseif (!$regex($prop, /^(?:Status|IsRef|IsChild|Error|Data|UrlStatus|UrlStatusText|UrlHeader|Fuzzy|FuzzyPath|Type|Length|ToBvar|IsParent)?$/i)) {
  472.     %error = Unknown prop specified
  473.   }
  474.   elseif ($0 == 1) {
  475.     if ($prop == isRef) {
  476.       return %ref
  477.     }
  478.     elseif ($prop == isChild) {
  479.       Debugger -i 0 $!JSON().isChild is depreciated use $!JSON().isRef
  480.       return %ref
  481.     }
  482.     elseif ($prop == status) {
  483.       if ($com(%com, eval, 1, bstr, status) && !$comerr) {
  484.         return $com(%com).result
  485.       }
  486.       else {
  487.         %error = Unable to determine status
  488.       }
  489.     }
  490.     elseif ($prop == error) {
  491.       if ($eval($+(%,%com,::Error),2)) {
  492.         return $v1
  493.       }
  494.       elseif ($com(%com, eval, 1, bstr, error) && !$comerr) {
  495.         return $com(%com).result
  496.       }
  497.       else {
  498.         %error = Unable to determine if there is an error
  499.       }
  500.     }
  501.     elseif ($prop == UrlStatus || $prop == UrlStatusText) {
  502.       if (0 ?* iswm $jsTry(%com, $JScript($prop))) {
  503.         %error = $gettok($v2, 2-, 32)
  504.       }
  505.       else {
  506.         return $v2
  507.       }
  508.     }
  509.     elseif (!$prop) {
  510.       return $regsubex(%com,/^JSONHandler::/,)
  511.     }
  512.   }
  513.   elseif (!$regex($prop, /^(?:fuzzy|fuzzyPath|data|type|length|toBvar|isParent)?$/i)) {
  514.     %error = $+(',$prop,') cannot be used when referencing items
  515.   }
  516.   elseif ($prop == toBvar && $chr(38) !== $left($2, 1) ) {
  517.     %error = Invalid bvar specified: bvar names must start with &
  518.   }
  519.   elseif ($prop == UrlHeader) {
  520.     if ($0 != 2) {
  521.       %error = Missing or excessive header parameter specified
  522.     }
  523.     elseif (0 ?* iswm $jsTry(%com, $JScript(UrlHeader), $escape($2).quote)) {
  524.       %error = $gettok($v2, 2-, 32)
  525.     }
  526.     else {
  527.       return $gettok($v2, 2-, 32)
  528.     }
  529.   }
  530.   elseif (fuzzy* iswm $prop) {
  531.     if ($0 < 2) {
  532.       %error = Missing parameters
  533.     }
  534.     else {
  535.       var %x = 2, %path, %res
  536.       while (%x <= $0) {
  537.         %path = %path $+ $escape($($ $+ %x, 2)).quote $+ $chr(44)
  538.         inc %x
  539.       }
  540.       %res = $jsTry(%com, $JScript(fuzzy), %get, $left(%path, -1))
  541.  
  542.       if (0 ? iswm %res) {
  543.         %error = $gettok(%res, 2-, 32)
  544.       }
  545.       elseif ($prop == fuzzy) {
  546.         %get = %get $+ $gettok(%res, 2-, 32)
  547.       }
  548.       else {
  549.         return $regsubex(%get, ^json, ) $+ $gettok(%res, 2-, 32)
  550.       }
  551.     }
  552.   }
  553.   if (!%error) {
  554.     if (fuzzy* !iswm $prop) {
  555.       %x = $iif($prop == toBvar, 3, 2)
  556.       while (%x <= $0) {
  557.         %i = $ [ $+ [ %x ] ]
  558.         if ($len(%i)) {
  559.           %get = $+(%get, [", $escape(%i), "])
  560.           inc %x
  561.         }
  562.         else {
  563.           %error = Empty index|item passed.
  564.           break
  565.         }
  566.       }
  567.     }
  568.     if (!%error) {
  569.       if ($prop == type) {
  570.         if (0 ?* iswm $jsTry(%com, $JScript(typeof), %get)) {
  571.           %error = $gettok($v2, 2-, 32)
  572.         }
  573.         else {
  574.           return $gettok($v2, 2-, 32)
  575.         }
  576.       }
  577.       elseif ($prop == length) {
  578.         if (0 ?* iswm $jsTry(%com, $JScript(length), %get)) {
  579.           %error = $gettok($v2, 2-, 32)
  580.         }
  581.         else {
  582.           return $gettok($v2, 2-, 32)
  583.         }
  584.       }
  585.       elseif ($prop == isParent) {
  586.         if (0 ?* iswm $jsTry(%com, $JScript(isparent), %get)) {
  587.           %error = $gettok($v2, 2-, 32)
  588.         }
  589.         else {
  590.           return $iif($gettok($v2, 2-, 32), $true, $false)
  591.         }
  592.       }
  593.       elseif ($prop == toBvar) {
  594.         %file = $tempfile
  595.         if (0 ?* iswm $jsTry(%com, $JScript(tofile), $escape(%file).quote, %get)) {
  596.           %error = $gettok($v2, 2-, 32)
  597.         }
  598.         else {
  599.           bread $qt(%file) 0 $file(%file) $2
  600.         }
  601.         if ($isfile(%file)) { .remove $qt(%file) }
  602.       }
  603.       elseif (0 ?* iswm $jsTry(%com, $JScript(get), %get)) {
  604.         %error = $gettok($v2, 2-, 32)
  605.         if (%error == Object or Array referenced) {
  606.           %error = $null
  607.           Debugger -s $+(%com,>$JSON) Result is an Object or Array; returning reference
  608.           return %com $+ :: $+ $regsubex(%get, /^json/, )
  609.         }
  610.       }
  611.       else {
  612.         var %res = $gettok($v2, 2-, 32)
  613.         Debugger -s $+(%com,>$JSON) %get references %res
  614.         return %res
  615.       }
  616.     }
  617.   }
  618.   :error
  619.   %error = $iif($error, $v1, %error)
  620.   if (%error) {
  621.     set -eu0 %JSONError
  622.     if (%com && $com(%com)) {
  623.       set -eu0 $+(%,%com,::Error) %error
  624.     }
  625.     var %r
  626.     %x = 0
  627.     while (%x < $0) {
  628.       inc %x
  629.       %r = $addtok(%r, $chr(32) $+ $ [ $+ [ %x ] ] , 44)
  630.     }
  631.     debugger -e $iif(%com && $com(%com),%com,0) $!JSON( $+ %r $+ ) $+ $+ $iif($prop,. $+ $prop) --RAISED-- %error
  632.   }
  633. }
  634. alias JSONDebug {
  635.   if ($isid) {
  636.     return $iif($group(#JSONForMircDebug) == on, $true, $false)
  637.   }
  638.   elseif ($0) {
  639.     tokenize 32 $iif($group(#JSONForMircDebug) == on, off, on)
  640.   }
  641.   if ($regex($1-,/^(?:on|enable)$/i)) {
  642.     .enable #JSONForMircDebug
  643.     debugger -i Debugger Now Enabled
  644.   }
  645.   elseif ($regex($1-, /^(?:off|disable)$/i)) {
  646.     .disable #JSONForMircDebug
  647.     if ($window(@JSONForMircDebug)) {
  648.       close -@ @JSONForMircDebug
  649.     }
  650.   }
  651. }
  652. #JSONForMircDebug off
  653. alias -l Debugger {
  654.   if ($isid) return
  655.   if (!$window(@JSONForMircDebug)) {
  656.     window -zk0 @JSONForMircDebug
  657.   }
  658.   var %switches = -, %c
  659.   if (-* iswm $1) {
  660.     %switches = $1
  661.     tokenize 32 $2-
  662.   }
  663.   if (e isincs %switches) {
  664.     %c = 04
  665.   }
  666.   elseif (s isincs %switches) {
  667.     %c = 12
  668.   }
  669.   else {
  670.     %c = 03
  671.   }
  672.   var %n = $iif($1, $1, JSONForMirc)
  673.   %n = $regsubex(%n, /^JSONHandler::, )
  674.   aline -p @JSONForMircDebug $+($chr(3),%c,[,%n,],$chr(15)) $2-
  675. }
  676. #JSONForMircDebug end
  677. alias -l Debugger return
  678. menu @JSONForMircDebug {
  679.   .Clear: clear -@ @JsonForMircDebug
  680.   .Disable and Close: JSONDebug off
  681. }
  682. alias -l tempfile {
  683.   var %n = 1
  684.   while ($isfile($scriptdirJSONTmpFile $+ %n $+ .json)) {
  685.     inc %n
  686.   }
  687.   return $scriptdirJSONTmpFile $+ %n $+ .json
  688. }
  689. alias -l escape {
  690.   var %esc = $replace($1-,\,\\,",\")
  691.   return $iif($prop == quote, $qt(%esc), %esc)
  692. }
  693. alias -l JScript {
  694.   if (!$isid) return
  695.   if (!$0) return (function(){status="init";json=null;url={m:"GET",u:null,h:[],d:null};response=null;var r,x=['MSXML2.SERVERXMLHTTP.6.0','MSXML2.SERVERXMLHTTP.3.0','MSXML2.SERVERXMLHTTP','MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.3.0','Microsoft.XMLHTTP'],i;while(x.length){try{r=new ActiveXObject(x.shift());break}catch(e){}}xhr=r?function(){r.open(url.m,url.u,false);for(i=0;i<url.h.length;i+=1)r.setRequestHeader(url.h[i][0],url.h[i][1]);r.send(url.d);return(response=r).responseText}:function(){throw new Error("HTTP Request object not found")};read=function(f){var a=new ActiveXObject("ADODB.stream"),d;a.CharSet="utf-8";a.Open();a.LoadFromFile(f);if(a.EOF){a.close();throw new Error("No content in file")}d=a.ReadText();a.Close();return d;};write=function(f,d){var a=new ActiveXObject("ADODB.stream");a.CharSet="utf-8";a.Open();a.WriteText(d);a.SaveToFile(f,2);a.Close()};parse=function(t){if(/^[\],:{}\s]*$/.test((t=(String(t)).replace(/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,function(a){return'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)})).replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){return eval('('+t+')')}throw new SyntaxError('Unable to Parse: Invalid JSON')};fuzzy=function(){var a=Array.prototype.slice.call(arguments),b=a.shift(),c="",d=Object.prototype.toString.call(b),e,f,g,h,i;for(e=0;e<a.length;e+=1){f=a[e];if(b.hasOwnProperty(f)){if(typeof b[f]==="function")throw new TypeError("Reference points to a function");b=b[f];c+="[\""+f+"\"]"}else if(d==="[object Object]"){if(typeof f==="number")f=f.toString(10);f=f.toLowerCase();g=-1;i=!1;for(h in b){if(b.hasOwnProperty(h)&&typeof b[h]!=="function"){g+=1;if(h.toLowerCase()===f){b=b[h];c+="[\""+h+"\"]";i=!0;break}else if(g.toString(10)===f){b=b[h];c+="[\""+h+"\"]";i=!0;break}}}if(!i)throw new Error("No matching reference found");}else{throw new Error("Reference does not exist")}d=Object.prototype.toString.call(b)}return c}}());
  696.   if ($1 == FileParse)     return if(status!=="init")throw new Error("Parse Not Pending");json=parse(read(@1@));status="done"
  697.   if ($1 == UrlInit)       return if(status!=="init")throw new Error("JSON handler not ready");url.u=@1@;status="url"
  698.   if ($1 == UrlMethod)     return if(status!=="url")throw new Error("URL Request Not Pending");url.m=@1@
  699.   if ($1 == UrlHeader)     return if(status!=="url")throw new Error("URL Request Not Pending");url.h.push([@1@,@2@])
  700.   if ($1 == UrlData)       return if(status!=="url")throw new Error("URL Request Not Pending");url.d=read(@1@)
  701.   if ($1 == UrlParse)      return if(status!=="url")throw new Error("URL Request Not Pending");json=parse(xhr());status="done"
  702.   if ($1 == UrlStatus)     return if(status!=="done")throw new Error("Data not parsed");if(!response)throw new Error("URL request not made");return response.status;
  703.   if ($1 == UrlStatusText) return if(status!=="done")throw new Error("Data not parsed");if(!response)throw new Error("URL request not made");return response.statusText;
  704.   if ($1 == UrlHeader)     return if(status!=="done")throw new Error("Data not parsed");if(!response)throw new Error("URL Request not made");return response.getResponseHeader(@1@)
  705.   if ($1 == fuzzy)         return if(status!=="done")throw new Error("Data not parsed");return "1 "+fuzzy(@1@,@2@)
  706.   if ($1 == typeof)        return if(status!=="done")throw new Error("Data not parsed");var i=@1@;if(i===undefined)throw new TypeError("Reference doesn't exist");if(i===null)return"1 null";var s=Object.prototype.toString.call(i);if(s==="[object Array]")return"1 array";if(s==="[object Object]")return"1 object";return "1 "+typeof(i)
  707.   if ($1 == length)        return if(status!=="done")throw new Error("Data not parsed");var i=@1@;if(i===undefined)throw new TypeError("Reference doesn't exist");if(/^\[object (?:String|Array)\]$/.test(Object.prototype.toString.call(i)))return"1 "+i.length.toString(10);throw new Error("Reference is not a string or array");
  708.   if ($1 == isparent)      return if(status!=="done")throw new Error("Data not parsed");var i=@1@;if(i===undefined)throw new TypeError("Reference doesn't exist");if(/^\[object (?:Object|Array)\]$/.test(Object.prototype.toString.call(i)))return"1 1";return"1 0"
  709.   if ($1 == tofile)        return if(status!=="done")throw new Error("Data not parsed");var i=@2@;if(i===undefined)throw new TypeError("Reference doesn't exist");if(typeof i!=="string")throw new TypeError("Reference must be a string");write(@1@,i);
  710.   if ($1 == get)           return if(status!=="done")throw new Error("Data not parsed");var i=@1@;if(i===undefined)throw new TypeError("Reference doesn't exist");if(i===null)return"1";if(/^\[object (?:Array|Object)\]$/.test(Object.prototype.toString.call(i)))throw new TypeError("Object or Array referenced");if(i.length>4000)throw new Error("Data would exceed mIRC's line length limit");if(typeof i == "boolean")return i?"1 1":"1 0";if(typeof i == "number")return "1 "+i.toString(10);return "1 "+i;
  711. }
  712. alias -l jsTry {
  713.   if ($isid) {
  714.     if ($0 < 2 || $prop == withError && $0 < 3) {
  715.       return 0 Missing parameters
  716.     }
  717.     elseif (!$com($1)) {
  718.       return 0 No such com
  719.     }
  720.     else {
  721.       var %code = $2, %error, %n = 2, %o, %js
  722.       if ($prop == withError) {
  723.         %error = $3
  724.         %n = 3
  725.       }
  726.       %o = %n
  727.       while (%n < $0) {
  728.         inc %n
  729.         set -l $+(%, arg, $calc(%n - %o)) $eval($+($, %n), 2)
  730.       }
  731.       %code = $regsubex($regsubex(%code, /@(\d+)@/g, $var($+(%, arg, \t ),1).value), [\s;]+$, )
  732.       %error = $regsubex($regsubex(%error, /@(\d+)@/g, $var($+(%, arg, \t ),1).value), [\s;]+$, )
  733.       if (%code) %code = $v1 $+ ;
  734.       if (%error) %error = $v1 $+ ;
  735.       %js = (function(){error=null;try{ $+ %code $+ return"1 OK"}catch(e){ $+ %error $+ error=e.message;return"0 "+error}}())
  736.       debugger $1>$jsTry Executing: %js
  737.       if (!$com($1, eval, 1, bstr, %js) || $comerr) {
  738.         return 0 Unable to execute specified javascript
  739.       }
  740.       return $com($1).result
  741.     }
  742.   }
  743. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement