| 1 |
elmex |
1.1 |
package Net::XMPP2::Client; |
| 2 |
|
|
use strict; |
| 3 |
|
|
use AnyEvent; |
| 4 |
elmex |
1.2 |
use Net::XMPP2::IM::Connection; |
| 5 |
|
|
use Net::XMPP2::Util qw/stringprep_jid prep_bare_jid/; |
| 6 |
|
|
use Net::XMPP2::Namespaces qw/xmpp_ns/; |
| 7 |
elmex |
1.1 |
use Net::XMPP2::Event; |
| 8 |
elmex |
1.2 |
use Net::XMPP2::IM::Account; |
| 9 |
|
|
|
| 10 |
|
|
use XML::Twig; |
| 11 |
|
|
|
| 12 |
|
|
sub _dumpxml { |
| 13 |
|
|
my $data = shift; |
| 14 |
|
|
my $t = XML::Twig->new; |
| 15 |
|
|
if ($t->safe_parse ("<deb>$data</deb>")) { |
| 16 |
|
|
$t->set_pretty_print ('indented'); |
| 17 |
|
|
$t->print; |
| 18 |
|
|
print "\n"; |
| 19 |
|
|
} else { |
| 20 |
|
|
print "[$data]\n"; |
| 21 |
|
|
} |
| 22 |
|
|
} |
| 23 |
elmex |
1.1 |
|
| 24 |
|
|
our @ISA = qw/Net::XMPP2::Event/; |
| 25 |
|
|
|
| 26 |
|
|
=head1 NAME |
| 27 |
|
|
|
| 28 |
|
|
Net::XMPP2::Client - A XMPP Client abstraction |
| 29 |
|
|
|
| 30 |
|
|
=head1 SYNOPSIS |
| 31 |
|
|
|
| 32 |
|
|
use Net::XMPP2::Client; |
| 33 |
|
|
use AnyEvent; |
| 34 |
|
|
|
| 35 |
|
|
my $j = AnyEvent->condvar; |
| 36 |
|
|
|
| 37 |
|
|
my $cl = Net::XMPP2::Client->new; |
| 38 |
|
|
$cl->start; |
| 39 |
|
|
|
| 40 |
|
|
$j->wait; |
| 41 |
|
|
|
| 42 |
|
|
=head1 DESCRIPTION |
| 43 |
|
|
|
| 44 |
|
|
This module tries to implement a straight forward and easy to |
| 45 |
|
|
use API to communicate with XMPP entities. L<Net::XMPP2::Client> |
| 46 |
|
|
handles connections and timeouts and all such stuff for you. |
| 47 |
|
|
|
| 48 |
|
|
For more flexibility please have a look at L<Net::XMPP2::Connection> |
| 49 |
|
|
and L<Net::XMPP2::IM::Connection>, they allow you to control what |
| 50 |
|
|
and how something is being sent more precisely. |
| 51 |
|
|
|
| 52 |
|
|
=head1 METHODS |
| 53 |
|
|
|
| 54 |
|
|
=head2 new (%args) |
| 55 |
|
|
|
| 56 |
|
|
Following arguments can be passed in C<%args>: |
| 57 |
|
|
|
| 58 |
|
|
=over 4 |
| 59 |
|
|
|
| 60 |
|
|
=back |
| 61 |
|
|
|
| 62 |
|
|
=cut |
| 63 |
|
|
|
| 64 |
|
|
sub new { |
| 65 |
|
|
my $this = shift; |
| 66 |
|
|
my $class = ref($this) || $this; |
| 67 |
|
|
my $self = { @_ }; |
| 68 |
|
|
bless $self, $class; |
| 69 |
elmex |
1.2 |
return $self; |
| 70 |
|
|
} |
| 71 |
|
|
|
| 72 |
|
|
=head2 add_account ($jid, $password, $host, $port) |
| 73 |
|
|
|
| 74 |
|
|
This method adds a jabber account for connection with the JID C<$jid> |
| 75 |
|
|
and the password C<$password>. |
| 76 |
|
|
|
| 77 |
|
|
C<$host> and C<$port> are optional and can be undef. C<$host> overrides the |
| 78 |
|
|
host to connect to. |
| 79 |
|
|
|
| 80 |
|
|
Returns 1 on success and undef when the account already exists. |
| 81 |
|
|
|
| 82 |
|
|
=cut |
| 83 |
|
|
|
| 84 |
|
|
sub add_account { |
| 85 |
|
|
my ($self, $jid, $password, $host, $port) = @_; |
| 86 |
|
|
|
| 87 |
|
|
$jid = stringprep_jid $jid; |
| 88 |
|
|
my $bj = prep_bare_jid $jid; |
| 89 |
|
|
|
| 90 |
|
|
return if exists $self->{accounts}->{$bj}; |
| 91 |
|
|
|
| 92 |
|
|
my $acc = |
| 93 |
|
|
$self->{accounts}->{$bj} = |
| 94 |
|
|
Net::XMPP2::IM::Account->new ( |
| 95 |
|
|
jid => $jid, |
| 96 |
|
|
password => $password, |
| 97 |
|
|
host => $host, |
| 98 |
|
|
port => $port, |
| 99 |
|
|
); |
| 100 |
|
|
|
| 101 |
|
|
$self->update_connections |
| 102 |
|
|
if $self->{started}; |
| 103 |
|
|
|
| 104 |
|
|
$acc |
| 105 |
|
|
} |
| 106 |
|
|
|
| 107 |
|
|
=head2 start () |
| 108 |
|
|
|
| 109 |
|
|
This method initiates the connections to the XMPP servers. |
| 110 |
|
|
|
| 111 |
|
|
=cut |
| 112 |
|
|
|
| 113 |
|
|
sub start { |
| 114 |
|
|
my ($self) = @_; |
| 115 |
|
|
$self->{started} = 1; |
| 116 |
|
|
$self->update_connections; |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
sub update_connections { |
| 120 |
|
|
my ($self) = @_; |
| 121 |
elmex |
1.1 |
|
| 122 |
elmex |
1.2 |
for my $acc (values %{$self->{accounts}}) { |
| 123 |
|
|
unless ($acc->is_connected) { |
| 124 |
|
|
my $con = $acc->spawn_connection; |
| 125 |
|
|
|
| 126 |
|
|
$con->reg_cb ( |
| 127 |
|
|
session_ready => sub { |
| 128 |
|
|
my ($con) = @_; |
| 129 |
|
|
$self->event (connected => $acc); |
| 130 |
|
|
warn "ADDED ACCOUNT $acc->{jid} [$con]\n"; |
| 131 |
|
|
0 # do once |
| 132 |
|
|
}, |
| 133 |
|
|
debug_recv => sub { print "RRRRRRRRECVVVVVV:\n"; _dumpxml ($_[1]); 1 }, |
| 134 |
|
|
debug_send => sub { print "SSSSSSSSENDDDDDD:\n"; _dumpxml ($_[1]); 1 }, |
| 135 |
|
|
message => sub { |
| 136 |
|
|
my ($con, $msg) = @_; |
| 137 |
|
|
$self->event (message => $acc, $msg); |
| 138 |
|
|
1 |
| 139 |
|
|
}, |
| 140 |
elmex |
1.3 |
sasl_error => sub { |
| 141 |
|
|
my ($con, $error) = @_; |
| 142 |
|
|
$self->event (sasl_error => $acc, $error); |
| 143 |
|
|
1 |
| 144 |
|
|
}, |
| 145 |
elmex |
1.2 |
presence_update => sub { |
| 146 |
|
|
my ($con, $roster, $contact, $old, $new) = @_; |
| 147 |
|
|
$self->event (presence_update => $acc, $roster, $contact, $old, $new); |
| 148 |
|
|
1 |
| 149 |
|
|
} |
| 150 |
|
|
); |
| 151 |
|
|
|
| 152 |
|
|
$con->connect |
| 153 |
|
|
or die "Couldn't connect to ".($acc->jid).": $!"; |
| 154 |
|
|
$con->init |
| 155 |
|
|
} |
| 156 |
|
|
} |
| 157 |
|
|
} |
| 158 |
|
|
|
| 159 |
|
|
=item send_message ($msg, $dest_jid, $src) |
| 160 |
|
|
|
| 161 |
|
|
Sends a message to the destination C<$dest_jid>. |
| 162 |
|
|
C<$msg> can either be a string or a L<Net::XMPP2::IM::Message> object. |
| 163 |
|
|
If C<$msg> is such an object C<$dest_jid> is optional, and will, when |
| 164 |
|
|
passed, override the destination of the message. |
| 165 |
|
|
|
| 166 |
|
|
C<$src> is optional. It specifies which account to use |
| 167 |
|
|
to send the message. If it is not passed L<Net::XMPP2::Client> will try |
| 168 |
|
|
to find an account itself. First it will look through all rosters |
| 169 |
|
|
to find C<$dest_jid> and if none found it will pick any of the accounts that |
| 170 |
|
|
are connected. |
| 171 |
|
|
|
| 172 |
|
|
C<$src> can either be a JID or a L<Net::XMPP2::IM::Account> object as returned |
| 173 |
|
|
by C<add_account> and C<get_account>. |
| 174 |
|
|
|
| 175 |
|
|
=cut |
| 176 |
|
|
|
| 177 |
|
|
sub send_message { |
| 178 |
|
|
my ($self, $msg, $dest_jid, $src) = @_; |
| 179 |
|
|
|
| 180 |
|
|
unless (ref $msg) { |
| 181 |
|
|
$msg = Net::XMPP2::IM::Message->new (body => $msg); |
| 182 |
|
|
} |
| 183 |
|
|
|
| 184 |
|
|
if (defined $dest_jid) { |
| 185 |
|
|
my $jid = stringprep_jid $dest_jid |
| 186 |
|
|
or die "send_message: \$dest_jid is not a proper JID"; |
| 187 |
|
|
$msg->to ($jid); |
| 188 |
|
|
} |
| 189 |
|
|
|
| 190 |
|
|
my $srcacc; |
| 191 |
|
|
if (ref $src) { |
| 192 |
|
|
$srcacc = $src; |
| 193 |
|
|
} elsif (defined $src) { |
| 194 |
|
|
$srcacc = $self->get_account ($src) |
| 195 |
|
|
} else { |
| 196 |
|
|
$srcacc = $self->find_account_for_dest_jid ($dest_jid); |
| 197 |
|
|
} |
| 198 |
|
|
|
| 199 |
|
|
unless ($srcacc && $srcacc->is_connected) { |
| 200 |
|
|
die "send_message: Couldn't get connected account for sending" |
| 201 |
|
|
} |
| 202 |
|
|
|
| 203 |
|
|
$msg->send ($srcacc->connection) |
| 204 |
|
|
} |
| 205 |
|
|
|
| 206 |
|
|
=item get_account ($jid) |
| 207 |
|
|
|
| 208 |
|
|
Returns the L<Net::XMPP2::IM::Account> account object for the JID C<$jid> |
| 209 |
|
|
if there is any such account added. (returns undef otherwise). |
| 210 |
|
|
|
| 211 |
|
|
=cut |
| 212 |
|
|
|
| 213 |
|
|
sub get_account { |
| 214 |
|
|
my ($self, $jid) = @_; |
| 215 |
|
|
$self->{accounts}->{prep_bare_jid $jid} |
| 216 |
|
|
} |
| 217 |
|
|
|
| 218 |
|
|
sub find_account_for_dest_jid { |
| 219 |
|
|
my ($self, $jid) = @_; |
| 220 |
|
|
|
| 221 |
|
|
my $any_acc; |
| 222 |
|
|
for my $acc (values %{$self->{accounts}}) { |
| 223 |
|
|
next unless $acc->is_connected; |
| 224 |
|
|
|
| 225 |
|
|
# take "first" active account |
| 226 |
|
|
$any_acc = $acc unless defined $any_acc; |
| 227 |
|
|
|
| 228 |
|
|
my $roster = $acc->connection ()->get_roster; |
| 229 |
|
|
if (my $c = $roster->get_contact ($jid)) { |
| 230 |
|
|
return $acc; |
| 231 |
|
|
} |
| 232 |
|
|
} |
| 233 |
|
|
|
| 234 |
|
|
$any_acc |
| 235 |
elmex |
1.1 |
} |
| 236 |
|
|
|
| 237 |
|
|
=head1 EVENTS |
| 238 |
|
|
|
| 239 |
|
|
These events can be registered on with C<reg_cb>: |
| 240 |
|
|
|
| 241 |
elmex |
1.2 |
In the following event descriptions the argument C<$account> |
| 242 |
|
|
is always a L<Net::XMMP2::IM::Account> object. |
| 243 |
|
|
|
| 244 |
elmex |
1.1 |
=over 4 |
| 245 |
|
|
|
| 246 |
elmex |
1.2 |
=item connected => $account |
| 247 |
|
|
|
| 248 |
|
|
This event is sent when the C<$account> was successfully connected. |
| 249 |
|
|
|
| 250 |
|
|
=item connect_error => $account |
| 251 |
|
|
|
| 252 |
|
|
This event is emitted when an error occured in the connection process for the |
| 253 |
|
|
account C<$account>. |
| 254 |
|
|
|
| 255 |
|
|
=item error => $account |
| 256 |
|
|
|
| 257 |
|
|
This event is emitted when any error occured while communicating |
| 258 |
|
|
over the connection to the C<$account> - after a connection was established. |
| 259 |
|
|
|
| 260 |
|
|
=item presence_update => $account, $roster, $contact, $old_presence, $new_presence |
| 261 |
elmex |
1.1 |
|
| 262 |
elmex |
1.2 |
This event is emitted when a presence update was received on the C<$account>. |
| 263 |
|
|
For a description of the other argument please look at the documentation |
| 264 |
|
|
of the C<presence_update> event in L<Net::XMPP2::IM::Connection>. |
| 265 |
elmex |
1.1 |
|
| 266 |
elmex |
1.2 |
=item message => $account, $msg |
| 267 |
elmex |
1.1 |
|
| 268 |
elmex |
1.2 |
This event is emited when a message has been received on the C<$account>. |
| 269 |
|
|
C<$msg> is a L<Net::XMPP2::IM::Message> object. |
| 270 |
elmex |
1.1 |
|
| 271 |
|
|
=back |
| 272 |
|
|
|
| 273 |
|
|
=head1 AUTHOR |
| 274 |
|
|
|
| 275 |
|
|
Robin Redeker, C<< <elmex at ta-sa.org> >> |
| 276 |
|
|
|
| 277 |
|
|
=head1 COPYRIGHT & LICENSE |
| 278 |
|
|
|
| 279 |
|
|
Copyright 2007 Robin Redeker, all rights reserved. |
| 280 |
|
|
|
| 281 |
|
|
This program is free software; you can redistribute it and/or modify it |
| 282 |
|
|
under the same terms as Perl itself. |
| 283 |
|
|
|
| 284 |
|
|
=cut |
| 285 |
|
|
|
| 286 |
|
|
1; # End of Net::XMPP2::Client |