XMPP/Ext/Version.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 124 CC 9 SCAR 19.3
CC
9
Cov
0%
_version_from_node
20.0
init
6.0
set_os
6.0
CRAP 6.9
1 package AnyEvent::XMPP::Ext::Version;
2 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
3 use AnyEvent::XMPP::Util qw/simxml/;
4 use AnyEvent::XMPP::Ext;
5 use strict;
6  
7 our @ISA = qw/AnyEvent::XMPP::Ext/;
8  
9 =head1 NAME
10  
11 AnyEvent::XMPP::Ext::Version - Software version
12  
13 =head1 SYNOPSIS
14  
15 use AnyEvent::XMPP::Ext::Version;
16  
17 my $version = AnyEvent::XMPP::Ext::Version->new;
18 $version->set_name ("My client");
19 $version->set_version ("0.3");
20 $version->set_os (`uname -a`);
21  
22 $disco->enable_feature ($version->disco_feature);
23  
24 =head1 DESCRIPTION
25  
26 This module defines an extension to provide the abilities
27 to answer to software version requests and to request software
28 version from other entities.
29  
30 See also XEP-0092
31  
32 This class is derived from L<AnyEvent::XMPP::Ext> and can be added as extension to
33 objects that implement the L<AnyEvent::XMPP::Extendable> interface or derive from
34 it.
35  
36 =head1 METHODS
37  
38 =over 4
39  
40 =item B<new (%args)>
41  
42 Creates a new software version handle.
43  
44 =cut
45  
46 sub new {
47 my $this = shift;
48 my $class = ref($this) || $this;
49 my $self = bless { @_ }, $class;
50 $self->init;
51 $self
52 }
53  
54 sub disco_feature { xmpp_ns ('version') }
55  
56 sub init {
57 my ($self) = @_;
58  
59 $self->set_name ("AnyEvent::XMPP");
60 $self->set_version ("$AnyEvent::XMPP::VERSION");
61  
62  
63 $self->{cb_id} = $self->reg_cb (
64 iq_get_request_xml => sub {
65 my ($self, $con, $node, $handled) = @_;
66  
67 if ($self->handle_query ($con, $node)) {
68 $$handled = 1;
69 }
70 }
71 );
72 }
73  
74 =item B<set_name ($name)>
75  
76 This method sets the software C<$name> string, the default is "AnyEvent::XMPP".
77  
78 =cut
79  
80 sub set_name {
81 my ($self, $name) = @_;
82 $self->{name} = $name;
83 }
84  
85 =item B<set_version ($version)>
86  
87 This method sets the software C<$version> string that is replied.
88  
89 The default is C<$AnyEvent::XMPP::VERSION>.
90  
91 =cut
92  
93 sub set_version {
94 my ($self, $version) = @_;
95 $self->{version} = $version;
96 }
97  
98 =item B<set_os ($os)>
99  
100 This method sets the operating system string C<$os>. If you pass
101 undef the string will be removed.
102  
103 The default is no operating system string at all.
104  
105 You may want to pass something like this:
106  
107 $version->set_os (`uname -s -r -m -o`);
108  
109 =cut
110  
111 sub set_os {
112 my ($self, $os) = @_;
113 $self->{os} = $os;
114 delete $self->{os} unless defined $os;
115 }
116  
117 sub version_result {
118 my ($self) = @_;
119 (
120 { name => 'name' , childs => [ $self->{name} ] },
121 { name => 'version', childs => [ $self->{version} ] },
122 (defined $self->{os}
123 ? { name => 'os', childs => [ $self->{os} ] }
124 : ()
125 ),
126 )
127 }
128  
129 sub handle_query {
130 my ($self, $con, $node) = @_;
131  
132 if (my ($q) = $node->find_all ([qw/version query/])) {
133 my @result = $self->version_result;
134 $con->reply_iq_result (
135 $node, {
136 defns => 'version',
137 node => {
138 ns => 'version', name => 'query', childs => [
139 @result
140 ]
141 }
142 }
143 );
144 return 1
145 }
146  
147 ()
148 }
149  
150 sub _version_from_node {
151 my ($node) = @_;
152 my (@vers) = $node->find_all ([qw/version query/], [qw/version version/]);
153 my (@name) = $node->find_all ([qw/version query/], [qw/version name/]);
154 my (@os) = $node->find_all ([qw/version query/], [qw/version os/]);
155  
156 my $v = {};
157  
158 $v->{jid} = $node->attr ('from');
159 $v->{version} = $vers[0]->text if @vers;
160 $v->{name} = $name[0]->text if @name;
161 $v->{os} = $os[0]->text if @os;
162  
163 $v
164 }
165  
166 =item B<request_version ($con, $dest, $cb)>
167  
168 This method sends a version request to C<$dest> on the connection C<$con>.
169  
170 C<$cb> is the callback that will be called if either an error occured or
171 the result was received. The callback will also be called after the default IQ
172 timeout for the connection C<$con>.
173 The second argument for the callback will be either undef if no error occured
174 or a L<AnyEvent::XMPP::Error::IQ> error.
175 The first argument will be a hash reference with the following fields:
176  
177 =over 4
178  
179 =item jid
180  
181 The JID of the entity this version reply belongs to.
182  
183 =item version
184  
185 The software version string of the entity.
186  
187 =item name
188  
189 The software name of the entity.
190  
191 =item os
192  
193 The operating system of the entity, which might be undefined if none
194 was provided.
195  
196 =back
197  
198 Here an example of the structure of the hash reference:
199  
200 {
201 jid => 'juliet@capulet.com/balcony',
202 name => 'Exodus',
203 version => '0.7.0.4',
204 os => 'Windows-XP 5.01.2600',
205 }
206  
207 =cut
208  
209 sub request_version {
210 my ($self, $con, $dest, $cb) = @_;
211  
212 $con->send_iq (get => {
213 defns => 'version',
214 node => { ns => 'version', name => 'query' }
215 }, sub {
216 my ($n, $e) = @_;
217 if ($e) {
218 $cb->(undef, $e);
219 } else {
220 $cb->(_version_from_node ($n), undef);
221 }
222 }, to => $dest);
223 }
224  
225 sub DESTROY {
226 my ($self) = @_;
227 $self->unreg_cb ($self->{cb_id})
228 }
229  
230  
231 =back
232  
233 =head1 AUTHOR
234  
235 Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
236  
237 =head1 COPYRIGHT & LICENSE
238  
239 Copyright 2007, 2008 Robin Redeker, all rights reserved.
240  
241 This program is free software; you can redistribute it and/or modify it
242 under the same terms as Perl itself.
243  
244 =cut
245  
246 1;