1 // ------------------------------------------------------------------
2 // Houses the jabber transport code
4 // 1. jabber_connection - high level jabber component
5 // 2. jabber_message - message class
6 // 3. jabber_socket - socket handling code (shouldn't have to
7 // use this class directly)
9 // Requires oils_utils.js
10 // ------------------------------------------------------------------
16 // ------------------------------------------------------------------
18 // High level transport code
20 // ------------------------------------------------------------------
22 // ------------------------------------------------------------------
23 jabber_connection.prototype = new transport_connection();
24 jabber_connection.prototype.constructor = jabber_connection;
25 jabber_connection.baseClass = transport_connection.prototype.constructor;
27 /** Initializes a jabber_connection object */
28 function jabber_connection( username, password, resource ) {
30 this.username = username;
31 this.password = password;
32 this.resource = resource;
33 this.socket = new jabber_socket();
39 /** Connects to the Jabber server. 'timeout' is the connect timeout
42 jabber_connection.prototype.connect = function( host, port, timeout ) {
44 return this.socket.connect(
45 this.username, this.password, this.resource, host, port, timeout );
48 /** Sends a message to 'recipient' with the provided message
51 jabber_connection.prototype.send = function( recipient, thread, body ) {
52 var jid = this.username+"@"+this.host+"/"+this.resource;
53 var msg = new jabber_message( jid, recipient, thread, body );
54 return this.socket.tcp_send( msg.to_string() );
57 /** This method will wait at most 'timeout' milliseconds
58 * for a Jabber message to arrive. If one arrives
59 * it is returned to the caller, other it returns null
61 jabber_connection.prototype.recv = function( timeout ) {
62 return this.socket.recv( timeout );
65 /** Disconnects from the jabber server */
66 jabber_connection.prototype.disconnect = function() {
67 return this.socket.disconnect();
70 /** Returns true if we are currently connected to the
73 jabber_connection.prototype.connected = function() {
74 return this.socket.connected();
79 // ------------------------------------------------------------------
81 // High level message handling code
84 jabber_message.prototype = new transport_message();
85 jabber_message.prototype.constructor = jabber_message;
86 jabber_message.prototype.baseClass = transport_message.prototype.constructor;
88 /** Builds a jabber_message object */
89 function jabber_message( sender, recipient, thread, body ) {
91 if( sender == null || recipient == null || recipient.length < 1 ) { return; }
93 this.doc = new DOMParser().parseFromString("<message></message>", "text/xml");
94 this.root = this.doc.documentElement;
95 this.root.setAttribute( "from", sender );
96 this.root.setAttribute( "to", recipient );
98 var body_node = this.doc.createElement("body");
99 body_node.appendChild( this.doc.createTextNode( body ) );
101 var thread_node = this.doc.createElement("thread");
102 thread_node.appendChild( this.doc.createTextNode( thread ) );
104 this.root.appendChild( body_node );
105 this.root.appendChild( thread_node );
109 /** Builds a new message from raw xml.
110 * If the message is a Jabber error message, then msg.is_error_msg
113 jabber_message.prototype.from_xml = function( xml ) {
114 var msg = new jabber_message();
115 msg.doc = new DOMParser().parseFromString( xml, "text/xml" );
116 msg.root = msg.doc.documentElement;
118 if( msg.root.getAttribute( "type" ) == "error" ) {
119 msg.is_error_msg = true;
121 this.is_error_msg = false;
127 /** Returns the 'from' field of the message */
128 jabber_message.prototype.get_sender = function() {
129 return this.root.getAttribute( "from" );
132 /** Returns the jabber thread */
133 jabber_message.prototype.get_thread = function() {
134 var nodes = this.root.getElementsByTagName( "thread" );
135 var thread_node = nodes.item(0);
136 return thread_node.firstChild.nodeValue;
139 /** Returns the message body */
140 jabber_message.prototype.get_body = function() {
141 var nodes = this.root.getElementsByTagName( "body" );
142 var body_node = nodes.item(0);
143 new Logger().transport( "Get Body returning:\n" + body_node.textContent, Logger.DEBUG );
144 return body_node.textContent;
147 /** Returns the message as a whole as an XML string */
148 jabber_message.prototype.to_string = function() {
149 return new XMLSerializer().serializeToString(this.root);
155 // ------------------------------------------------------------------
158 /** Initializes a new jabber_socket object */
159 function jabber_socket() {
161 this.is_connected = false;
169 /** Connects to the jabber server */
170 jabber_socket.prototype.connect =
171 function( username, password, resource, host, port, timeout ) {
173 var starttime = new Date().getTime();
175 // there has to be at least some kind of timeout
176 if( ! timeout || timeout < 100 ) { timeout = 1000; }
180 this.xpcom_init( host, port );
181 this.tcp_send( "<stream:stream to='"+host
182 +"' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>" );
184 if( !this.tcp_recv( timeout ) ) { throw 1; }
187 throw new oils_ex_transport( "Could not open a socket to the transport server\n"
188 + "Server: " + host + " Port: " + port );
191 // Send the auth packet
192 this.tcp_send( "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'><username>"
193 + username + "</username><password>" + password +
194 "</password><resource>" + resource + "</resource></query></iq>" );
196 var cur = new Date().getTime();
197 var remaining = timeout - ( cur - starttime );
198 this.tcp_recv( remaining );
200 if( ! this.connected() ) {
201 throw new oils_ex_transport( "Connection to transport server timed out" );
210 /** Sets up all of the xpcom components */
211 jabber_socket.prototype.xpcom_init = function( host, port ) {
213 var transportService =
214 Components.classes["@mozilla.org/network/socket-transport-service;1"]
215 .getService(Components.interfaces.nsISocketTransportService);
217 this.transport = transportService.createTransport( null, 0, host, port, null);
219 // ------------------------------------------------------------------
220 // Build the stream objects
221 // ------------------------------------------------------------------
222 this.outstream = this.transport.openOutputStream(0,0,0);
224 var stream = this.transport.openInputStream(0,0,0);
226 this.instream = Components.classes["@mozilla.org/scriptableinputstream;1"]
227 .createInstance(Components.interfaces.nsIScriptableInputStream);
229 this.instream.init(stream);
233 /** Send data to the TCP pipe */
234 jabber_socket.prototype.tcp_send = function( data ) {
235 new Logger().transport( "Sending Data: \n" + data, Logger.INFO );
236 this.outstream.write(data,data.length);
240 /** Accepts data coming directly from the socket. If we're not
241 * connected, we pass it off to procecc_connect(). Otherwise,
242 * this method adds the data to the local buffer.
244 jabber_socket.prototype.process_data = function( data ) {
246 new Logger().transport( "Received TCP data: " + data, Logger.DEBUG );
248 if( ! this.connected() ) {
249 this.process_connect( data );
257 /** Processes connect data to verify we are logged in correctly */
258 jabber_socket.prototype.process_connect = function( data ) {
260 var reg = /type=["\']result["\']/;
263 if( reg.exec( data ) ) {
264 this.is_connected = true;
266 if( err.exec( data ) ) {
267 //throw new oils_ex_transport( "Server returned: \n" + data );
268 throw new oils_ex_jabber_auth( "Server returned: \n" + data );
269 // Throw exception, return something...
274 /** Waits up to at most 'timeout' milliseconds for data to arrive
275 * in the TCP buffer. If there is at least one byte of data
276 * in the buffer, then all of the data that is in the buffer is sent
277 * to the process_data method for processing and the method returns.
279 jabber_socket.prototype.tcp_recv = function( timeout ) {
281 var count = this.instream.available();
282 var did_receive = false;
284 // ------------------------------------------------------------------
285 // If there is any data in the tcp buffer, process it and return
286 // ------------------------------------------------------------------
291 new Logger().transport(
292 "before process data", Logger.DEBUG );
294 this.process_data( this.instream.read( count ) );
296 new Logger().transport(
297 "after process data", Logger.DEBUG );
299 count = this.instream.available();
301 new Logger().transport(
302 "received " + count + " bytes" , Logger.DEBUG );
307 // ------------------------------------------------------------------
308 // Do the timeout dance
309 // ------------------------------------------------------------------
311 // ------------------------------------------------------------------
312 // If there is no data in the buffer, wait up to timeout seconds
313 // for some data to arrive. Once it arrives, suck it all out
314 // and send it on for processing
315 // ------------------------------------------------------------------
318 now = new Date().getTime();
321 // ------------------------------------------------------------------
322 // Loop and poll for data every 50 ms.
323 // ------------------------------------------------------------------
324 while( ((now-then) <= timeout) && count <= 0 ) {
326 count = this.instream.available();
327 now = new Date().getTime();
330 // ------------------------------------------------------------------
331 // if we finally get some data, process it.
332 // ------------------------------------------------------------------
336 while( count > 0 ) { // pull in all of the data there is
337 this.process_data( this.instream.read( count ) );
338 count = this.instream.available();
347 /** If a message is already sitting in the queue, it is returned.
348 * If not, this method waits till at most 'timeout' milliseconds
349 * for a full jabber message to arrive and then returns that.
350 * If none ever arrives, returns null.
352 jabber_socket.prototype.recv = function( timeout ) {
355 now = new Date().getTime();
358 var first_pass = true;
359 while( ((now-then) <= timeout) ) {
361 if( this.buffer.length == 0 || !first_pass ) {
362 if( ! this.tcp_recv( timeout ) ) {
368 //new Logger().transport( "\n\nTCP Buffer Before: \n" + this.buffer, Logger.DEBUG );
370 var buf = this.buffer;
373 new Logger().transport( "CURRENT BUFFER\n" + buf,
376 buf = buf.replace( /\n/g, '' ); // remove pesky newlines
378 var reg = /<message.*?>.*?<\/message>/;
379 var iqr = /<iq.*?>.*?<\/iq>/;
380 var out = reg.exec(buf);
384 var msg_xml = out[0];
385 this.buffer = buf.substring( msg_xml.length, buf.length );
386 new Logger().transport( "Building Jabber message\n\n" + msg_xml, Logger.DEBUG );
387 var jab_msg = new jabber_message().from_xml( msg_xml );
388 if( jab_msg.is_error_msg ) {
389 new Logger().transport( "Received Jabber error message \n\n" + msg_xml, Logger.ERROR );
400 var msg_xml = out[0];
401 this.buffer = buf.substring( msg_xml.length, buf.length );
402 process_iq_data( msg_xml );
410 now = new Date().getTime();
416 jabber_socket.prototype.process_iq_data = function( data ) {
417 new Logger().transport( "IQ Packet received... Not Implemented\n" + data, Logger.ERROR );
420 /** Disconnects from the jabber server and closes down shop */
421 jabber_socket.prototype.disconnect = function() {
422 this.tcp_send( "</stream:stream>" );
423 this.instream.close();
424 this.outstream.close();
427 /** True if connected */
428 jabber_socket.prototype.connected = function() {
429 return this.is_connected;