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