-
-
Save Zirak/fc7d566853555b779d24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//(function () { | |
var input = { | |
//change the following to whatever you want | |
pattern : 'ie>', | |
cb : null, | |
init : function () { | |
var self = this; | |
$.post( '/ws-auth', fkey({roomid:17}), finish ); | |
function finish ( resp ) { | |
self.openSocket( resp.url ); | |
} | |
}, | |
openSocket : function ( url ) { | |
var socket = this.socket = new WebSocket( url + '?l=9999999999999' ); | |
socket.onmessage = this.onmessage.bind( this ); | |
}, | |
onmessage : function ( data ) { | |
if ( !data ) { | |
return; | |
} | |
var self = this, | |
resp = JSON.parse( data.data ); | |
Object.keys( resp ).forEach(function ( room ) { | |
var msgObj = resp[ room ]; | |
if ( msgObj.e ) { | |
msgObj.e.forEach( self.handleMessage, self ); | |
} | |
}); | |
}, | |
handleMessage : function ( message ) { | |
message.content = decodeHtmlEntities( message.content ); | |
console.log( message ); | |
if ( this.acceptMessage(message) ) { | |
this.cb && this.cb( message ); | |
} | |
else { | |
console.log( 'not accepting' ); | |
} | |
}, | |
acceptMessage : function ( message ) { | |
var et = message.event_type; | |
//et = 1 => new message | |
//et = 2 => edited message | |
return ( et === 1 || et === 2 ) && | |
message.content.indexOf(this.pattern) === 0; | |
} | |
}; | |
var output = { | |
send : function ( text, room ) { | |
var pr = $.post( | |
'/chats/' + room + '/messages/new', | |
fkey({text : text}) | |
); | |
pr.always( finish ); | |
function finish ( resp, _, jqXhr ) { | |
var status = jqXhr.status; | |
if ( status === 409 ) { | |
//409 means throttling | |
setTimeout(function () { | |
output.send( text, room ); | |
}, 1000 ); | |
} | |
else if ( status !== 200 ) { | |
console.error( resp, status ); | |
} | |
} | |
} | |
}; | |
//here's the bot itself, a flimsky callback | |
input.cb = function ( msg ) { | |
console.log('got message', msg); | |
var user = users[msg.user_id], | |
res; | |
if ( !legitUser(user) ) { | |
res = 'Sorry buster, no eval for you!'; | |
} | |
else { | |
try { | |
res = dressUpAnswer( eval(msg.content.replace(input.pattern, '')) ); | |
} | |
catch ( e ) { | |
res = e.toString(); | |
} | |
} | |
output.send( ':' + msg.message_id + ' ' + res, msg.room_id ); | |
}; | |
function dressUpAnswer ( answer ) { | |
/*JSON does not like any of the following*/ | |
var strung = { | |
Function : true, Error : true, | |
Undefined : true, RegExp : true | |
}; | |
var should_string = function ( value ) { | |
var type = ( {} ).toString.call( value ).slice( 8, -1 ); | |
if ( type in strung ) { | |
return true; | |
} | |
/*neither does it feel compassionate about NaN or Infinity*/ | |
return value !== value || value === Infinity; | |
}; | |
var reviver = function ( key, value ) { | |
var output; | |
if ( should_string(value) ) { | |
output = '' + value; | |
} | |
else { | |
output = value; | |
} | |
return output; | |
}; | |
return snipAndCodify( JSON.stringify(answer, reviver) ); | |
} | |
function snipAndCodify ( str ) { | |
if ( str.length < 400 ) { | |
return '`' + str +'`'; | |
} | |
return '`' + str.slice(0, 400) + '` (snip)'; | |
} | |
function legitUser ( user ) { | |
return user && | |
user.is_owner || | |
user.is_moderator || | |
user.reputation > 2000; | |
} | |
var decodeHtmlEntities = (function (){ | |
var entities = {"quot":"\"","amp":"&","apos":"'","lt":"<","gt":">","nbsp":" ","iexcl":"¡","cent":"¢","pound":"£","curren":"¤","yen":"¥","brvbar":"¦","sect":"§","uml":"¨","copy":"©","ordf":"ª","laquo":"«","not":"¬","reg":"®","macr":"¯","deg":"°","plusmn":"±","sup2":"²","sup3":"³","acute":"´","micro":"µ","para":"¶","middot":"·","cedil":"¸","sup1":"¹","ordm":"º","raquo":"»","frac14":"¼","frac12":"½","frac34":"¾","iquest":"¿","Agrave":"À","Aacute":"Á","Acirc":"Â","Atilde":"Ã","Auml":"Ä","Aring":"Å","AElig":"Æ","Ccedil":"Ç","Egrave":"È","Eacute":"É","Ecirc":"Ê","Euml":"Ë","Igrave":"Ì","Iacute":"Í","Icirc":"Î","Iuml":"Ï","ETH":"Ð","Ntilde":"Ñ","Ograve":"Ò","Oacute":"Ó","Ocirc":"Ô","Otilde":"Õ","Ouml":"Ö","times":"×","Oslash":"Ø","Ugrave":"Ù","Uacute":"Ú","Ucirc":"Û","Uuml":"Ü","Yacute":"Ý","THORN":"Þ","szlig":"ß","agrave":"à","aacute":"á","acirc":"â","atilde":"ã","auml":"ä","aring":"å","aelig":"æ","ccedil":"ç","egrave":"è","eacute":"é","ecirc":"ê","euml":"ë","igrave":"ì","iacute":"í","icirc":"î","iuml":"ï","eth":"ð","ntilde":"ñ","ograve":"ò","oacute":"ó","ocirc":"ô","otilde":"õ","ouml":"ö","divide":"÷","oslash":"ø","ugrave":"ù","uacute":"ú","ucirc":"û","uuml":"ü","yacute":"ý","thorn":"þ","yuml":"ÿ","OElig":"Œ","oelig":"œ","Scaron":"Š","scaron":"š","Yuml":"Ÿ","fnof":"ƒ","circ":"ˆ","tilde":"˜","Alpha":"Α","Beta":"Β","Gamma":"Γ","Delta":"Δ","Epsilon":"Ε","Zeta":"Ζ","Eta":"Η","Theta":"Θ","Iota":"Ι","Kappa":"Κ","Lambda":"Λ","Mu":"Μ","Nu":"Ν","Xi":"Ξ","Omicron":"Ο","Pi":"Π","Rho":"Ρ","Sigma":"Σ","Tau":"Τ","Upsilon":"Υ","Phi":"Φ","Chi":"Χ","Psi":"Ψ","Omega":"Ω","alpha":"α","beta":"β","gamma":"γ","delta":"δ","epsilon":"ε","zeta":"ζ","eta":"η","theta":"θ","iota":"ι","kappa":"κ","lambda":"λ","mu":"μ","nu":"ν","xi":"ξ","omicron":"ο","pi":"π","rho":"ρ","sigmaf":"ς","sigma":"σ","tau":"τ","upsilon":"υ","phi":"φ","chi":"χ","psi":"ψ","omega":"ω","thetasym":"ϑ","upsih":"ϒ","piv":"ϖ","ensp":" ","emsp":" ","thinsp":" ","ndash":"–","mdash":"—","lsquo":"‘","rsquo":"’","sbquo":"‚","ldquo":"“","rdquo":"”","bdquo":"„","dagger":"†","Dagger":"‡","bull":"•","hellip":"…","permil":"‰","prime":"′","Prime":"″","lsaquo":"‹","rsaquo":"›","oline":"‾","frasl":"⁄","euro":"€","image":"ℑ","weierp":"℘","real":"ℜ","trade":"™","alefsym":"ℵ","larr":"←","uarr":"↑","rarr":"→","darr":"↓","harr":"↔","crarr":"↵","lArr":"⇐","uArr":"⇑","rArr":"⇒","dArr":"⇓","hArr":"⇔","forall":"∀","part":"∂","exist":"∃","empty":"∅","nabla":"∇","isin":"∈","notin":"∉","ni":"∋","prod":"∏","sum":"∑","minus":"−","lowast":"∗","radic":"√","prop":"∝","infin":"∞","ang":"∠","and":"∧","or":"∨","cap":"∩","cup":"∪","int":"∫","there4":"∴","sim":"∼","cong":"≅","asymp":"≈","ne":"≠","equiv":"≡","le":"≤","ge":"≥","sub":"⊂","sup":"⊃","nsub":"⊄","sube":"⊆","supe":"⊇","oplus":"⊕","otimes":"⊗","perp":"⊥","sdot":"⋅","lceil":"⌈","rceil":"⌉","lfloor":"⌊","rfloor":"⌋","lang":"〈","rang":"〉","loz":"◊","spades":"♠","clubs":"♣","hearts":"♥","diams":"♦", "zwnj":""}; | |
/* | |
& -all entities start with & | |
( | |
# -charcode entities also have a # | |
x? -hex charcodes | |
)? | |
[\w;] -now the entity (alphanumeric, separated by ;) | |
+? -capture em until there aint no more (don't get the trailing ;) | |
; -trailing ; | |
*/ | |
var entityRegex = /&(#x?)?[\w;]+?;/g; | |
var replaceEntities = function ( entities ) { | |
//remove the & and split into each separate entity | |
return entities.slice( 1 ).split( ';' ).map( decodeEntity ).join( '' ); | |
}; | |
var decodeEntity = function ( entity ) { | |
if ( !entity ) { | |
return ''; | |
} | |
//starts with a #, it's charcode | |
if ( entity[0] === '#' ) { | |
return decodeCharcodeEntity( entity ); | |
} | |
if ( !entities.hasOwnProperty(entity) ) { | |
//I hate this so. so. so much. it's just wrong. | |
return '&' + entity +';'; | |
} | |
return entities[ entity ]; | |
}; | |
var decodeCharcodeEntity = function ( entity ) { | |
//remove the # prefix | |
entity = entity.slice( 1 ); | |
var cc; | |
//hex entities | |
if ( entity[0] === 'x' ) { | |
cc = parseInt( entity.slice(1), 16 ); | |
} | |
//decimal entities | |
else { | |
cc = parseInt( entity, 10 ); | |
} | |
return String.fromCharCode( cc ); | |
}; | |
return function ( html ) { | |
return html.replace( entityRegex, replaceEntities ); | |
}; | |
}()); | |
//get the ball rolling | |
input.init(); | |
//}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment