annotate lightstring.js @ 2:f31a75c3b6c8

code cleaning
author Sonny Piers <sonny.piers@gmail.com>
date Sun, 18 Dec 2011 22:57:47 +0100
parents 96087680669f
children 029c12b8f048
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) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
41 if(aJid)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
42 this.jid = aJid;
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
43 if(this.jid) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
44 this.domain = this.jid.split('@')[1];
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
45 this.node = this.jid.split('@')[0];
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
46 this.resource = this.jid.split('/')[1];
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
47 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
48 if(aPassword)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
49 this.password = aPassword;
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
50
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
51 if(!this.jid)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
52 throw "Lightstring: Connection.jid is undefined.";
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
53 if(!this.password)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
54 throw "Lightstring: Connection.password is undefined.";
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
55 if(!this.service)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
56 throw "Lightstring: Connection.service is undefined.";
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
57
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
58 //"Bug 695635 - tracking bug: unprefix WebSockets" https://bugzil.la/695635
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
59 if(MozWebSocket)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
60 this.socket = new MozWebSocket(this.service);
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
61 else if(WebSocket)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
62 this.socket = new WebSocket(this.service);
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
63 else
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
64 this.emit('error', 'No WebSocket support.');
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
65
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 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
67 this.socket.addEventListener('open', function() {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
68 that.emit('connecting');
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
69 //FIXME there shouldn't be an ending "/"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
70 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
71 "<stream:stream to='"+that.domain+"'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
72 xmlns='jabber:client'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
73 xmlns:stream='http://etherx.jabber.org/streams'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
74 version='1.0'/>"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
75 );
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
76 });
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
77 this.socket.addEventListener('error', function(e) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
78 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
79 });
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
80 this.socket.addEventListener('close', function(e) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
81 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
82 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
83 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
84 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
85 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
86 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
87 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
88
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
89 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
90 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
91 });
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 };
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
93 this.send = function(aStanza, aCallback) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
94 if(typeof aStanza === 'string') {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
95 var str = aStanza;
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
96 var elm = this.parse(str);
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
97 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
98 else if(aStanza instanceof Element) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
99 var elm = aStanza;
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
100 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
101 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
102 else {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
103 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
104 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
105
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 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
108 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
109 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
110 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
111 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
112 }
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
113 if(aCallback)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
114 this.on(elm.getAttribute('id'), aCallback);
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
115 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
116 else if(aCallback) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
117 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
118 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
119
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 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
122 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
123 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
124 };
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.disconnect = function() {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
126 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
127 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
128 this.socket.close();
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
129 this.emit('disconnected');
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
130 };
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 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
132 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
133 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
134 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
135
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 //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
137 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
138 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
139
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 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
141 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
142 };
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 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
144 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
145 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].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
147 };
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 //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
149 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
150 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
151 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].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
153 };
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 //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
155 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
156 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
157 //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
158 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
159 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
160 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
161 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
162 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
163
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 //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
166 if('DIGEST-MD5' in mechanisms)
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
167 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
168 "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
169 mechanism='DIGEST-MD5'/>"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
170 );
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
171 else if('PLAIN' in mechanisms) {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
172 var token = btoa(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
173 that.jid +
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
174 "\u0000" +
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
175 that.jid.node +
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
176 "\u0000" +
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
177 that.password
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
178 );
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
179 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
180 "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
181 mechanism='PLAIN'>"+token+"</auth>"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
182 );
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
183 }
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 //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
186 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
187 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
188 //Bind http://xmpp.org/rfcs/rfc3920.html#bind
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
189 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
190 "<iq type='set' xmlns='jabber:client'>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
191 <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
192 </iq>",
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
193 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
194 //Session http://xmpp.org/rfcs/rfc3921.html#session
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
195 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
196 "<iq type='set' xmlns='jabber:client'>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
197 <session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
198 </iq>",
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
199 function() {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
200 that.emit('connected');
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
201 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
202 );
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
203 });
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 //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
207 this.on('success', function(stanza, that) {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
208 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
209 "<stream:stream to='"+that.domain+"'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
210 xmlns='jabber:client'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
211 xmlns:stream='http://etherx.jabber.org/streams'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
212 version='1.0' />"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
213 );
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
214 });
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 //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
216 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
217 //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
218
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
219 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
220 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
221 };
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
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 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
224
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 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
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 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
228 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
229 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
230 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
231 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
232 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
233
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 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
235 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
236 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
237 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
238 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
239 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
240 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
241 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
242 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
243 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
244 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
245 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
246 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
247 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
248 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
249 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
250 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
251 }
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 }
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
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 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
255 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
256 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
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 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
259 ":" + 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
260 ":" + 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
261 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
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 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
264 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
265 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
266 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
267 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
268 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
269 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
270 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
271 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
272 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
273 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
274 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
275 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
276 responseText += 'charset="utf-8"';
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
277 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
278 "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
279 +btoa(responseText)+
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
280 "</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
281 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
282 };