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.
This commit is contained in:
Alexander D Brown 2011-11-09 23:51:01 +00:00
parent cb6568d27d
commit 52cdddf3f7
3 changed files with 48 additions and 3 deletions

View File

@ -24,13 +24,13 @@ var webInterface = function(dbot) {
// Lists the quotes in a category // Lists the quotes in a category
var key = req.params.key.toLowerCase(); var key = req.params.key.toLowerCase();
if(dbot.db.quoteArrs.hasOwnProperty(key)) { 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 { } else {
res.render('error', { 'message': 'No quotes under that key.' }); res.render('error', { 'message': 'No quotes under that key.' });
} }
}); });
app.listen(443); app.listen(9443);
return { return {
'onDestroy': function() { 'onDestroy': function() {

View File

@ -122,3 +122,44 @@ Number.prototype.chanceIn = function(x, y) {
var num = Math.floor(Math.random() * (y + 1)) / x; var num = Math.floor(Math.random() * (y + 1)) / x;
return num == 1; 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;
}

View File

@ -1,3 +1,7 @@
div#quotelist div#quotelist
-each quote in quotes -each quote in quotes
-if(quote.match(locals.url_regex))
div.quote
a(href=quote)=quote
-else
div.quote #{quote} div.quote #{quote}