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";
27 my $socket = $self->{_socket};
28 if( $socket and $socket->connected() ) {
29 print $socket "</stream:stream>";
37 Creates a new Client object.
39 debug and log_file are not required if you don't care to log the activity,
40 however all other parameters are.
54 my( $class, %params ) = @_;
56 $class = ref( $class ) || $class;
58 my $port = $params{'port'} || return undef;
59 my $username = $params{'username'} || return undef;
60 my $resource = $params{'resource'} || return undef;
61 my $password = $params{'password'} || return undef;
62 my $host = $params{'host'} || return undef;
64 my $jid = "$username\@$host\/$resource";
66 my $self = bless {} => $class;
71 $self->username( $username );
72 $self->resource( $resource );
73 $self->password( $password );
74 $self->{temp_buffer} = "";
76 $logger->transport( "Creating Client instance: $host:$port, $username, $resource",
82 # clears the tmp buffer as well as the TCP buffer
86 $self->{temp_buffer} = "";
88 my $fh = $self->{_socket};
91 while( sysread( $fh, $t_buf, 4096 ) ) {}
94 # -------------------------------------------------
98 Gathers all Jabber messages sitting in the collection queue
99 and hands them each to their respective callbacks. This call
100 does not block (calls Process(0))
104 sub gather { my $self = shift; $self->process( 0 ); }
106 # -------------------------------------------------
110 Blocks and gathers incoming messages as they arrive. Does not return
111 unless an error occurs.
113 Throws an OpenSRF::EX::JabberException if the call to Process ever fails.
119 my $sock = $self->unix_sock();
120 my $socket = IO::Socket::UNIX->new( Peer => $sock );
121 $logger->transport( "Unix Socket opened by Listener", INTERNAL );
123 throw OpenSRF::EX::Socket( "Unable to connect to UnixServer: socket-file: $sock \n :=> $! " )
124 unless ($socket->connected);
127 my $o = $self->process( -1 );
128 $logger->transport( "Call to process() in listener returned:\n $o", INTERNAL );
129 if( ! defined( $o ) ) {
130 throw OpenSRF::EX::Jabber( "Listen Loop failed at 'process()'" );
135 throw OpenSRF::EX::Socket( "How did we get here?!?!" );
140 my $flags = fcntl($fh, F_GETFL, 0)
141 or die "Can't get flags for the socket: $!\n";
143 $logger->transport( "Setting NONBLOCK: original flags: $flags", INTERNAL );
145 fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
146 or die "Can't set flags for the socket: $!\n";
154 $logger->transport( "Restoring BLOCK: to flags $flags", INTERNAL );
155 fcntl($fh, F_SETFL, $flags) if defined $flags;
161 my $flags = fcntl($fh, F_GETFL, 0)
162 or die "Can't get flags for the socket: $!\n";
164 $flags &= ~O_NONBLOCK;
166 fcntl($fh, F_SETFL, $flags)
167 or die "Can't set flags for the socket: $!\n";
172 my ($self, $timeout) = @_;
174 $logger->transport( "Temp Buffer Contained: \n". $self->{temp_buffer}, INTERNAL) if $self->{temp_buffer};
175 if( $self->can( "app" ) ) {
176 $logger->transport( "timed_read called for ".$self->app.", I am: ".$self->jid, INTERNAL );
179 # See if there is a complete message in the temp_buffer
181 if( $self->{temp_buffer} ) {
182 my $buffer = $self->{temp_buffer};
184 $self->{temp_buffer} = '';
186 my ($tag) = ($buffer =~ /<([^\s\?\>]+)/o);
187 $logger->transport("Using tag: $tag ", INTERNAL);
189 if ( $buffer =~ /^(.*?<\/$tag>)(.*)/s) {
191 $self->{temp_buffer} = $2;
193 $logger->transport( "completed read with $buffer", INTERNAL );
194 } elsif ( $buffer =~ /^<$tag[^>]*?\/>(.*)/) {
195 $self->{temp_buffer} = $1;
197 $logger->transport( "completed read with $buffer", INTERNAL );
199 $self->{temp_buffer} = $buffer;
202 if( $buffer and $complete ) {
209 my $fh = $self->{_socket};
211 unless( $fh and $fh->connected ) {
212 throw OpenSRF::EX::Socket ("Attempted read on closed socket", ERROR );
215 $logger->transport( "Temp Buffer After first attempt: \n ".$self->{temp_buffer}, INTERNAL) if $self->{temp_buffer};
218 if (defined($timeout) && !$timeout) {
219 $flags = set_nonblock( $fh );
223 $logger->transport( "Calling timed_read with timetout $timeout", INTERNAL );
233 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
235 # alarm needs a number greater => 1.
236 my $alarm_timeout = $timeout;
237 if( $alarm_timeout > 0 and $alarm_timeout < 1 ) {
240 alarm $alarm_timeout;
243 my $buffer = $self->{temp_buffer};
244 $self->{temp_buffer} = '';
247 my $ff = fcntl($fh, F_GETFL, 0);
248 if ($ff == ($ff | O_NONBLOCK) and $timeout > 0 ) {
249 #throw OpenSRF::EX::ERROR ("File flags are set to NONBLOCK but timeout is $timeout", ERROR );
253 my $read_size = 1024;
255 while( my $n = sysread( $fh, $t_buf, $read_size ) ) {
257 if( $n < $read_size ) {
258 #reset_fl( $fh, $f ) if $f;
262 # see if there is any more data to grab...
263 $f = set_nonblock( $fh );
266 #sysread($fh, $buffer, 2048, length($buffer) );
267 #sysread( $fh, $t_buf, 2048 );
271 $logger->transport(" Got [$buffer] from the socket", INTERNAL);
274 $logger->transport(" First read Buffer\n [$buffer]", INTERNAL);
275 ($tag) = ($buffer =~ /<([^\s\?\>]+){1}/o);
277 $logger->transport("Using tag: $tag ", INTERNAL);
280 if (!$first_read && $buffer =~ /^(.*?<\/$tag>){1}(.*)/s) {
282 $self->{temp_buffer} = $2;
284 $logger->transport( "completed read with $buffer", INTERNAL );
285 } elsif (!$first_read && $buffer =~ /^<$tag[^>]*?\/>(.*)/) {
286 $self->{temp_buffer} = $1;
288 $logger->transport( "completed read with $buffer", INTERNAL );
293 } while (!$complete && $xml);
299 $logger->transport( "XML Read: $xml", INTERNAL );
300 #reset_fl( $fh, $flags) if defined $flags;
301 set_block( $fh ) if defined $flags;
313 # -------------------------------------------------
318 return 1 if ($self->{_socket} and $self->{_socket}->connected);
323 my( $self, $password ) = @_;
324 $self->{'oils:password'} = $password if $password;
325 return $self->{'oils:password'};
328 # -------------------------------------------------
331 my( $self, $username ) = @_;
332 $self->{'oils:username'} = $username if $username;
333 return $self->{'oils:username'};
336 # -------------------------------------------------
339 my( $self, $resource ) = @_;
340 $self->{'oils:resource'} = $resource if $resource;
341 return $self->{'oils:resource'};
344 # -------------------------------------------------
347 my( $self, $jid ) = @_;
348 $self->{'oils:jid'} = $jid if $jid;
349 return $self->{'oils:jid'};
353 my( $self, $port ) = @_;
354 $self->{'oils:port'} = $port if $port;
355 return $self->{'oils:port'};
359 my( $self, $host ) = @_;
360 $self->{'oils:host'} = $host if $host;
361 return $self->{'oils:host'};
364 # -------------------------------------------------
368 Sends a Jabber message.
371 to - The JID of the recipient
372 thread - The Jabber thread
373 body - The body of the message
381 my $to = $params{'to'} || return undef;
382 my $body = $params{'body'} || return undef;
383 my $thread = $params{'thread'} || "";
384 my $router_command = $params{'router_command'} || "";
385 my $router_class = $params{'router_class'} || "";
387 my $msg = OpenSRF::Transport::SlimJabber::MessageWrapper->new;
390 $msg->setThread( $thread ) if $thread;
391 $msg->setBody( $body );
392 $msg->set_router_command( $router_command );
393 $msg->set_router_class( $router_class );
397 "JabberClient Sending message to $to with thread $thread and body: \n$body", INTERNAL );
399 my $soc = $self->{_socket};
400 unless( $soc and $soc->connected ) {
401 throw OpenSRF::EX::Jabber ("No longer connected to jabber server");
403 print $soc $msg->toString;
406 "JabberClient Sent message to $to with thread $thread and body: \n$body", INTERNAL );
412 Connect to the server and log in.
414 Throws an OpenSRF::EX::JabberException if we cannot connect
415 to the server or if the authentication fails.
419 # --- The logging lines have been commented out until we decide
420 # on which log files we're using.
426 my $jid = $self->jid;
427 my $host = $self->host;
428 my $port = $self->port;
429 my $username = $self->username;
430 my $resource = $self->resource;
431 my $password = $self->password;
433 my $stream = <<" XML";
434 <stream:stream to='$host' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>
438 <iq id='123' type='set'>
439 <query xmlns='jabber:iq:auth'>
440 <username>$username</username>
441 <password>$password</password>
442 <resource>$resource</resource>
448 # --- 5 tries to connect to the jabber server
451 $logger->transport( "$jid: Attempting to connect to server...$host:$port (Try # $_)", WARN );
452 $socket = IO::Socket::INET->new( PeerHost => $host,
455 $logger->transport( "$jid: $_ connect attempt to $host:$port", WARN );
456 last if ( $socket and $socket->connected );
460 unless ( $socket and $socket->connected ) {
461 throw OpenSRF::EX::Jabber( " Could not connect to Jabber server: $!" );
464 $logger->transport( "Logging into jabber as $jid " .
465 "from " . ref( $self ), DEBUG );
467 print $socket $stream;
472 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
474 sysread($socket, $buffer, 4096);
475 $logger->transport( "Login buffer 1: $buffer", INTERNAL );
483 if( $socket and $socket->connected() ) {
484 $self->{_socket} = $socket;
486 throw OpenSRF::EX::Jabber( " ** Unable to connect to Jabber server", ERROR );
490 $buffer = $self->timed_read(10);
492 if( $buffer ) {$logger->transport( "Login buffer 2: $buffer", INTERNAL );}
494 if( $buffer and $buffer =~ /type=["\']result["\']/ ) {
495 $logger->transport( " * $jid: Jabber authenticated and connected", DEBUG );
497 if( !$buffer ) { $buffer = " "; }
499 throw OpenSRF::EX::Jabber( " * $jid: Unable to authenticate: $buffer", ERROR );
506 my( $class, $app ) = @_;
507 $logger->transport("Constructing new Jabber connection for $app, my class $class", INTERNAL );
509 $class->new( $app )->initialize() );
514 my( $self, $timeout ) = @_;
517 undef $timeout if ( $timeout == -1 );
519 unless( $self->{_socket}->connected ) {
520 OpenSRF::EX::JabberDisconnected->throw(
521 "This JabberClient instance is no longer connected to the server", ERROR );
524 my $val = $self->timed_read( $timeout );
526 $timeout = "FOREVER" unless ( defined $timeout );
528 if ( ! defined( $val ) ) {
529 OpenSRF::EX::Jabber->throw(
530 "Call to Client->timed_read( $timeout ) failed", ERROR );
533 "Call to Client->timed_read( $timeout ) returned 0 bytes of data", DEBUG );
536 "Call to Client->timed_read( $timeout ) successfully returned data", INTERNAL );