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 Time::HiRes qw(ualarm);
8 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
13 OpenSRF::Transport::SlimJabber::Client
15 Home-brewed slimmed down jabber connection agent. Supports SSL connections
16 with a config file options:
18 transport->server->sslport # the ssl port
19 transport->server->ssl # is this ssl?
23 my $logger = "OpenSRF::Utils::Logger";
32 my $socket = $self->{_socket};
33 if( $socket and $socket->connected() ) {
34 print $socket "</stream:stream>";
42 Creates a new Client object.
44 debug and log_file are not required if you don't care to log the activity,
45 however all other parameters are.
59 my( $class, %params ) = @_;
61 $class = ref( $class ) || $class;
63 my $port = $params{'port'} || return undef;
64 my $username = $params{'username'} || return undef;
65 my $resource = $params{'resource'} || return undef;
66 my $password = $params{'password'} || return undef;
67 my $host = $params{'host'} || return undef;
69 my $jid = "$username\@$host\/$resource";
71 my $self = bless {} => $class;
76 $self->username( $username );
77 $self->resource( $resource );
78 $self->password( $password );
79 $self->{temp_buffer} = "";
81 $logger->transport( "Creating Client instance: $host:$port, $username, $resource",
87 # clears the tmp buffer as well as the TCP buffer
91 $self->{temp_buffer} = "";
93 my $fh = $self->{_socket};
96 while( sysread( $fh, $t_buf, 4096 ) ) {}
99 # -------------------------------------------------
103 Gathers all Jabber messages sitting in the collection queue
104 and hands them each to their respective callbacks. This call
105 does not block (calls Process(0))
109 sub gather { my $self = shift; $self->process( 0 ); }
111 # -------------------------------------------------
115 Blocks and gathers incoming messages as they arrive. Does not return
116 unless an error occurs.
118 Throws an OpenSRF::EX::JabberException if the call to Process ever fails.
124 my $sock = $self->unix_sock();
125 my $socket = IO::Socket::UNIX->new( Peer => $sock );
126 $logger->transport( "Unix Socket opened by Listener", INTERNAL );
128 throw OpenSRF::EX::Socket( "Unable to connect to UnixServer: socket-file: $sock \n :=> $! " )
129 unless ($socket->connected);
132 my $o = $self->process( -1 );
133 $logger->transport( "Call to process() in listener returned:\n $o", INTERNAL );
134 if( ! defined( $o ) ) {
135 throw OpenSRF::EX::Jabber( "Listen Loop failed at 'process()'" );
140 throw OpenSRF::EX::Socket( "How did we get here?!?!" );
145 my $flags = fcntl($fh, F_GETFL, 0)
146 or die "Can't get flags for the socket: $!\n";
148 $logger->transport( "Setting NONBLOCK: original flags: $flags", INTERNAL );
150 fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
151 or die "Can't set flags for the socket: $!\n";
159 $logger->transport( "Restoring BLOCK: to flags $flags", INTERNAL );
160 fcntl($fh, F_SETFL, $flags) if defined $flags;
166 my $flags = fcntl($fh, F_GETFL, 0)
167 or die "Can't get flags for the socket: $!\n";
169 $flags &= ~O_NONBLOCK;
171 fcntl($fh, F_SETFL, $flags)
172 or die "Can't set flags for the socket: $!\n";
177 my ($self, $timeout) = @_;
179 $logger->transport( "Temp Buffer Contained: \n". $self->{temp_buffer}, INTERNAL) if $self->{temp_buffer};
180 if( $self->can( "app" ) ) {
181 $logger->transport( "timed_read called for ".$self->app.", I am: ".$self->jid, INTERNAL );
184 # See if there is a complete message in the temp_buffer
186 if( $self->{temp_buffer} ) {
187 my $buffer = $self->{temp_buffer};
189 $self->{temp_buffer} = '';
191 my ($tag) = ($buffer =~ /<([^\s\?\>]+)/o);
192 $logger->transport("Using tag: $tag ", INTERNAL);
194 if ( $buffer =~ /^(.*?<\/$tag>)(.*)/s) {
196 $self->{temp_buffer} = $2;
198 $logger->transport( "completed read with $buffer", INTERNAL );
199 } elsif ( $buffer =~ /^<$tag[^>]*?\/>(.*)/) {
200 $self->{temp_buffer} = $1;
202 $logger->transport( "completed read with $buffer", INTERNAL );
204 $self->{temp_buffer} = $buffer;
207 if( $buffer and $complete ) {
214 my $fh = $self->{_socket};
216 unless( $fh and $fh->connected ) {
217 throw OpenSRF::EX::Socket ("Attempted read on closed socket", ERROR );
220 $logger->transport( "Temp Buffer After first attempt: \n ".$self->{temp_buffer}, INTERNAL) if $self->{temp_buffer};
223 if (defined($timeout) && !$timeout) {
224 $flags = set_nonblock( $fh );
228 $logger->transport( "Calling timed_read with timetout $timeout", INTERNAL );
238 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
240 # alarm needs a number greater => 1.
241 my $alarm_timeout = $timeout;
242 if( $alarm_timeout > 0 and $alarm_timeout < 1 ) {
245 alarm $alarm_timeout;
248 my $buffer = $self->{temp_buffer};
249 $self->{temp_buffer} = '';
252 my $ff = fcntl($fh, F_GETFL, 0);
253 if ($ff == ($ff | O_NONBLOCK) and $timeout > 0 ) {
254 #throw OpenSRF::EX::ERROR ("File flags are set to NONBLOCK but timeout is $timeout", ERROR );
258 my $read_size = 1024;
260 while( my $n = sysread( $fh, $t_buf, $read_size ) ) {
262 if( $n < $read_size ) {
263 #reset_fl( $fh, $f ) if $f;
267 # see if there is any more data to grab...
268 $f = set_nonblock( $fh );
271 #sysread($fh, $buffer, 2048, length($buffer) );
272 #sysread( $fh, $t_buf, 2048 );
276 $logger->transport(" Got [$buffer] from the socket", INTERNAL);
279 $logger->transport(" First read Buffer\n [$buffer]", INTERNAL);
280 ($tag) = ($buffer =~ /<([^\s\?\>]+){1}/o);
282 $logger->transport("Using tag: $tag ", INTERNAL);
285 if (!$first_read && $buffer =~ /^(.*?<\/$tag>){1}(.*)/s) {
287 $self->{temp_buffer} = $2;
289 $logger->transport( "completed read with $buffer", INTERNAL );
290 } elsif (!$first_read && $buffer =~ /^<$tag[^>]*?\/>(.*)/) {
291 $self->{temp_buffer} = $1;
293 $logger->transport( "completed read with $buffer", INTERNAL );
298 } while (!$complete && $xml);
304 $logger->transport( "XML Read: $xml", INTERNAL );
305 #reset_fl( $fh, $flags) if defined $flags;
306 set_block( $fh ) if defined $flags;
318 # -------------------------------------------------
323 return 1 if ($self->{_socket} and $self->{_socket}->connected);
328 my( $self, $password ) = @_;
329 $self->{'oils:password'} = $password if $password;
330 return $self->{'oils:password'};
333 # -------------------------------------------------
336 my( $self, $username ) = @_;
337 $self->{'oils:username'} = $username if $username;
338 return $self->{'oils:username'};
341 # -------------------------------------------------
344 my( $self, $resource ) = @_;
345 $self->{'oils:resource'} = $resource if $resource;
346 return $self->{'oils:resource'};
349 # -------------------------------------------------
352 my( $self, $jid ) = @_;
353 $self->{'oils:jid'} = $jid if $jid;
354 return $self->{'oils:jid'};
358 my( $self, $port ) = @_;
359 $self->{'oils:port'} = $port if $port;
360 return $self->{'oils:port'};
364 my( $self, $host ) = @_;
365 $self->{'oils:host'} = $host if $host;
366 return $self->{'oils:host'};
369 # -------------------------------------------------
373 Sends a Jabber message.
376 to - The JID of the recipient
377 thread - The Jabber thread
378 body - The body of the message
386 my $to = $params{'to'} || return undef;
387 my $body = $params{'body'} || return undef;
388 my $thread = $params{'thread'} || "";
389 my $router_command = $params{'router_command'} || "";
390 my $router_class = $params{'router_class'} || "";
392 my $msg = OpenSRF::Transport::SlimJabber::MessageWrapper->new;
395 $msg->setThread( $thread ) if $thread;
396 $msg->setBody( $body );
397 $msg->set_router_command( $router_command );
398 $msg->set_router_class( $router_class );
402 "JabberClient Sending message to $to with thread $thread and body: \n$body", INTERNAL );
404 my $soc = $self->{_socket};
405 unless( $soc and $soc->connected ) {
406 throw OpenSRF::EX::Jabber ("No longer connected to jabber server");
408 print $soc $msg->toString;
411 "JabberClient Sent message to $to with thread $thread and body: \n$body", INTERNAL );
417 Connect to the server and log in.
419 Throws an OpenSRF::EX::JabberException if we cannot connect
420 to the server or if the authentication fails.
424 # --- The logging lines have been commented out until we decide
425 # on which log files we're using.
431 my $jid = $self->jid;
432 my $host = $self->host;
433 my $port = $self->port;
434 my $username = $self->username;
435 my $resource = $self->resource;
436 my $password = $self->password;
438 my $stream = <<" XML";
439 <stream:stream to='$host' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>
443 <iq id='123' type='set'>
444 <query xmlns='jabber:iq:auth'>
445 <username>$username</username>
446 <password>$password</password>
447 <resource>${resource}_$$</resource>
453 # --- 5 tries to connect to the jabber server
456 $logger->transport( "$jid: Attempting to connect to server...$host:$port (Try # $_)", WARN );
457 $socket = IO::Socket::INET->new( PeerHost => $host,
460 $logger->transport( "$jid: $_ connect attempt to $host:$port", WARN );
461 last if ( $socket and $socket->connected );
465 unless ( $socket and $socket->connected ) {
466 throw OpenSRF::EX::Jabber( " Could not connect to Jabber server: $!" );
469 $logger->transport( "Logging into jabber as $jid " .
470 "from " . ref( $self ), DEBUG );
472 print $socket $stream;
477 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
479 sysread($socket, $buffer, 4096);
480 $logger->transport( "Login buffer 1: $buffer", INTERNAL );
488 if( $socket and $socket->connected() ) {
489 $self->{_socket} = $socket;
491 throw OpenSRF::EX::Jabber( " ** Unable to connect to Jabber server", ERROR );
495 $buffer = $self->timed_read(10);
497 if( $buffer ) {$logger->transport( "Login buffer 2: $buffer", INTERNAL );}
499 if( $buffer and $buffer =~ /type=["\']result["\']/ ) {
500 $logger->transport( " * $jid: Jabber authenticated and connected", DEBUG );
502 if( !$buffer ) { $buffer = " "; }
504 throw OpenSRF::EX::Jabber( " * $jid: Unable to authenticate: $buffer", ERROR );
511 my( $class, $app ) = @_;
512 $logger->transport("Constructing new Jabber connection for $app, my class $class", INTERNAL );
514 $class->new( $app )->initialize() );
519 my( $self, $timeout ) = @_;
522 undef $timeout if ( $timeout == -1 );
524 unless( $self->{_socket}->connected ) {
525 OpenSRF::EX::JabberDisconnected->throw(
526 "This JabberClient instance is no longer connected to the server " .
527 $self->username . " : " . $self->resource, ERROR );
530 my $val = $self->timed_read( $timeout );
532 $timeout = "FOREVER" unless ( defined $timeout );
534 if ( ! defined( $val ) ) {
535 OpenSRF::EX::Jabber->throw(
536 "Call to Client->timed_read( $timeout ) failed", ERROR );
539 "Call to Client->timed_read( $timeout ) returned 0 bytes of data", DEBUG );
542 "Call to Client->timed_read( $timeout ) successfully returned data", INTERNAL );