Mercurial > eldonilo > lightstring
comparison lightstring.js @ 44:3dfb596cf669
Add a plugin loader.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 31 Jan 2012 18:44:42 +0100 |
parents | 136df1708856 |
children | 770bb8460df8 |
comparison
equal
deleted
inserted
replaced
43:136df1708856 | 44:3dfb596cf669 |
---|---|
22 */ | 22 */ |
23 var Lightstring = { | 23 var Lightstring = { |
24 /** | 24 /** |
25 * @namespace Holds XMPP namespaces. | 25 * @namespace Holds XMPP namespaces. |
26 */ | 26 */ |
27 NS: { | 27 ns: { |
28 stream: 'http://etherx.jabber.org/streams', | 28 stream: 'http://etherx.jabber.org/streams', |
29 jabberClient: 'jabber:client' | 29 jabberClient: 'jabber:client' |
30 }, | 30 }, |
31 /** | 31 /** |
32 * @namespace Holds XMPP stanza builders. | 32 * @namespace Holds XMPP stanza builders. |
33 */ | 33 */ |
34 stanza: { | 34 stanzas: { |
35 stream: { | 35 stream: { |
36 open: function(aService) { | 36 open: function(aService) { |
37 //FIXME no ending "/" - node-xmpp-bosh bug | 37 //FIXME no ending "/" - node-xmpp-bosh bug |
38 return "<stream:stream to='" + aService + "'" + | 38 return "<stream:stream to='" + aService + "'" + |
39 " xmlns='" + Lightstring.NS.jabberClient + "'" + | 39 " xmlns='" + Lightstring.ns.jabberClient + "'" + |
40 " xmlns:stream='" + Lightstring.NS.stream + "'" + | 40 " xmlns:stream='" + Lightstring.ns.stream + "'" + |
41 " version='1.0'/>"; | 41 " version='1.0'/>"; |
42 }, | 42 }, |
43 close: function() { | 43 close: function() { |
44 return "</stream:stream>"; | 44 return "</stream:stream>"; |
45 } | 45 } |
46 } | 46 } |
47 }, | 47 }, |
48 plugins: {}, | |
48 /** | 49 /** |
49 * @private | 50 * @private |
50 */ | 51 */ |
51 parser: new DOMParser(), | 52 parser: new DOMParser(), |
52 /** | 53 /** |
277 | 278 |
278 var that = this; | 279 var that = this; |
279 this.socket.addEventListener('open', function() { | 280 this.socket.addEventListener('open', function() { |
280 //TODO: if (this.protocol !== 'xmpp') | 281 //TODO: if (this.protocol !== 'xmpp') |
281 | 282 |
282 var stream = Lightstring.stanza.stream.open(that.jid.domain); | 283 var stream = Lightstring.stanzas.stream.open(that.jid.domain); |
283 //TODO: Use Lightstring.Connection.send (problem with parsing steam); | 284 //TODO: Use Lightstring.Connection.send (problem with parsing steam); |
284 that.socket.send(stream); | 285 that.socket.send(stream); |
285 var stanza = { | 286 var stanza = { |
286 XML: stream | 287 XML: stream |
287 }; | 288 }; |
374 /** | 375 /** |
375 * @function Closes the XMPP stream and the socket. | 376 * @function Closes the XMPP stream and the socket. |
376 */ | 377 */ |
377 disconnect: function() { | 378 disconnect: function() { |
378 this.emit('disconnecting'); | 379 this.emit('disconnecting'); |
379 var stream = Lightstring.stanza.stream.close(); | 380 var stream = Lightstring.stanzas.stream.close(); |
380 this.send(stream); | 381 this.send(stream); |
381 this.emit('XMLOutput', stream); | 382 this.emit('XMLOutput', stream); |
382 this.socket.close(); | 383 this.socket.close(); |
384 }, | |
385 load: function() { | |
386 for (var i = 0; i < arguments.length; i++) { | |
387 var name = arguments[i]; | |
388 if (!(name in Lightstring.plugins)) | |
389 continue; //TODO: throw an error? | |
390 | |
391 var plugin = Lightstring.plugins[name]; | |
392 | |
393 for (var ns in plugin.namespaces) | |
394 Lightstring.ns[ns] = plugin.namespaces[ns]; | |
395 | |
396 for (var stanza in plugin.stanzas) | |
397 Lightstring.stanzas[stanza] = plugin.stanzas[stanza]; | |
398 | |
399 for (var handler in plugin.handlers) | |
400 this.on(handler, plugin.handlers[handler]); | |
401 | |
402 for (var method in plugin.methods) | |
403 this.methods[method] = plugin.methods[method]; | |
404 } | |
383 }, | 405 }, |
384 /** | 406 /** |
385 * @function Emits an event. | 407 * @function Emits an event. |
386 * @param {String} aName The event name. | 408 * @param {String} aName The event name. |
387 * @param {Function|Array|Object} [aData] Data about the event. | 409 * @param {Function|Array|Object} [aData] Data about the event. |