1 package OpenSRF::Transport::SlimJabber::Client;
2 use strict; use warnings;
4 use base qw( OpenSRF );
5 use OpenSRF::Utils::Logger qw(:level);
6 use OpenSRF::Utils::Config;
7 use Time::HiRes qw(ualarm);
8 use OpenSRF::Utils::Config;
10 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
16 OpenSRF::Transport::SlimJabber::Client
18 Home-brewed slimmed down jabber connection agent. Supports SSL connections
19 with a config file options:
21 transport->server->sslport # the ssl port
22 transport->server->ssl # is this ssl?
26 my $logger = "OpenSRF::Utils::Logger";
35 my $socket = $self->{_socket};
36 if( $socket and $socket->connected() ) {
37 print $socket "</stream:stream>";
45 Creates a new Client object.
47 debug and log_file are not required if you don't care to log the activity,
48 however all other parameters are.
62 my( $class, %params ) = @_;
64 $class = ref( $class ) || $class;
66 my $port = $params{'port'} || return undef;
67 my $username = $params{'username'} || return undef;
68 my $resource = $params{'resource'} || return undef;
69 my $password = $params{'password'} || return undef;
70 my $host = $params{'host'} || return undef;
72 my $jid = "$username\@$host\/$resource";
74 my $self = bless {} => $class;
79 $self->username( $username );
80 $self->resource( $resource );
81 $self->password( $password );
82 $self->{temp_buffer} = "";
84 $logger->transport( "Creating Client instance: $host:$port, $username, $resource",
90 # clears the tmp buffer as well as the TCP buffer
94 $self->{temp_buffer} = "";
96 my $fh = $self->{_socket};
99 while( sysread( $fh, $t_buf, 4096 ) ) {}
102 # -------------------------------------------------
106 Gathers all Jabber messages sitting in the collection queue
107 and hands them each to their respective callbacks. This call
108 does not block (calls Process(0))
112 sub gather { my $self = shift; $self->process( 0 ); }
114 # -------------------------------------------------
118 Blocks and gathers incoming messages as they arrive. Does not return
119 unless an error occurs.
121 Throws an OpenSRF::EX::JabberException if the call to Process ever fails.
127 my $sock = $self->unix_sock();
128 my $socket = IO::Socket::UNIX->new( Peer => $sock );
129 $logger->transport( "Unix Socket opened by Listener", INTERNAL );
131 throw OpenSRF::EX::Socket( "Unable to connect to UnixServer: socket-file: $sock \n :=> $! " )
132 unless ($socket->connected);
135 my $o = $self->process( -1 );
136 $logger->transport( "Call to process() in listener returned:\n $o", INTERNAL );
137 if( ! defined( $o ) ) {
138 throw OpenSRF::EX::Jabber( "Listen Loop failed at 'process()'" );
143 throw OpenSRF::EX::Socket( "How did we get here?!?!" );
148 my $flags = fcntl($fh, F_GETFL, 0)
149 or die "Can't get flags for the socket: $!\n";
151 $logger->transport( "Setting NONBLOCK: original flags: $flags", INTERNAL );
153 fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
154 or die "Can't set flags for the socket: $!\n";
162 $logger->transport( "Restoring BLOCK: to flags $flags", INTERNAL );
163 fcntl($fh, F_SETFL, $flags) if defined $flags;
169 my $flags = fcntl($fh, F_GETFL, 0)
170 or die "Can't get flags for the socket: $!\n";
172 $flags &= ~O_NONBLOCK;
174 fcntl($fh, F_SETFL, $flags)
175 or die "Can't set flags for the socket: $!\n";
180 my ($self, $timeout) = @_;
182 $logger->transport( "Temp Buffer Contained: \n". $self->{temp_buffer}, INTERNAL) if $self->{temp_buffer};
183 if( $self->can( "app" ) ) {
184 $logger->transport( "timed_read called for ".$self->app.", I am: ".$self->jid, INTERNAL );
187 # See if there is a complete message in the temp_buffer
189 if( $self->{temp_buffer} ) {
190 my $buffer = $self->{temp_buffer};
192 $self->{temp_buffer} = '';
194 my ($tag) = ($buffer =~ /<([^\s\?\>]+)/o);
195 $logger->transport("Using tag: $tag ", INTERNAL);
197 if ( $buffer =~ /^(.*?<\/$tag>)(.*)/s) {
199 $self->{temp_buffer} = $2;
201 $logger->transport( "completed read with $buffer", INTERNAL );
202 } elsif ( $buffer =~ /^<$tag[^>]*?\/>(.*)/) {
203 $self->{temp_buffer} = $1;
205 $logger->transport( "completed read with $buffer", INTERNAL );
207 $self->{temp_buffer} = $buffer;
210 if( $buffer and $complete ) {
217 my $fh = $self->{_socket};
219 unless( $fh and $fh->connected ) {
220 throw OpenSRF::EX::Socket ("Attempted read on closed socket", ERROR );
223 $logger->transport( "Temp Buffer After first attempt: \n ".$self->{temp_buffer}, INTERNAL) if $self->{temp_buffer};
226 if (defined($timeout) && !$timeout) {
227 $flags = set_nonblock( $fh );
231 $logger->transport( "Calling timed_read with timetout $timeout", INTERNAL );
241 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
243 # alarm needs a number greater => 1.
244 my $alarm_timeout = $timeout;
245 if( $alarm_timeout > 0 and $alarm_timeout < 1 ) {
248 alarm $alarm_timeout;
251 my $buffer = $self->{temp_buffer};
252 $self->{temp_buffer} = '';
255 my $ff = fcntl($fh, F_GETFL, 0);
256 if ($ff == ($ff | O_NONBLOCK) and $timeout > 0 ) {
257 #throw OpenSRF::EX::ERROR ("File flags are set to NONBLOCK but timeout is $timeout", ERROR );
261 my $read_size = 1024;
263 while( my $n = sysread( $fh, $t_buf, $read_size ) ) {
265 if( $n < $read_size ) {
266 #reset_fl( $fh, $f ) if $f;
270 # see if there is any more data to grab...
271 $f = set_nonblock( $fh );
274 #sysread($fh, $buffer, 2048, length($buffer) );
275 #sysread( $fh, $t_buf, 2048 );
279 $logger->transport(" Got [$buffer] from the socket", INTERNAL);
282 $logger->transport(" First read Buffer\n [$buffer]", INTERNAL);
283 ($tag) = ($buffer =~ /<([^\s\?\>]+){1}/o);
285 $logger->transport("Using tag: $tag ", INTERNAL);
288 if (!$first_read && $buffer =~ /^(.*?<\/$tag>){1}(.*)/s) {
290 $self->{temp_buffer} = $2;
292 $logger->transport( "completed read with $buffer", INTERNAL );
293 } elsif (!$first_read && $buffer =~ /^<$tag[^>]*?\/>(.*)/) {
294 $self->{temp_buffer} = $1;
296 $logger->transport( "completed read with $buffer", INTERNAL );
301 } while (!$complete && $xml);
307 $logger->transport( "XML Read: $xml", INTERNAL );
308 #reset_fl( $fh, $flags) if defined $flags;
309 set_block( $fh ) if defined $flags;
321 # -------------------------------------------------
326 return 1 if ($self->{_socket} and $self->{_socket}->connected);
331 my( $self, $password ) = @_;
332 $self->{'oils:password'} = $password if $password;
333 return $self->{'oils:password'};
336 # -------------------------------------------------
339 my( $self, $username ) = @_;
340 $self->{'oils:username'} = $username if $username;
341 return $self->{'oils:username'};
344 # -------------------------------------------------
347 my( $self, $resource ) = @_;
348 $self->{'oils:resource'} = $resource if $resource;
349 return $self->{'oils:resource'};
352 # -------------------------------------------------
355 my( $self, $jid ) = @_;
356 $self->{'oils:jid'} = $jid if $jid;
357 return $self->{'oils:jid'};
361 my( $self, $port ) = @_;
362 $self->{'oils:port'} = $port if $port;
363 return $self->{'oils:port'};
367 my( $self, $host ) = @_;
368 $self->{'oils:host'} = $host if $host;
369 return $self->{'oils:host'};
372 # -------------------------------------------------
376 Sends a Jabber message.
379 to - The JID of the recipient
380 thread - The Jabber thread
381 body - The body of the message
389 my $to = $params{'to'} || return undef;
390 my $body = $params{'body'} || return undef;
391 my $thread = $params{'thread'} || "";
392 my $router_command = $params{'router_command'} || "";
393 my $router_class = $params{'router_class'} || "";
395 my $msg = OpenSRF::Transport::SlimJabber::MessageWrapper->new;
398 $msg->setThread( $thread ) if $thread;
399 $msg->setBody( $body );
400 $msg->set_router_command( $router_command );
401 $msg->set_router_class( $router_class );
405 "JabberClient Sending message to $to with thread $thread and body: \n$body", INTERNAL );
407 my $soc = $self->{_socket};
408 unless( $soc and $soc->connected ) {
409 throw OpenSRF::EX::Jabber ("No longer connected to jabber server");
411 print $soc $msg->toString;
414 "JabberClient Sent message to $to with thread $thread and body: \n$body", INTERNAL );
420 Connect to the server and log in.
422 Throws an OpenSRF::EX::JabberException if we cannot connect
423 to the server or if the authentication fails.
427 # --- The logging lines have been commented out until we decide
428 # on which log files we're using.
434 my $jid = $self->jid;
435 my $host = $self->host;
436 my $port = $self->port;
437 my $username = $self->username;
438 my $resource = $self->resource;
439 my $password = $self->password;
441 my $stream = <<" XML";
442 <stream:stream to='$host' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>
445 my $conf = OpenSRF::Utils::Config->current;
447 if(!$conf->bootstrap->router_name && $username eq "router") {
452 <iq id='123' type='set'>
453 <query xmlns='jabber:iq:auth'>
454 <username>$username</username>
455 <password>$password</password>
456 <resource>${resource}$tail</resource>
461 my $sock_type = 'IO::Socket::INET';
463 # if port is a string, then we're connecting to a UNIX socket
464 unless( $port =~ /^\d+$/ ) {
465 $sock_type = 'IO::Socket::UNIX';
468 # --- 5 tries to connect to the jabber server
471 $logger->transport( "$jid: Attempting to connect to server...$host:$port (Try # $_)", WARN );
472 $socket = $sock_type->new( PeerHost => $host,
476 $logger->transport( "$jid: $_ connect attempt to $host:$port", WARN );
477 last if ( $socket and $socket->connected );
481 unless ( $socket and $socket->connected ) {
482 throw OpenSRF::EX::Jabber( " Could not connect to Jabber server: $!" );
485 $logger->transport( "Logging into jabber as $jid " .
486 "from " . ref( $self ), DEBUG );
488 print $socket $stream;
493 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
495 sysread($socket, $buffer, 4096);
496 $logger->transport( "Login buffer 1: $buffer", INTERNAL );
504 if( $socket and $socket->connected() ) {
505 $self->{_socket} = $socket;
507 throw OpenSRF::EX::Jabber( " ** Unable to connect to Jabber server", ERROR );
511 $buffer = $self->timed_read(10);
513 if( $buffer ) {$logger->transport( "Login buffer 2: $buffer", INTERNAL );}
515 if( $buffer and $buffer =~ /type=["\']result["\']/ ) {
516 $logger->transport( " * $jid: Jabber authenticated and connected", DEBUG );
518 if( !$buffer ) { $buffer = " "; }
520 throw OpenSRF::EX::Jabber( " * $jid: Unable to authenticate: $buffer", ERROR );
527 my( $class, $app ) = @_;
528 $logger->transport("Constructing new Jabber connection for $app, my class $class", INTERNAL );
530 $class->new( $app )->initialize() );
535 my( $self, $timeout ) = @_;
538 undef $timeout if ( $timeout == -1 );
540 unless( $self->{_socket}->connected ) {
541 OpenSRF::EX::JabberDisconnected->throw(
542 "This JabberClient instance is no longer connected to the server " .
543 $self->username . " : " . $self->resource, ERROR );
546 my $val = $self->timed_read( $timeout );
548 $timeout = "FOREVER" unless ( defined $timeout );
550 if ( ! defined( $val ) ) {
551 OpenSRF::EX::Jabber->throw(
552 "Call to Client->timed_read( $timeout ) failed", ERROR );
555 "Call to Client->timed_read( $timeout ) returned 0 bytes of data", DEBUG );
558 "Call to Client->timed_read( $timeout ) successfully returned data", INTERNAL );