| 1 |
elmex |
1.1 |
package Net::XMPP2::IM::Contact; |
| 2 |
|
|
use strict; |
| 3 |
|
|
use Net::XMPP2::Util; |
| 4 |
|
|
use Net::XMPP2::IM::Presence; |
| 5 |
|
|
use Net::XMPP2::IM::Message; |
| 6 |
|
|
|
| 7 |
|
|
=head1 NAME |
| 8 |
|
|
|
| 9 |
|
|
Net::XMPP2::IM::Contact - A instant messaging roster contact |
| 10 |
|
|
|
| 11 |
|
|
=head1 SYNOPSIS |
| 12 |
|
|
|
| 13 |
|
|
my $con = Net::XMPP2::IM::Connection->new (...); |
| 14 |
|
|
... |
| 15 |
|
|
my $ro = $con->roster; |
| 16 |
|
|
if (my $c = $ro->get_contact ('test@example.com')) { |
| 17 |
|
|
$c->make_message ()->add_body ("Hello there!")->send; |
| 18 |
|
|
} |
| 19 |
|
|
|
| 20 |
|
|
=head1 DESCRIPTION |
| 21 |
|
|
|
| 22 |
|
|
This module represents a class for contact objects which populate |
| 23 |
|
|
a roster (L<Net::XMPP2::IM::Roster>. |
| 24 |
|
|
|
| 25 |
|
|
You can get an instance of this class only by calling the C<get_contact> |
| 26 |
|
|
function on a roster object. |
| 27 |
|
|
|
| 28 |
|
|
=cut |
| 29 |
|
|
|
| 30 |
|
|
|
| 31 |
|
|
sub new { |
| 32 |
|
|
my $this = shift; |
| 33 |
|
|
my $class = ref($this) || $this; |
| 34 |
|
|
bless { @_ }, $class; |
| 35 |
|
|
} |
| 36 |
|
|
|
| 37 |
|
|
sub _set { |
| 38 |
|
|
my ($self, %data) = @_; |
| 39 |
|
|
$self->{$_} = $data{$_} for keys %data; |
| 40 |
elmex |
1.2 |
$self |
| 41 |
elmex |
1.1 |
} |
| 42 |
|
|
|
| 43 |
|
|
sub remove_presence { |
| 44 |
|
|
my ($self, $jid) = @_; |
| 45 |
|
|
my $sjid = Net::XMPP2::Util::stringprep_jid ($jid); |
| 46 |
|
|
delete $self->{presences}->{$sjid} |
| 47 |
|
|
} |
| 48 |
|
|
|
| 49 |
|
|
sub touch_presence { |
| 50 |
|
|
my ($self, $jid) = @_; |
| 51 |
|
|
my $sjid = Net::XMPP2::Util::stringprep_jid ($jid); |
| 52 |
|
|
|
| 53 |
|
|
unless (exists $self->{presences}->{$sjid}) { |
| 54 |
|
|
$self->{presences}->{$sjid} = |
| 55 |
|
|
Net::XMPP2::IM::Presence->new (connection => $self->{connection}, jid => $jid); |
| 56 |
|
|
} |
| 57 |
|
|
$self->{presences}->{$sjid} |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
=head2 get_presence ($jid) |
| 61 |
|
|
|
| 62 |
|
|
This method returns a presence of this contact if |
| 63 |
|
|
it is available. The return value is an instance of L<Net::XMPP2::IM::Presence> |
| 64 |
|
|
or undef if no such presence exists. |
| 65 |
|
|
|
| 66 |
|
|
=cut |
| 67 |
|
|
|
| 68 |
|
|
sub get_presence { |
| 69 |
|
|
my ($self, $jid) = @_; |
| 70 |
|
|
my $sjid = Net::XMPP2::Util::stringprep_jid ($jid); |
| 71 |
|
|
$self->{presences}->{$sjid} |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
|
=head2 get_presences |
| 75 |
|
|
|
| 76 |
|
|
Returns all presences of this contact in form of |
| 77 |
|
|
L<Net::XMPP2::IM::Presence> objects. |
| 78 |
|
|
|
| 79 |
|
|
=cut |
| 80 |
|
|
|
| 81 |
|
|
sub get_presences { values %{$_[0]->{presences}} } |
| 82 |
|
|
|
| 83 |
|
|
sub set_presence { |
| 84 |
|
|
my ($self, $jid, %data) = @_; |
| 85 |
elmex |
1.2 |
my $old; |
| 86 |
elmex |
1.1 |
if ($data{type} eq 'unavailable') { |
| 87 |
elmex |
1.2 |
$old = $self->remove_presence ($jid); |
| 88 |
elmex |
1.1 |
} else { |
| 89 |
|
|
my $p = $self->touch_presence ($jid); |
| 90 |
elmex |
1.2 |
$old = $p->clone; |
| 91 |
elmex |
1.1 |
$p->_set (%data); |
| 92 |
|
|
} |
| 93 |
elmex |
1.2 |
$old |
| 94 |
elmex |
1.1 |
} |
| 95 |
|
|
|
| 96 |
|
|
=head2 groups |
| 97 |
|
|
|
| 98 |
|
|
Returns the list of groups (strings) this contact is in. |
| 99 |
|
|
|
| 100 |
|
|
=cut |
| 101 |
|
|
|
| 102 |
|
|
sub groups { |
| 103 |
|
|
@{$_[0]->{groups} || []} |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
|
|
=head2 jid |
| 107 |
|
|
|
| 108 |
|
|
Returns the bare JID of this contact. |
| 109 |
|
|
|
| 110 |
|
|
=cut |
| 111 |
|
|
|
| 112 |
|
|
sub jid { |
| 113 |
|
|
$_[0]->{jid} |
| 114 |
|
|
} |
| 115 |
|
|
|
| 116 |
|
|
=head2 name |
| 117 |
|
|
|
| 118 |
|
|
Returns the (nick)name of this contact. |
| 119 |
|
|
|
| 120 |
|
|
=cut |
| 121 |
|
|
|
| 122 |
|
|
sub name { |
| 123 |
|
|
$_[0]->{name} |
| 124 |
|
|
} |
| 125 |
|
|
|
| 126 |
elmex |
1.2 |
=head2 is_on_roster () |
| 127 |
|
|
|
| 128 |
|
|
Returns 1 if this is a contact that is officially on the |
| 129 |
|
|
roster and not just a contact we've received presence information |
| 130 |
|
|
for. |
| 131 |
|
|
|
| 132 |
|
|
=cut |
| 133 |
|
|
|
| 134 |
|
|
sub is_on_roster { |
| 135 |
|
|
my ($self) = @_; |
| 136 |
|
|
$self->{subscription} && $self->{subscription} ne '' |
| 137 |
|
|
} |
| 138 |
|
|
|
| 139 |
elmex |
1.1 |
=head2 subscription |
| 140 |
|
|
|
| 141 |
|
|
Returns the subscription state of this contact, which |
| 142 |
|
|
can be one of: |
| 143 |
|
|
|
| 144 |
|
|
'none', 'to', 'from', 'both' |
| 145 |
|
|
|
| 146 |
|
|
=cut |
| 147 |
|
|
|
| 148 |
|
|
sub subscription { |
| 149 |
|
|
$_[0]->{subscription} |
| 150 |
|
|
} |
| 151 |
|
|
|
| 152 |
|
|
sub make_message { |
| 153 |
|
|
my ($self) = @_; |
| 154 |
|
|
Net::XMPP2::IM::Message->new ( |
| 155 |
|
|
connection => $self->{connection}, |
| 156 |
|
|
to => $self->jid |
| 157 |
|
|
); |
| 158 |
|
|
} |
| 159 |
|
|
|
| 160 |
|
|
sub debug_dump { |
| 161 |
|
|
my ($self) = @_; |
| 162 |
|
|
printf "- %-30s [%-20s] (%s)\n", |
| 163 |
|
|
$self->jid, |
| 164 |
|
|
$self->name || '', |
| 165 |
|
|
$self->subscription; |
| 166 |
|
|
|
| 167 |
|
|
for ($self->get_presences) { |
| 168 |
|
|
$_->debug_dump; |
| 169 |
|
|
} |
| 170 |
|
|
} |
| 171 |
|
|
|
| 172 |
|
|
1; |