1 package OpenSRF::Transport::SlimJabber::Client;
2 use strict; use warnings;
4 #use Net::Jabber qw( Client );
5 use base qw( OpenSRF );
6 use OpenSRF::Utils::Logger qw(:level);
7 use Time::HiRes qw(ualarm);
9 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
14 OpenSRF::Transport::SlimJabber::Client
16 Home-brewed slimmed down jabber connection agent. Supports SSL connections
17 with a config file options:
19 transport->server->sslport # the ssl port
20 transport->server->ssl # is this ssl?
24 my $logger = "OpenSRF::Utils::Logger";
28 my $socket = $self->{_socket};
29 if( $socket and $socket->connected() ) {
30 print $socket "</stream:stream>";
38 Creates a new Client object.
40 debug and log_file are not required if you don't care to log the activity,
41 however all other parameters are.
55 my( $class, %params ) = @_;
57 $class = ref( $class ) || $class;
59 my $conf = OpenSRF::Utils::Config->current;
63 my $port = $conf->transport->server->port;
64 my $username = $params{'username'} || return undef;
65 my $resource = $params{'resource'} || return undef;
66 my $password = $params{'password'} || return undef;
69 $host = $params{host};
71 $host = $conf->transport->server->system_primary;
74 my $jid = "$username\@$host\/$resource";
76 my $self = bless {} => $class;
81 $self->username( $username );
82 $self->resource( $resource );
83 $self->password( $password );
84 $self->{temp_buffer} = "";
86 $logger->transport( "Creating Client instance: $host:$port, $username, $resource",
92 # clears the tmp buffer as well as the TCP buffer
96 $self->{temp_buffer} = "";
98 my $fh = $self->{_socket};
101 while( sysread( $fh, $t_buf, 4096 ) ) {}
104 # -------------------------------------------------
108 Gathers all Jabber messages sitting in the collection queue
109 and hands them each to their respective callbacks. This call
110 does not block (calls Process(0))
114 sub gather { my $self = shift; $self->process( 0 ); }
116 # -------------------------------------------------
120 Blocks and gathers incoming messages as they arrive. Does not return
121 unless an error occurs.
123 Throws an OpenSRF::EX::JabberException if the call to Process ever fails.
129 my $sock = $self->unix_sock();
130 my $socket = IO::Socket::UNIX->new( Peer => $sock );
131 $logger->transport( "Unix Socket opened by Listener", INTERNAL );
133 throw OpenSRF::EX::Socket( "Unable to connect to UnixServer: socket-file: $sock \n :=> $! " )
134 unless ($socket->connected);
137 my $o = $self->process( -1 );
138 $logger->transport( "Call to process() in listener returned:\n $o", INTERNAL );
139 if( ! defined( $o ) ) {
140 throw OpenSRF::EX::Jabber( "Listen Loop failed at 'process()'" );
145 throw OpenSRF::EX::Socket( "How did we get here?!?!" );
150 my $flags = fcntl($fh, F_GETFL, 0)
151 or die "Can't get flags for the socket: $!\n";
153 $logger->transport( "Setting NONBLOCK: original flags: $flags", INTERNAL );
155 fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
156 or die "Can't set flags for the socket: $!\n";
164 $logger->transport( "Restoring BLOCK: to flags $flags", INTERNAL );
165 fcntl($fh, F_SETFL, $flags) if defined $flags;
171 my $flags = fcntl($fh, F_GETFL, 0)
172 or die "Can't get flags for the socket: $!\n";
174 $flags &= ~O_NONBLOCK;
176 fcntl($fh, F_SETFL, $flags)
177 or die "Can't set flags for the socket: $!\n";
182 my ($self, $timeout) = @_;
184 $logger->transport( "Temp Buffer Contained: \n". $self->{temp_buffer}, INTERNAL) if $self->{temp_buffer};
185 if( $self->can( "app" ) ) {
186 $logger->transport( "timed_read called for ".$self->app.", I am: ".$self->jid, INTERNAL );
189 # See if there is a complete message in the temp_buffer
191 if( $self->{temp_buffer} ) {
192 my $buffer = $self->{temp_buffer};
194 $self->{temp_buffer} = '';
196 my ($tag) = ($buffer =~ /<([^\s\?\>]+)/o);
197 $logger->transport("Using tag: $tag ", INTERNAL);
199 if ( $buffer =~ /^(.*?<\/$tag>)(.*)/s) {
201 $self->{temp_buffer} = $2;
203 $logger->transport( "completed read with $buffer", INTERNAL );
204 } elsif ( $buffer =~ /^<$tag[^>]*?\/>(.*)/) {
205 $self->{temp_buffer} = $1;
207 $logger->transport( "completed read with $buffer", INTERNAL );
209 $self->{temp_buffer} = $buffer;
212 if( $buffer and $complete ) {
219 my $fh = $self->{_socket};
221 unless( $fh and $fh->connected ) {
222 throw OpenSRF::EX::Socket ("Attempted read on closed socket", ERROR );
225 $logger->transport( "Temp Buffer After first attempt: \n ".$self->{temp_buffer}, INTERNAL) if $self->{temp_buffer};
228 if (defined($timeout) && !$timeout) {
229 $flags = set_nonblock( $fh );
233 $logger->transport( "Calling timed_read with timetout $timeout", INTERNAL );
243 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
245 # alarm needs a number greater => 1.
246 my $alarm_timeout = $timeout;
247 if( $alarm_timeout > 0 and $alarm_timeout < 1 ) {
250 alarm $alarm_timeout;
253 my $buffer = $self->{temp_buffer};
254 $self->{temp_buffer} = '';
257 my $ff = fcntl($fh, F_GETFL, 0);
258 if ($ff == ($ff | O_NONBLOCK) and $timeout > 0 ) {
259 #throw OpenSRF::EX::ERROR ("File flags are set to NONBLOCK but timeout is $timeout", ERROR );
263 my $read_size = 1024;
265 while( my $n = sysread( $fh, $t_buf, $read_size ) ) {
267 if( $n < $read_size ) {
268 #reset_fl( $fh, $f ) if $f;
272 # see if there is any more data to grab...
273 $f = set_nonblock( $fh );
276 #sysread($fh, $buffer, 2048, length($buffer) );
277 #sysread( $fh, $t_buf, 2048 );
281 $logger->transport(" Got [$buffer] from the socket", INTERNAL);
284 $logger->transport(" First read Buffer\n [$buffer]", INTERNAL);
285 ($tag) = ($buffer =~ /<([^\s\?\>]+){1}/o);
287 $logger->transport("Using tag: $tag ", INTERNAL);
290 if (!$first_read && $buffer =~ /^(.*?<\/$tag>){1}(.*)/s) {
292 $self->{temp_buffer} = $2;
294 $logger->transport( "completed read with $buffer", INTERNAL );
295 } elsif (!$first_read && $buffer =~ /^<$tag[^>]*?\/>(.*)/) {
296 $self->{temp_buffer} = $1;
298 $logger->transport( "completed read with $buffer", INTERNAL );
303 } while (!$complete && $xml);
309 $logger->transport( "XML Read: $xml", INTERNAL );
310 #reset_fl( $fh, $flags) if defined $flags;
311 set_block( $fh ) if defined $flags;
323 # -------------------------------------------------
328 return 1 if ($self->{_socket} and $self->{_socket}->connected);
333 my( $self, $password ) = @_;
334 $self->{'oils:password'} = $password if $password;
335 return $self->{'oils:password'};
338 # -------------------------------------------------
341 my( $self, $username ) = @_;
342 $self->{'oils:username'} = $username if $username;
343 return $self->{'oils:username'};
346 # -------------------------------------------------
349 my( $self, $resource ) = @_;
350 $self->{'oils:resource'} = $resource if $resource;
351 return $self->{'oils:resource'};
354 # -------------------------------------------------
357 my( $self, $jid ) = @_;
358 $self->{'oils:jid'} = $jid if $jid;
359 return $self->{'oils:jid'};
363 my( $self, $port ) = @_;
364 $self->{'oils:port'} = $port if $port;
365 return $self->{'oils:port'};
369 my( $self, $host ) = @_;
370 $self->{'oils:host'} = $host if $host;
371 return $self->{'oils:host'};
374 # -------------------------------------------------
378 Sends a Jabber message.
381 to - The JID of the recipient
382 thread - The Jabber thread
383 body - The body of the message
391 my $to = $params{'to'} || return undef;
392 my $body = $params{'body'} || return undef;
393 my $thread = $params{'thread'} || "";
394 my $router_command = $params{'router_command'} || "";
395 my $router_class = $params{'router_class'} || "";
397 my $msg = OpenSRF::Transport::SlimJabber::MessageWrapper->new;
400 $msg->setThread( $thread ) if $thread;
401 $msg->setBody( $body );
402 $msg->set_router_command( $router_command );
403 $msg->set_router_class( $router_class );
407 "JabberClient Sending message to $to with thread $thread and body: \n$body", INTERNAL );
409 my $soc = $self->{_socket};
410 print $soc $msg->toString;
416 Connect to the server and log in.
418 Throws an OpenSRF::EX::JabberException if we cannot connect
419 to the server or if the authentication fails.
423 # --- The logging lines have been commented out until we decide
424 # on which log files we're using.
430 my $jid = $self->jid;
431 my $host = $self->host;
432 my $port = $self->port;
433 my $username = $self->username;
434 my $resource = $self->resource;
435 my $password = $self->password;
437 my $stream = <<" XML";
438 <stream:stream to='$host' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>
442 <iq id='123' type='set'>
443 <query xmlns='jabber:iq:auth'>
444 <username>$username</username>
445 <password>$password</password>
446 <resource>$resource</resource>
452 # --- 5 tries to connect to the jabber server
455 $logger->transport( "$jid: Attempting to connect to server... (Try # $_)", WARN );
456 $socket = IO::Socket::INET->new( PeerHost => $host,
459 last if ( $socket and $socket->connected );
463 unless ( $socket and $socket->connected ) {
464 throw OpenSRF::EX::Jabber( " Could not connect to Jabber server: $!" );
467 $logger->transport( "Logging into jabber as $jid " .
468 "from " . ref( $self ), DEBUG );
470 print $socket $stream;
475 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
477 sysread($socket, $buffer, 4096);
478 $logger->transport( "Login buffer 1: $buffer", INTERNAL );
486 if( $socket and $socket->connected() ) {
487 $self->{_socket} = $socket;
489 throw OpenSRF::EX::Jabber( " ** Unable to connect to Jabber server", ERROR );
493 $buffer = $self->timed_read(10);
495 if( $buffer ) {$logger->transport( "Login buffer 2: $buffer", INTERNAL );}
497 if( $buffer and $buffer =~ /type=["\']result["\']/ ) {
498 $logger->transport( " * $jid: Jabber authenticated and connected", DEBUG );
500 if( !$buffer ) { $buffer = " "; }
502 throw OpenSRF::EX::Jabber( " * $jid: Unable to authenticate: $buffer", ERROR );
509 my( $class, $app ) = @_;
510 $logger->transport("Constructing new Jabber connection for $app, my class $class", INTERNAL );
512 $class->new( $app )->initialize() );
517 my( $self, $timeout ) = @_;
520 undef $timeout if ( $timeout == -1 );
522 unless( $self->{_socket}->connected ) {
523 OpenSRF::EX::JabberDisconnected->throw(
524 "This JabberClient instance is no longer connected to the server", ERROR );
527 my $val = $self->timed_read( $timeout );
529 $timeout = "FOREVER" unless ( defined $timeout );
531 if ( ! defined( $val ) ) {
532 OpenSRF::EX::Jabber->throw(
533 "Call to Client->timed_read( $timeout ) failed", ERROR );
536 "Call to Client->timed_read( $timeout ) returned 0 bytes of data", DEBUG );
539 "Call to Client->timed_read( $timeout ) successfully returned data", INTERNAL );