File Coverage

blib/lib/AnyEvent/XMPP/Ext/OOB.pm
Criterion Covered Total %
statement 9 42 21.4
branch 0 16 0.0
condition 0 9 0.0
subroutine 3 12 25.0
pod 6 7 85.7
total 18 86 20.9


line stmt bran cond sub pod time code
1             package AnyEvent::XMPP::Ext::OOB;
2 1     1   1537 use strict;
  1         2  
  1         68  
3 1     1   7 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
  1         2  
  1         60  
4 1     1   643 use AnyEvent::XMPP::Ext;
  1         4  
  1         519  
5              
6             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
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).
62              
63             C<$node> must be the XML node which contains the and optionally element
64             (which is eg. a element)!
65              
66             (This method searches both, the jabber:x:oob and jabber:iq:oob namespaces for
67             the and 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             sub url_from_node {
81 0     0 1   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   0       $url ||= $url2;
87 0   0       $desc ||= $desc2;
88              
89 0 0         defined $url
    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
101              
102             This is the constructor, it takes no further arguments.
103              
104             =cut
105              
106             sub new {
107 0     0 1   my $this = shift;
108 0   0       my $class = ref($this) || $this;
109 0           my $self = bless { @_ }, $class;
110 0           $self->init;
111 0           $self
112             }
113              
114             sub init {
115 0     0 0   my ($self) = @_;
116              
117             $self->reg_cb (
118             iq_set_request_xml => sub {
119 0     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     0 1   sub disco_feature { (xmpp_ns ('x_oob'), xmpp_ns ('iq_oob')) }
131              
132             =item B
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 event you want to reply to.
139              
140             =cut
141              
142             sub reply_success {
143 0     0 1   my ($self, $con, $node) = @_;
144 0           $con->reply_iq_result ($node);
145             }
146              
147             =item B
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 event you want to reply to.
157              
158             =cut
159              
160             sub reply_failure {
161 0     0 1   my ($self, $con, $node, $type) = @_;
162              
163 0 0         if ($type eq 'reject') {
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
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             sub send_url {
186 0     0 1   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             }}, sub {
194 0     0     my ($n, $e) = @_;
195 0 0         if ($e) {
196 0 0         $cb->($e->condition eq 'item-not-found' ? 'not-found' : 'reject')
    0          
197             if $cb;
198             } else {
199 0 0         $cb->() if $cb;
200             }
201 0 0         }, to => $jid);
202             }
203              
204             =back
205              
206             =head1 EVENTS
207              
208             These events can be registered to whith C:
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.
216              
217             C<$con> is the L (Or L)
218             the data was received from.
219              
220             C<$node> is the L 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.
224             If you want to reject the file or couldn't get it call C.
225              
226             =back
227              
228             =cut
229              
230             1