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);
9 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
15 OpenSRF::Transport::SlimJabber::Client
17 Home-brewed slimmed down jabber connection agent. Supports SSL connections
18 with a config file options:
20 transport->server->sslport # the ssl port
21 transport->server->ssl # is this ssl?
25 my $logger = "OpenSRF::Utils::Logger";
34 my $socket = $self->{_socket};
35 if( $socket and $socket->connected() ) {
36 print $socket "</stream:stream>";
44 Creates a new Client object.
46 debug and log_file are not required if you don't care to log the activity,
47 however all other parameters are.
61 my( $class, %params ) = @_;
63 $class = ref( $class ) || $class;
65 my $port = $params{'port'} || return undef;
66 my $username = $params{'username'} || return undef;
67 my $resource = $params{'resource'} || return undef;
68 my $password = $params{'password'} || return undef;
69 my $host = $params{'host'} || return undef;
71 my $jid = "$username\@$host\/$resource";
73 my $self = bless {} => $class;
78 $self->username( $username );
79 $self->resource( $resource );
80 $self->password( $password );
81 $self->{temp_buffer} = "";
83 $logger->transport( "Creating Client instance: $host:$port, $username, $resource",
89 # clears the tmp buffer as well as the TCP buffer
93 $self->{temp_buffer} = "";
95 my $fh = $self->{_socket};
98 while( sysread( $fh, $t_buf, 4096 ) ) {}
101 # -------------------------------------------------
105 Gathers all Jabber messages sitting in the collection queue
106 and hands them each to their respective callbacks. This call
107 does not block (calls Process(0))
111 sub gather { my $self = shift; $self->process( 0 ); }
113 # -------------------------------------------------
117 Blocks and gathers incoming messages as they arrive. Does not return
118 unless an error occurs.
120 Throws an OpenSRF::EX::JabberException if the call to Process ever fails.
126 my $sock = $self->unix_sock();
127 my $socket = IO::Socket::UNIX->new( Peer => $sock );
128 $logger->transport( "Unix Socket opened by Listener", INTERNAL );
130 throw OpenSRF::EX::Socket( "Unable to connect to UnixServer: socket-file: $sock \n :=> $! " )
131 unless ($socket->connected);
134 my $o = $self->process( -1 );
135 $logger->transport( "Call to process() in listener returned:\n $o", INTERNAL );
136 if( ! defined( $o ) ) {
137 throw OpenSRF::EX::Jabber( "Listen Loop failed at 'process()'" );
142 throw OpenSRF::EX::Socket( "How did we get here?!?!" );
147 my $flags = fcntl($fh, F_GETFL, 0)
148 or die "Can't get flags for the socket: $!\n";
150 $logger->transport( "Setting NONBLOCK: original flags: $flags", INTERNAL );
152 fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
153 or die "Can't set flags for the socket: $!\n";
161 $logger->transport( "Restoring BLOCK: to flags $flags", INTERNAL );
162 fcntl($fh, F_SETFL, $flags) if defined $flags;
168 my $flags = fcntl($fh, F_GETFL, 0)
169 or die "Can't get flags for the socket: $!\n";
171 $flags &= ~O_NONBLOCK;
173 fcntl($fh, F_SETFL, $flags)
174 or die "Can't set flags for the socket: $!\n";
179 my ($self, $timeout) = @_;
181 $logger->transport( "Temp Buffer Contained: \n". $self->{temp_buffer}, INTERNAL) if $self->{temp_buffer};
182 if( $self->can( "app" ) ) {
183 $logger->transport( "timed_read called for ".$self->app.", I am: ".$self->jid, INTERNAL );
186 # See if there is a complete message in the temp_buffer
188 if( $self->{temp_buffer} ) {
189 my $buffer = $self->{temp_buffer};
191 $self->{temp_buffer} = '';
193 my ($tag) = ($buffer =~ /<([^\s\?\>]+)/o);
194 $logger->transport("Using tag: $tag ", INTERNAL);
196 if ( $buffer =~ /^(.*?<\/$tag>)(.*)/s) {
198 $self->{temp_buffer} = $2;
200 $logger->transport( "completed read with $buffer", INTERNAL );
201 } elsif ( $buffer =~ /^<$tag[^>]*?\/>(.*)/) {
202 $self->{temp_buffer} = $1;
204 $logger->transport( "completed read with $buffer", INTERNAL );
206 $self->{temp_buffer} = $buffer;
209 if( $buffer and $complete ) {
216 my $fh = $self->{_socket};
218 unless( $fh and $fh->connected ) {
219 throw OpenSRF::EX::Socket ("Attempted read on closed socket", ERROR );
222 $logger->transport( "Temp Buffer After first attempt: \n ".$self->{temp_buffer}, INTERNAL) if $self->{temp_buffer};
225 if (defined($timeout) && !$timeout) {
226 $flags = set_nonblock( $fh );
230 $logger->transport( "Calling timed_read with timetout $timeout", INTERNAL );
240 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
242 # alarm needs a number greater => 1.
243 my $alarm_timeout = $timeout;
244 if( $alarm_timeout > 0 and $alarm_timeout < 1 ) {
247 alarm $alarm_timeout;
250 my $buffer = $self->{temp_buffer};
251 $self->{temp_buffer} = '';
254 my $ff = fcntl($fh, F_GETFL, 0);
255 if ($ff == ($ff | O_NONBLOCK) and $timeout > 0 ) {
256 #throw OpenSRF::EX::ERROR ("File flags are set to NONBLOCK but timeout is $timeout", ERROR );
260 my $read_size = 1024;
262 while( my $n = sysread( $fh, $t_buf, $read_size ) ) {
264 if( $n < $read_size ) {
265 #reset_fl( $fh, $f ) if $f;
269 # see if there is any more data to grab...
270 $f = set_nonblock( $fh );
273 #sysread($fh, $buffer, 2048, length($buffer) );
274 #sysread( $fh, $t_buf, 2048 );
278 $logger->transport(" Got [$buffer] from the socket", INTERNAL);
281 $logger->transport(" First read Buffer\n [$buffer]", INTERNAL);
282 ($tag) = ($buffer =~ /<([^\s\?\>]+){1}/o);
284 $logger->transport("Using tag: $tag ", INTERNAL);
287 if (!$first_read && $buffer =~ /^(.*?<\/$tag>){1}(.*)/s) {
289 $self->{temp_buffer} = $2;
291 $logger->transport( "completed read with $buffer", INTERNAL );
292 } elsif (!$first_read && $buffer =~ /^<$tag[^>]*?\/>(.*)/) {
293 $self->{temp_buffer} = $1;
295 $logger->transport( "completed read with $buffer", INTERNAL );
300 } while (!$complete && $xml);
306 $logger->transport( "XML Read: $xml", INTERNAL );
307 #reset_fl( $fh, $flags) if defined $flags;
308 set_block( $fh ) if defined $flags;
320 # -------------------------------------------------
325 return 1 if ($self->{_socket} and $self->{_socket}->connected);
330 my( $self, $password ) = @_;
331 $self->{'oils:password'} = $password if $password;
332 return $self->{'oils:password'};
335 # -------------------------------------------------
338 my( $self, $username ) = @_;
339 $self->{'oils:username'} = $username if $username;
340 return $self->{'oils:username'};
343 # -------------------------------------------------
346 my( $self, $resource ) = @_;
347 $self->{'oils:resource'} = $resource if $resource;
348 return $self->{'oils:resource'};
351 # -------------------------------------------------
354 my( $self, $jid ) = @_;
355 $self->{'oils:jid'} = $jid if $jid;
356 return $self->{'oils:jid'};
360 my( $self, $port ) = @_;
361 $self->{'oils:port'} = $port if $port;
362 return $self->{'oils:port'};
366 my( $self, $host ) = @_;
367 $self->{'oils:host'} = $host if $host;
368 return $self->{'oils:host'};
371 # -------------------------------------------------
375 Sends a Jabber message.
378 to - The JID of the recipient
379 thread - The Jabber thread
380 body - The body of the message
388 my $to = $params{'to'} || return undef;
389 my $body = $params{'body'} || return undef;
390 my $thread = $params{'thread'} || "";
391 my $router_command = $params{'router_command'} || "";
392 my $router_class = $params{'router_class'} || "";
394 my $msg = OpenSRF::Transport::SlimJabber::MessageWrapper->new;
397 $msg->setThread( $thread ) if $thread;
398 $msg->setBody( $body );
399 $msg->set_router_command( $router_command );
400 $msg->set_router_class( $router_class );
404 "JabberClient Sending message to $to with thread $thread and body: \n$body", INTERNAL );
406 my $soc = $self->{_socket};
407 unless( $soc and $soc->connected ) {
408 throw OpenSRF::EX::Jabber ("No longer connected to jabber server");
410 print $soc $msg->toString;
413 "JabberClient Sent message to $to with thread $thread and body: \n$body", INTERNAL );
419 Connect to the server and log in.
421 Throws an OpenSRF::EX::JabberException if we cannot connect
422 to the server or if the authentication fails.
426 # --- The logging lines have been commented out until we decide
427 # on which log files we're using.
433 my $jid = $self->jid;
434 my $host = $self->host;
435 my $port = $self->port;
436 my $username = $self->username;
437 my $resource = $self->resource;
438 my $password = $self->password;
440 my $stream = <<" XML";
441 <stream:stream to='$host' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>
445 <iq id='123' type='set'>
446 <query xmlns='jabber:iq:auth'>
447 <username>$username</username>
448 <password>$password</password>
449 <resource>${resource}_$$</resource>
454 my $sock_type = 'IO::Socket::INET';
456 $sock_type = 'IO::Socket::UNIX';
459 # --- 5 tries to connect to the jabber server
462 $logger->transport( "$jid: Attempting to connect to server...$host:$port (Try # $_)", WARN );
463 $socket = $sock_type->new( PeerHost => $host,
467 $logger->transport( "$jid: $_ connect attempt to $host:$port", WARN );
468 last if ( $socket and $socket->connected );
472 unless ( $socket and $socket->connected ) {
473 throw OpenSRF::EX::Jabber( " Could not connect to Jabber server: $!" );
476 $logger->transport( "Logging into jabber as $jid " .
477 "from " . ref( $self ), DEBUG );
479 print $socket $stream;
484 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
486 sysread($socket, $buffer, 4096);
487 $logger->transport( "Login buffer 1: $buffer", INTERNAL );
495 if( $socket and $socket->connected() ) {
496 $self->{_socket} = $socket;
498 throw OpenSRF::EX::Jabber( " ** Unable to connect to Jabber server", ERROR );
502 $buffer = $self->timed_read(10);
504 if( $buffer ) {$logger->transport( "Login buffer 2: $buffer", INTERNAL );}
506 if( $buffer and $buffer =~ /type=["\']result["\']/ ) {
507 $logger->transport( " * $jid: Jabber authenticated and connected", DEBUG );
509 if( !$buffer ) { $buffer = " "; }
511 throw OpenSRF::EX::Jabber( " * $jid: Unable to authenticate: $buffer", ERROR );
518 my( $class, $app ) = @_;
519 $logger->transport("Constructing new Jabber connection for $app, my class $class", INTERNAL );
521 $class->new( $app )->initialize() );
526 my( $self, $timeout ) = @_;
529 undef $timeout if ( $timeout == -1 );
531 unless( $self->{_socket}->connected ) {
532 OpenSRF::EX::JabberDisconnected->throw(
533 "This JabberClient instance is no longer connected to the server " .
534 $self->username . " : " . $self->resource, ERROR );
537 my $val = $self->timed_read( $timeout );
539 $timeout = "FOREVER" unless ( defined $timeout );
541 if ( ! defined( $val ) ) {
542 OpenSRF::EX::Jabber->throw(
543 "Call to Client->timed_read( $timeout ) failed", ERROR );
546 "Call to Client->timed_read( $timeout ) returned 0 bytes of data", DEBUG );
549 "Call to Client->timed_read( $timeout ) successfully returned data", INTERNAL );