File Coverage

blib/lib/AnyEvent/XMPP/IM/Presence.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package AnyEvent::XMPP::IM::Presence;
2 1     1   2325 use strict;
  1         3  
  1         44  
3 1     1   51 use AnyEvent::XMPP::Util;
  0            
  0            
4             use AnyEvent::XMPP::IM::Message;
5             use AnyEvent::XMPP::IM::Delayed;
6              
7             our @ISA = qw/AnyEvent::XMPP::IM::Delayed/;
8              
9             =head1 NAME
10              
11             AnyEvent::XMPP::IM::Presence - XMPP presence
12              
13             =head1 SYNOPSIS
14              
15             =head1 DESCRIPTION
16              
17             This module represents an XMPP presence. It stores
18             the full JID of the contact, the show value, status value
19             and priority.
20              
21             L is derived from L,
22             use the interface described there to find out whether this presence was delayed.
23              
24             =head1 METHODS
25              
26             =over 4
27              
28             =cut
29              
30             sub new {
31             my $this = shift;
32             my $class = ref($this) || $this;
33             bless { @_ }, $class;
34             }
35              
36             sub clone {
37             my ($self) = @_;
38             my $p = $self->new (connection => $self->{connection});
39             $p->{$_} = $self->{$_} for qw/show jid priority status/;
40             $p
41             }
42              
43             sub update {
44             my ($self, $node) = @_;
45              
46             $self->fetch_delay_from_node ($node);
47              
48             my $type = $node->attr ('type');
49             my ($show) = $node->find_all ([qw/client show/]);
50             my ($priority) = $node->find_all ([qw/client priority/]);
51              
52             my %stati;
53             $stati{$_->attr ('lang') || ''} = $_->text
54             for $node->find_all ([qw/client status/]);
55              
56             my $old = $self->clone;
57              
58             $self->{show} = $show ? $show->text : undef;
59             $self->{priority} = $priority ? $priority->text : undef;
60             $self->{status} = \%stati;
61             $self->{type} = $type;
62              
63             $old
64             }
65              
66             =item B
67              
68             Returns the full JID of this presence.
69              
70             =cut
71              
72             sub jid { $_[0]->{jid} }
73              
74             =item B
75              
76             Returns the priority of this presence.
77              
78             =cut
79              
80             sub priority { $_[0]->{priority} }
81              
82             =item B
83              
84             Returns all language tags of available status descriptions.
85             See also L.
86              
87             =cut
88              
89             sub status_all_lang {
90             my ($self, $jid) = @_;
91             keys %{$self->{status} || []}
92             }
93              
94             =item B
95              
96             Returns the show value of this presence, which is one of:
97              
98             'away', 'chat', 'dnd', 'xa'
99              
100             or the empty string if the presence is 'available'.
101              
102             =cut
103              
104             sub show { $_[0]->{show} }
105              
106             =item B
107              
108             Returns the presence description. C<$lang> is optional can should be one of
109             the tags returned by C.
110              
111             =cut
112              
113             sub status {
114             my ($self, $lang) = @_;
115              
116             if (defined $lang) {
117             return $self->{status}->{$lang}
118             } else {
119             return $self->{status}->{''}
120             if defined $self->{status}->{''};
121             return $self->{status}->{en}
122             if defined $self->{status}->{en};
123             }
124              
125             undef
126             }
127              
128             =item B
129              
130             Returns a L object with the to field set to
131             this presence full JID.
132              
133             C<%args> are further arguments to the constructor of the message.
134              
135             =cut
136              
137             sub message_class { 'AnyEvent::XMPP::IM::Message' }
138              
139             sub make_message {
140             my ($self, %args) = @_;
141             $self->message_class ()->new (
142             connection => $self->{connection},
143             to => $self->jid,
144             %args
145             );
146             }
147              
148             sub debug_dump {
149             my ($self) = @_;
150             printf " * %-30s [%-5s] (%3d) {%s}\n",
151             $self->jid,
152             $self->show || '',
153             $self->priority || 0,
154             $self->status || '',
155             }
156              
157             =back
158              
159             =head1 AUTHOR
160              
161             Robin Redeker, C<< >>, JID: C<< >>
162              
163             =head1 COPYRIGHT & LICENSE
164              
165             Copyright 2007, 2008 Robin Redeker, all rights reserved.
166              
167             This program is free software; you can redistribute it and/or modify it
168             under the same terms as Perl itself.
169              
170             =cut
171              
172             1;