comparison strophe.pubsub.js @ 0:f62b5c395a48

Initial commit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 04 Jun 2011 05:02:47 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f62b5c395a48
1 /*
2 Copyright 2008, Stanziq Inc.
3 */
4
5 Strophe.addConnectionPlugin('pubsub', {
6 /*
7 Extend connection object to have plugin name 'pubsub'.
8 */
9 _connection: null,
10
11 //The plugin must have the init function.
12 init: function(conn) {
13
14 this._connection = conn;
15
16 /*
17 Function used to setup plugin.
18 */
19
20 /* extend name space
21 * NS.PUBSUB - XMPP Publish Subscribe namespace
22 * from XEP 60.
23 *
24 * NS.PUBSUB_SUBSCRIBE_OPTIONS - XMPP pubsub
25 * options namespace from XEP 60.
26 */
27 Strophe.addNamespace('PUBSUB',"http://jabber.org/protocol/pubsub");
28 Strophe.addNamespace('PUBSUB_SUBSCRIBE_OPTIONS',
29 Strophe.NS.PUBSUB+"#subscribe_options");
30 Strophe.addNamespace('PUBSUB_ERRORS',Strophe.NS.PUBSUB+"#errors");
31 Strophe.addNamespace('PUBSUB_EVENT',Strophe.NS.PUBSUB+"#event");
32 Strophe.addNamespace('PUBSUB_OWNER',Strophe.NS.PUBSUB+"#owner");
33 Strophe.addNamespace('PUBSUB_AUTO_CREATE',
34 Strophe.NS.PUBSUB+"#auto-create");
35 Strophe.addNamespace('PUBSUB_PUBLISH_OPTIONS',
36 Strophe.NS.PUBSUB+"#publish-options");
37 Strophe.addNamespace('PUBSUB_NODE_CONFIG',
38 Strophe.NS.PUBSUB+"#node_config");
39 Strophe.addNamespace('PUBSUB_CREATE_AND_CONFIGURE',
40 Strophe.NS.PUBSUB+"#create-and-configure");
41 Strophe.addNamespace('PUBSUB_SUBSCRIBE_AUTHORIZATION',
42 Strophe.NS.PUBSUB+"#subscribe_authorization");
43 Strophe.addNamespace('PUBSUB_GET_PENDING',
44 Strophe.NS.PUBSUB+"#get-pending");
45 Strophe.addNamespace('PUBSUB_MANAGE_SUBSCRIPTIONS',
46 Strophe.NS.PUBSUB+"#manage-subscriptions");
47 Strophe.addNamespace('PUBSUB_META_DATA',
48 Strophe.NS.PUBSUB+"#meta-data");
49
50 },
51 /***Function
52
53 Create a pubsub node on the given service with the given node
54 name.
55
56 Parameters:
57 (String) jid - The node owner's jid.
58 (String) service - The name of the pubsub service.
59 (String) node - The name of the pubsub node.
60 (Dictionary) options - The configuration options for the node.
61 (Function) call_back - Used to determine if node
62 creation was sucessful.
63
64 Returns:
65 Iq id used to send subscription.
66 */
67 createNode: function(jid,service,node,options, call_back) {
68
69 var iqid = this._connection.getUniqueId("pubsubcreatenode");
70
71 var iq = $iq({from:jid, to:service, type:'set', id:iqid});
72
73 var c_options = Strophe.xmlElement("configure",[]);
74 var x = Strophe.xmlElement("x",[["xmlns","jabber:x:data"]]);
75 var form_field = Strophe.xmlElement("field",[["var","FORM_TYPE"],
76 ["type","hidden"]]);
77 var value = Strophe.xmlElement("value",[]);
78 var text = Strophe.xmlTextNode(Strophe.NS.PUBSUB+"#node_config");
79 value.appendChild(text);
80 form_field.appendChild(value);
81 x.appendChild(form_field);
82
83 for (var i in options)
84 {
85 var val = options[i];
86 x.appendChild(val);
87 }
88
89 if(options.length && options.length != 0)
90 {
91 c_options.appendChild(x);
92 }
93
94 iq.c('pubsub',
95 {xmlns:Strophe.NS.PUBSUB}).c('create',
96 {node:node}).up().cnode(c_options);
97
98 this._connection.addHandler(call_back,
99 null,
100 'iq',
101 null,
102 iqid,
103 null);
104 this._connection.send(iq.tree());
105 return iqid;
106 },
107 /***Function
108 Subscribe to a node in order to receive event items.
109
110 Parameters:
111 (String) jid - The node owner's jid.
112 (String) service - The name of the pubsub service.
113 (String) node - The name of the pubsub node.
114 (Array) options - The configuration options for the node.
115 (Function) event_cb - Used to recieve subscription events.
116 (Function) call_back - Used to determine if node
117 creation was sucessful.
118
119 Returns:
120 Iq id used to send subscription.
121 */
122 subscribe: function(jid,service,node,options, event_cb, call_back) {
123
124 var subid = this._connection.getUniqueId("subscribenode");
125
126 var sub = $iq({from:jid, to:service, type:'set', id:subid})
127
128 if(options && options.length && options.length !== 0)
129 {
130
131 //create subscription options
132 var sub_options = Strophe.xmlElement("options",[]);
133 var x = Strophe.xmlElement("x",[["xmlns","jabber:x:data"]]);
134 var form_field = Strophe.xmlElement("field",[["var","FORM_TYPE"],
135 ["type","hidden"]]);
136 var value = Strophe.xmlElement("value",[]);
137 var text = Strophe.xmlTextNode(Strophe.NS.PUBSUB_SUBSCRIBE_OPTIONS);
138 value.appendChild(text);
139 form_field.appendChild(value);
140 x.appendChild(form_field);
141
142 for (var i = 0; i < options.length; i++)
143 {
144 var val = options[i];
145 x.appendChild(val);
146 }
147 sub_options.appendChild(x);
148
149 sub.c('pubsub', { xmlns:Strophe.NS.PUBSUB }).c('subscribe',
150 {node:node,jid:jid}).up().cnode(sub_options);
151 }
152 else
153 {
154
155 sub.c('pubsub', { xmlns:Strophe.NS.PUBSUB }).c('subscribe',
156 {node:node,jid:jid});
157 }
158
159
160 this._connection.addHandler(call_back,
161 null,
162 'iq',
163 null,
164 subid,
165 null);
166
167 //add the event handler to receive items
168 this._connection.addHandler(event_cb,
169 null,
170 'message',
171 null,
172 null,
173 null);
174 this._connection.send(sub.tree());
175 return subid;
176
177 },
178 /***Function
179 Unsubscribe from a node.
180
181 Parameters:
182 (String) jid - The node owner's jid.
183 (String) service - The name of the pubsub service.
184 (String) node - The name of the pubsub node.
185 (Function) call_back - Used to determine if node
186 creation was sucessful.
187
188 */
189 unsubscribe: function(jid,service,node, call_back) {
190
191 var subid = this._connection.getUniqueId("unsubscribenode");
192
193
194 var sub = $iq({from:jid, to:service, type:'set', id:subid})
195 sub.c('pubsub', { xmlns:Strophe.NS.PUBSUB }).c('unsubscribe',
196 {node:node,jid:jid});
197
198
199
200 this._connection.addHandler(call_back,
201 null,
202 'iq',
203 null,
204 subid,
205 null);
206 this._connection.send(sub.tree());
207
208
209 return subid;
210
211 },
212 /***Function
213
214 Publish and item to the given pubsub node.
215
216 Parameters:
217 (String) jid - The node owner's jid.
218 (String) service - The name of the pubsub service.
219 (String) node - The name of the pubsub node.
220 (Array) items - The list of items to be published.
221 (Function) call_back - Used to determine if node
222 creation was sucessful.
223 */
224 publish: function(jid, service, node, items, call_back) {
225 var pubid = this._connection.getUniqueId("publishnode");
226
227
228 var publish_elem = Strophe.xmlElement("publish",
229 [["node",
230 node],
231 ["jid",
232 jid]]);
233 for (var i in items)
234 {
235 var item = Strophe.xmlElement("item",[]);
236 var entry = Strophe.xmlElement("entry",[]);
237 var t = Strophe.xmlTextNode(items[i]);
238 entry.appendChild(t);
239 item.appendChild(entry);
240 publish_elem.appendChild(item);
241 }
242
243 var pub = $iq({from:jid, to:service, type:'set', id:pubid})
244 pub.c('pubsub', { xmlns:Strophe.NS.PUBSUB }).cnode(publish_elem);
245
246
247 this._connection.addHandler(call_back,
248 null,
249 'iq',
250 null,
251 pubid,
252 null);
253 this._connection.send(pub.tree());
254
255
256 return pubid;
257 },
258 /*Function: items
259 Used to retrieve the persistent items from the pubsub node.
260
261 */
262 items: function(jid,service,node,ok_callback,error_back) {
263 var pub = $iq({from:jid, to:service, type:'get'})
264
265 //ask for all items
266 pub.c('pubsub',
267 { xmlns:Strophe.NS.PUBSUB }).c('items',{node:node});
268
269 return this._connection.sendIQ(pub.tree(),ok_callback,error_back);
270 },
271 /*Function: info
272 Used to retrieve the persistent infos from the pubsub node.
273
274 */
275 info: function(jid,service,node,ok_callback,error_back) {
276 var pub = $iq({from:jid, to:service, type:'get'})
277
278 //ask for all items
279 pub.c('query',
280 { xmlns:'http://jabber.org/protocol/disco#info', node:node });
281
282 return this._connection.sendIQ(pub.tree(),ok_callback,error_back);
283 }
284 });