Wed, 03 Feb 2010 21:02:33 +0000
Add sendIQ() method
0 | 1 | // Node libs |
2 | var tcp = require("tcp"); | |
3 | ||
4 | // External libs | |
5 | var xml = require("./node-xml"); | |
6 | var sha1 = require("./sha1"); | |
7 | ||
8 | // This lib | |
9 | var xmpp = exports; | |
10 | ||
11 | // Wraps a function so that its 'this' is always 'context' when called | |
12 | var recontext = function (context, f) { return function () { return f.apply(context, arguments); }; }; | |
13 | ||
14 | xmpp.xmlns = { | |
15 | streams: "http://etherx.jabber.org/streams", | |
16 | component_accept: "jabber:component:accept" | |
17 | }; | |
18 | ||
19 | xmpp.Status = { | |
20 | ERROR: 0, | |
21 | CONNECTING: 1, | |
22 | CONNFAIL: 2, | |
23 | AUTHENTICATING: 3, | |
24 | AUTHFAIL: 4, | |
25 | CONNECTED: 5, | |
26 | DISCONNECTED: 6, | |
27 | DISCONNECTING: 7, | |
28 | }; | |
29 | ||
30 | xmpp.LogLevel = { | |
31 | DEBUG: 0, | |
32 | INFO: 1, | |
33 | WARN: 2, | |
34 | ERROR: 3, | |
35 | FATAL: 4 | |
36 | }; | |
37 | /** XMPPStream: Takes a parser, eats bytes, fires callbacks on stream events **/ | |
38 | xmpp.Stream = function (callbacks) | |
39 | { | |
40 | this.callbacks = callbacks; | |
41 | var stream = this; | |
42 | var stanza; | |
43 | this.parser = new xml.SaxParser(function (cb) | |
44 | { | |
45 | cb.onStartElementNS(function (tagname, attr_arr, prefix, uri, namespaces) | |
46 | { | |
4
67b1d93509d3
Strip default stream namespace from stanza objects
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
47 | var attr = {}; |
67b1d93509d3
Strip default stream namespace from stanza objects
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
48 | if(uri != xmpp.xmlns.component_accept) |
67b1d93509d3
Strip default stream namespace from stanza objects
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
49 | attr.xmlns = uri; |
0 | 50 | for(var i=0;i<attr_arr.length;i++) |
51 | attr[attr_arr[i][0]] = attr_arr[i][1]; | |
52 | for(var i=0;i<namespaces.length;i++) | |
53 | if(namespaces[i][0].length > 0) | |
54 | attr["xmlns:"+namespaces[i][0]] = namespaces[i][1]; | |
55 | if(!stanza) | |
56 | { | |
57 | if(stream.opened) | |
58 | stanza = xmpp.stanza(tagname, attr); | |
59 | else if(tagname == "stream" && uri == xmpp.xmlns.streams) | |
60 | { | |
61 | stream.opened = true; | |
62 | callbacks.opened(attr); | |
63 | } | |
64 | else | |
65 | { | |
66 | callbacks.error("no-stream"); | |
67 | } | |
68 | } | |
69 | else | |
70 | { | |
71 | stanza.c(tagname, attr); | |
72 | } | |
73 | ||
74 | }); | |
75 | ||
76 | cb.onEndElementNS(function(tagname) { | |
77 | if(stanza) | |
78 | if(stanza.last_node.length == 1) | |
79 | { | |
80 | callbacks.stanza(stanza); | |
81 | stanza = null; | |
82 | } | |
83 | else | |
84 | stanza.up(); | |
85 | else | |
86 | { | |
87 | stream.opened = false; | |
88 | callbacks.closed(); | |
89 | } | |
90 | }); | |
91 | ||
92 | cb.onCharacters(function(chars) { | |
93 | if(stanza) | |
94 | stanza.t(chars); | |
95 | }); | |
96 | }); | |
97 | ||
98 | this.data = function (data) | |
99 | { | |
100 | return this.parser.parseString(data); | |
101 | } | |
102 | ||
103 | return this; | |
104 | }; | |
105 | ||
106 | ||
107 | /** Connection: Takes host/port, manages stream **/ | |
108 | xmpp.Connection = function (host, port) | |
109 | { | |
110 | this.host = host || "localhost"; | |
111 | this.port = port || 5347; | |
112 | ||
113 | this.socket = tcp.createConnection(); | |
114 | ||
115 | this.stream = new xmpp.Stream({ | |
116 | opened: recontext(this, this._stream_opened), | |
117 | stanza: recontext(this, this._handle_stanza), | |
118 | closed: recontext(this, this._stream_closed) | |
119 | }); | |
120 | ||
5 | 121 | this._uniqueId = 0; |
122 | ||
0 | 123 | return this; |
124 | }; | |
125 | ||
126 | exports.Connection.prototype = { | |
127 | connect: function (jid, pass, callback) | |
128 | { | |
129 | this.jid = jid; | |
130 | this.password = pass; | |
131 | this.connect_callback = callback; | |
132 | ||
133 | var conn = this; | |
134 | this.socket.addListener("connect", recontext(this, conn._socket_connected)); | |
135 | this.socket.addListener("disconnect", recontext(this, conn._socket_disconnected)); | |
136 | this.socket.addListener("receive", recontext(this, conn._socket_received)); | |
137 | ||
6 | 138 | this.handlers = []; |
139 | ||
0 | 140 | // Connect TCP socket |
141 | this.socket.connect(this.port, this.host); | |
142 | ||
143 | this._setStatus(xmpp.Status.CONNECTING); | |
144 | }, | |
145 | ||
146 | send: function (data) | |
147 | { | |
148 | this.debug("SND: "+data); | |
149 | this.socket.send(data.toString()); | |
150 | }, | |
151 | ||
8 | 152 | sendIQ: function (iq, on_result, on_error) |
153 | { | |
154 | if(!iq.attr.id) | |
155 | iq.attr.id = this.getUniqueId(); | |
156 | this.addHandler(function (reply) { | |
157 | if(reply.attr.type == "result") | |
158 | return on_result(reply); | |
159 | elseif(on_error) | |
160 | return on_error(reply); | |
161 | return false; | |
162 | ||
163 | }, null, "iq", null, iq.attr.id); | |
164 | this.send(iq); | |
165 | }, | |
5 | 166 | |
6 | 167 | addHandler: function (handler, ns, name, type, id, from, options) |
168 | { | |
169 | return this.handlers.push({ | |
170 | callback: handler, | |
171 | xmlns: ns, | |
172 | name: name, | |
173 | type: type, | |
174 | id: id, | |
175 | from: from, | |
176 | matchBare: options && options.matchBare}); | |
177 | }, | |
178 | ||
5 | 179 | getUniqueId: function (suffix) |
180 | { | |
181 | return ++this._uniqueId + (suffix?(":"+suffix):""); | |
182 | }, | |
183 | ||
0 | 184 | // Update the status of the connection, call connect_callback |
185 | _setStatus: function (status, condition) | |
186 | { | |
187 | this.status = status; | |
188 | this.connect_callback(status, condition); | |
189 | }, | |
190 | ||
191 | // Socket listeners, called on TCP-level events | |
192 | _socket_connected: function () | |
193 | { | |
194 | this.info("CONNECTED."); | |
195 | this.send("<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' to='"+this.jid+"'>"); | |
196 | }, | |
197 | ||
198 | _socket_disconnected: function (had_error) | |
199 | { | |
200 | if(this.status == xmpp.Status.CONNECTING) | |
201 | this._setStatus(xmpp.Status.CONNFAIL); | |
202 | elseif(this.status == xmpp.Status.CONNECTED) | |
203 | this._setStatus(xmpp.Status.DISCONNECTED); | |
204 | this.info("DISCONNECTED."); | |
205 | }, | |
206 | ||
207 | _socket_received: function (data) | |
208 | { | |
209 | this.debug("RCV: "+data); | |
210 | // Push to parser | |
211 | this.stream.data(data); | |
212 | }, | |
213 | ||
214 | // Stream listeners, called on XMPP-level events | |
215 | _stream_opened: function (attr) | |
216 | { | |
217 | this.debug("STREAM: opened."); | |
218 | this._setStatus(xmpp.Status.AUTHENTICATING); | |
219 | var handshake = sha1.hex(attr.id + this.password); | |
220 | this.debug("Sending authentication token..."); | |
221 | this.send("<handshake>"+handshake+"</handshake>"); | |
222 | }, | |
223 | ||
224 | _handle_stanza: function (stanza) | |
225 | { | |
4
67b1d93509d3
Strip default stream namespace from stanza objects
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
226 | if(!stanza.attr.xmlns) // Default namespace |
0 | 227 | { |
228 | if(stanza.name == "handshake") | |
229 | { | |
230 | this._setStatus(xmpp.Status.CONNECTED); | |
231 | } | |
232 | } | |
233 | this.debug("STANZA: "+stanza.toString()); | |
7
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
234 | |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
235 | // Match and call handlers |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
236 | var removeHandlers = []; |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
237 | for(var i=0;i<this.handlers.length;i++) |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
238 | { |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
239 | var handler = this.handlers[i]; |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
240 | if( |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
241 | (!handler.name || handler.name == stanza.name) && |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
242 | (!handler.xmlns || (handler.xmlns == stanza.attr.xmlns |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
243 | || (stanza.tags[0] && handler.xmlns == stanza.tags[0].attr.xmlns))) && |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
244 | (!handler.type || handler.type == stanza.attr.type) && |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
245 | (!handler.id || handler.id == stanza.attr.id) && |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
246 | (!handler.from || (handler.from == (handler.matchBare?xmpp.getBareJID(stanza.attr.from):stanza.attr.from))) && |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
247 | (!handler.to || (handler.to == (handler.matchBare?xmpp.getBareJID(stanza.attr.to):stanza.attr.to))) |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
248 | ) |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
249 | { |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
250 | var ret = handler.callback(stanza); |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
251 | if(ret == false) |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
252 | removeHandlers.push(i); |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
253 | } |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
254 | } |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
255 | |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
256 | var adjust = 0; |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
257 | for(var i=0;i<removeHandlers.length;i++) |
394b0c8cad04
Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
258 | this.handlers.splice(removeHandlers[i]-(adjust++), 1); |
0 | 259 | }, |
260 | ||
261 | _stream_closed: function () | |
262 | { | |
263 | this.debug("STREAM: closed."); | |
264 | this.socket.close(); | |
265 | if(this.status == xmpp.Status.CONNECTING) | |
266 | this._setStatus(xmpp.status.CONNFAIL); | |
267 | else | |
268 | this._setStatus(xmpp.Status.DISCONNECTED); | |
269 | }, | |
270 | ||
271 | _stream_error: function (condition) | |
272 | { | |
273 | this._setStatus(xmpp.Status.ERROR, condition); | |
274 | }, | |
275 | ||
276 | // Logging | |
277 | log: function (level, message) {}, | |
278 | debug: function (message) { return this.log(xmpp.LogLevel.DEBUG, message); }, | |
279 | info: function (message) { return this.log(xmpp.LogLevel.INFO , message); }, | |
280 | warn: function (message) { return this.log(xmpp.LogLevel.WARN , message); }, | |
281 | error: function (message) { return this.log(xmpp.LogLevel.ERROR, message); }, | |
282 | fatal: function (message) { return this.log(xmpp.LogLevel.FATAL, message); } | |
283 | ||
284 | }; | |
285 | ||
286 | function xmlescape(s) | |
287 | { | |
288 | return s.replace(/&/g, "&") | |
289 | .replace(/</g, "<") | |
290 | .replace(/>/g, ">") | |
1 | 291 | .replace(/\"/g, """) |
292 | .replace(/\'/g, "'"); | |
0 | 293 | } |
294 | ||
295 | /** StanzaBuilder: Helps create and manipulate XML snippets **/ | |
296 | xmpp.StanzaBuilder = function (name, attr) | |
297 | { | |
298 | this.name = name; | |
299 | this.attr = attr || {}; | |
300 | this.tags = []; | |
301 | this.children = []; | |
302 | this.last_node = [this]; | |
303 | return this; | |
304 | }; | |
305 | ||
306 | xmpp.StanzaBuilder.prototype = { | |
307 | c: function (name, attr) | |
308 | { | |
309 | var s = new xmpp.StanzaBuilder(name, attr); | |
310 | var parent = this.last_node[this.last_node.length-1]; | |
311 | parent.tags.push(s); | |
312 | parent.children.push(s); | |
313 | this.last_node.push(s); | |
314 | return this; | |
315 | }, | |
316 | ||
317 | t: function (text) | |
318 | { | |
319 | var parent = this.last_node[this.last_node.length-1]; | |
320 | parent.children.push(text); | |
321 | return this; | |
322 | }, | |
323 | ||
324 | up: function () | |
325 | { | |
326 | this.last_node.pop(); | |
327 | return this; | |
328 | }, | |
329 | ||
330 | toString: function (top_tag_only) | |
331 | { | |
332 | var buf = []; | |
333 | buf.push("<" + this.name); | |
334 | for(var attr in this.attr) | |
335 | { | |
336 | buf.push(" " + attr + "='" + xmlescape(this.attr[attr]) + "'"); | |
337 | } | |
338 | ||
339 | // Now add children if wanted | |
340 | if(top_tag_only) | |
341 | { | |
342 | buf.push(">"); | |
343 | } | |
344 | else if(this.children.length == 0) | |
345 | { | |
346 | buf.push("/>"); | |
347 | } | |
348 | else | |
349 | { | |
350 | buf.push(">"); | |
351 | for(var i = 0; i<this.children.length; i++) | |
352 | { | |
353 | var child = this.children[i]; | |
354 | if(typeof(child) == "string") | |
355 | buf.push(xmlescape(child)); | |
356 | else | |
357 | buf.push(child.toString()); | |
358 | } | |
359 | buf.push("</" + this.name + ">"); | |
360 | } | |
361 | return buf.join(""); | |
2
b88bcbbe08e1
Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
362 | }, |
b88bcbbe08e1
Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
363 | |
b88bcbbe08e1
Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
364 | getChild: function (name, xmlns) { |
b88bcbbe08e1
Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
365 | for(var i=0;i<this.tags.length;i++) |
b88bcbbe08e1
Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
366 | { |
b88bcbbe08e1
Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
367 | var child = this.tags[i]; |
b88bcbbe08e1
Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
368 | if((!name || child.name == name) && (!xmlns || child.attr.xmlns == xmlns)) |
b88bcbbe08e1
Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
369 | return child; |
b88bcbbe08e1
Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
370 | } |
b88bcbbe08e1
Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
371 | return null; |
b88bcbbe08e1
Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
372 | }, |
3
2d83fe899f5f
Add stanza.getText() to retrieve all text nodes joined as a string
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
373 | |
2d83fe899f5f
Add stanza.getText() to retrieve all text nodes joined as a string
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
374 | getText: function () { |
2d83fe899f5f
Add stanza.getText() to retrieve all text nodes joined as a string
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
375 | var buf = []; |
2d83fe899f5f
Add stanza.getText() to retrieve all text nodes joined as a string
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
376 | for(var i=0;i<this.children.length;i++) |
2d83fe899f5f
Add stanza.getText() to retrieve all text nodes joined as a string
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
377 | if(typeof(this.children[i]) == "string") |
2d83fe899f5f
Add stanza.getText() to retrieve all text nodes joined as a string
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
378 | buf.push(this.children[i]); |
2d83fe899f5f
Add stanza.getText() to retrieve all text nodes joined as a string
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
379 | return buf.join(""); |
0 | 380 | } |
381 | } | |
382 | ||
383 | xmpp.stanza = function (name, attr) | |
384 | { | |
385 | return new xmpp.StanzaBuilder(name, attr); | |
386 | } | |
387 | ||
388 | xmpp.message = function (attr) | |
389 | { | |
390 | return xmpp.stanza("message", attr); | |
391 | } | |
392 | ||
393 | xmpp.presence = function (attr) | |
394 | { | |
395 | return xmpp.stanza("presence", attr); | |
396 | } | |
397 | ||
398 | xmpp.iq = function (attr) | |
399 | { | |
400 | return xmpp.stanza("iq", attr); | |
401 | } |