1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
package AnyEvent::XMPP::IM::Contact;
use strict;
no warnings;
use AnyEvent::XMPP::Util qw/split_jid node_jid/;
use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
use AnyEvent::XMPP::IM::Presence;
use AnyEvent::XMPP::IM::Message;
=head1 NAME
AnyEvent::XMPP::IM::Contact - Instant messaging roster contact
=head1 SYNOPSIS
my $con = AnyEvent::XMPP::IM::Connection->new (...);
...
my $ro = $con->roster;
if (my $c = $ro->get_contact ('test@example.com')) {
$c->make_message ()->add_body ("Hello there!")->send;
}
=head1 DESCRIPTION
This module represents a class for contact objects which populate
a roster (L<AnyEvent::XMPP::IM::Roster>.
There are two types of 'contacts' that are managed by this class.
The first are contacts that are on the users roster, and the second
are contacts that are B<not> on the users roster.
To find our whether this is a contact which is actually available
as roster item in the users roster, you should consult the C<is_on_roster>
method (see below).
Another special kind of contact is the contact which stands for ourself
and is mostly only used for keeping track of our own presences and resources.
But note that even if the C<is_me> method returns true, the C<is_on_roster>
method might also return a true value, in case we have a roster item
of ourself on the roster (which might happen in rare cases :).
You can get an instance of this class only by calling the C<get_contact>
function on a roster object.
=head1 METHODS
=over 4
=cut
sub new {
my $this = shift;
my $class = ref($this) || $this;
bless { @_ }, $class;
}
=item B<send_update ($cb, %upd)>
This method updates a contact. If the request is finished
it will call C<$cb>. If it resulted in an error the first argument
of that callback will be a L<AnyEvent::XMPP::Error::IQ> object.
The C<%upd> hash should have one of the following keys and defines
what parts of the contact to update:
=over 4
=item name => $name
Updates the name of the contact. C<$name> = '' erases the contact.
=item add_group => $groups
Adds the contact to the groups in the array reference C<$groups>.
=item remove_group => $groups
Removes the contact from the groups in the array reference C<$groups>.
=item groups => $groups
This sets the groups of the contact. C<$groups> should be an array reference
of the groups.
=back
=cut
sub send_update {
my ($self, $cb, %upd) = @_;
if ($upd{groups}) {
$self->{groups} = $upd{groups};
}
for my $g (@{$upd{add_group} || []}) {
push @{$self->{groups}}, $g unless grep { $g eq $_ } $self->groups;
}
for my $g (@{$upd{remove_group} || []}) {
push @{$self->{groups}}, grep { $g ne $_ } $self->groups;
}
$self->{connection}->send_iq (
set => sub {
my ($w) = @_;
$w->addPrefix (xmpp_ns ('roster'), '');
$w->startTag ([xmpp_ns ('roster'), 'query']);
$w->startTag ([xmpp_ns ('roster'), 'item'],
jid => $self->jid,
(defined $upd{name} ? (name => $upd{name}) : ())
);
for ($self->groups) {
$w->startTag ([xmpp_ns ('roster'), 'group']);
$w->characters ($_);
$w->endTag;
}
$w->endTag;
$w->endTag;
},
sub {
my ($node, $error) = @_;
my $con = undef;
unless ($error) { $con = $self }
$cb->($con, $error) if $cb
}
);
}
=item B<send_subscribe ()>
This method sends this contact a subscription request.
=cut
sub send_subscribe {
my ($self) = @_;
$self->{connection}->send_presence ('subscribe', undef, to => $self->jid);
}
=item B<send_subscribed ()>
This method accepts a contact's subscription request.
=cut
sub send_subscribed {
my ($self) = @_;
$self->{connection}->send_presence ('subscribed', undef, to => $self->jid);
}
=item B<send_unsubscribe ()>
This method sends this contact a unsubscription request.
=cut
sub send_unsubscribe {
my ($self) = @_;
$self->{connection}->send_presence ('unsubscribe', undef, to => $self->jid);
}
=item B<send_unsubscribed ()>
This method sends this contact a unsubscription request which unsubscribes
ones own presence from him (he wont get any further presence from us).
=cut
sub send_unsubscribed {
my ($self) = @_;
$self->{connection}->send_presence ('unsubscribed', undef, to => $self->jid);
}
=item B<update ($item)>
This method wants a L<AnyEvent::XMPP::Node> in C<$item> which
should be a roster item received from the server. The method will
update the contact accordingly and return it self.
=cut
sub update {
my ($self, $item) = @_;
my ($jid, $name, $subscription, $ask) =
(
$item->attr ('jid'),
$item->attr ('name'),
$item->attr ('subscription'),
$item->attr ('ask')
);
$self->{name} = $name;
$self->{subscription} = $subscription;
$self->{groups} = [ map { $_->text } $item->find_all ([qw/roster group/]) ];
$self->{ask} = $ask;
$self
}
=item B<update_presence ($presence)>
This method updates the presence of contacts on the roster.
C<$presence> must be a L<AnyEvent::XMPP::Node> object and should be
a presence packet.
=cut
sub update_presence {
my ($self, $node) = @_;
my $type = $node->attr ('type');
my $jid = $node->attr ('from');
# XXX: should check whether C<$jid> is nice JID.
$self->touch_presence ($jid);
my $old;
my $new;
if ($type eq 'unavailable') {
$old = $self->remove_presence ($jid);
} else {
$old = $self->touch_presence ($jid)->update ($node);
$new = $self->touch_presence ($jid);
}
($self, $old, $new)
}
sub remove_presence {
my ($self, $jid) = @_;
my $sjid = AnyEvent::XMPP::Util::stringprep_jid ($jid);
delete $self->{presences}->{$sjid}
}
sub touch_presence {
my ($self, $jid) = @_;
my $sjid = AnyEvent::XMPP::Util::stringprep_jid ($jid);
unless (exists $self->{presences}->{$sjid}) {
$self->{presences}->{$sjid} =
AnyEvent::XMPP::IM::Presence->new (connection => $self->{connection}, jid => $jid);
}
$self->{presences}->{$sjid}
}
=item B<get_presence ($jid)>
This method returns a presence of this contact if
it is available. The return value is an instance of L<AnyEvent::XMPP::IM::Presence>
or undef if no such presence exists.
=cut
sub get_presence {
my ($self, $jid) = @_;
my $sjid = AnyEvent::XMPP::Util::stringprep_jid ($jid);
$self->{presences}->{$sjid}
}
=item B<get_presences>
Returns all presences of this contact in form of
L<AnyEvent::XMPP::IM::Presence> objects.
=cut
sub get_presences { values %{$_[0]->{presences}} }
=item B<get_priority_presence>
Returns the presence with the highest priority.
=cut
sub get_priority_presence {
my ($self) = @_;
my (@pres) =
sort {
$self->{presences}->{$b}->priority <=> $self->{presences}->{$a}->priority
} keys %{$self->{presences}};
return unless defined $pres[0];
$self->{presences}->{$pres[0]}
}
=item B<groups>
Returns the list of groups (strings) this contact is in.
=cut
sub groups {
@{$_[0]->{groups} || []}
}
=item B<jid>
Returns the bare JID of this contact.
=cut
sub jid {
$_[0]->{jid}
}
=item B<name>
Returns the (nick)name of this contact.
=cut
sub name {
$_[0]->{name}
}
=item B<is_on_roster ()>
Returns 1 if this is a contact that is officially on the
roster and not just a contact we've received presence information
for.
=cut
sub is_on_roster {
my ($self) = @_;
$self->{subscription} && $self->{subscription} ne ''
}
=item B<is_me>
Returns a true value when this contacts stands for ourself
and is only used for receiving presences of our own resources.
=cut
sub is_me {
my ($self) = @_;
$self->{is_me}
}
=item B<subscription>
Returns the subscription state of this contact, which
can be one of:
'none', 'to', 'from', 'both'
If the contact isn't on the roster anymore this method
returns:
'remove'
=cut
sub subscription {
$_[0]->{subscription}
}
=item B<ask>
Returns 'subscribe' if we asked this contact for subscription.
=cut
sub ask {
$_[0]->{ask}
}
=item B<subscription_pending>
Returns true if this contact has a pending subscription.
That means: the contact has to aknowledge the subscription.
=cut
sub subscription_pending {
my ($self) = @_;
$self->{ask}
}
=item B<nickname>
Returns the nickname of this contact (or, if none is set in the
roster, it returns the node part of the JID)
=cut
sub nickname {
my ($self) = @_;
my $n = $self->name;
if ($n eq '') {
$n = node_jid ($self->jid);
}
$n
}
sub message_class { 'AnyEvent::XMPP::IM::Message' }
=item B<make_message (%args)>
This method returns a L<AnyEvent::XMPP::IM::Message>
object with the to field set to this contacts JID.
C<%args> are further arguments for the message constructor.
=cut
sub make_message {
my ($self, %args) = @_;
$self->message_class ()->new (
connection => $self->{connection},
to => $self->jid,
%args
);
}
sub debug_dump {
my ($self) = @_;
printf "- %-30s [%-20s] (%s){%s}\n",
$self->jid,
$self->name || '',
$self->subscription,
$self->ask;
for ($self->get_presences) {
$_->debug_dump;
}
}
=back
=head1 AUTHOR
Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
=head1 COPYRIGHT & LICENSE
Copyright 2007, 2008 Robin Redeker, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1; # End of AnyEvent::XMPP::Contact
|