xmpp.js

Wed, 10 Feb 2010 15:01:40 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 10 Feb 2010 15:01:40 +0000
changeset 11
080d81e07112
parent 9
c4ff2c2fea6d
child 12
1fe880c7383e
permissions
-rw-r--r--

README.markdown: Add the missing URLs

0
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 // Node libs
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 var tcp = require("tcp");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 // External libs
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 var xml = require("./node-xml");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 var sha1 = require("./sha1");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 // This lib
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 var xmpp = exports;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 // 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
12 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
13
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 xmpp.xmlns = {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 streams: "http://etherx.jabber.org/streams",
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 component_accept: "jabber:component:accept"
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 };
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.Status = {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 ERROR: 0,
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 CONNECTING: 1,
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 CONNFAIL: 2,
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 AUTHENTICATING: 3,
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 AUTHFAIL: 4,
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 CONNECTED: 5,
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 DISCONNECTED: 6,
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 DISCONNECTING: 7,
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 };
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 xmpp.LogLevel = {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 DEBUG: 0,
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 INFO: 1,
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 WARN: 2,
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 ERROR: 3,
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 FATAL: 4
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 };
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 /** XMPPStream: Takes a parser, eats bytes, fires callbacks on stream events **/
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 xmpp.Stream = function (callbacks)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 this.callbacks = callbacks;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 var stream = this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 var stanza;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 this.parser = new xml.SaxParser(function (cb)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 cb.onStartElementNS(function (tagname, attr_arr, prefix, uri, namespaces)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 for(var i=0;i<attr_arr.length;i++)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 attr[attr_arr[i][0]] = attr_arr[i][1];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 for(var i=0;i<namespaces.length;i++)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if(namespaces[i][0].length > 0)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 attr["xmlns:"+namespaces[i][0]] = namespaces[i][1];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 if(!stanza)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if(stream.opened)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 stanza = xmpp.stanza(tagname, attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 else if(tagname == "stream" && uri == xmpp.xmlns.streams)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 stream.opened = true;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 callbacks.opened(attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 else
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 callbacks.error("no-stream");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 else
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 stanza.c(tagname, attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 }
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
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 cb.onEndElementNS(function(tagname) {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 if(stanza)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 if(stanza.last_node.length == 1)
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 callbacks.stanza(stanza);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 stanza = null;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 else
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 stanza.up();
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 else
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 stream.opened = false;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 callbacks.closed();
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 });
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 cb.onCharacters(function(chars) {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 if(stanza)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 stanza.t(chars);
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 this.data = function (data)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 return this.parser.parseString(data);
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 return this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 };
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
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 /** Connection: Takes host/port, manages stream **/
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 xmpp.Connection = function (host, port)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 this.host = host || "localhost";
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 this.port = port || 5347;
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 this.socket = tcp.createConnection();
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 this.stream = new xmpp.Stream({
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 opened: recontext(this, this._stream_opened),
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 stanza: recontext(this, this._handle_stanza),
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 closed: recontext(this, this._stream_closed)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 });
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120
5
20a58dcf2323 Add getUniqueId() method
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
121 this._uniqueId = 0;
20a58dcf2323 Add getUniqueId() method
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
122
0
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 return this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 };
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 exports.Connection.prototype = {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 connect: function (jid, pass, callback)
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 this.jid = jid;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 this.password = pass;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 this.connect_callback = 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 var conn = this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 this.socket.addListener("connect", recontext(this, conn._socket_connected));
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 this.socket.addListener("disconnect", recontext(this, conn._socket_disconnected));
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 this.socket.addListener("receive", recontext(this, conn._socket_received));
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137
6
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
138 this.handlers = [];
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
139
0
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 // Connect TCP socket
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 this.socket.connect(this.port, this.host);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 this._setStatus(xmpp.Status.CONNECTING);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 send: function (data)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 this.debug("SND: "+data);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 this.socket.send(data.toString());
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151
8
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
152 sendIQ: function (iq, on_result, on_error)
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
153 {
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
154 if(!iq.attr.id)
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
155 iq.attr.id = this.getUniqueId();
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
156 this.addHandler(function (reply) {
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
157 if(reply.attr.type == "result")
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
158 return on_result(reply);
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
159 elseif(on_error)
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
160 return on_error(reply);
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
161 return false;
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
162
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
163 }, null, "iq", null, iq.attr.id);
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
164 this.send(iq);
ca881dab2577 Add sendIQ() method
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
165 },
5
20a58dcf2323 Add getUniqueId() method
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
166
6
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
167 addHandler: function (handler, ns, name, type, id, from, options)
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
168 {
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
169 return this.handlers.push({
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
170 callback: handler,
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
171 xmlns: ns,
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
172 name: name,
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
173 type: type,
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
174 id: id,
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
175 from: from,
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
176 matchBare: options && options.matchBare});
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
177 },
ec839631a35f Add addHandler() method
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
178
5
20a58dcf2323 Add getUniqueId() method
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
179 getUniqueId: function (suffix)
20a58dcf2323 Add getUniqueId() method
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
180 {
20a58dcf2323 Add getUniqueId() method
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
181 return ++this._uniqueId + (suffix?(":"+suffix):"");
20a58dcf2323 Add getUniqueId() method
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
182 },
20a58dcf2323 Add getUniqueId() method
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
183
0
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 // Update the status of the connection, call connect_callback
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 _setStatus: function (status, condition)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 this.status = status;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 this.connect_callback(status, condition);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 // Socket listeners, called on TCP-level events
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 _socket_connected: function ()
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.info("CONNECTED.");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 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
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_disconnected: function (had_error)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 if(this.status == xmpp.Status.CONNECTING)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 this._setStatus(xmpp.Status.CONNFAIL);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 elseif(this.status == xmpp.Status.CONNECTED)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 this._setStatus(xmpp.Status.DISCONNECTED);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 this.info("DISCONNECTED.");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 },
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 _socket_received: function (data)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 this.debug("RCV: "+data);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 // Push to parser
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 this.stream.data(data);
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 // Stream listeners, called on XMPP-level events
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 _stream_opened: function (attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 this.debug("STREAM: opened.");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 this._setStatus(xmpp.Status.AUTHENTICATING);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 var handshake = sha1.hex(attr.id + this.password);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 this.debug("Sending authentication token...");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 this.send("<handshake>"+handshake+"</handshake>");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 },
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 _handle_stanza: function (stanza)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 {
4
67b1d93509d3 Strip default stream namespace from stanza objects
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
226 if(!stanza.attr.xmlns) // Default namespace
0
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 if(stanza.name == "handshake")
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 this._setStatus(xmpp.Status.CONNECTED);
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 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 this.debug("STANZA: "+stanza.toString());
7
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
234
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
235 // Match and call handlers
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
236 var removeHandlers = [];
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
237 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
238 {
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
239 var handler = this.handlers[i];
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
240 if(
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
241 (!handler.name || handler.name == stanza.name) &&
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
242 (!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
243 || (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
244 (!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
245 (!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
246 (!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
247 (!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
248 )
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
249 {
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
250 var ret = handler.callback(stanza);
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
251 if(ret == false)
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
252 removeHandlers.push(i);
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
253 }
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
254 }
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
255
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
256 var adjust = 0;
394b0c8cad04 Match and fire handler callbacks for incoming stanzas
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
257 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
258 this.handlers.splice(removeHandlers[i]-(adjust++), 1);
0
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 _stream_closed: function ()
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 this.debug("STREAM: closed.");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 this.socket.close();
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 if(this.status == xmpp.Status.CONNECTING)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 this._setStatus(xmpp.status.CONNFAIL);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 else
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 this._setStatus(xmpp.Status.DISCONNECTED);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 },
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 _stream_error: function (condition)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 this._setStatus(xmpp.Status.ERROR, condition);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 // Logging
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 log: function (level, message) {},
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 debug: function (message) { return this.log(xmpp.LogLevel.DEBUG, message); },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 info: function (message) { return this.log(xmpp.LogLevel.INFO , message); },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 warn: function (message) { return this.log(xmpp.LogLevel.WARN , message); },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 error: function (message) { return this.log(xmpp.LogLevel.ERROR, message); },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 fatal: function (message) { return this.log(xmpp.LogLevel.FATAL, message); }
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 };
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 function xmlescape(s)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 return s.replace(/&/g, "&amp;")
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 .replace(/</g, "&lt;")
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 .replace(/>/g, "&gt;")
1
065b90eb8b57 Escape &apos;
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
291 .replace(/\"/g, "&quot;")
065b90eb8b57 Escape &apos;
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
292 .replace(/\'/g, "&apos;");
0
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
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 /** StanzaBuilder: Helps create and manipulate XML snippets **/
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 xmpp.StanzaBuilder = function (name, attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 this.name = name;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 this.attr = attr || {};
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 this.tags = [];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 this.children = [];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 this.last_node = [this];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 return this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 };
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 xmpp.StanzaBuilder.prototype = {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 c: function (name, attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 var s = new xmpp.StanzaBuilder(name, attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 var parent = this.last_node[this.last_node.length-1];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 parent.tags.push(s);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 parent.children.push(s);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 this.last_node.push(s);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 return this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 t: function (text)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 var parent = this.last_node[this.last_node.length-1];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 parent.children.push(text);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 return this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 up: function ()
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 this.last_node.pop();
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 return this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 },
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 toString: function (top_tag_only)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 var buf = [];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 buf.push("<" + this.name);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 for(var attr in this.attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 buf.push(" " + attr + "='" + xmlescape(this.attr[attr]) + "'");
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
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 // Now add children if wanted
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340 if(top_tag_only)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 buf.push(">");
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 else if(this.children.length == 0)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 buf.push("/>");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 else
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 buf.push(">");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 for(var i = 0; i<this.children.length; i++)
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 child = this.children[i];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 if(typeof(child) == "string")
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 buf.push(xmlescape(child));
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 else
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 buf.push(child.toString());
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 buf.push("</" + this.name + ">");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 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
362 },
b88bcbbe08e1 Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
363
b88bcbbe08e1 Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
364 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
365 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
366 {
b88bcbbe08e1 Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
367 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
368 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
369 return child;
b88bcbbe08e1 Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
370 }
b88bcbbe08e1 Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
371 return null;
b88bcbbe08e1 Add stanza.getChild(name, xmlns) method to mirror Prosody's API
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
372 },
3
2d83fe899f5f Add stanza.getText() to retrieve all text nodes joined as a string
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
373
2d83fe899f5f Add stanza.getText() to retrieve all text nodes joined as a string
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
374 getText: function () {
2d83fe899f5f Add stanza.getText() to retrieve all text nodes joined as a string
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
375 var buf = [];
2d83fe899f5f Add stanza.getText() to retrieve all text nodes joined as a string
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
376 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
377 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
378 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
379 return buf.join("");
9
c4ff2c2fea6d Add stanza.getAttribute() method
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
380 },
c4ff2c2fea6d Add stanza.getAttribute() method
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
381
c4ff2c2fea6d Add stanza.getAttribute() method
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
382 getAttribute: function (name) {
c4ff2c2fea6d Add stanza.getAttribute() method
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
383 return this.attr[name] || null;
0
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
387 xmpp.stanza = function (name, attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
389 return new xmpp.StanzaBuilder(name, attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
390 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
391
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
392 xmpp.message = function (attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
393 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
394 return xmpp.stanza("message", attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
395 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
397 xmpp.presence = function (attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
398 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
399 return xmpp.stanza("presence", attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
400 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
401
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
402 xmpp.iq = function (attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
403 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
404 return xmpp.stanza("iq", attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
405 }

mercurial