alias -l twitch.token {
if ($1 == MY-TWITCH-CHANEL) return MY-CLIENTID
}
on @*:text:!topic*:#:{
if ($2 != $null) {
if ($nick isop #) twitch.topic # $2-
}
else { twitch.topic # }
}
alias twitch.topic {
var %chan = $1, %topic = $2-
var %sockname = twitch.topic. $+ $ticks
hfree -w %sockname | hmake %sockname
hadd %sockname headerfile %sockname $+ .header.txt
hadd %sockname datafile %sockname $+ .data.txt
hadd %sockname params %chan
hadd %sockname oath.token $twitch.token($mid(%chan,2-))
hadd %sockname request /kraken/channels/ $+ $mid(%chan,2-)
if (%topic != $null) {
hadd %sockname method PUT
hadd %sockname data channel[status]= $+ $urlencode(%topic)
hadd %sockname signal topic.set
}
else {
hadd %sockname method GET
hadd %sockname signal topic.get
}
sockopen -e %sockname api.twitch.tv 443
}
on *:signal:topic.get:{
var %err = $1, %sockname = $2, %header = $3, %data = $4, %chan = $5
if (* 200 OK iswm $read(%header,1)) msg %chan Status: $json(%data,status)
else msg %chan Could not get status.
hfree -w %sockname
.remove %header | .remove %data
}
on *:signal:topic.set:{
var %err = $1, %sockname = $2, %header = $3, %data = $4, %chan = $5
if (* 200 OK iswm $read(%header,1)) msg %chan Status set: $json(%data,status)
else msg %chan Could not set status.
hfree -w %sockname
.remove %header | .remove %data
}
on *:sockopen:twitch.*:{
var %a = sockwrite -n $sockname
%a $hget($sockname,method) $hget($sockname,request) HTTP/1.1
%a Host: api.twitch.tv
%a Connection: close
%a Accept: application/vnd.twitchtv.v2+json
if ($hget($sockname,oath.token) != $null) %a Authorization: OAuth $v1
if ($hget($sockname,data) != $null) {
%a Content-Type: application/x-www-form-urlencoded
%a Content-Length: $len($v1)
}
%a $+($crlf,$hget($sockname,data))
}
on *:sockread:twitch.*:{
var %header
var %headerfile = $hget($sockname,headerfile)
var %datafile = $hget($sockname,datafile)
if (!$hget($sockname,header.complete)) {
sockread %header
while (%header != $null) {
write %headerfile %header
sockread %header
}
if ($sockbr) hadd $sockname header.complete $true
}
if ($hget($sockname,header.complete)) {
sockread &read
while ($sockbr) {
bwrite %datafile -1 -1 &read
sockread &read
}
}
}
on *:sockclose:twitch.*:{
var %header = $hget($sockname,headerfile)
var %data = $hget($sockname,datafile)
var %signal = $hget($sockname,signal)
var %params = $hget($sockname,params)
if (%signal) .signal %signal 0 $sockname %header %data %params
else {
hfree -w $sockname
.remove %header
.remove %data
}
}
alias json {
if ($isid) {
;name of the com interface declared so I don't have to type it over and over again :D
var %c = jsonidentifier,%x = 2,%str,%p,%v,%addr
;filter out some characters in $1 (address)
if ($isfile($1)) { %addr = $qt($replace($1,\,\\,;,\u003b,",\u0022)) }
else { %addr = $qt($replace($1,;,\u003b,",\u0022)) }
;if the interface hasnt been open and initialized, do it.
json.comcheck
if (!$timer(jsonclearcache)) { .timerjsonclearcache -o 0 300 jsonclearcache }
;get the list of parameters
while (%x <= $0) {
%p = $($+($,%x),2)
if (%p == $null) { noop }
elseif (%p isnum || $qt($noqt(%p)) == %p) { %str = $+(%str,[,%p,]) }
else { %str = $+(%str,[",%p,"]) }
inc %x
}
if ($prop == count) { %str = %str $+ .length }
;check to see if source is file
if ($isfile($1)) {
if ($com(%c,eval,1,bstr,$+(str2json,$chr(40),filejson,$chr(40),%addr,$chr(41),$chr(41),%str))) { return $com(%c).result }
}
;check to see if source is url
elseif (http://* iswm $1 || https://* iswm $1) {
;if url is in cache, used cached data
if ($com(%c,eval,1,bstr,$+(str2json,$chr(40),urlcache[,%addr,],$chr(41),%str))) { return $com(%c).result }
;otherwise, get data
elseif ($com(%c,eval,1,bstr,$+(urlcache[,%addr,]) = $+(httpjson,$chr(40),$qt($1),$chr(41)))) {
if ($com(%c,eval,1,bstr,$+(str2json,$chr(40),urlcache[,%addr,],$chr(41),%str))) { return $com(%c).result }
}
}
;get data from inputted json
elseif ($com(%c,eval,1,bstr,$+(x=,%addr,;,x,%str,;))) { return $com(%c).result }
}
}
alias jsonclearcache {
if ($com(jsonidentifier)) {
if (!$1) { noop $com(jsonidentifier,executestatement,1,bstr,urlcache = {}) }
else { echo -a $com(jsonidentifier,executestatement,1,bstr,urlcache[" $+ $1 $+ "] = "") }
}
}
alias json.enc {
json.comcheck
if ($com(jsonidentifier,eval,1,bstr,encodeURI(" $+ $1- $+ "))) { return $com(jsonidentifier).result }
}
alias json.enccomponent {
json.comcheck
if ($com(jsonidentifier,eval,1,bstr,encodeURIComponent(" $+ $1- $+ "))) { return $com(jsonidentifier).result }
}
alias -l json.comcheck {
var %c = jsonidentifier
if (!$com(%c)) {
.comopen %c MSScriptControl.ScriptControl
;add two javascript functions for getting json from urls and files
noop $com(%c,language,4,bstr,jscript) $com(%c,addcode,1,bstr,function httpjson(url) $({,0) y=new ActiveXObject("Microsoft.XMLHTTP");y.open("GET",url,false);y.send();return y.responseText; $(},0))
noop $com(%c,addcode,1,bstr,function filejson (file) $({,0) x = new ActiveXObject("Scripting.FileSystemObject"); txt1 = x.OpenTextFile(file,1); txt2 = txt1.ReadAll(); txt1.Close(); return txt2; $(},0))
;add function to securely evaluate json
noop $com(%c,addcode,1,bstr,function str2json (json) $({,0) return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(json.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + json + ')'); $(},0))
;add a cache for urls
noop $com(%c,addcode,1,bstr,urlcache = {})
}
}
alias -l urlencode return $regsubex($1-,/([^A-Z0-9])/gi,$+(%,$base($asc(\1),10,16)))