ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-XMPP2/lib/Net/XMPP2/Client.pm
Revision: 1.16
Committed: Sat Jul 7 12:39:45 2007 UTC (19 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.15: +23 -2 lines
Log Message:
fixed some bugs, redesigned disco mechanism, probably added some bugs,
added samples/disco_test example.

File Contents

# User Rev Content
1 elmex 1.1 package Net::XMPP2::Client;
2     use strict;
3     use AnyEvent;
4 elmex 1.2 use Net::XMPP2::IM::Connection;
5 elmex 1.16 use Net::XMPP2::Util qw/stringprep_jid prep_bare_jid dump_twig_xml/;
6 elmex 1.2 use Net::XMPP2::Namespaces qw/xmpp_ns/;
7 elmex 1.1 use Net::XMPP2::Event;
8 elmex 1.16 use Net::XMPP2::Extendable;
9 elmex 1.2 use Net::XMPP2::IM::Account;
10    
11 elmex 1.11 #use XML::Twig;
12     #
13     #sub _dumpxml {
14     # my $data = shift;
15     # my $t = XML::Twig->new;
16     # if ($t->safe_parse ("<deb>$data</deb>")) {
17     # $t->set_pretty_print ('indented');
18     # $t->print;
19     # print "\n";
20     # } else {
21     # print "[$data]\n";
22     # }
23     #}
24 elmex 1.1
25 elmex 1.16 our @ISA = qw/Net::XMPP2::Event Net::XMPP2::Extendable/;
26 elmex 1.1
27     =head1 NAME
28    
29 elmex 1.13 Net::XMPP2::Client - XMPP Client abstraction
30 elmex 1.1
31     =head1 SYNOPSIS
32    
33     use Net::XMPP2::Client;
34     use AnyEvent;
35    
36     my $j = AnyEvent->condvar;
37    
38     my $cl = Net::XMPP2::Client->new;
39     $cl->start;
40    
41     $j->wait;
42    
43     =head1 DESCRIPTION
44    
45     This module tries to implement a straight forward and easy to
46     use API to communicate with XMPP entities. L<Net::XMPP2::Client>
47     handles connections and timeouts and all such stuff for you.
48    
49     For more flexibility please have a look at L<Net::XMPP2::Connection>
50     and L<Net::XMPP2::IM::Connection>, they allow you to control what
51     and how something is being sent more precisely.
52    
53     =head1 METHODS
54    
55     =head2 new (%args)
56    
57     Following arguments can be passed in C<%args>:
58    
59     =over 4
60    
61     =back
62    
63     =cut
64    
65     sub new {
66     my $this = shift;
67     my $class = ref($this) || $this;
68     my $self = { @_ };
69     bless $self, $class;
70 elmex 1.16 if ($self->{debug}) {
71     $self->reg_cb (
72     debug_recv => sub {
73     my ($self, $acc, $data) = @_;
74     printf "recv>> %s\n%s", $acc->jid, dump_twig_xml ($data)
75     },
76     debug_send => sub {
77     my ($self, $acc, $data) = @_;
78     printf "send<< %s\n%s", $acc->jid, dump_twig_xml ($data)
79     },
80     )
81     }
82 elmex 1.2 return $self;
83     }
84    
85 elmex 1.16 sub add_extension {
86     my ($self, $ext) = @_;
87     $self->add_forward ($ext, sub {
88     my ($self, $ext, $ev, $acc, @args) = @_;
89     $ext->event ($ev, $acc->connection (), @args);
90     });
91     }
92    
93 elmex 1.2 =head2 add_account ($jid, $password, $host, $port)
94    
95     This method adds a jabber account for connection with the JID C<$jid>
96     and the password C<$password>.
97    
98     C<$host> and C<$port> are optional and can be undef. C<$host> overrides the
99     host to connect to.
100    
101     Returns 1 on success and undef when the account already exists.
102    
103     =cut
104    
105     sub add_account {
106     my ($self, $jid, $password, $host, $port) = @_;
107    
108     $jid = stringprep_jid $jid;
109     my $bj = prep_bare_jid $jid;
110    
111     return if exists $self->{accounts}->{$bj};
112    
113     my $acc =
114     $self->{accounts}->{$bj} =
115     Net::XMPP2::IM::Account->new (
116     jid => $jid,
117     password => $password,
118     host => $host,
119     port => $port,
120     );
121    
122     $self->update_connections
123     if $self->{started};
124    
125     $acc
126     }
127    
128     =head2 start ()
129    
130     This method initiates the connections to the XMPP servers.
131    
132     =cut
133    
134     sub start {
135     my ($self) = @_;
136     $self->{started} = 1;
137     $self->update_connections;
138     }
139    
140 elmex 1.11 =head2 update_connections ()
141    
142     This method tries to connect all unconnected accounts.
143    
144     =cut
145    
146 elmex 1.2 sub update_connections {
147     my ($self) = @_;
148 elmex 1.1
149 elmex 1.2 for my $acc (values %{$self->{accounts}}) {
150     unless ($acc->is_connected) {
151     my $con = $acc->spawn_connection;
152    
153 elmex 1.5 $con->add_forward ($self, sub {
154     my ($con, $self, $ev, @arg) = @_;
155     $self->event ($ev, $acc, @arg);
156     });
157    
158 elmex 1.2 $con->reg_cb (
159     session_ready => sub {
160     my ($con) = @_;
161     $self->event (connected => $acc);
162     0 # do once
163     },
164 elmex 1.10 # debug_recv => sub { print "RRRRRRRRECVVVVVV:\n"; _dumpxml ($_[1]); 1 },
165     # debug_send => sub { print "SSSSSSSSENDDDDDD:\n"; _dumpxml ($_[1]); 1 },
166 elmex 1.7 disconnect => sub {
167     delete $self->{accounts}->{$acc};
168     0
169     }
170 elmex 1.2 );
171    
172 elmex 1.7 unless ($con->connect) {
173     $self->event (connect_error => "Couldn't connect to ".($acc->jid).": $!");
174     next
175     }
176 elmex 1.2 $con->init
177     }
178     }
179     }
180    
181 elmex 1.11 =head2 disconnect ($msg)
182 elmex 1.6
183     Disconnect all accounts.
184    
185     =cut
186    
187     sub disconnect {
188     my ($self, $msg) = @_;
189     for my $acc (values %{$self->{accounts}}) {
190     if ($acc->is_connected) { $acc->connection ()->disconnect ($msg) }
191     }
192     }
193    
194 elmex 1.11 =head2 remove_accounts ($msg)
195 elmex 1.6
196     Removes all accounts and disconnects.
197    
198     =cut
199    
200     sub remove_accounts {
201     my ($self, $msg) = @_;
202     for my $acc (keys %{$self->{accounts}}) {
203     my $acca = $self->{accounts}->{$acc};
204     if ($acca->is_connected) { $acca->connection ()->disconnect ($msg) }
205     delete $self->{accounts}->{$acc};
206     }
207     }
208    
209 elmex 1.11 =head2 remove_account ($acc)
210 elmex 1.7
211     Removes and disconnects account C<$acc>.
212    
213     =cut
214    
215     sub remove_account {
216     my ($self, $acc, $reason) = @_;
217     if ($acc->is_connected) {
218     $acc->connection ()->disconnect ($reason);
219     }
220     delete $self->{accounts}->{$acc};
221     }
222    
223 elmex 1.15 =head2 send_message ($msg, $dest_jid, $src, $type)
224 elmex 1.2
225     Sends a message to the destination C<$dest_jid>.
226     C<$msg> can either be a string or a L<Net::XMPP2::IM::Message> object.
227 elmex 1.15 If C<$msg> is such an object C<$dest_jid> is optional, but will, when
228 elmex 1.2 passed, override the destination of the message.
229    
230     C<$src> is optional. It specifies which account to use
231     to send the message. If it is not passed L<Net::XMPP2::Client> will try
232     to find an account itself. First it will look through all rosters
233     to find C<$dest_jid> and if none found it will pick any of the accounts that
234     are connected.
235    
236     C<$src> can either be a JID or a L<Net::XMPP2::IM::Account> object as returned
237     by C<add_account> and C<get_account>.
238    
239 elmex 1.15 C<$type> is optional but overrides the type of the message object in C<$msg>
240     if C<$msg> is such an object.
241    
242     C<$type> should be 'chat' for normal chatter. If no C<$type> is specified
243     the type of the message defaults to the value documented in L<Net::XMPP2::IM::Message>
244     (should be 'normal').
245    
246 elmex 1.2 =cut
247    
248     sub send_message {
249 elmex 1.15 my ($self, $msg, $dest_jid, $src, $type) = @_;
250 elmex 1.2
251     unless (ref $msg) {
252     $msg = Net::XMPP2::IM::Message->new (body => $msg);
253     }
254    
255     if (defined $dest_jid) {
256     my $jid = stringprep_jid $dest_jid
257     or die "send_message: \$dest_jid is not a proper JID";
258     $msg->to ($jid);
259     }
260    
261 elmex 1.15 $msg->type ($type) if defined $type;
262    
263 elmex 1.2 my $srcacc;
264     if (ref $src) {
265     $srcacc = $src;
266     } elsif (defined $src) {
267     $srcacc = $self->get_account ($src)
268     } else {
269     $srcacc = $self->find_account_for_dest_jid ($dest_jid);
270     }
271    
272     unless ($srcacc && $srcacc->is_connected) {
273     die "send_message: Couldn't get connected account for sending"
274     }
275    
276     $msg->send ($srcacc->connection)
277     }
278    
279 elmex 1.11 =head2 get_account ($jid)
280 elmex 1.2
281     Returns the L<Net::XMPP2::IM::Account> account object for the JID C<$jid>
282     if there is any such account added. (returns undef otherwise).
283    
284     =cut
285    
286     sub get_account {
287     my ($self, $jid) = @_;
288     $self->{accounts}->{prep_bare_jid $jid}
289     }
290    
291 elmex 1.11 =head2 get_accounts ()
292    
293     Returns a list of L<Net::XMPP2::IM::Account>s.
294    
295     =cut
296    
297 elmex 1.9 sub get_accounts {
298     my ($self) = @_;
299     values %{$self->{accounts}}
300     }
301    
302 elmex 1.11 =head2 get_accounts ()
303    
304     Returns a list of connected L<Net::XMPP2::IM::Account>s.
305    
306     Same as:
307    
308     grep { $_->is_connected } $client->get_accounts ();
309    
310     =cut
311    
312 elmex 1.7 sub get_connected_accounts {
313     my ($self, $jid) = @_;
314     my (@a) = grep $_->is_connected, values %{$self->{accounts}};
315     @a
316     }
317    
318 elmex 1.11 =head2 find_account_for_dest_jid ($jid)
319    
320     This method tries to find any account that has the contact C<$jid>
321     on his roster. If no account with C<$jid> on his roster was found
322     it takes the first one that is connected. (Return value is a L<Net::XMPP2::IM::Account>
323     object).
324    
325     If no account is connected it returns undef.
326    
327     =cut
328    
329 elmex 1.2 sub find_account_for_dest_jid {
330     my ($self, $jid) = @_;
331    
332     my $any_acc;
333     for my $acc (values %{$self->{accounts}}) {
334     next unless $acc->is_connected;
335    
336     # take "first" active account
337     $any_acc = $acc unless defined $any_acc;
338    
339     my $roster = $acc->connection ()->get_roster;
340     if (my $c = $roster->get_contact ($jid)) {
341     return $acc;
342     }
343     }
344    
345     $any_acc
346 elmex 1.1 }
347    
348 elmex 1.11 =head2 get_contacts_for_jid ($jid)
349    
350     This method returns all contacts that we are connected to.
351     That means: It joins the contact lists of all account's rosters
352     that we are connected to.
353    
354     =cut
355    
356 elmex 1.7 sub get_contacts_for_jid {
357     my ($self, $jid) = @_;
358     my @cons;
359     for ($self->get_connected_accounts) {
360     my $roster = $_->connection ()->get_roster ();
361     my $con = $roster->get_contact ($jid);
362     push @cons, $con if $con;
363     }
364     return @cons;
365     }
366    
367 elmex 1.11 =head2 get_priority_presence_for_jid ($jid)
368    
369     This method returns the presence for the contact C<$jid> with the highest
370     priority.
371    
372     If the contact C<$jid> is on multiple account's rosters it's undefined which
373     roster the presence belongs to.
374    
375     =cut
376    
377 elmex 1.8 sub get_priority_presence_for_jid {
378     my ($self, $jid) = @_;
379    
380     my $lpres;
381     for ($self->get_connected_accounts) {
382     my $roster = $_->connection ()->get_roster ();
383     my $con = $roster->get_contact ($jid);
384     next unless defined $con;
385     my $pres = $con->get_priority_presence ($jid);
386     next unless defined $pres;
387     if ((not defined $lpres) || $lpres->priority < $pres->priority) {
388     $lpres = $pres;
389     }
390     }
391    
392     $lpres
393     }
394    
395 elmex 1.11 =head2 set_presence ($show, $status, $priority)
396    
397     This sets the presence of all accounts. For a meaning of C<$show>, C<$status>
398     and C<$priority> see the description of the C<%attrs> hash in
399 elmex 1.12 C<send_presence> method of L<Net::XMPP2::Writer>.
400 elmex 1.11
401     =cut
402    
403 elmex 1.9 sub set_presence {
404     my ($self, $show, $status, $priority) = @_;
405    
406     for my $ac ($self->get_connected_accounts) {
407     my $con = $ac->connection ();
408     $con->send_presence (
409     undef, undef,
410     show => $show,
411     status => $status,
412     priority => $priority
413     );
414     }
415     }
416    
417 elmex 1.1 =head1 EVENTS
418    
419 elmex 1.2 In the following event descriptions the argument C<$account>
420 elmex 1.12 is always a L<Net::XMPP2::IM::Account> object.
421 elmex 1.2
422 elmex 1.5 All events from L<Net::XMPP2::IM::Connection> are forwarded to the client,
423     only that the first argument for every event is a C<$account> object.
424    
425     Aside fom those, these events can be registered on with C<reg_cb>:
426    
427 elmex 1.1 =over 4
428    
429 elmex 1.2 =item connected => $account
430    
431     This event is sent when the C<$account> was successfully connected.
432    
433 elmex 1.14 =item connect_error => $account, $reason
434 elmex 1.2
435     This event is emitted when an error occured in the connection process for the
436     account C<$account>.
437    
438     =item error => $account
439    
440     This event is emitted when any error occured while communicating
441     over the connection to the C<$account> - after a connection was established.
442    
443 elmex 1.1 =back
444    
445     =head1 AUTHOR
446    
447 elmex 1.11 Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
448 elmex 1.1
449     =head1 COPYRIGHT & LICENSE
450    
451     Copyright 2007 Robin Redeker, all rights reserved.
452    
453     This program is free software; you can redistribute it and/or modify it
454     under the same terms as Perl itself.
455    
456     =cut
457    
458     1; # End of Net::XMPP2::Client