XMPP/Ext/Receipts.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 180 CC 23 SCAR 58.5
CC
23
Cov
0%
init
506.0
new
6.0
disco_feature
2.0
CRAP 348.8
1 # vim:ts=4:sw=4:et
2 package AnyEvent::XMPP::Ext::Receipts;
3 use AnyEvent;
4 use AnyEvent::XMPP::Ext;
5 use AnyEvent::XMPP::Util qw/is_bare_jid/;
6 use AnyEvent::XMPP::Namespaces qw/set_xmpp_ns_alias/;
7 use Data::Dumper;
8 use warnings;
9 use strict;
10  
11 our @ISA = qw/AnyEvent::XMPP::Ext/;
12  
13 =head1 NAME
14  
15 AnyEvent::XMPP::Ext::Receipts - XEP-0184 message receipts
16  
17 =head1 SYNOPSIS
18  
19 use AnyEvent::XMPP::Ext::Disco;
20 use AnyEvent::XMPP::Ext::Receipts;
21  
22 my $disco = AnyEvent::XMPP::Ext::Disco->new();
23 $xmpp->add_extension($disco);
24  
25 my $receipts = AnyEvent::XMPP::Ext::Receipts->new(disco => $disco);
26 $xmpp->add_extension($receipts);
27  
28 $disco->enable_feature($receipts->disco_feature);
29  
30 =head1 DESCRIPTION
31  
32 This module adds support for XEP-0184 message receipts.
33  
34 Message receipts provide a way to verify that messages were received by the
35 recipient, as long as the recipient's client supports it.
36  
37 Note that you need to send messages with message receipts to full Jabber IDs
38 (e.g. jabber@example.com/android3948128), not bare Jabber IDs (e.g.
39 jabber@example.com).
40  
41 =head1 METHODS
42  
43 =over
44  
45 =cut
46  
47 # A hash which stores whether a certain presence supports XEP-0184 receipts.
48 # Entries are added after we actually send a message and entries are purged
49 # when the presence goes offline or is replaced (since the new presence might
50 # have a different feature set while keeping the same jid).
51 my %supports_receipts = ();
52  
53 # A hash which stores timers by message id. When a message is acknowledged, the
54 # corresponding timer is deleted.
55 my %timers = ();
56  
57 =item B<new (%args)>
58  
59 Creates a new receipts handle.
60  
61 The following keys can be specified:
62  
63 =over
64  
65 =item B<disco>
66  
67 An C<AnyEvent::XMPP::Ext::Disco> object so that it can be figured out whether
68 the recipient supports message receipts (via service discovery).
69  
70 This is required.
71  
72 =item B<debug>
73  
74 If you pass a value that evaluates to true, debug messages will be printed to
75 STDOUT.
76  
77 =item B<auto_resend>
78  
79 Amount of time in seconds after which messages will be re-sent when no receipt
80 was received. Of course messages will only be re-sent if the recipient is known
81 to support message receipts.
82  
83 Defaults to 30 (seconds).
84  
85 Set to 0 to disable automatic re-sending.
86  
87 =back
88  
89 Here is an example with all keys set:
90  
91 my $receipts = AnyEvent::XMPP::Ext::Receipts->new(
92 disco => $disco,
93 auto_resend => 30,
94 debug => 1,
95 );
96  
97 =cut
98 sub new {
99 my $this = shift;
100 my $class = ref($this) || $this;
101 my $self = bless { @_ }, $class;
102 die "You did not pass an AnyEvent::XMPP::Ext::Disco object as 'disco', see SYNOPSIS"
103 unless defined($self->{disco});
104 $self->{debug} //= 0;
105 # Re-send messages after unacknowledged for 30 seconds.
106 $self->{auto_resend} //= 30;
107 $self->init;
108 $self
109 }
110  
111 sub init {
112 my ($self) = @_;
113  
114 set_xmpp_ns_alias(receipts => 'urn:xmpp:receipts');
115  
116 $self->reg_cb(
117 ext_before_message_xml => sub {
118 my ($self, $con, $node) = @_;
119  
120 # Figure out if this is a receive receipt (XEP-0184), such a message
121 # looks like this:
122 # <message from="recipient@jabber.ccc.de/androidDc9226M8"
123 # id="CA597-36"
124 # to="me@jabber.ccc.de/18327446281349735808246801">
125 # <received id="foobar23" xmlns="urn:xmpp:receipts"/>
126 # </message>
127 my ($receipt) = $node->find_all ([qw/receipts received/]);
128 if (defined($receipt)) {
129 my $id = $receipt->attr('id');
130 print "(xep0184) message $id acknowledged\n" if $self->{debug};
131 delete $timers{$id};
132 # If the recipient acknowledged our message, he *obviously*
133 # supports receipts.
134 $supports_receipts{$node->attr('from')} = 1;
135 $self->stop_event;
136 }
137  
138 # Figure out if this is a message which requests a receipt, such as
139 # <message from="me@jabber.ccc.de/Psi+" id="aae0a" to="recipient@jabber.ccc.de/presence" type="chat" xml:lang="en">
140 # <body>Did you get this?</body>
141 # <request xmlns="urn:xmpp:receipts"/>
142 # </message>
143 my ($request) = $node->find_all ([qw/receipts request/]);
144 if (defined($request)) {
145 my $id = $node->attr('id');
146 print "(xep0184) sending receipt for $id\n" if $self->{debug};
147  
148 # A receipt looks like this:
149 # <message from="recipient@jabber.ccc.de/presence" to="me@jabber.ccc.de/Psi+">
150 # <received xmlns="urn:xmpp:receipts" id="aae0a"/>
151 # </message>
152 $con->send_message(
153 $node->attr('from'),
154 $node->attr('type'),
155 # Add a receipt request tag to the message, like this:
156 # <received xmlns='urn:xmpp:receipts' id="aae0a"/>
157 sub {
158 my $w = shift;
159 $w->addPrefix('urn:xmpp:receipts', '');
160 $w->startTag(['urn:xmpp:receipts', 'received'],
161 id => $id);
162 $w->endTag;
163 },
164 _is_receipt => 1);
165 }
166 },
167  
168 ext_before_send_message_hook => sub {
169 my ($self, $con, $id, $to, $type, $attrs, $create_cb) = @_;
170  
171 # We can only handle full jids as per XEP-0184 5.1:
172 # "If the sender knows only the recipient's bare JID, it cannot
173 # cannot determine [...] whether the intended recipient supports
174 # the Message Delivery Receipts protoocl. [...] the sender MUST NOT
175 # depend on receiving an ack message in reply."
176 # If we can’t rely on ack messages, receipts are useless.
177 return if is_bare_jid($to);
178  
179 # If we have already figured out that the recipient does not
180 # support message receipts, sending them (and especially waiting
181 # for acknowledge) is pointless.
182 return if exists($supports_receipts{$to}) && !$supports_receipts{$to};
183  
184 # If this is a message receipt (sent by us), do not add a receipt
185 # request, that might lead to an endless loop.
186 if ($attrs->{_is_receipt}) {
187 # XXX: The need to check a special attribute and delete it here
188 # is ugly, but the only way the API provides, as far as I can
189 # tell. Patches welcome.
190 delete $attrs->{_is_receipt};
191 return;
192 }
193  
194 # Add a receipt request tag to the message, like this:
195 # <request xmlns='urn:xmpp:receipts'/>
196 push @$create_cb, sub {
197 my $w = shift;
198 $w->addPrefix('urn:xmpp:receipts', '');
199 $w->startTag(['urn:xmpp:receipts', 'request']);
200 $w->endTag;
201 };
202  
203 if ($self->{auto_resend} > 0) {
204 print "(xep0184) expecting reply within " . $self->{auto_resend} . "s\n" if $self->{debug};
205 # This timer will be deleted when the recipient acknowledges the
206 # message. Otherwise, it re-sends the message.
207 $timers{$id} = AnyEvent->timer(
208 after => $self->{auto_resend},
209 cb => sub {
210 print "(xep0184) timeout for id $id\n" if $self->{debug};
211 if (!$con->is_connected) {
212 print "(xep0184) skipping re-send: jabber connection offline\n" if $self->{debug};
213 return;
214 }
215 if (!exists($supports_receipts{$to}) || !$supports_receipts{$to}) {
216 # If we don’t know whether the recipient supports
217 # message receipts (and we should by now, since we
218 # start a discovery request when sending the message),
219 # we don’t re-send. Better safe than duplicate msgs :).
220 print "(xep0184) not re-sending, no receipts support\n" if $self->{debug};
221 return;
222 }
223 print "(xep0184) re-sending message $id to $to\n" if $self->{debug};
224 $con->send_message($to, $type, undef, %$attrs);
225 });
226 }
227  
228 # If we don’t know yet whether the recipient supports message
229 # receipts, let’s send a discovery request.
230 if (!exists($supports_receipts{$to})) {
231 $self->{disco}->request_info($con, $to, undef, sub {
232 my ($disco, $items, $error) = @_;
233 if ($error) {
234 # We can’t figure out whether the recipient supports
235 # receipts, most likely due to a timeout to our
236 # request. We will retry the next time a message is
237 # sent anyways, so do nothing.
238 print "(xep0184) error discovering features: " . $error->string . "\n" if $self->{debug};
239 return;
240 }
241  
242 $supports_receipts{$to} = exists($items->features()->{'urn:xmpp:receipts'});
243 print "(xep0184) cache: $to = " . $supports_receipts{$to} . "\n" if $self->{debug};
244 });
245 }
246 },
247  
248 ext_before_presence_xml => sub {
249 my ($self, $con, $node) = @_;
250  
251 if (($node->attr('type') // '') eq 'unavailable') {
252 my $jid = $node->attr('from');
253 print "(xep0184) $jid is offline, invalidating cache\n" if $self->{debug};
254 delete $supports_receipts{$jid};
255 }
256 },
257 );
258 }
259  
260 sub disco_feature {
261 'urn:xmpp:receipts';
262 }
263  
264 =back
265  
266 =head1 AUTHOR
267  
268 Michael Stapelberg, C<< <michael at stapelberg.de> >>
269  
270 =head1 COPYRIGHT & LICENSE
271  
272 Copyright 2012 Michael Stapelberg
273  
274 This program is free software; you can redistribute it and/or modify it
275 under the same terms as Perl itself.
276  
277 =cut
278  
279 1;