4 * XXX For reference only until this code is updated to match new opensrf.js layout XXX
10 // ------------------------------------------------------------------
11 // Houses the jabber transport code
13 // 1. jabber_connection - high level jabber component
14 // 2. jabber_message - message class
15 // 3. jabber_socket - socket handling code (shouldn't have to
16 // use this class directly)
18 // Requires oils_utils.js
19 // ------------------------------------------------------------------
25 // ------------------------------------------------------------------
27 // High level transport code
29 // ------------------------------------------------------------------
31 // ------------------------------------------------------------------
32 jabber_connection.prototype = new transport_connection();
33 jabber_connection.prototype.constructor = jabber_connection;
34 jabber_connection.baseClass = transport_connection.prototype.constructor;
36 /** Initializes a jabber_connection object */
37 function jabber_connection( username, password, resource ) {
39 this.username = username;
40 this.password = password;
41 this.resource = resource;
42 this.socket = new jabber_socket();
48 /** Connects to the Jabber server. 'timeout' is the connect timeout
51 jabber_connection.prototype.connect = function( host, port, timeout ) {
53 return this.socket.connect(
54 this.username, this.password, this.resource, host, port, timeout );
57 /** Sends a message to 'recipient' with the provided message
60 jabber_connection.prototype.send = function( recipient, thread, body ) {
61 var jid = this.username+"@"+this.host+"/"+this.resource;
62 var msg = new jabber_message( jid, recipient, thread, body );
63 return this.socket.tcp_send( msg.to_string() );
66 /** This method will wait at most 'timeout' milliseconds
67 * for a Jabber message to arrive. If one arrives
68 * it is returned to the caller, other it returns null
70 jabber_connection.prototype.recv = function( timeout ) {
71 return this.socket.recv( timeout );
74 /** Disconnects from the jabber server */
75 jabber_connection.prototype.disconnect = function() {
76 return this.socket.disconnect();
79 /** Returns true if we are currently connected to the
82 jabber_connection.prototype.connected = function() {
83 return this.socket.connected();
88 // ------------------------------------------------------------------
90 // High level message handling code
93 jabber_message.prototype = new transport_message();
94 jabber_message.prototype.constructor = jabber_message;
95 jabber_message.prototype.baseClass = transport_message.prototype.constructor;
97 /** Builds a jabber_message object */
98 function jabber_message( sender, recipient, thread, body ) {
100 if( sender == null || recipient == null || recipient.length < 1 ) { return; }
102 this.doc = new DOMParser().parseFromString("<message></message>", "text/xml");
103 this.root = this.doc.documentElement;
104 this.root.setAttribute( "from", sender );
105 this.root.setAttribute( "to", recipient );
107 var body_node = this.doc.createElement("body");
108 body_node.appendChild( this.doc.createTextNode( body ) );
110 var thread_node = this.doc.createElement("thread");
111 thread_node.appendChild( this.doc.createTextNode( thread ) );
113 this.root.appendChild( body_node );
114 this.root.appendChild( thread_node );
118 /** Builds a new message from raw xml.
119 * If the message is a Jabber error message, then msg.is_error_msg
122 jabber_message.prototype.from_xml = function( xml ) {
123 var msg = new jabber_message();
124 msg.doc = new DOMParser().parseFromString( xml, "text/xml" );
125 msg.root = msg.doc.documentElement;
127 if( msg.root.getAttribute( "type" ) == "error" ) {
128 msg.is_error_msg = true;
130 this.is_error_msg = false;
136 /** Returns the 'from' field of the message */
137 jabber_message.prototype.get_sender = function() {
138 return this.root.getAttribute( "from" );
141 /** Returns the jabber thread */
142 jabber_message.prototype.get_thread = function() {
143 var nodes = this.root.getElementsByTagName( "thread" );
144 var thread_node = nodes.item(0);
145 return thread_node.firstChild.nodeValue;
148 /** Returns the message body */
149 jabber_message.prototype.get_body = function() {
150 var nodes = this.root.getElementsByTagName( "body" );
151 var body_node = nodes.item(0);
152 new Logger().transport( "Get Body returning:\n" + body_node.textContent, Logger.DEBUG );
153 return body_node.textContent;
156 /** Returns the message as a whole as an XML string */
157 jabber_message.prototype.to_string = function() {
158 return new XMLSerializer().serializeToString(this.root);
164 // ------------------------------------------------------------------
167 /** Initializes a new jabber_socket object */
168 function jabber_socket() {
170 this.is_connected = false;
178 /** Connects to the jabber server */
179 jabber_socket.prototype.connect =
180 function( username, password, resource, host, port, timeout ) {
182 var starttime = new Date().getTime();
184 // there has to be at least some kind of timeout
185 if( ! timeout || timeout < 100 ) { timeout = 1000; }
189 this.xpcom_init( host, port );
190 this.tcp_send( "<stream:stream to='"+host
191 +"' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>" );
193 if( !this.tcp_recv( timeout ) ) { throw 1; }
196 throw new oils_ex_transport( "Could not open a socket to the transport server\n"
197 + "Server: " + host + " Port: " + port );
200 // Send the auth packet
201 this.tcp_send( "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'><username>"
202 + username + "</username><password>" + password +
203 "</password><resource>" + resource + "</resource></query></iq>" );
205 var cur = new Date().getTime();
206 var remaining = timeout - ( cur - starttime );
207 this.tcp_recv( remaining );
209 if( ! this.connected() ) {
210 throw new oils_ex_transport( "Connection to transport server timed out" );
219 /** Sets up all of the xpcom components */
220 jabber_socket.prototype.xpcom_init = function( host, port ) {
222 var transportService =
223 Components.classes["@mozilla.org/network/socket-transport-service;1"]
224 .getService(Components.interfaces.nsISocketTransportService);
226 this.transport = transportService.createTransport( null, 0, host, port, null);
228 // ------------------------------------------------------------------
229 // Build the stream objects
230 // ------------------------------------------------------------------
231 this.outstream = this.transport.openOutputStream(0,0,0);
233 var stream = this.transport.openInputStream(0,0,0);
235 this.instream = Components.classes["@mozilla.org/scriptableinputstream;1"]
236 .createInstance(Components.interfaces.nsIScriptableInputStream);
238 this.instream.init(stream);
242 /** Send data to the TCP pipe */
243 jabber_socket.prototype.tcp_send = function( data ) {
244 new Logger().transport( "Sending Data: \n" + data, Logger.INFO );
245 this.outstream.write(data,data.length);
249 /** Accepts data coming directly from the socket. If we're not
250 * connected, we pass it off to procecc_connect(). Otherwise,
251 * this method adds the data to the local buffer.
253 jabber_socket.prototype.process_data = function( data ) {
255 new Logger().transport( "Received TCP data: " + data, Logger.DEBUG );
257 if( ! this.connected() ) {
258 this.process_connect( data );
266 /** Processes connect data to verify we are logged in correctly */
267 jabber_socket.prototype.process_connect = function( data ) {
269 var reg = /type=["\']result["\']/;
272 if( reg.exec( data ) ) {
273 this.is_connected = true;
275 if( err.exec( data ) ) {
276 //throw new oils_ex_transport( "Server returned: \n" + data );
277 throw new oils_ex_jabber_auth( "Server returned: \n" + data );
278 // Throw exception, return something...
283 /** Waits up to at most 'timeout' milliseconds for data to arrive
284 * in the TCP buffer. If there is at least one byte of data
285 * in the buffer, then all of the data that is in the buffer is sent
286 * to the process_data method for processing and the method returns.
288 jabber_socket.prototype.tcp_recv = function( timeout ) {
290 var count = this.instream.available();
291 var did_receive = false;
293 // ------------------------------------------------------------------
294 // If there is any data in the tcp buffer, process it and return
295 // ------------------------------------------------------------------
300 new Logger().transport(
301 "before process data", Logger.DEBUG );
303 this.process_data( this.instream.read( count ) );
305 new Logger().transport(
306 "after process data", Logger.DEBUG );
308 count = this.instream.available();
310 new Logger().transport(
311 "received " + count + " bytes" , Logger.DEBUG );
316 // ------------------------------------------------------------------
317 // Do the timeout dance
318 // ------------------------------------------------------------------
320 // ------------------------------------------------------------------
321 // If there is no data in the buffer, wait up to timeout seconds
322 // for some data to arrive. Once it arrives, suck it all out
323 // and send it on for processing
324 // ------------------------------------------------------------------
327 now = new Date().getTime();
330 // ------------------------------------------------------------------
331 // Loop and poll for data every 50 ms.
332 // ------------------------------------------------------------------
333 while( ((now-then) <= timeout) && count <= 0 ) {
335 count = this.instream.available();
336 now = new Date().getTime();
339 // ------------------------------------------------------------------
340 // if we finally get some data, process it.
341 // ------------------------------------------------------------------
345 while( count > 0 ) { // pull in all of the data there is
346 this.process_data( this.instream.read( count ) );
347 count = this.instream.available();
356 /** If a message is already sitting in the queue, it is returned.
357 * If not, this method waits till at most 'timeout' milliseconds
358 * for a full jabber message to arrive and then returns that.
359 * If none ever arrives, returns null.
361 jabber_socket.prototype.recv = function( timeout ) {
364 now = new Date().getTime();
367 var first_pass = true;
368 while( ((now-then) <= timeout) ) {
370 if( this.buffer.length == 0 || !first_pass ) {
371 if( ! this.tcp_recv( timeout ) ) {
377 //new Logger().transport( "\n\nTCP Buffer Before: \n" + this.buffer, Logger.DEBUG );
379 var buf = this.buffer;
382 new Logger().transport( "CURRENT BUFFER\n" + buf,
385 buf = buf.replace( /\n/g, '' ); // remove pesky newlines
387 var reg = /<message.*?>.*?<\/message>/;
388 var iqr = /<iq.*?>.*?<\/iq>/;
389 var out = reg.exec(buf);
393 var msg_xml = out[0];
394 this.buffer = buf.substring( msg_xml.length, buf.length );
395 new Logger().transport( "Building Jabber message\n\n" + msg_xml, Logger.DEBUG );
396 var jab_msg = new jabber_message().from_xml( msg_xml );
397 if( jab_msg.is_error_msg ) {
398 new Logger().transport( "Received Jabber error message \n\n" + msg_xml, Logger.ERROR );
409 var msg_xml = out[0];
410 this.buffer = buf.substring( msg_xml.length, buf.length );
411 process_iq_data( msg_xml );
419 now = new Date().getTime();
425 jabber_socket.prototype.process_iq_data = function( data ) {
426 new Logger().transport( "IQ Packet received... Not Implemented\n" + data, Logger.ERROR );
429 /** Disconnects from the jabber server and closes down shop */
430 jabber_socket.prototype.disconnect = function() {
431 this.tcp_send( "</stream:stream>" );
432 this.instream.close();
433 this.outstream.close();
436 /** True if connected */
437 jabber_socket.prototype.connected = function() {
438 return this.is_connected;