Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactored search and phrases and junk

  • Loading branch information...
commit 665445f120e5d9b115bda1310ee360e90ee94fab 1 parent 833cebd
@wookiehangover authored
View
90 hack-bot.coffee
@@ -1,31 +1,22 @@
-token = process.env.TOKEN
-
+# Module Requirements
+http = require('http')
Sandbox = require('sandbox')
Campfire = require('./lib/vendor/campfire').Campfire
-Google = require('./lib/vendor/google')
+
+User = require('./lib/models').user
Reminder = require('./lib/reminder')
Phrases = require('./lib/phrases')
-User = require('./lib/models').user
-http = require('http')
-
-sandbox = new Sandbox()
-google = new Google()
+Search = require('./lib/search')
-instance = new Campfire({
- ssl: true
- token: token
- account: 'quickleft'
-})
+# Module Instances
+sandbox = new Sandbox()
+instance = new Campfire { ssl: true, token: process.env.TOKEN, account: 'quickleft' }
logger = ( d ) ->
console.log "#{d.message.created_at}: #{d.message.body}"
-#bot-room
bot_room = 401915
-
-#everyone
-#room_id = 265458
-
+#everyone 265458
room_id = process.env.ROOM || bot_room
quack = ( room ) ->
@@ -33,82 +24,33 @@ quack = ( room ) ->
bot_id = doc.user_id
room.join ->
-
console.log "Joining #{room.name}"
room.speak("hai guys", logger) unless process.env.SILENT
room.listen ( msg ) ->
-
+ # ignore it if I said it
return if msg.user_id is parseInt(bot_id)
- console.log(msg.user_id, bot_id)
-
- Reminder.poller( msg, room )
-
- if /^tell (\w+\s\w+|\@\w+)\s(.+)$/.test( msg.body )
- Reminder.save msg, ->
- room.speak "sure", logger
-
- Phrases.listen( msg, room )
-
- setter = /^([^=]+)\s\=\s(.+)$/
-
- if /^destroy (.+)$/.test( msg.body )
- match = /^destroy (.+)$/.exec(msg.body)[1]
- Phrases.remove match, ->
- room.speak "#{match} removed"
-
- if setter.test( msg.body )
- params = setter.exec( msg.body )
- Phrases.store params[1], params[2], ->
- room.speak "#{params[1]} saved", logger
-
- if /^show me the money$/.test( msg.body )
- Phrases.all( room )
+ # eval JS in the sandbox
if /^eval (.+)/.test( msg.body )
sandbox.run /^eval (.+)/.exec(msg.body)[1], ( output ) ->
output = output.result.replace( /\n/g, ' ' )
room.speak output, logger
- g_exp = /^g ([^#@]+)?$/
-
- if g_exp.test( msg.body )
-
- google.search msg.body.match(g_exp)[1], ( results ) ->
- if results.length
- room.speak "#{results[0].titleNoFormatting} - #{results[0].unescapedUrl}", logger
- else
- room.speak "Sorry, no results for", logger
- mdc_exp = /^mdc ([^#@]+)(?:\s*#([1-9]))?$/
-
- if mdc_exp.test( msg.body )
- google.search msg.body.match(mdc_exp)[1] + ' site:developer.mozilla.org', ( results ) ->
- if results.length
- room.speak "#{results[0].titleNoFormatting} - #{results[0].unescapedUrl}", logger
- else
- room.speak "Sorry, no results for", logger
-
- yt_exp = /^[\/.`?]?yt ([^#@]+)(?:\s*#([1-9]))?$/
-
- if yt_exp.test( msg.body )
- google.search msg.body.match(yt_exp)[1] + ' site:youtube.com', ( results ) ->
- if results.length
- room.speak "#{results[0].unescapedUrl}", logger
- else
- room.speak "Sorry, no results for", logger
-
- return
+ Reminder.listen( msg, room )
+ Phrases.listen( msg, room )
+ Search.listen( msg, room )
+ # leave the room on exit
process.on 'SIGINT', ->
room.leave ->
console.log('\nGood Luck, Star Fox')
process.exit()
-
instance.room room_id, quack
-
+# heroku wants the app to bind to a port, so lets do that
server = http.createServer ( req, res ) ->
res.writeHead 200, { 'Content-Type': 'text/plain' }
res.end 'quack bot <3s you\n'
View
70 hack-bot.js
@@ -1,17 +1,15 @@
-var Campfire, Google, Phrases, Reminder, Sandbox, User, bot_room, google, http, instance, logger, port, quack, room_id, sandbox, server, token;
-token = process.env.TOKEN;
+var Campfire, Phrases, Reminder, Sandbox, Search, User, bot_room, http, instance, logger, port, quack, room_id, sandbox, server;
+http = require('http');
Sandbox = require('sandbox');
Campfire = require('./lib/vendor/campfire').Campfire;
-Google = require('./lib/vendor/google');
+User = require('./lib/models').user;
Reminder = require('./lib/reminder');
Phrases = require('./lib/phrases');
-User = require('./lib/models').user;
-http = require('http');
+Search = require('./lib/search');
sandbox = new Sandbox();
-google = new Google();
instance = new Campfire({
ssl: true,
- token: token,
+ token: process.env.TOKEN,
account: 'quickleft'
});
logger = function(d) {
@@ -31,70 +29,18 @@ quack = function(room) {
room.speak("hai guys", logger);
}
return room.listen(function(msg) {
- var g_exp, match, mdc_exp, params, setter, yt_exp;
if (msg.user_id === parseInt(bot_id)) {
return;
}
- console.log(msg.user_id, bot_id);
- Reminder.poller(msg, room);
- if (/^tell (\w+\s\w+|\@\w+)\s(.+)$/.test(msg.body)) {
- Reminder.save(msg, function() {
- return room.speak("sure", logger);
- });
- }
- Phrases.listen(msg, room);
- setter = /^([^=]+)\s\=\s(.+)$/;
- if (/^destroy (.+)$/.test(msg.body)) {
- match = /^destroy (.+)$/.exec(msg.body)[1];
- Phrases.remove(match, function() {
- return room.speak("" + match + " removed");
- });
- }
- if (setter.test(msg.body)) {
- params = setter.exec(msg.body);
- Phrases.store(params[1], params[2], function() {
- return room.speak("" + params[1] + " saved", logger);
- });
- }
- if (/^show me the money$/.test(msg.body)) {
- Phrases.all(room);
- }
if (/^eval (.+)/.test(msg.body)) {
sandbox.run(/^eval (.+)/.exec(msg.body)[1], function(output) {
output = output.result.replace(/\n/g, ' ');
return room.speak(output, logger);
});
}
- g_exp = /^g ([^#@]+)?$/;
- if (g_exp.test(msg.body)) {
- google.search(msg.body.match(g_exp)[1], function(results) {
- if (results.length) {
- return room.speak("" + results[0].titleNoFormatting + " - " + results[0].unescapedUrl, logger);
- } else {
- return room.speak("Sorry, no results for", logger);
- }
- });
- }
- mdc_exp = /^mdc ([^#@]+)(?:\s*#([1-9]))?$/;
- if (mdc_exp.test(msg.body)) {
- google.search(msg.body.match(mdc_exp)[1] + ' site:developer.mozilla.org', function(results) {
- if (results.length) {
- return room.speak("" + results[0].titleNoFormatting + " - " + results[0].unescapedUrl, logger);
- } else {
- return room.speak("Sorry, no results for", logger);
- }
- });
- }
- yt_exp = /^[\/.`?]?yt ([^#@]+)(?:\s*#([1-9]))?$/;
- if (yt_exp.test(msg.body)) {
- google.search(msg.body.match(yt_exp)[1] + ' site:youtube.com', function(results) {
- if (results.length) {
- return room.speak("" + results[0].unescapedUrl, logger);
- } else {
- return room.speak("Sorry, no results for", logger);
- }
- });
- }
+ Reminder.listen(msg, room);
+ Phrases.listen(msg, room);
+ return Search.listen(msg, room);
});
});
return process.on('SIGINT', function() {
View
23 lib/phrases.coffee
@@ -27,7 +27,6 @@ api =
room.paste blob, api.logger
-
store: ( match, msg, callback ) ->
p = new Phrase({ regex: match, msg: msg })
@@ -45,9 +44,28 @@ api =
phrases.push { regex: match, msg: msg ? false, callback: callback ? false }
- phrases: (-> phrases)()
+ phrases: phrases
listen: ( message, room ) ->
+ # learn and destroy phrases
+ setter = /^([^=]+)\s\=\s(.+)$/
+ destroyer = /^destroy (.+)$/
+
+ if setter.test( message.body )
+ params = setter.exec( message.body )
+ return api.store params[1], params[2], ->
+ room.speak "#{params[1]} saved", api.logger
+
+ if destroyer.test( message.body )
+ match = destroyer.exec( message.body)[1]
+ return api.remove match, ->
+ room.speak "#{match} removed", api.logger
+
+ # show all the memorized phrases
+ if /^show me the money$/.test( message.body )
+ return api.all( room )
+
+ # loop through the static phrases
_.each api.phrases, ( phrase ) ->
return if phrase.regex.test( message.body ) is false
@@ -59,6 +77,7 @@ api =
phrase.callback() if _.isFunction( phrase.callback )
+ # and then the user entered phrases
Phrase.find {}, ( err, doc ) ->
_.each doc, ( phrase ) ->
regex = new RegExp(phrase.regex)
View
22 lib/phrases.js
@@ -80,10 +80,26 @@ api = {
callback: callback != null ? callback : false
});
},
- phrases: (function() {
- return phrases;
- })(),
+ phrases: phrases,
listen: function(message, room) {
+ var destroyer, match, params, setter;
+ setter = /^([^=]+)\s\=\s(.+)$/;
+ destroyer = /^destroy (.+)$/;
+ if (setter.test(message.body)) {
+ params = setter.exec(message.body);
+ return api.store(params[1], params[2], function() {
+ return room.speak("" + params[1] + " saved", api.logger);
+ });
+ }
+ if (destroyer.test(message.body)) {
+ match = destroyer.exec(message.body)[1];
+ return api.remove(match, function() {
+ return room.speak("" + match + " removed", api.logger);
+ });
+ }
+ if (/^show me the money$/.test(message.body)) {
+ return api.all(room);
+ }
_.each(api.phrases, function(phrase) {
if (phrase.regex.test(message.body) === false) {
return;
View
9 lib/reminder.coffee
@@ -40,14 +40,15 @@ api =
catch error
callback.call @, error if _.isFunction callback
- poller: ( msg, room ) ->
+ listen: ( msg, room ) ->
- Note.find {}, ( err, doc ) ->
+ if /^tell (\w+\s\w+|\@\w+)\s(.+)$/.test( msg.body )
+ return api.save msg, ->
+ room.speak "sure", logger
+ Note.find {}, ( err, doc ) ->
_.each doc, ( note ) ->
-
if msg.user_id is parseInt(note.target_id, 10)
-
room.speak "@#{note.target_name.split(' ')[0]}, #{note.sender_name.split(' ')[0]} says '#{note.msg}'", (d)->
note.remove()
View
7 lib/reminder.js
@@ -49,7 +49,12 @@ api = {
});
});
},
- poller: function(msg, room) {
+ listen: function(msg, room) {
+ if (/^tell (\w+\s\w+|\@\w+)\s(.+)$/.test(msg.body)) {
+ return api.save(msg, function() {
+ return room.speak("sure", logger);
+ });
+ }
return Note.find({}, function(err, doc) {
return _.each(doc, function(note) {
if (msg.user_id === parseInt(note.target_id, 10)) {
View
49 lib/search.coffee
@@ -0,0 +1,49 @@
+Google = require('./vendor/google')
+unescape = require('./vendor/unescape')
+
+google = new Google()
+
+logger = ( d ) ->
+ try
+ console.log "#{d.message.created_at}: #{d.message.body}"
+ catch e
+ console.log(d)
+
+module.exports =
+ listen: ( msg, room ) ->
+ g_exp = /^g ([^#@]+)?$/
+ mdc_exp = /^mdc ([^#@]+)(?:\s*#([1-9]))?$/
+ yt_exp = /^[\/.`?]?yt ([^#@]+)(?:\s*#([1-9]))?$/
+ jq_exp = /^jq ([^#@]+)(?:\s*#([1-9]))?$/
+
+
+ if g_exp.test( msg.body )
+
+ google.search msg.body.match(g_exp)[1], ( results ) ->
+ if results.length
+ room.speak "#{results[0].titleNoFormatting} - #{results[0].unescapedUrl}", logger
+ else
+ room.speak "Sorry, no results for", logger
+
+ if jq_exp.test( msg.body )
+ google.search msg.body.match(jq_exp)[1] + ' site:api.jquery.com', ( results ) ->
+ if results.length
+ room.speak "#{results[0].titleNoFormatting.replace('', '-')} - #{results[0].unescapedUrl}", logger
+ else
+ room.speak "Sorry, no results for", logger
+
+
+ if mdc_exp.test( msg.body )
+ google.search msg.body.match(mdc_exp)[1] + ' site:developer.mozilla.org', ( results ) ->
+ if results.length
+ room.speak "#{results[0].titleNoFormatting} - #{results[0].unescapedUrl}", logger
+ else
+ room.speak "Sorry, no results for", logger
+
+ if yt_exp.test( msg.body )
+ google.search msg.body.match(yt_exp)[1] + ' site:youtube.com', ( results ) ->
+ if results.length
+ room.speak "#{results[0].unescapedUrl}", logger
+ else
+ room.speak "Sorry, no results for", logger
+
View
56 lib/search.js
@@ -0,0 +1,56 @@
+var Google, google, logger, unescape;
+Google = require('./vendor/google');
+unescape = require('./vendor/unescape');
+google = new Google();
+logger = function(d) {
+ try {
+ return console.log("" + d.message.created_at + ": " + d.message.body);
+ } catch (e) {
+ return console.log(d);
+ }
+};
+module.exports = {
+ listen: function(msg, room) {
+ var g_exp, jq_exp, mdc_exp, yt_exp;
+ g_exp = /^g ([^#@]+)?$/;
+ mdc_exp = /^mdc ([^#@]+)(?:\s*#([1-9]))?$/;
+ yt_exp = /^[\/.`?]?yt ([^#@]+)(?:\s*#([1-9]))?$/;
+ jq_exp = /^jq ([^#@]+)(?:\s*#([1-9]))?$/;
+ if (g_exp.test(msg.body)) {
+ google.search(msg.body.match(g_exp)[1], function(results) {
+ if (results.length) {
+ return room.speak("" + results[0].titleNoFormatting + " - " + results[0].unescapedUrl, logger);
+ } else {
+ return room.speak("Sorry, no results for", logger);
+ }
+ });
+ }
+ if (jq_exp.test(msg.body)) {
+ google.search(msg.body.match(jq_exp)[1] + ' site:api.jquery.com', function(results) {
+ if (results.length) {
+ return room.speak("" + (results[0].titleNoFormatting.replace('', '-')) + " - " + results[0].unescapedUrl, logger);
+ } else {
+ return room.speak("Sorry, no results for", logger);
+ }
+ });
+ }
+ if (mdc_exp.test(msg.body)) {
+ google.search(msg.body.match(mdc_exp)[1] + ' site:developer.mozilla.org', function(results) {
+ if (results.length) {
+ return room.speak("" + results[0].titleNoFormatting + " - " + results[0].unescapedUrl, logger);
+ } else {
+ return room.speak("Sorry, no results for", logger);
+ }
+ });
+ }
+ if (yt_exp.test(msg.body)) {
+ return google.search(msg.body.match(yt_exp)[1] + ' site:youtube.com', function(results) {
+ if (results.length) {
+ return room.speak("" + results[0].unescapedUrl, logger);
+ } else {
+ return room.speak("Sorry, no results for", logger);
+ }
+ });
+ }
+ }
+};
View
266 lib/vendor/unescape.js
@@ -0,0 +1,266 @@
+var entities =
+ { '&nbsp;': ' '
+ , '&iexcl;': '¡'
+ , '&cent;': '¢'
+ , '&pound;': '£'
+ , '&curren;': '¤'
+ , '&yen;': '¥'
+ , '&brvbar;': '¦'
+ , '&sect;': '§'
+ , '&uml;': '¨'
+ , '&copy;': '©'
+ , '&ordf;': 'ª'
+ , '&laquo;': '«'
+ , '&not;': '¬'
+ , '&shy;': ''
+ , '&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;': 'ÿ'
+ , '&fnof;': 'ƒ'
+ , '&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;': 'ϖ'
+ , '&bull;': ''
+ , '&hellip;': ''
+ , '&prime;': ''
+ , '&Prime;': ''
+ , '&oline;': ''
+ , '&frasl;': ''
+ , '&weierp;': ''
+ , '&image;': ''
+ , '&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;': ''
+ , '&quot;': '"'
+ , '&amp;': '&'
+ , '&lt;': '<'
+ , '&gt;': '>'
+ , '&OElig;': 'Œ'
+ , '&oelig;': 'œ'
+ , '&Scaron;': 'Š'
+ , '&scaron;': 'š'
+ , '&Yuml;': 'Ÿ'
+ , '&circ;': 'ˆ'
+ , '&tilde;': '˜'
+ , '&ensp;': ''
+ , '&emsp;': ''
+ , '&thinsp;': ''
+ , '&zwnj;': ''
+ , '&zwj;': ''
+ , '&lrm;': ''
+ , '&rlm;': ''
+ , '&ndash;': ''
+ , '&mdash;': ''
+ , '&lsquo;': ''
+ , '&rsquo;': ''
+ , '&sbquo;': ''
+ , '&ldquo;': ''
+ , '&rdquo;': ''
+ , '&bdquo;': ''
+ , '&dagger;': ''
+ , '&Dagger;': ''
+ , '&permil;': ''
+ , '&lsaquo;': ''
+ , '&rsaquo;': ''
+ , '&euro;': ''
+ }
+
+function unescapeEntity( input ) {
+ if ( input.charAt(1) === '#' )
+ return String.fromCharCode( parseInt( input.substr(2), 10 ) )
+ else if ( entities.hasOwnProperty( input ) )
+ return entities[ input ]
+}
+
+module.exports = function unescapeAll( input ) {
+ var entityRe = /&(#?)(\d{1,5}|\w{1,8});/gm
+ return input.replace( entityRe, unescapeEntity )
+}
View
30 spec/PhrasesSpec.coffee
@@ -8,22 +8,19 @@ room =
logger( { message: msg } ) if _.isFunction( logger )
-describe 'Phrases', ->
+describe 'Static Phrases', ->
it 'should match and speak static phrases', ->
spyOn room, 'speak'
- # phrases are currently hard coded, so let's test o`ne that's in ther
- # TODO change once db support added
+ # phrases are currently hard coded, so let's test one that's in ther
Phrases.listen( { body: "wet" }, room )
-
expect( room.speak ).toHaveBeenCalled()
- it 'should not match non-(static) phrases', ->
+ it 'should not match non-phrases', ->
spyOn room, 'speak'
Phrases.listen( { body: "ertyuiuytrertyui" }, room )
-
expect( room.speak ).not.toHaveBeenCalled()
it 'should match complex (static) phrases', ->
@@ -33,7 +30,6 @@ describe 'Phrases', ->
# match to a known 2 part phrase
Phrases.listen( { body: 'deal' }, room )
-
expect( room.speak ).toHaveBeenCalled()
expect( lock ).toEqual( 2 )
@@ -52,19 +48,18 @@ describe 'Phrases', ->
back: -> return
spyOn( holla, 'back' )
- spyOn( room, 'speak' )
Phrases.register( /holla/, 'back', holla.back )
Phrases.listen( { body: 'holla' }, room )
expect( holla.back ).toHaveBeenCalled()
+describe 'Persistant Phrases', ->
+
it 'should store phrases persistently', ->
Phrases.store 'testing', 'this is just a test', ->
-
models.phrase.findOne { msg: 'this is just a test' }, ( err, doc ) ->
-
expect( doc.msg ).toEqual( 'this is just a test' )
-
+ # clean up and advance runner
doc.remove()
jasmine.asyncSpecDone()
@@ -72,17 +67,22 @@ describe 'Phrases', ->
it 'should remove phrases', ->
Phrases.store 'testing', 'this is just a test', ->
-
Phrases.remove 'testing', ->
-
models.phrase.find { regex: 'testing' }, ( err, doc ) ->
expect( doc ).toEqual( [] )
jasmine.asyncSpecDone()
- jasmine.asyncSpecWait()
-
+ jasmine.asyncSpecWait()
+ it 'should add phrases via the listener', ->
+ spyOn( Phrases, 'store' )
+ Phrases.listen { body: 'testing1234 = testing stuff' }, room
+ expect( Phrases.store ).toHaveBeenCalled()
+ it 'should remove phrases via the listener', ->
+ spyOn( Phrases, 'remove' )
+ Phrases.listen { body: 'destroy testing1234' }, room
+ expect( Phrases.remove ).toHaveBeenCalled()
View
25 spec/PhrasesSpec.js
@@ -11,7 +11,7 @@ room = {
}
}
};
-describe('Phrases', function() {
+describe('Static Phrases', function() {
it('should match and speak static phrases', function() {
spyOn(room, 'speak');
Phrases.listen({
@@ -19,7 +19,7 @@ describe('Phrases', function() {
}, room);
return expect(room.speak).toHaveBeenCalled();
});
- it('should not match non-(static) phrases', function() {
+ it('should not match non-phrases', function() {
spyOn(room, 'speak');
Phrases.listen({
body: "ertyuiuytrertyui"
@@ -49,19 +49,20 @@ describe('Phrases', function() {
}, room);
return expect(room.speak).toHaveBeenCalled();
});
- it('should call your registerd callbacks', function() {
+ return it('should call your registerd callbacks', function() {
var holla;
holla = {
back: function() {}
};
spyOn(holla, 'back');
- spyOn(room, 'speak');
Phrases.register(/holla/, 'back', holla.back);
Phrases.listen({
body: 'holla'
}, room);
return expect(holla.back).toHaveBeenCalled();
});
+});
+describe('Persistant Phrases', function() {
it('should store phrases persistently', function() {
Phrases.store('testing', 'this is just a test', function() {
return models.phrase.findOne({
@@ -74,7 +75,7 @@ describe('Phrases', function() {
});
return jasmine.asyncSpecWait();
});
- return it('should remove phrases', function() {
+ it('should remove phrases', function() {
Phrases.store('testing', 'this is just a test', function() {
return Phrases.remove('testing', function() {
return models.phrase.find({
@@ -87,4 +88,18 @@ describe('Phrases', function() {
});
return jasmine.asyncSpecWait();
});
+ it('should add phrases via the listener', function() {
+ spyOn(Phrases, 'store');
+ Phrases.listen({
+ body: 'testing1234 = testing stuff'
+ }, room);
+ return expect(Phrases.store).toHaveBeenCalled();
+ });
+ return it('should remove phrases via the listener', function() {
+ spyOn(Phrases, 'remove');
+ Phrases.listen({
+ body: 'destroy testing1234'
+ }, room);
+ return expect(Phrases.remove).toHaveBeenCalled();
+ });
});
View
40 spec/ReminderSpec.coffee
@@ -1,5 +1,4 @@
-Reminder = require('../lib/reminder')
-
+Reminder = require('../lib/reminder')
Note = require('../lib/models').note
User = require('../lib/models').user
@@ -8,25 +7,50 @@ test_user = new User
user_id: 1
email: "test@quickleft.com"
+# mocking the campfire room api
+room =
+ speak: ( msg, logger ) ->
+ logger( { message: msg } ) if _.isFunction( logger )
+
describe 'Reminders', ->
it 'should save a message with full name', ->
+ msg =
+ body: "tell Test Dude whatever man"
+ user_id: 794174
test_user.save ( error, user ) ->
-
- msg =
- body: "tell Test Dude whatever man"
- user_id: 794174
-
Reminder.save msg, ( e ) ->
Note.findOne { target_name: "Test Dude" }, ( err, doc ) ->
expect( doc.target_name ).toEqual( "Test Dude" )
expect( doc.msg ).toEqual( "whatever man" )
- test_user.remove()
doc.remove()
+ User.remove { name: /Test/ }
jasmine.asyncSpecDone()
jasmine.asyncSpecWait()
+ it 'should save via the listener', ->
+ spyOn( Reminder, 'save')
+
+ test_user.save ( error, user ) ->
+ Reminder.listen( { body: 'tell @test that his shits realy fly' }, room )
+ expect( Reminder.save ).toHaveBeenCalled()
+
+ User.remove { name: /Test/ }
+
+ #it 'should reply to a saved message via the listener', ->
+ #spyOn( room, 'speak' )
+
+ #test_user.save ( error, user ) ->
+ #Reminder.listen( { body: 'woot', user_id: 1 }, room )
+
+ #expect( room.speak ).toHaveBeenCalled()
+ #User.remove { name: /Test/ }
+ #jasmine.asyncSpecDone()
+
+ #jasmine.asyncSpecWait()
+
+
View
39 spec/ReminderSpec.js
@@ -1,4 +1,4 @@
-var Note, Reminder, User, test_user;
+var Note, Reminder, User, room, test_user;
Reminder = require('../lib/reminder');
Note = require('../lib/models').note;
User = require('../lib/models').user;
@@ -7,26 +7,49 @@ test_user = new User({
user_id: 1,
email: "test@quickleft.com"
});
+room = {
+ speak: function(msg, logger) {
+ if (_.isFunction(logger)) {
+ return logger({
+ message: msg
+ });
+ }
+ }
+};
describe('Reminders', function() {
- return it('should save a message with full name', function() {
+ it('should save a message with full name', function() {
+ var msg;
+ msg = {
+ body: "tell Test Dude whatever man",
+ user_id: 794174
+ };
test_user.save(function(error, user) {
- var msg;
- msg = {
- body: "tell Test Dude whatever man",
- user_id: 794174
- };
return Reminder.save(msg, function(e) {
return Note.findOne({
target_name: "Test Dude"
}, function(err, doc) {
expect(doc.target_name).toEqual("Test Dude");
expect(doc.msg).toEqual("whatever man");
- test_user.remove();
doc.remove();
+ User.remove({
+ name: /Test/
+ });
return jasmine.asyncSpecDone();
});
});
});
return jasmine.asyncSpecWait();
});
+ return it('should save via the listener', function() {
+ spyOn(Reminder, 'save');
+ return test_user.save(function(error, user) {
+ Reminder.listen({
+ body: 'tell @test that his shits realy fly'
+ }, room);
+ expect(Reminder.save).toHaveBeenCalled();
+ return User.remove({
+ name: /Test/
+ });
+ });
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.