xmpp.js

Wed, 24 Feb 2010 22:26:09 -0800

author
zuwiki <zuwiki@gmail.com>
date
Wed, 24 Feb 2010 22:26:09 -0800
changeset 16
74a24eb1fb44
parent 15
b328899c936a
child 17
701b83c8b687
permissions
-rw-r--r--

Making node-xml into a submodule. Gosh I hope I know what I'm doing.

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

mercurial