ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-XMPP2/lib/Net/XMPP2/IM/Contact.pm
Revision: 1.1
Committed: Sat Mar 17 12:27:57 2007 UTC (19 years, 8 months ago) by elmex
Branch: MAIN
Log Message:
some minor cleanups.

File Contents

# User Rev Content
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     }
41    
42     sub remove_presence {
43     my ($self, $jid) = @_;
44     my $sjid = Net::XMPP2::Util::stringprep_jid ($jid);
45     delete $self->{presences}->{$sjid}
46     }
47    
48     sub touch_presence {
49     my ($self, $jid) = @_;
50     my $sjid = Net::XMPP2::Util::stringprep_jid ($jid);
51    
52     unless (exists $self->{presences}->{$sjid}) {
53     $self->{presences}->{$sjid} =
54     Net::XMPP2::IM::Presence->new (connection => $self->{connection}, jid => $jid);
55     }
56     $self->{presences}->{$sjid}
57     }
58    
59     =head2 get_presence ($jid)
60    
61     This method returns a presence of this contact if
62     it is available. The return value is an instance of L<Net::XMPP2::IM::Presence>
63     or undef if no such presence exists.
64    
65     =cut
66    
67     sub get_presence {
68     my ($self, $jid) = @_;
69     my $sjid = Net::XMPP2::Util::stringprep_jid ($jid);
70     $self->{presences}->{$sjid}
71     }
72    
73     =head2 get_presences
74    
75     Returns all presences of this contact in form of
76     L<Net::XMPP2::IM::Presence> objects.
77    
78     =cut
79    
80     sub get_presences { values %{$_[0]->{presences}} }
81    
82     sub set_presence {
83     my ($self, $jid, %data) = @_;
84     if ($data{type} eq 'unavailable') {
85     $self->remove_presence ($jid);
86     } else {
87     my $p = $self->touch_presence ($jid);
88     $p->_set (%data);
89     }
90     }
91    
92     =head2 groups
93    
94     Returns the list of groups (strings) this contact is in.
95    
96     =cut
97    
98     sub groups {
99     @{$_[0]->{groups} || []}
100     }
101    
102     =head2 jid
103    
104     Returns the bare JID of this contact.
105    
106     =cut
107    
108     sub jid {
109     $_[0]->{jid}
110     }
111    
112     =head2 name
113    
114     Returns the (nick)name of this contact.
115    
116     =cut
117    
118     sub name {
119     $_[0]->{name}
120     }
121    
122     =head2 subscription
123    
124     Returns the subscription state of this contact, which
125     can be one of:
126    
127     'none', 'to', 'from', 'both'
128    
129     =cut
130    
131     sub subscription {
132     $_[0]->{subscription}
133     }
134    
135     sub make_message {
136     my ($self) = @_;
137     Net::XMPP2::IM::Message->new (
138     connection => $self->{connection},
139     to => $self->jid
140     );
141     }
142    
143     sub debug_dump {
144     my ($self) = @_;
145     printf "- %-30s [%-20s] (%s)\n",
146     $self->jid,
147     $self->name || '',
148     $self->subscription;
149    
150     for ($self->get_presences) {
151     $_->debug_dump;
152     }
153     }
154    
155     1;