# HG changeset patch # User Emmanuel Gil Peyrot # Date 1288962105 -3600 # Node ID 62cbb1c49bc561bdd52d7589c31edd16d7870296 # Parent 8735fc6f6f531cc5a76e9a4bb43d8d974b179770 Fix publish; add owner metadata field; add HTTP module and Atom HTTP module. diff --git a/backends/directory.js b/backends/directory.js --- a/backends/directory.js +++ b/backends/directory.js @@ -117,7 +117,7 @@ backend.load = function(dir) { var file = files[i]; var nodeID = file.replace(/\.json$/, ''); - var data = fs.readFileSync(dir+'/'+file); + var data = fs.readFileSync(dir+'/'+file).toString(); var obj = eval('('+data+')'); backend.list[nodeID] = endParsing(obj); diff --git a/modules/http/mod_atom.js b/modules/http/mod_atom.js new file mode 100644 --- /dev/null +++ b/modules/http/mod_atom.js @@ -0,0 +1,55 @@ +var config = require('../../configuration'); +var storage = require('../../storage'); +var forms = require('../../forms'); +var errors = require('../../errors'); +var makeError = errors.makeError; +var NS = require('../../namespaces'); +var xmpp = require('xmpp'); + +exports.atom = { + url: /^\/atom/, + func: function (req, res) { + var url = require('url').parse(req.url); + nodeID = url.pathname.substr(url.pathname.indexOf('/', 1)+1); + + var children; + if (nodeID && nodeID != '') { + var md = storage.getMetadata(nodeID); + if (md['pubsub#type'] != NS.ATOM) + return false; + + var response = xmpp.stanza('feed', {xmlns: 'http://www.w3.org/2005/Atom'}); + res.writeHead(200, {'Content-Type': 'text/xml'}); + if (!storage.existsNode(nodeID)) + return false; + + if (md['pubsub#title']) + response.c('title').t(md['pubsub#title']).up(); + if (md['pubsub#description']) + response.c('subtitle').t(md['pubsub#description']).up(); + if (md['pubsub#creation_date']) + response.c('published').t(md['pubsub#creation_date']).up(); + + children = storage.getItems(nodeID); + if (typeof children == 'number') + return false; + + for (var i in children) + response.cnode(children[i].content).up(); + } else { + res.writeHead(200, {'Content-Type': 'text/xml'}); + var response = xmpp.stanza('ul', {xmlns: 'http://www.w3.org/1999/xhtml'}); + + children = storage.getChildren(); + if (typeof children == 'number') + return false; + + for (var i in children) + response.c('li').c('a', {href: i}).t(i).up().up(); + } + + res.end(response.toString()); + + return true; + }, +} diff --git a/modules/mod_http.js b/modules/mod_http.js new file mode 100644 --- /dev/null +++ b/modules/mod_http.js @@ -0,0 +1,37 @@ +var config = require('../configuration'); +var http = require('http'); + +var files = require('fs').readdirSync('modules/http'); +var regex = /^mod_.*\.js/; + +var sites = {}; + +for (var i in files) { + var file = files[i]; + if (!regex.test(file)) + continue; + + var module = require('./http/' + files[i]); + for (var j in module) { + if (typeof sites[j] != 'undefined') + console.log('WARNING: module '+j+' already loaded.'); + + sites[j] = module[j]; + } +} + +http.createServer(function (req, res) { + var sent = false; + for (var i in sites) { + var site = sites[i]; + if (!site.url.test(req.url)) + continue; + + sent = site.func(req, res); + return; + } + if (!sent) { + res.writeHead(404, {'Content-Type': 'text/plain'}); + res.end('Error 404'); + } +}).listen(8484); diff --git a/modules/mod_publish.js b/modules/mod_publish.js --- a/modules/mod_publish.js +++ b/modules/mod_publish.js @@ -1,10 +1,11 @@ var config = require('../configuration'); var storage = require('../storage'); +var notifs = require('../notifs'); var toBareJID = require('../util').toBareJID; var NS = require('../namespaces'); // SECTION 7.1: Publish an Item to a Node -exports.retrieveSub = { +exports.publish = { type: 'set', child: 'pubsub', ns: NS.PUBSUB, @@ -41,7 +42,7 @@ exports.retrieveSub = { return makeError(response, errors.pub.publish.bad_payload.n); var conf = storage.getConfiguration(nodeID); - var publishOptions = pubsub.getChild('publish-options'); + /*var publishOptions = pubsub.getChild('publish-options'); if (publishOptions && config.enabled('publish-options')) { var x = publishOptions.getChild('x', 'jabber:x:data'); if (!x || x.getAttribute('type') != 'submit') @@ -50,11 +51,11 @@ exports.retrieveSub = { var form = forms.parse(x, true); if (form.access_model != conf['pubsub#access_model'] && !autocreate) return makeError(response, errors.pub.configuration.precondition.n); - } + }*/ if (!config.enabled('persistent-items')) { - var notifs = storage.purgeNode(nodeID); - if (typeof notifs == 'number') + var notifications = storage.purgeNode(nodeID); + if (typeof notifications == 'number') return makeError(response, r); } @@ -90,7 +91,7 @@ exports.retrieveSub = { } // SECTION 7.2: Delete an Item from a Node -exports.retrieveSub = { +exports.retract = { type: 'set', child: 'pubsub', ns: NS.PUBSUB, diff --git a/namespaces.js b/namespaces.js --- a/namespaces.js +++ b/namespaces.js @@ -15,3 +15,5 @@ NS.DATAFORMS = 'jabber:x:data'; NS.DELAY = 'urn:xmpp:delay'; NS.VERSION = 'jabber:iq:version'; NS.COMMANDS = 'http://jabber.org/protocol/commands'; + +NS.ATOM = 'http://www.w3.org/2005/Atom'; diff --git a/storage.js b/storage.js --- a/storage.js +++ b/storage.js @@ -64,6 +64,8 @@ storage.getMetadata = function(nodeID) { md[i] = node.configuration[i]; else if (node.metadata[i]) md[i] = node.metadata[i]; + else if (i == 'pubsub#owner') + md[i] = node.owner; } return md; };