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