AnyEvent-XMPP

 view release on metacpan or  search on metacpan

lib/AnyEvent/XMPP/Client.pm  view on Meta::CPAN

C<$jid> and the default for the C<port> parameter to the constructor of
L<AnyEvent::XMPP::Connection> (look there for details about DNS-SRV lookups).

C<$connection_args> must either be undef or a hash reference to
additional arguments for the constructor of the L<AnyEvent::XMPP::IM::Connection>
that will be used to connect the account.

Returns 1 on success and undef when the account already exists.

=cut

sub add_account {
   my ($self, $jid, $password, $host, $port, $connection_args) = @_;
   my $bj = prep_bare_jid $jid;

   my $acc = $self->{accounts}->{$bj};
   if ($acc) {
      $acc->{password} = $password;
      $acc->{host}     = $host;
      $acc->{port}     = $port;
      $acc->{args}     = $connection_args;
      return;
   }

   $acc =
      $self->{accounts}->{$bj} =
         AnyEvent::XMPP::IM::Account->new (
            jid      => $jid,
            password => $password,
            host     => $host,
            port     => $port,
            args     => $connection_args,
         );

   $self->event (added_account => $acc);

   $self->update_connections
      if $self->{started};

   $acc
}

=head2 start ()

This method initiates the connections to the XMPP servers.

=cut

sub start {
   my ($self) = @_;
   $self->{started} = 1;
   $self->update_connections;
}

=head2 update_connections ()

This method tries to connect all unconnected accounts.

=cut

sub update_connections {
   my ($self) = @_;

   Scalar::Util::weaken $self;

   for (values %{$self->{accounts}}) {
      my $acc = $_;

      if (!$acc->is_connected && !$self->{prep_connections}->{$acc->bare_jid}) {
         my %args = (initial_presence => 10);

         if (defined $self->{presence}) {
            if (defined $self->{presence}->{priority}) {
               $args{initial_presence} = $self->{presence}->{priority};
            }
         }

         my $con = $acc->spawn_connection (%args);
         $self->{prep_connections}->{$acc->bare_jid} = $con;

         $con->add_forward ($self, sub {
            my ($con, $self, $ev, @arg) = @_;
            $self->_event ($ev, $acc, @arg);
         });

         $con->reg_cb (
            session_ready => sub {
               my ($con) = @_;
               delete $self->{prep_connections}->{$acc->bare_jid};
               $self->event (connected => $acc);
               if (defined $self->{presence}) {
                  $con->send_presence (undef, undef, %{$self->{presence} || {}});
               }
               $con->unreg_me
            },
            disconnect => sub {
               my ($con, $h, $p, $err) = @_;
               $self->event (connect_error => $acc, $err);
               delete $self->{prep_connections}->{$acc->bare_jid};
               $con->unreg_me;
            },
            after_disconnect => sub {
               my ($con, $h, $p, $err) = @_;
               $con->remove_forward ($self);
            }
         );

         $con->connect;
      }
   }
}

=head2 disconnect ($msg)

Disconnect all accounts.

=cut

sub disconnect {
   my ($self, $msg) = @_;
   for my $acc (values %{$self->{accounts}}) {

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.077 second using v1.00-cache-2.02-grep-82fe00e-cpan-1310916c57ae )