annotate lightstring.js @ 7:715726598b23

improvements on plugins
author Sonny Piers <sonny.piers@gmail.com>
date Sun, 15 Jan 2012 01:25:11 +0100
parents b7a582a2b32c
children 24bc40461b2c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
1 'use strict';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
2
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
3 /**
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
4 Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net>
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
5
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
6 Permission to use, copy, modify, and/or distribute this software for any
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
7 purpose with or without fee is hereby granted, provided that the above
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
8 copyright notice and this permission notice appear in all copies.
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
9
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
17 */
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
18
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
19 var Lightstring = {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
20 NS: {},
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
21 stanza: {},
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
22 };
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
23
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
24 Lightstring.Connection = function (aService) {
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
25 var parser = new DOMParser();
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
26 var serializer = new XMLSerializer();
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
27 this.service = aService;
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
28 this.handlers = {};
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
29 this.iqid = 1024;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
30 this.getNewId = function() {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
31 this.iqid++;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
32 return 'sendiq:'+this.iqid;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
33 };
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
34 this.parse = function(str) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
35 return parser.parseFromString(str, 'text/xml').documentElement;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
36 };
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
37 this.serialize = function(elm) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
38 return serializer.serializeToString(elm);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
39 };
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
40 this.connect = function(aJid, aPassword) {
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
41 this.emit('connecting');
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
42 if(aJid)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
43 this.jid = aJid;
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
44 if(this.jid) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
45 this.domain = this.jid.split('@')[1];
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
46 this.node = this.jid.split('@')[0];
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
47 this.resource = this.jid.split('/')[1];
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
48 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
49 if(aPassword)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
50 this.password = aPassword;
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
51
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
52 if(!this.jid)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
53 throw "Lightstring: Connection.jid is undefined.";
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
54 if(!this.password)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
55 throw "Lightstring: Connection.password is undefined.";
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
56 if(!this.service)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
57 throw "Lightstring: Connection.service is undefined.";
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
58
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
59 //"Bug 695635 - tracking bug: unprefix WebSockets" https://bugzil.la/695635
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
60 try {
6
b7a582a2b32c add the sub-protocol (xmpp) negociation
Sonny Piers <sonny.piers@gmail.com>
parents: 3
diff changeset
61 this.socket = new WebSocket(this.service, 'xmpp');
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
62 }
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
63 catch(error) {
6
b7a582a2b32c add the sub-protocol (xmpp) negociation
Sonny Piers <sonny.piers@gmail.com>
parents: 3
diff changeset
64 this.socket = new MozWebSocket(this.service, 'xmpp');
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
65 }
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
66
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
67 var that = this;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
68 this.socket.addEventListener('open', function() {
6
b7a582a2b32c add the sub-protocol (xmpp) negociation
Sonny Piers <sonny.piers@gmail.com>
parents: 3
diff changeset
69 if(this.protocol !== 'xmpp')
b7a582a2b32c add the sub-protocol (xmpp) negociation
Sonny Piers <sonny.piers@gmail.com>
parents: 3
diff changeset
70 throw "Lightstring: The server located at "+that.service+" is not XMPP aware.";
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
71 var stream =
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
72 "<stream:stream to='"+that.domain+"'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
73 xmlns='jabber:client'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
74 xmlns:stream='http://etherx.jabber.org/streams'\
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
75 version='1.0'/>";
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
76 that.socket.send(stream)
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
77 that.emit('XMLOutput', stream);
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
78 });
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
79 this.socket.addEventListener('error', function(e) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
80 that.emit('error', e.data);
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
81 });
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
82 this.socket.addEventListener('close', function(e) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
83 that.emit('disconnected', e.data);
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
84 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
85 this.socket.addEventListener('message', function(e) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
86 that.emit('XMLInput', e.data);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
87 var elm = that.parse(e.data);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
88 that.emit('DOMInput', elm);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
89 that.emit(elm.tagName, elm);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
90
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
91 if(elm.tagName === 'iq')
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
92 that.emit(elm.getAttribute('id'), elm);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
93 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
94 };
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
95 this.send = function(aStanza, aCallback) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
96 if(typeof aStanza === 'string') {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
97 var str = aStanza;
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
98 var elm = this.parse(str);
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
99 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
100 else if(aStanza instanceof Element) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
101 var elm = aStanza;
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
102 var str = this.serialize(elm);
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
103 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
104 else {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
105 that.emit('error', 'Unsupported data type.');
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
106 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
107
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
108
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
109 if(elm.tagName === 'iq') {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
110 var id = elm.getAttribute('id');
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
111 if(!id) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
112 elm.setAttribute('id', this.getNewId())
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
113 str = this.serialize(elm)
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
114 }
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
115 if(aCallback)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
116 this.on(elm.getAttribute('id'), aCallback);
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
117 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
118 else if(aCallback) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
119 that.emit('warning', 'Callback can\'t be called with non-iq stanza.');
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
120 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
121
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
122
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
123 this.socket.send(str);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
124 this.emit('XMLOutput', str);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
125 this.emit('DOMOutput', elm);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
126 };
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
127 this.disconnect = function() {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
128 this.emit('disconnecting');
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
129 this.send('</stream:stream>');
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
130 this.socket.close();
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
131 };
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
132 this.emit = function(name, data) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
133 var handlers = this.handlers[name];
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
134 if(!handlers)
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
135 return;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
136
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
137 //FIXME Better idea than passing the context as argument?
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
138 for(var i=0; i<handlers.length; i++)
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
139 handlers[i](data, this);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
140
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
141 if(name.match('sendiq:'))
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
142 delete this.handlers[name];
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
143 };
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
144 this.on = function(name, callback) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
145 if(!this.handlers[name])
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
146 this.handlers[name] = [];
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
147 this.handlers[name].push(callback);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
148 };
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
149 //FIXME do this!
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
150 this.once = function(name, callback) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
151 if(!this.handlers[name])
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
152 this.handlers[name] = [];
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
153 this.handlers[name].push(callback);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
154 };
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
155 //Internal
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
156 this.on('stream:features', function(stanza, that) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
157 var nodes = stanza.querySelectorAll('mechanism');
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
158 //SASL/Auth features
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
159 if(nodes.length > 0) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
160 that.emit('mechanisms', stanza);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
161 var mechanisms = {};
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
162 for(var i=0; i<nodes.length; i++)
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
163 mechanisms[nodes[i].textContent] = true;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
164
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
165
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
166 //FIXME support SCRAM-SHA1 && allow specify method preferences
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
167 if('DIGEST-MD5' in mechanisms)
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
168 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
169 "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
170 mechanism='DIGEST-MD5'/>"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
171 );
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
172 else if('PLAIN' in mechanisms) {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
173 var token = btoa(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
174 that.jid +
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
175 "\u0000" +
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
176 that.jid.node +
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
177 "\u0000" +
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
178 that.password
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
179 );
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
180 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
181 "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
182 mechanism='PLAIN'>"+token+"</auth>"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
183 );
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
184 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
185 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
186 //XMPP features
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
187 else {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
188 that.emit('features', stanza);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
189 //Bind http://xmpp.org/rfcs/rfc3920.html#bind
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
190 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
191 "<iq type='set' xmlns='jabber:client'>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
192 <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
193 </iq>",
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
194 function() {
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
195 //Session http://xmpp.org/rfcs/rfc3921.html#session
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
196 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
197 "<iq type='set' xmlns='jabber:client'>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
198 <session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
199 </iq>",
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
200 function() {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
201 that.emit('connected');
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
202 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
203 );
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
204 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
205 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
206 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
207 //Internal
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
208 this.on('success', function(stanza, that) {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
209 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
210 "<stream:stream to='"+that.domain+"'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
211 xmlns='jabber:client'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
212 xmlns:stream='http://etherx.jabber.org/streams'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
213 version='1.0' />"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
214 );
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
215 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
216 //Internal
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
217 this.on('failure', function(stanza, that) {
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
218 that.emit('conn-error', stanza.firstChild.tagName);
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
219 });
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
220 //Internal
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
221 this.on('challenge', function(stanza, that) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
222 //FIXME this is mostly Strophe code
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
223
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
224 function _quote(str) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
225 return '"' + str.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '"';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
226 };
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
227
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
228 var challenge = atob(stanza.textContent);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
229
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
230 var attribMatch = /([a-z]+)=("[^"]+"|[^,"]+)(?:,|$)/;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
231
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
232 var cnonce = MD5.hexdigest(Math.random() * 1234567890);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
233 var realm = "";
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
234 var host = null;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
235 var nonce = "";
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
236 var qop = "";
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
237 var matches;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
238
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
239 while (challenge.match(attribMatch)) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
240 matches = challenge.match(attribMatch);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
241 challenge = challenge.replace(matches[0], "");
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
242 matches[2] = matches[2].replace(/^"(.+)"$/, "$1");
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
243 switch (matches[1]) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
244 case "realm":
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
245 realm = matches[2];
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
246 break;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
247 case "nonce":
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
248 nonce = matches[2];
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
249 break;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
250 case "qop":
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
251 qop = matches[2];
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
252 break;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
253 case "host":
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
254 host = matches[2];
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
255 break;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
256 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
257 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
258
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
259 var digest_uri = "xmpp/" + that.domain;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
260 if (host !== null) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
261 digest_uri = digest_uri + "/" + host;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
262 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
263 var A1 = MD5.hash(that.node +
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
264 ":" + realm + ":" + that.password) +
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
265 ":" + nonce + ":" + cnonce;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
266 var A2 = 'AUTHENTICATE:' + digest_uri;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
267
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
268 var responseText = "";
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
269 responseText += 'username=' + _quote(that.node) + ',';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
270 responseText += 'realm=' + _quote(realm) + ',';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
271 responseText += 'nonce=' + _quote(nonce) + ',';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
272 responseText += 'cnonce=' + _quote(cnonce) + ',';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
273 responseText += 'nc="00000001",';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
274 responseText += 'qop="auth",';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
275 responseText += 'digest-uri=' + _quote(digest_uri) + ',';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
276 responseText += 'response=' + _quote(
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
277 MD5.hexdigest(MD5.hexdigest(A1) + ":" +
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
278 nonce + ":00000001:" +
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
279 cnonce + ":auth:" +
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
280 MD5.hexdigest(A2))) + ',';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
281 responseText += 'charset="utf-8"';
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
282 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
283 "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
284 +btoa(responseText)+
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
285 "</response>");
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
286 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
287 };