XMPP/Ext/OOB.pm

filter: Statementstmt 25.0% 11 / 44 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 25.0% 3 / 12 Podpod 85.7% 6 / 7 totaltotal 21.2% 20 / 94 CC 13 SCAR 27.0
CC
13
Cov
16%
url_from_node
30.0
__ANON__
30.0
new
6.0
CRAP 14.9
« XMPP/Ext/MUC.pm ↑ Summary XMPP/Ext/Ping.pm »
1 package AnyEvent::XMPP::Ext::OOB;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use AnyEvent::XMPP::Ext;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our @ISA = qw/AnyEvent::XMPP::Ext/;
7  
8 =head1 NAME
9  
10 AnyEvent::XMPP::Ext::OOB - XEP-0066 Out of Band Data
11  
12 =head1 SYNOPSIS
13  
14  
15 my $con = AnyEvent::XMPP::Connection->new (...);
16 $con->add_extension (my $disco = AnyEvent::XMPP::Ext::Disco->new);
17 $con->add_extension (my $oob = AnyEvent::XMPP::Ext::OOB->new);
18 $disco->enable_feature ($oob->disco_feature);
19  
20 $oob->reg_cb (oob_recv => sub {
21 my ($oob, $con, $node, $url) = @_;
22  
23 if (got ($url)) {
24 $oob->reply_success ($con, $node);
25 } else {
26 $oob->reply_failure ($con, $node, 'not-found');
27 }
28 });
29  
30 $oob->send_url (
31 $con, 'someonewho@wants.an.url.com', "http://nakedgirls.com/marie_021.jpg",
32 "Yaww!!! Hot like SUN!",
33 sub {
34 my ($error) = @_;
35 if ($error) { # then error
36 } else { # everything fine
37 }
38 }
39 )
40  
41  
42 =head1 DESCRIPTION
43  
44 This module provides a helper abstraction for handling out of band
45 data as specified in XEP-0066.
46  
47 The object that is generated handles out of band data requests to and
48 from others.
49  
50 There is are also some utility function defined to get for example the
51 oob info from an XML element:
52  
53 =head1 FUNCTIONS
54  
55 =over 4
56  
57 =item B<url_from_node ($node)>
58  
59 This function extracts the URL and optionally a description
60 field from the XML element in C<$node> (which must be a
61 L<AnyEvent::XMPP::Node>).
62  
63 C<$node> must be the XML node which contains the <url> and optionally <desc> element
64 (which is eg. a <x xmlns='jabber:x:oob'> element)!
65  
66 (This method searches both, the jabber:x:oob and jabber:iq:oob namespaces for
67 the <url> and <desc> elements).
68  
69 It returns a hash reference which should have following structure:
70  
71 {
72 url => "http://someurl.org/mycoolparty.jpg",
73 desc => "That was a party!",
74 }
75  
76 If nothing was found this method returns nothing (undef).
77  
78 =cut
79  
80 0 ▶sub url_from_node {
Subroutine url_from_node: not called CC=5 SCAR=34.0
Pod documented
81 0 my ($node) = @_;
82 0 my ($url) = $node->find_all ([qw/x_oob url/]);
83 0 my ($desc) = $node->find_all ([qw/x_oob desc/]);
84 0 my ($url2) = $node->find_all ([qw/iq_oob url/]);
85 0 my ($desc2) = $node->find_all ([qw/iq_oob desc/]);
86 0 ▶ $url ||= $url2;
Condition0%0/3
$url ||= $url2
l!l&&r!l&&!r
000
MC/DC0%0/2
$url ||= $url2
$url $url2
Truth table0%0/3
A ||= B
A= $urlB= $url2
ABresult
1X1
011
000
87 0 ▶ $desc ||= $desc2;
Condition0%0/3
$desc ||= $desc2
l!l&&r!l&&!r
000
MC/DC0%0/2
$desc ||= $desc2
$desc $desc2
Truth table0%0/3
A ||= B
A= $descB= $desc2
ABresult
1X1
011
000
88  
89 0 ▶ defined $url
Branch: 2 branches
BranchTrueFalseTotal
defined $url ? : 0 0 0
$desc ? : 0 0 0
90 ? { url => $url->text, desc => ($desc ? $desc->text : undef) }
91 : ()
92 }
93  
94 =back
95  
96 =head1 METHODS
97  
98 =over 4
99  
100 =item B<new ()>
101  
102 This is the constructor, it takes no further arguments.
103  
104 =cut
105  
106 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
107 0 my $this = shift;
108 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
109 0 my $self = bless { @_ }, $class;
110 0 $self->init;
111 0 $self
112 }
113  
114 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
115 0 my ($self) = @_;
116  
117 $self->reg_cb (
118 0 ▶ iq_set_request_xml => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
119 0 my ($self, $con, $node, $handled) = @_;
120  
121 0 for ($node->find_all ([qw/iq_oob query/])) {
122 0 my $url = url_from_node ($_);
123 0 $self->event (oob_recv => $con, $node, $url);
124 0 $$handled = 1;
125 }
126 }
127 0 );
128 }
129  
130 0 ▶sub disco_feature { (xmpp_ns ('x_oob'), xmpp_ns ('iq_oob')) }
Subroutine disco_feature: not called CC=1 SCAR=6.9
Pod documented
131  
132 =item B<reply_success ($con, $node)>
133  
134 This method replies to the sender of the oob that the URL
135 was retrieved successfully.
136  
137 C<$con> and C<$node> are the C<$con> and C<$node> arguments
138 of the C<oob_recv> event you want to reply to.
139  
140 =cut
141  
142 0 ▶sub reply_success {
Subroutine reply_success: not called CC=1 SCAR=6.9
Pod documented
143 0 my ($self, $con, $node) = @_;
144 0 $con->reply_iq_result ($node);
145 }
146  
147 =item B<reply_failure ($con, $node, $type)>
148  
149 This method replies to the sender that either the transfer was rejected
150 or it was not fount.
151  
152 If the transfer was rejectes you have to set C<$type> to 'reject',
153 otherwise C<$type> must be 'not-found'.
154  
155 C<$con> and C<$node> are the C<$con> and C<$node> arguments
156 of the C<oob_recv> event you want to reply to.
157  
158 =cut
159  
160 0 ▶sub reply_failure {
Subroutine reply_failure: not called CC=2 SCAR=17.9
Pod documented
161 0 my ($self, $con, $node, $type) = @_;
162  
163 0 ▶ if ($type eq 'reject') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'reject') { } 0 0 0
164 0 $con->reply_iq_error ($node, 'cancel', 'item-not-found');
165 } else {
166 0 $con->reply_iq_error ($node, 'modify', 'not-acceptable');
167 }
168 }
169  
170 =item B<send_url ($con, $jid, $url, $desc, $cb)>
171  
172 This method sends a out of band file transfer request to C<$jid>.
173 C<$url> is the URL that the otherone has to download. C<$desc> is an optional
174 description string (human readable) for the file pointed at by the url and
175 can be undef when you don't want to transmit any description.
176  
177 C<$cb> is a callback that will be called once the transfer is successful.
178  
179 The first argument to the callback will either be undef in case of success
180 or 'reject' when the other side rejected the file or 'not-found' if the other
181 side was unable to download the file.
182  
183 =cut
184  
185 0 ▶sub send_url {
Subroutine send_url: not called CC=2 SCAR=17.9
Pod documented
186 0 my ($self, $con, $jid, $url, $desc, $cb) = @_;
187  
188 $con->send_iq (set => { defns => iq_oob => node => {
189 ns => iq_oob => name => 'query', childs => [
190 { ns => iq_oob => name => 'url', childs => [ $url ] },
191 { ns => iq_oob => name => 'desc', (defined $desc ? (childs => [ $desc ]) : ()) }
192 ]
193 0 ▶ }}, sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
194 0 my ($n, $e) = @_;
195 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) { } 0 0 0
196 0 ▶ $cb->($e->condition eq 'item-not-found' ? 'not-found' : 'reject')
Branch: 2 branches
BranchTrueFalseTotal
if $cb 0 0 0
$e->condition eq 'item-not-found' ? : 0 0 0
197 if $cb;
198 } else {
199 0 ▶ $cb->() if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
200 }
201 0 ▶ }, to => $jid);
Branch: 1 branch
BranchTrueFalseTotal
defined $desc ? : 0 0 0
202 }
203  
204 =back
205  
206 =head1 EVENTS
207  
208 These events can be registered to whith C<reg_cb>:
209  
210 =over 4
211  
212 =item oob_recv => $con, $node, $url
213  
214 This event is generated whenever someone wants to send you a out of band data file.
215 C<$url> is a hash reference like it's returned by C<url_from_node>.
216  
217 C<$con> is the L<AnyEvent::XMPP::Connection> (Or L<AnyEvent::XMPP::IM::Connection>)
218 the data was received from.
219  
220 C<$node> is the L<AnyEvent::XMPP::Node> of the IQ request, you can get the senders
221 JID from the 'from' attribute of it.
222  
223 If you fetched the file successfully you have to call C<reply_success>.
224 If you want to reject the file or couldn't get it call C<reply_failure>.
225  
226 =back
227  
228 =cut
229  
230 1 1
« XMPP/Ext/MUC.pm ↑ Summary XMPP/Ext/Ping.pm »