From 52cdddf3f79779e45ffdabfe7dffd341b49485ab Mon Sep 17 00:00:00 2001 From: Alexander D Brown Date: Wed, 9 Nov 2011 23:51:01 +0000 Subject: [PATCH] Added in URL support for the web interface. The Jade templating code in `views/quotes.jade` will now check against the Regular Expression which has been added to `snippets.js` and is delivered to the template by `modules/web.js`. The reguar expression used should be able to handle any URL and ensure no non-URL elements are made into URLs. It may be worth adding checking for 4XX error codes at a later date. This shouldn't affect any of the other modules; features have only been added, not modified. --- modules/web.js | 4 ++-- snippets.js | 41 +++++++++++++++++++++++++++++++++++++++++ views/quotes.jade | 6 +++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/modules/web.js b/modules/web.js index cee8df5..bc14d05 100644 --- a/modules/web.js +++ b/modules/web.js @@ -24,13 +24,13 @@ var webInterface = function(dbot) { // Lists the quotes in a category var key = req.params.key.toLowerCase(); if(dbot.db.quoteArrs.hasOwnProperty(key)) { - res.render('quotes', { 'quotes': dbot.db.quoteArrs[key]}); + res.render('quotes', { 'quotes': dbot.db.quoteArrs[key], locals: {url_regex: RegExp.prototype.url_regex()}}); } else { res.render('error', { 'message': 'No quotes under that key.' }); } }); - app.listen(443); + app.listen(9443); return { 'onDestroy': function() { diff --git a/snippets.js b/snippets.js index be41eb1..54c235b 100644 --- a/snippets.js +++ b/snippets.js @@ -122,3 +122,44 @@ Number.prototype.chanceIn = function(x, y) { var num = Math.floor(Math.random() * (y + 1)) / x; return num == 1; }; + +/*** Regex ***/ +RegExp.prototype.url_regex = function() { + var reg = new RegExp( + "^" + + // protocol identifier + "(?:(?:https?|ftp)://)" + + // user:pass authentication + "(?:\\S+(?::\\S*)?@)?" + + "(?:" + + // IP address exclusion + // private & local networks + "(?!10(?:\\.\\d{1,3}){3})" + + "(?!127(?:\\.\\d{1,3}){3})" + + "(?!169\\.254(?:\\.\\d{1,3}){2})" + + "(?!192\\.168(?:\\.\\d{1,3}){2})" + + "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" + + // IP address dotted notation octets + // excludes loopback network 0.0.0.0 + // excludes reserved space >= 224.0.0.0 + // excludes network & broacast addresses + // (first & last IP address of each class) + "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" + + "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" + + "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" + + "|" + + // host name + "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" + + // domain name + "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" + + // TLD identifier + "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" + + ")" + + // port number + "(?::\\d{2,5})?" + + // resource path + "(?:/[^\\s]*)?" + + "$", "i" + ); + return reg; +} diff --git a/views/quotes.jade b/views/quotes.jade index ce8a7a8..4299ba4 100644 --- a/views/quotes.jade +++ b/views/quotes.jade @@ -1,3 +1,7 @@ div#quotelist -each quote in quotes - div.quote #{quote} + -if(quote.match(locals.url_regex)) + div.quote + a(href=quote)=quote + -else + div.quote #{quote}