view notifs.js @ 24:b80ab94da447

Add new modules files.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 01 Nov 2010 00:02:27 +0100
parents
children 023f767662d3
line wrap: on
line source

var xmpp = require('xmpp');
var storage = require('./storage');
var config = require('./configuration');
var conn;

function _(obj, color) {
	var str = require('sys').inspect(obj, false, null);
	if (color)
		console.log('\033['+c+';1m' + str + '\033[0m');
	else
		console.log(str);
};

exports.send = function(notifs, type, nodeID, a1, a2) {
	var ev = xmpp.stanza('event', {xmlns: 'http://jabber.org/protocol/pubsub#event'});

	if (type == 'affiliations') {
		ev.attr.xmlns = 'http://jabber.org/protocol/pubsub';

		var args = {};
		for (i in a1) {
			var attr = a1[i];
			if (i == 'affiliation')
				args.affiliation = attr;
			else if (i == 'jid')
				args.jid = attr;
		}
		var affiliations = xmpp.stanza('affiliations', {node: nodeID})
			.c('affiliation', args);
		ev.cnode(affiliations);
	} else if (type == 'collection') {
		var collection = xmpp.stanza('collection', {node: nodeID});
		if (a1 == 'associate')
			collection.cnode('associate', {node: nodeID});
		else
			collection.cnode('disassociate', {node: nodeID});
		ev.cnode(collection);
	} else if (type == 'configuration') {
		if (!config.enabled('config-node')) {
			_('Error #4', 41)
			return;
		}

		var configuration = xmpp.stanza('configuration', {node: nodeID});
		if (a1) {
			var x = forms.build('node_config', service_configuration.node_config, storage.getConfiguration(nodeID));
			if (x)
				configuration.cnode(x); //TODO: voir exemple 150
		}
		ev.cnode(configuration);
	} else if (type == 'delete') {
		var del = xmpp.stanza('delete', {node: nodeID});
		if (a1)
			del.c('redirect', {uri: a1});
		ev.cnode(del);
	} else if (type == 'items') {
		var items = xmpp.stanza(type, {node: nodeID});
		if (a2 == 'retract')
			for (var i in a1)
				items.s('retract', {id: i});
		else {
			for (var i in a1) {
				var item = a1[i];
				var args = {};
				if (i != '')
					args.id = i;
				if (item.node)
					args.node = item.node;
				if (item.publisher)
					args.publisher = item.publisher;
				var it = xmpp.stanza('item', args);
				if (item.content)
					it.cnode(item.content);
				items.cnode(it);
			}
		}
		ev.cnode(items);
	} else if (type == 'purge') {
		ev.c('purge', {node: nodeID});
	} else if (type == 'subscription') {
		if (!config.enabled('subscription-notifications'))
			return;

		var args = {node: nodeID};
		for (i in a1) {
			var attr = a1[i];
			if (i == 'subscription') {
				if (attr == 'none' || attr == 'pending' || attr == 'subscribed' || attr == 'unconfigured')
					args[i] = attr;
				else {
					_('Error #3', 41)
					return;
				}
			} else if (i == 'jid' || i == 'subid')
				args[i] = attr;
			else if (i == 'expiry')
				args[i] = attr.toString();
		}
		if (!args.jid || args.jid == '') {
			_('Error #2', 41)
			return;
		}
		var sub = xmpp.stanza('subscription', args);
		ev.cnode(sub);
	} else {
		_('Error #1', 41)
		return;
	}

	var subs;
	if (typeof notifs == 'string') {
		subs = {};
		subs[notifs] = storage.getSubscription(notifs, nodeID);
	} else
		subs = notifs;

	for (var i in subs) {
		var sub = subs[i];

		if (sub.options) {
			if (typeof sub.options['pubsub#deliver'] != 'undefined' && !sub.options['pubsub#deliver'])
				continue;

			if (typeof sub.options['pubsub#digest'] != 'undefined' && sub.options['pubsub#digest']) {
				if (!sub.digest)
					sub.digest = [];
				sub.digest.push(ev)

				if (sub.digestTimeout)
					continue;

				var freq;
				if (typeof sub.options['pubsub#digest_frequency'] == 'undefined')
					freq = 0;
				else
					freq = parseInt(sub.options['pubsub#digest_frequency']);

				if (freq == 0)
					freq = 24*60*60*1000;

				setTimeout(sendDigest, freq, notifs[i], nodeID);
				sub.digestTimeout = true;
				continue;
			}
		}

		var message = xmpp.message({to: i, from: config.jid, id: conn.getUniqueId()});
		message.cnode(ev);
		conn.send(message);
	}
}

exports.sendDigest = function(jid, nodeID) {
	var sub = storage.getSubscription(jid, nodeID);
	if (sub.digestTimeout)
		sub.digestTimeout = false;

	var message = xmpp.message({to: jid, from: config.jid, id: conn.getUniqueId()});
	for (var i in sub.digest)
		message.cnode(sub.digest[i]);
	conn.send(message);
}

exports.setConnection = function(c) {
	conn = c;
}