xmpp.js

Tue, 02 Feb 2010 23:31:00 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 02 Feb 2010 23:31:00 +0000
changeset 0
0da83180e975
child 1
065b90eb8b57
permissions
-rw-r--r--

Initial commit, hello world!

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 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 var attr = {xmlns:uri};
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 for(var i=0;i<attr_arr.length;i++)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 attr[attr_arr[i][0]] = attr_arr[i][1];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 for(var i=0;i<namespaces.length;i++)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 if(namespaces[i][0].length > 0)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 attr["xmlns:"+namespaces[i][0]] = namespaces[i][1];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if(!stanza)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 if(stream.opened)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 stanza = xmpp.stanza(tagname, attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 else if(tagname == "stream" && uri == xmpp.xmlns.streams)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 stream.opened = true;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 callbacks.opened(attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 else
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 callbacks.error("no-stream");
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 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 else
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 stanza.c(tagname, attr);
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
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 cb.onEndElementNS(function(tagname) {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 if(stanza)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 if(stanza.last_node.length == 1)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 callbacks.stanza(stanza);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 stanza = null;
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 else
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 stanza.up();
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 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 stream.opened = false;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 callbacks.closed();
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 }
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
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 cb.onCharacters(function(chars) {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 if(stanza)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 stanza.t(chars);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 });
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 });
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 this.data = function (data)
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 return this.parser.parseString(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
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 return this;
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
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 /** Connection: Takes host/port, manages stream **/
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 xmpp.Connection = function (host, port)
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 this.host = host || "localhost";
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 this.port = port || 5347;
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 this.socket = tcp.createConnection();
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.stream = new xmpp.Stream({
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 opened: recontext(this, this._stream_opened),
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 stanza: recontext(this, this._handle_stanza),
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 closed: recontext(this, this._stream_closed)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 });
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 return this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 };
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 exports.Connection.prototype = {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 connect: function (jid, pass, callback)
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 this.jid = jid;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 this.password = pass;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 this.connect_callback = 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 var conn = this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 this.socket.addListener("connect", recontext(this, conn._socket_connected));
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 this.socket.addListener("disconnect", recontext(this, conn._socket_disconnected));
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 this.socket.addListener("receive", recontext(this, conn._socket_received));
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 // Connect TCP socket
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 this.socket.connect(this.port, this.host);
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 this._setStatus(xmpp.Status.CONNECTING);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 send: function (data)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 this.debug("SND: "+data);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 this.socket.send(data.toString());
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 // Update the status of the connection, call connect_callback
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 _setStatus: function (status, condition)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 this.status = status;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 this.connect_callback(status, condition);
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 // Socket listeners, called on TCP-level events
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 _socket_connected: function ()
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 this.info("CONNECTED.");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 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
158 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 _socket_disconnected: function (had_error)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 if(this.status == xmpp.Status.CONNECTING)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 this._setStatus(xmpp.Status.CONNFAIL);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 elseif(this.status == xmpp.Status.CONNECTED)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 this._setStatus(xmpp.Status.DISCONNECTED);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 this.info("DISCONNECTED.");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 _socket_received: function (data)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 this.debug("RCV: "+data);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 // Push to parser
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 this.stream.data(data);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 // Stream listeners, called on XMPP-level events
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 _stream_opened: function (attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 this.debug("STREAM: opened.");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 this._setStatus(xmpp.Status.AUTHENTICATING);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 var handshake = sha1.hex(attr.id + this.password);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 this.debug("Sending authentication token...");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 this.send("<handshake>"+handshake+"</handshake>");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 _handle_stanza: function (stanza)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 if(stanza.attr.xmlns == xmpp.xmlns.component_accept)
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 if(stanza.name == "handshake")
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 this._setStatus(xmpp.Status.CONNECTED);
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 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 this.debug("STANZA: "+stanza.toString());
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 _stream_closed: function ()
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 this.debug("STREAM: closed.");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 this.socket.close();
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 if(this.status == xmpp.Status.CONNECTING)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 this._setStatus(xmpp.status.CONNFAIL);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 else
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 this._setStatus(xmpp.Status.DISCONNECTED);
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
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 _stream_error: function (condition)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 this._setStatus(xmpp.Status.ERROR, condition);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 },
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 // Logging
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 log: function (level, message) {},
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 debug: function (message) { return this.log(xmpp.LogLevel.DEBUG, message); },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 info: function (message) { return this.log(xmpp.LogLevel.INFO , message); },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 warn: function (message) { return this.log(xmpp.LogLevel.WARN , message); },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 error: function (message) { return this.log(xmpp.LogLevel.ERROR, message); },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 fatal: function (message) { return this.log(xmpp.LogLevel.FATAL, message); }
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 };
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 function xmlescape(s)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 return s.replace(/&/g, "&amp;")
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 .replace(/</g, "&lt;")
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 .replace(/>/g, "&gt;")
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 .replace(/\"/g, "&quot;");
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
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 /** StanzaBuilder: Helps create and manipulate XML snippets **/
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 xmpp.StanzaBuilder = function (name, attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 this.name = name;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 this.attr = attr || {};
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 this.tags = [];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 this.children = [];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 this.last_node = [this];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 return this;
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
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 xmpp.StanzaBuilder.prototype = {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 c: function (name, attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 var s = new xmpp.StanzaBuilder(name, attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 var parent = this.last_node[this.last_node.length-1];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 parent.tags.push(s);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 parent.children.push(s);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 this.last_node.push(s);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 return this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 t: function (text)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 var parent = this.last_node[this.last_node.length-1];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 parent.children.push(text);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 return this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 },
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 up: function ()
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 this.last_node.pop();
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 return this;
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 },
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 toString: function (top_tag_only)
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 var buf = [];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 buf.push("<" + this.name);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 for(var attr in this.attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 buf.push(" " + attr + "='" + xmlescape(this.attr[attr]) + "'");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 }
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 // Now add children if wanted
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 if(top_tag_only)
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 buf.push(">");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 else if(this.children.length == 0)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 buf.push("/>");
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 else
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 buf.push(">");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 for(var i = 0; i<this.children.length; i++)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 var child = this.children[i];
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 if(typeof(child) == "string")
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 buf.push(xmlescape(child));
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 else
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 buf.push(child.toString());
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 buf.push("</" + this.name + ">");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 return buf.join("");
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 xmpp.stanza = function (name, attr)
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 return new xmpp.StanzaBuilder(name, attr);
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.message = function (attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 return xmpp.stanza("message", attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 }
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 xmpp.presence = function (attr)
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 return xmpp.stanza("presence", attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 }
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 xmpp.iq = function (attr)
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 {
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 return xmpp.stanza("iq", attr);
0da83180e975 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 }

mercurial