changeset 26:f2e9365bc8ec

Can use differents backends for node storage. Writing directory-based backend in top of file-based backend.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 01 Nov 2010 02:39:58 +0100
parents c774f2ffb271
children a36a514e8be8
files configuration.js storage.js
diffstat 2 files changed, 21 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/configuration.js
+++ b/configuration.js
@@ -25,7 +25,7 @@ config.superOwner = ['you@example.com'];
 config.version = '0.1';
 config.os = 'GNU/Linux';
 config.allowCreateNode = [/@example\.com$/]; // value is an array of RegExp JIDs. If only super-owner should be able to create nodes, use [].
-
+config.backend = 'directory'; // Put backends in “backends” directory.
 config.access = 'open'; // values include open, whitelist (by members) and authorize (owners of the node receive notification). presence and roster aren’t implemented yet.
 
 config.activated = [
--- a/storage.js
+++ b/storage.js
@@ -17,6 +17,7 @@
  *  along with PSĜS.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+var fs = require('fs');
 var sha1hex = require('sha1').hex;
 require('./iso8601');
 var errors = require('./errors');
@@ -30,17 +31,18 @@ var nodes = require('./nodes');
 var Node = nodes.Node;
 var Item = nodes.Item;
 
-var fs = require('fs');
-
-var list;
+var storage = exports;
 
-var storage = exports;
+var backend = require('./backends/' + config.backend);
+storage.load = backend.load;
+storage.save = backend.save;
+
 storage.createNode = function(nodeID, params) {
-	for (var i in list)
+	for (var i in backend.list)
 		if (i == nodeID)
 			return errors.owner.create.nodeid_already_exists.n;
 
-	list[nodeID] = new Node(params);
+	backend.list[nodeID] = new Node(params);
 	storage.save();
 	return errors.success;
 };
@@ -119,7 +121,7 @@ storage.getChildren = function(node) {
 	if (node)
 		n = storage.getNode(node).items;
 	else
-		n = list;
+		n = backend.list;
 
 	for (var i in n) {
 		var type;
@@ -133,13 +135,13 @@ storage.getChildren = function(node) {
 };
 
 storage.getNode = function(nodeID) {
-	if (list[nodeID])
-		return list[nodeID];
+	if (backend.list[nodeID])
+		return backend.list[nodeID];
 	return errors.node_does_not_exist.n;
 };
 
 storage.existsNode = function(nodeID) {
-	if (list[nodeID])
+	if (backend.list[nodeID])
 		return true;
 	return false;
 };
@@ -169,7 +171,7 @@ storage.deleteNodeWithRedirect = functio
 	if (typeof del == 'number')
 		return del;
 
-	list[nodeID] = uri;
+	backend.list[nodeID] = uri;
 	storage.save();
 	return errors.success;
 };
@@ -191,7 +193,7 @@ storage.deleteNode = function(nodeID) {
 		for (var i in node.publishOnly)
 			notifs[node.publishOnly[i]] = {};
 
-	delete list[nodeID];
+	delete backend.list[nodeID];
 	storage.save();
 	return notifs;
 };
@@ -305,10 +307,10 @@ storage.getSubscription = function(jid, 
 			if (toBareJID(sub) == jid)
 				return node.subscribers[sub]
 	} else {
-		for (var node in list) {
-			for (var sub in list[node].subscribers) {
+		for (var node in backend.list) {
+			for (var sub in backend.list[node].subscribers) {
 				if (toBareJID(sub) == jid)
-					subs[node] = list[node].subscribers[sub];
+					subs[node] = backend.list[node].subscribers[sub];
 			}
 		}
 	}
@@ -413,8 +415,8 @@ storage.getAffiliation = function(jid, n
 
 storage.getAffiliationsFromJID = function(jid) {
 	var affils = {};
-	for (var nodeID in list) {
-		var node = list[nodeID];
+	for (var nodeID in backend.list) {
+		var node = backend.list[nodeID];
 		for (var affil in config.owner)
 			if (typeof affil == 'string' && config.owner[affil] == jid) {
 				affils[nodeID] = 'super-owner';
@@ -488,72 +490,6 @@ storage.getAffiliationsFromNodeID = func
 	return affils;
 };
 
-storage.save = function(file) {
-	function sanitize(o) {
-		var n = {};
-		for (var i in o) {
-			if (i == 'content' || o[i].setISO8601)
-				n[i] = o[i].toString();
-			else if (o[i] instanceof Array)
-				n[i] = o[i];
-			else if (typeof o[i] == 'object')
-				n[i] = sanitize(o[i]);
-			else if (typeof o[i] == 'function')
-				;
-			else
-				n[i] = o[i];
-		}
-		return n;
-	}
-
-	var data = sanitize(list);
-
-	if (!file)
-		file = 'save.json';
-
-	fs.writeFile(file, require('sys').inspect(data, null, null));
-}
-
-storage.load = function(file) {
-	var xmpp = require('xmpp');
-	function parseStanza(path, content) {
-		var stanza = null;
-		var stream = new xmpp.Stream({
-			stanza: function (stanza) {
-				path[content] = stanza;
-			}
-		});
-		stream.opened = true;
-		stream.data(path[content]);
-	}
-
-	function endParsing(o) {
-		var regexp = /\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ/;
-		for (var i in o) {
-			if (typeof o[i] == 'string' && i == 'content')
-				parseStanza(o, i);
-			else if (typeof o[i] == 'string' && regexp(o[i])) {
-				var today = new Date();
-				today.setISO8601(o[i]);
-				o[i] = today;
-			} else if (typeof o[i] == 'object')
-				endParsing(o[i]);
-		}
-		return o;
-	}
-
-	if (!file)
-		file = 'save.json';
-
-	var data = fs.readFileSync(file);
-	var obj = eval('('+data+')');
-	list = endParsing(obj);
-
-	for (var i in list)
-		for (var j in Node.prototype)
-			list[i][j] = Node.prototype[j];
-}
-
 storage.debug = function() {
-	console.log('\033[1;33m' + require('sys').inspect(list, null, null) + '\033[0m');
+	console.log('\033[1;33m' + require('sys').inspect(backend.list, null, null) + '\033[0m');
 };