comparison 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
comparison
equal deleted inserted replaced
23:5fc4ee90c1bc 24:b80ab94da447
1 var xmpp = require('xmpp');
2 var storage = require('./storage');
3 var config = require('./configuration');
4 var conn;
5
6 function _(obj, color) {
7 var str = require('sys').inspect(obj, false, null);
8 if (color)
9 console.log('\033['+c+';1m' + str + '\033[0m');
10 else
11 console.log(str);
12 };
13
14 exports.send = function(notifs, type, nodeID, a1, a2) {
15 var ev = xmpp.stanza('event', {xmlns: 'http://jabber.org/protocol/pubsub#event'});
16
17 if (type == 'affiliations') {
18 ev.attr.xmlns = 'http://jabber.org/protocol/pubsub';
19
20 var args = {};
21 for (i in a1) {
22 var attr = a1[i];
23 if (i == 'affiliation')
24 args.affiliation = attr;
25 else if (i == 'jid')
26 args.jid = attr;
27 }
28 var affiliations = xmpp.stanza('affiliations', {node: nodeID})
29 .c('affiliation', args);
30 ev.cnode(affiliations);
31 } else if (type == 'collection') {
32 var collection = xmpp.stanza('collection', {node: nodeID});
33 if (a1 == 'associate')
34 collection.cnode('associate', {node: nodeID});
35 else
36 collection.cnode('disassociate', {node: nodeID});
37 ev.cnode(collection);
38 } else if (type == 'configuration') {
39 if (!config.enabled('config-node')) {
40 _('Error #4', 41)
41 return;
42 }
43
44 var configuration = xmpp.stanza('configuration', {node: nodeID});
45 if (a1) {
46 var x = forms.build('node_config', service_configuration.node_config, storage.getConfiguration(nodeID));
47 if (x)
48 configuration.cnode(x); //TODO: voir exemple 150
49 }
50 ev.cnode(configuration);
51 } else if (type == 'delete') {
52 var del = xmpp.stanza('delete', {node: nodeID});
53 if (a1)
54 del.c('redirect', {uri: a1});
55 ev.cnode(del);
56 } else if (type == 'items') {
57 var items = xmpp.stanza(type, {node: nodeID});
58 if (a2 == 'retract')
59 for (var i in a1)
60 items.s('retract', {id: i});
61 else {
62 for (var i in a1) {
63 var item = a1[i];
64 var args = {};
65 if (i != '')
66 args.id = i;
67 if (item.node)
68 args.node = item.node;
69 if (item.publisher)
70 args.publisher = item.publisher;
71 var it = xmpp.stanza('item', args);
72 if (item.content)
73 it.cnode(item.content);
74 items.cnode(it);
75 }
76 }
77 ev.cnode(items);
78 } else if (type == 'purge') {
79 ev.c('purge', {node: nodeID});
80 } else if (type == 'subscription') {
81 if (!config.enabled('subscription-notifications'))
82 return;
83
84 var args = {node: nodeID};
85 for (i in a1) {
86 var attr = a1[i];
87 if (i == 'subscription') {
88 if (attr == 'none' || attr == 'pending' || attr == 'subscribed' || attr == 'unconfigured')
89 args[i] = attr;
90 else {
91 _('Error #3', 41)
92 return;
93 }
94 } else if (i == 'jid' || i == 'subid')
95 args[i] = attr;
96 else if (i == 'expiry')
97 args[i] = attr.toString();
98 }
99 if (!args.jid || args.jid == '') {
100 _('Error #2', 41)
101 return;
102 }
103 var sub = xmpp.stanza('subscription', args);
104 ev.cnode(sub);
105 } else {
106 _('Error #1', 41)
107 return;
108 }
109
110 var subs;
111 if (typeof notifs == 'string') {
112 subs = {};
113 subs[notifs] = storage.getSubscription(notifs, nodeID);
114 } else
115 subs = notifs;
116
117 for (var i in subs) {
118 var sub = subs[i];
119
120 if (sub.options) {
121 if (typeof sub.options['pubsub#deliver'] != 'undefined' && !sub.options['pubsub#deliver'])
122 continue;
123
124 if (typeof sub.options['pubsub#digest'] != 'undefined' && sub.options['pubsub#digest']) {
125 if (!sub.digest)
126 sub.digest = [];
127 sub.digest.push(ev)
128
129 if (sub.digestTimeout)
130 continue;
131
132 var freq;
133 if (typeof sub.options['pubsub#digest_frequency'] == 'undefined')
134 freq = 0;
135 else
136 freq = parseInt(sub.options['pubsub#digest_frequency']);
137
138 if (freq == 0)
139 freq = 24*60*60*1000;
140
141 setTimeout(sendDigest, freq, notifs[i], nodeID);
142 sub.digestTimeout = true;
143 continue;
144 }
145 }
146
147 var message = xmpp.message({to: i, from: config.jid, id: conn.getUniqueId()});
148 message.cnode(ev);
149 conn.send(message);
150 }
151 }
152
153 exports.sendDigest = function(jid, nodeID) {
154 var sub = storage.getSubscription(jid, nodeID);
155 if (sub.digestTimeout)
156 sub.digestTimeout = false;
157
158 var message = xmpp.message({to: jid, from: config.jid, id: conn.getUniqueId()});
159 for (var i in sub.digest)
160 message.cnode(sub.digest[i]);
161 conn.send(message);
162 }
163
164 exports.setConnection = function(c) {
165 conn = c;
166 }