DJabberd/Presence.pm

filter: Statementstmt 5.6% 18 / 317 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 7.0% 5 / 71 Podpod 0.0% 0 / 21 totaltotal 4.0% 23 / 566 CC 68 SCAR 29.0
CC
68
Cov
4%
process_inbound
90.0
process_outbound
56.0
_process_inbound_subscribe
30.0
CRAP 18.1
« DJabberd/Delivery/LocalVHosts.pm ↑ Summary DJabberd/Delivery/S2S.pm »
1 package DJabberd::Presence;
2 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 ▶use base qw(DJabberd::Stanza);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 'dont_load_rosteritem', # bool: if set, don't load roster item for this probe. it's a trusted probe. (internally generated)
7 20 );
8  
9  
10 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod undocumented
11 0 my $self = shift;
12 0 my $clone = $self->SUPER::clone;
13 0 $clone->{dont_load_rosteritem} = $self->{dont_load_rosteritem};
14 0 return $clone;
15 }
16  
17 # TODO: _process_outbound_invisible -- seen in wild. not in spec, but how to handle?
18 # Wildfire crew says:
19 # Presences of type invisible are not XMPP compliant. That was the
20 # old way invisibility was implemented before. The correct way to #
21 # implement invisibility is to use JEP-0126: Invisibility that is #
22 # based on privacy lists. The server will ignore presences of type
23 # # invisible and instead assume that an available presence was
24 # sent. In # other words, the server will ignore the invisibility
25 # request.
26  
27 # used by DJabberd::PresenceChecker::Local.
28 20 my %last_bcast; # barejidstring -> { full_jid_string -> $cloned_pres_stanza }
29  
30 0 ▶sub forget_last_presence {
Subroutine forget_last_presence: not called CC=3 SCAR=24.8
Pod undocumented
31 0 my ($class, $jid) = @_;
32  
33 0 my $barestr = $jid->as_bare_string;
34 0 ▶ my $map = $last_bcast{$barestr} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $map = $last_bcast{$barestr} or return 0 0 0
35 0 delete $map->{$jid->as_string};
36 0 ▶ delete $last_bcast{$barestr} unless %$map;
Branch: 1 branch
BranchTrueFalseTotal
unless %$map 0 0 0
37 }
38  
39 0 ▶sub set_local_presence {
Subroutine set_local_presence: not called CC=2 SCAR=17.9
Pod undocumented
40 0 my ($class, $jid, $prepkt) = @_;
41 0 ▶ return 0 unless $jid;
Branch: 1 branch
BranchTrueFalseTotal
unless $jid 0 0 0
42 0 $last_bcast{$jid->as_bare_string}{$jid->as_string} = $prepkt;
43 }
44  
45 # is this directed presence? must be to a JID, and must be available/unavailable, not probe/subscribe/etc.
46 0 ▶sub is_directed {
Subroutine is_directed: not called CC=4 SCAR=30.0
Pod undocumented
47 0 my $self = shift;
48 0 ▶ return 0 unless $self->to_jid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->to_jid 0 0 0
49 0 my $type = $self->type;
50 0 ▶ return 0 if $type && $type ne "unavailable";
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type ne "unavailable" 0 0 0
Condition0%0/3
$type and $type ne "unavailable"
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $type ne "unavailable"
$type $type ne "unavailable"
Truth table0%0/3
A and B
A= $typeB= $type ne "unavailable"
ABresult
0X0
100
111
51 0 return 1;
52 }
53  
54 0 ▶sub on_recv_from_server {
Subroutine on_recv_from_server: not called CC=1 SCAR=6.9
Pod undocumented
55 0 my ($self, $conn) = @_;
56 0 $DJabberd::Stats::counter{"s2si-Presence"}++;
57 0 $self->process_inbound($conn->vhost);
58 }
59  
60 0 ▶sub on_recv_from_client {
Subroutine on_recv_from_client: not called CC=1 SCAR=6.9
Pod undocumented
61 0 my ($self, $conn) = @_;
62 0 $DJabberd::Stats::counter{"c2s-Presence"}++;
63 0 $self->process_outbound($conn);
64 }
65  
66 0 ▶sub local_presence_info {
Subroutine local_presence_info: not called CC=2 SCAR=17.9
Pod undocumented
67 0 my ($class, $jid) = @_;
68 0 my $barestr = $jid->as_bare_string;
69 0 ▶ return $last_bcast{$barestr} || {};
Condition0%0/2
$last_bcast{$barestr} || {}
l!l
00
MC/DC0%0/1
$last_bcast{$barestr} || {}
$last_bcast{$barestr}
Truth table0%0/2
A
A= $last_bcast{$barestr}
Aresult
11
00
70 }
71  
72 # constructor
73 0 ▶sub available {
Subroutine available: not called CC=2 SCAR=17.9
Pod undocumented
74 0 my ($class, %opts) = @_;
75 0 my ($from) = map { delete $opts{$_} } qw(from);
76 0 ▶ croak "Invalid options" if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
77  
78 0 my $xml = DJabberd::XMLElement->new("", "presence", {
79 '{}from' => $from->as_string,
80 }, []);
81 0 return $class->downbless($xml);
82 }
83  
84 # constructor
85 0 ▶sub probe {
Subroutine probe: not called CC=2 SCAR=17.9
Pod undocumented
86 0 my ($class, %opts) = @_;
87 0 my ($from, $to) = map { delete $opts{$_} } qw(from to);
88 0 ▶ croak "Invalid options" if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
89  
90 0 my $xml = DJabberd::XMLElement->new("", "presence", { '{}type' => 'probe',
91 '{}from' => $from->as_string,
92 '{}to' => $to->as_bare_string }, []);
93 0 return $class->downbless($xml);
94 }
95  
96 # constructor
97 0 ▶sub make_subscribed {
Subroutine make_subscribed: not called CC=2 SCAR=17.9
Pod undocumented
98 0 my ($class, %opts) = @_;
99 0 my ($from, $to) = map { delete $opts{$_} } qw(from to);
100 0 ▶ croak "Invalid options" if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
101  
102 0 my $xml = DJabberd::XMLElement->new("", "presence", { '{}type' => 'subscribed',
103 '{}from' => $from->as_bare_string,
104 '{}to' => $to->as_bare_string }, []);
105 0 return $class->downbless($xml);
106 }
107  
108 # constructor
109 0 ▶sub make_subscribe {
Subroutine make_subscribe: not called CC=2 SCAR=17.9
Pod undocumented
110 0 my ($class, %opts) = @_;
111 0 my ($from, $to) = map { delete $opts{$_} } qw(from to);
112 0 ▶ croak "Invalid options" if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
113  
114 0 my $xml = DJabberd::XMLElement->new("", "presence", { '{}type' => 'subscribe',
115 '{}from' => $from->as_bare_string,
116 '{}to' => $to->as_bare_string }, []);
117 0 return $class->downbless($xml);
118 }
119  
120 # constructor
121 0 ▶sub available_stanza {
Subroutine available_stanza: not called CC=1 SCAR=6.9
Pod undocumented
122 0 my ($class) = @_;
123 0 my $xml = DJabberd::XMLElement->new("", "presence", {}, []);
124 0 return $class->downbless($xml);
125 }
126  
127 # constructor
128 0 ▶sub unavailable_stanza {
Subroutine unavailable_stanza: not called CC=1 SCAR=6.9
Pod undocumented
129 0 my ($class) = @_;
130 0 my $xml = DJabberd::XMLElement->new("", "presence", { '{}type' => "unavailable" }, []);
131 0 return $class->downbless($xml);
132 }
133  
134 0 ▶sub is_unavailable {
Subroutine is_unavailable: not called CC=1 SCAR=0.6
Pod undocumented
135 0 my $self = shift;
136 20 ▶ no warnings 'uninitialized'; # type can be uninitialized and that is ok
Subroutine BEGIN: called CC=1 SCAR=0.0
137 0 return $self->type eq 'unavailable';
138 }
139  
140 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod undocumented
141 0 my $self = shift;
142 0 return $self->attr("{}type");
143 }
144  
145 0 ▶sub fail {
Subroutine fail: not called CC=1 SCAR=6.9
Pod undocumented
146 0 my ($self, $vhost, $reason) = @_;
147 # TODO: figure this out (presence type='error' stuff, when?)
148 0 warn "PRESENCE FAILURE: $reason\n";
149 0 return;
150 }
151  
152 # like delivery, but handles inbound processing if the target
153 # is somebody on our domain. TODO: IQs are going to need
154 # this same out-vs-in processing. it should be generic.
155 0 ▶sub procdeliver {
Subroutine procdeliver: not called CC=4 SCAR=30.0
Pod undocumented
156 0 my ($self, $vhost) = @_;
157  
158 0 ▶ if ($vhost->isa("DJabberd::Connection")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vhost->isa("DJabberd::Connection")) 0 0 0
159 0 warn "Deprecated arg of connection to procdeliver at " . join(", ", caller);
160 0 $vhost = $vhost->vhost;
161 }
162  
163 # TODO: this needs some re-thinking for the cluster case, as
164 # "handles_jid" means one of two things in general: 1) I'm the
165 # sole handler of this JID (the below interpretation), vs 2) I can
166 # handle at least some of this vhost's domain, at least I don't
167 # handle none of it.
168 # The fear is that in the cluster case you'd have to always deliver,
169 # which we want to avoid.
170 # We should have another API that's like ->handles_jid_and_shes_online_here($jid)
171 0 ▶ my $contact_jid = $self->to_jid or die;
Branch: 1 branch
BranchTrueFalseTotal
my $contact_jid = $self->to_jid or die 0 0 0
172 0 ▶ if ($vhost->handles_jid($contact_jid)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vhost->handles_jid($contact_jid)) { } 0 0 0
173 0 my $clone = $self->clone;
174 0 $clone->process_inbound($vhost);
175 } else {
176 0 $self->deliver($vhost);
177 }
178 }
179  
180 0 ▶sub process {
Subroutine process: not called CC=1 SCAR=6.9
Pod undocumented
181 0 confess "No generic 'process' method for $_[0]";
182 }
183  
184 20 our %outbound_need_ritem = (
185 unsubscribe => 1,
186 unsubscribed => 1,
187 );
188  
189 0 ▶sub process_outbound {
Subroutine process_outbound: not called CC=7 SCAR=40.3
Pod undocumented
190 0 my ($self, $conn) = @_;
191 0 ▶ my $type = $self->type || "available";
Condition0%0/2
$self->type || 'available'
l!l
00
MC/DC0%0/1
$self->type || 'available'
$self->type
Truth table0%0/2
A
A= $self->type
Aresult
11
00
192  
193  
194 0 ▶ return 0 unless $conn->bound_jid;
Branch: 1 branch
BranchTrueFalseTotal
unless $conn->bound_jid 0 0 0
195 0 ▶ return $self->fail($conn->vhost, "bogus type") unless $type =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^\w+$/ 0 0 0
196  
197 0 ▶ my $call_method = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
198 0 my $ritem = shift;
199 0 my $meth = "_process_outbound_$type";
200 0 eval { $self->$meth($conn,$ritem) };
201 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
202 0 warn " ... ERROR: [$@]\n";
203 }
204 0 return;
205 0 };
206  
207 0 ▶ if ($outbound_need_ritem{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DJabberd::Presence::outbound_need_ritem{$type}) { } 0 0 0
208 0 ▶ my $to_jid = $self->to_jid
Branch: 1 branch
BranchTrueFalseTotal
my $to_jid = $self->to_jid or return $self->fail($conn->vhost, "no/invalid 'to' attribute") 0 0 0
209 or return $self->fail($conn->vhost, "no/invalid 'to' attribute");
210 0 ▶ my $from_jid = $self->from_jid
Branch: 1 branch
BranchTrueFalseTotal
my $from_jid = $self->from_jid or return $self->fail($conn->vhost, "no/invalid 'from' attribute") 0 0 0
211 or return $self->fail($conn->vhost, "no/invalid 'from' attribute");
212 0 $self->_roster_load_item($conn->vhost, $from_jid, $to_jid, $call_method);
213 } else {
214 0 $call_method->();
215 }
216  
217 }
218  
219 0 ▶sub process_inbound {
Subroutine process_inbound: not called CC=9 SCAR=45.0
Pod undocumented
220 0 my ($self, $vhost) = @_;
221 0 ▶ Carp::confess("Not a vhost") unless $vhost->isa("DJabberd::VHost");
Branch: 1 branch
BranchTrueFalseTotal
unless $vhost->isa("DJabberd::VHost") 0 0 0
222  
223 0 ▶ my $type = $self->type || "available";
Condition0%0/2
$self->type || 'available'
l!l
00
MC/DC0%0/1
$self->type || 'available'
$self->type
Truth table0%0/2
A
A= $self->type
Aresult
11
00
224  
225 0 ▶ return $self->fail($vhost, "bogus type") unless $type =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^\w+$/ 0 0 0
226  
227 0 ▶ my $to_jid = $self->to_jid
Branch: 1 branch
BranchTrueFalseTotal
my $to_jid = $self->to_jid or return $self->fail($vhost, "no/invalid 'to' attribute") 0 0 0
228 or return $self->fail($vhost, "no/invalid 'to' attribute");
229 0 ▶ my $from_jid = $self->from_jid
Branch: 1 branch
BranchTrueFalseTotal
my $from_jid = $self->from_jid or return $self->fail($vhost, "no/invalid 'from' attribute") 0 0 0
230 or return $self->fail($vhost, "no/invalid 'from' attribute");
231  
232 0 ▶ my $call_method = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
233 0 my $ritem = shift;
234 0 my $meth = "_process_inbound_$type";
235 0 eval { $self->$meth($vhost, $ritem, $from_jid) };
236 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
237 0 warn " ... ERROR: [$@].\n";
238 }
239 0 };
240  
241 # the presence packet is flagged as internally-generated and not
242 # wanting us to load the roster item (because it's probably a
243 # trusted probe). also, for available/unavailable directed
244 # presence don't load ritem because those handlers don't need it:
245 # they just deliver.
246 0 ▶ if ($self->{dont_load_rosteritem} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dont_load_rosteritem"} or $type eq "available" or $type eq "unavailable") 0 0 0
Condition0%0/6
$self->{"dont_load_rosteritem"} or $type eq "available" or $type eq "unavailable"
l!l&&r!l&&!r
000
$self->{"dont_load_rosteritem"} or $type eq "available"
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"dont_load_rosteritem"} or $type eq "available" or $type eq "unavailable"
$self->{"dont_load_rosteritem"} $type eq "available" $type eq "unavailable"
Truth table0%0/4
A or B or C
A= $self->{"dont_load_rosteritem"}B= $type eq "available"C= $type eq "unavailable"
ABCresult
1XX1
01X1
0011
0000
247 $type eq "available" || $type eq "unavailable")
248 {
249 0 $call_method->(undef);
250 0 return;
251 }
252  
253 # find the RosterItem corresponding to this sender, and only once
254 # we have it, invoke the next handler
255 0 $self->_roster_load_item($vhost, $to_jid, $from_jid, $call_method);
256 }
257  
258 0 ▶sub _roster_load_item {
Subroutine _roster_load_item: not called CC=1 SCAR=6.9
259 0 my ($self, $vhost, $my_jid, $contact_jid, $call_method) = @_;
260  
261 $vhost->run_hook_chain(phase => "RosterLoadItem",
262 args => [ $my_jid, $contact_jid ],
263 methods => {
264 0 ▶ error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
265 0 my ($cb, $reason) = @_;
266 0 return $self->fail($vhost, "RosterLoadItem hook failed: $reason");
267 },
268 0 ▶ set => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
269 0 my ($cb, $ritem) = @_;
270 0 $call_method->($ritem);
271 },
272 0 });
273 0 return 0;
274 }
275  
276 0 ▶sub _process_inbound_available {
Subroutine _process_inbound_available: not called CC=1 SCAR=6.9
277 0 my ($self, $vhost) = @_;
278 0 $self->deliver($vhost);
279 }
280  
281 0 ▶sub _process_inbound_unavailable {
Subroutine _process_inbound_unavailable: not called CC=1 SCAR=6.9
282 0 my ($self, $vhost) = @_;
283 0 $self->deliver($vhost);
284 }
285  
286 0 ▶sub _process_inbound_subscribe {
Subroutine _process_inbound_subscribe: not called CC=5 SCAR=34.0
287 0 my ($self, $vhost, $ritem, $from_jid) = @_;
288  
289 0 my $to_jid = $self->to_jid;
290  
291 # XMPP: server SHOULD auto-reply if contact already subscribed from
292 0 ▶ if ($ritem && $ritem->subscription->sub_from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ritem and $ritem->subscription->sub_from) 0 0 0
Condition0%0/3
$ritem and $ritem->subscription->sub_from
!ll&&!rl&&r
000
MC/DC0%0/2
$ritem and $ritem->subscription->sub_from
$ritem $ritem->subscription->sub_from
Truth table0%0/3
A and B
A= $ritemB= $ritem->subscription->sub_from
ABresult
0X0
100
111
293 0 my $subd = DJabberd::Presence->make_subscribed(to => $from_jid,
294 from => $to_jid);
295 0 $subd->procdeliver($vhost);
296  
297 # let's act like they probed us too, so we send them our presence.
298 0 my $probe = DJabberd::Presence->probe(from => $from_jid,
299 to => $to_jid);
300 0 $probe->procdeliver($vhost);
301 0 return;
302 }
303  
304 #warn " ... not already subscribed from, didn't shortcut.\n";
305  
306 0 ▶ $ritem ||= DJabberd::RosterItem->new($from_jid);
Condition0%0/3
$ritem ||= "DJabberd::RosterItem"->new($from_jid)
l!l&&r!l&&!r
000
MC/DC0%0/2
$ritem ||= "DJabberd::RosterItem"->new($from_jid)
$ritem "DJabberd::RosterItem"->new($from_jid)
Truth table0%0/3
A ||= B
A= $ritemB= "DJabberd::RosterItem"->new($from_jid)
ABresult
1X1
011
000
307  
308 # ignore duplicate pending-in subscriptions
309 0 ▶ if ($ritem->subscription->pending_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ritem->subscription->pending_in) 0 0 0
310 0 warn "ignoring dup inbound subscribe, already pending-in.\n";
311 0 return;
312 }
313  
314 # TODO: HOOK FOR auto-subscribed sending. violates spec, but LiveJournal
315 # could use it. i think spec isn't thoughtful enough there.
316  
317 # mark the roster item as pending-in, and save it:
318 0 $ritem->subscription->set_pending_in;
319  
320 $vhost->run_hook_chain(phase => "RosterSetItem",
321 args => [ $to_jid, $ritem ],
322 methods => {
323 0 ▶ done => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
324 0 $self->deliver($vhost);
325 },
326 0 ▶ error => sub { my $reason = $_[1]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
327 },
328 0 );
329 }
330  
331 0 ▶sub _process_inbound_subscribed {
Subroutine _process_inbound_subscribed: not called CC=4 SCAR=30.0
332 0 my ($self, $vhost, $ritem) = @_;
333 0 ▶ Carp::confess("Not a vhost") unless $vhost->isa("DJabberd::VHost");
Branch: 1 branch
BranchTrueFalseTotal
unless $vhost->isa("DJabberd::VHost") 0 0 0
334  
335 # MUST ignore inbound subscribed if we weren't awaiting
336 # its arrival
337 0 ▶ return unless $ritem && $ritem->subscription->pending_out;
Branch: 1 branch
BranchTrueFalseTotal
unless $ritem and $ritem->subscription->pending_out 0 0 0
Condition0%0/3
$ritem and $ritem->subscription->pending_out
!ll&&!rl&&r
000
MC/DC0%0/2
$ritem and $ritem->subscription->pending_out
$ritem $ritem->subscription->pending_out
Truth table0%0/3
A and B
A= $ritemB= $ritem->subscription->pending_out
ABresult
0X0
100
111
338  
339 0 my $to_jid = $self->to_jid;
340  
341 #warn "processing inbound subscribed...\n";
342 0 $ritem->subscription->got_inbound_subscribed;
343  
344 $vhost->run_hook_chain(phase => "RosterSetItem",
345 args => [ $to_jid, $ritem ],
346 methods => {
347 0 ▶ done => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
348 0 $vhost->roster_push($to_jid, $ritem);
349  
350 0 my $probe = DJabberd::Presence->probe(from => $to_jid,
351 to => $ritem->jid);
352 0 $probe->procdeliver($vhost);
353 0 $self->deliver($vhost);
354 },
355 0 ▶ error => sub { my $reason = $_[1]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
356 },
357 0 );
358  
359 }
360  
361 0 ▶sub _process_inbound_probe {
Subroutine _process_inbound_probe: not called CC=4 SCAR=30.0
362 0 my ($self, $vhost, $ritem, $from_jid) = @_;
363 0 ▶ unless ($self->{dont_load_rosteritem}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dont_load_rosteritem"}) 0 0 0
364 0 ▶ return unless $ritem && $ritem->subscription->sub_from;
Branch: 1 branch
BranchTrueFalseTotal
unless $ritem and $ritem->subscription->sub_from 0 0 0
Condition0%0/3
$ritem and $ritem->subscription->sub_from
!ll&&!rl&&r
000
MC/DC0%0/2
$ritem and $ritem->subscription->sub_from
$ritem $ritem->subscription->sub_from
Truth table0%0/3
A and B
A= $ritemB= $ritem->subscription->sub_from
ABresult
0X0
100
111
365 }
366  
367 0 my $jid = $self->to_jid;
368  
369 0 ▶ $vhost->check_presence($jid, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
370 0 my $map = shift;
371 0 foreach my $fullstr (keys %$map) {
372 0 my $stanza = $map->{$fullstr};
373 0 my $to_send = $stanza->clone;
374 0 $to_send->set_to($from_jid);
375 0 $to_send->deliver($vhost);
376 }
377 0 });
378 }
379  
380 0 ▶sub _process_inbound_unsubscribe {
Subroutine _process_inbound_unsubscribe: not called CC=2 SCAR=17.9
381 0 my ($self, $vhost, $ritem) = @_;
382  
383 # if we don't know the user, just drop it
384 0 ▶ return unless $ritem;
Branch: 1 branch
BranchTrueFalseTotal
unless $ritem 0 0 0
385  
386 0 my $to_jid = $self->to_jid;
387  
388 0 $ritem->subscription->got_inbound_unsubscribe;
389  
390 $vhost->run_hook_chain(phase => "RosterSetItem",
391 args => [ $to_jid, $ritem ],
392 methods => {
393 0 ▶ done => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
394 0 $vhost->roster_push($to_jid, $ritem);
395 0 $self->deliver($vhost);
396 },
397 0 ▶ error => sub { my $reason = $_[1]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
398 },
399 0 );
400 }
401  
402 0 ▶sub _process_inbound_unsubscribed {
Subroutine _process_inbound_unsubscribed: not called CC=1 SCAR=6.9
403 0 my ($self, $vhost, $ritem) = @_;
404  
405 # TODO:
406 # 1) MUST roster push
407 # 2) MUST deliver to all available resources
408  
409 # to -> none
410 # keep it in the roster as 'none', don't remove. client does that with type='remove'
411 }
412  
413 0 ▶sub broadcast_from {
Subroutine broadcast_from: not called CC=1 SCAR=6.9
Pod undocumented
414 0 my ($self, $conn) = @_;
415  
416 0 my $from_jid = $conn->bound_jid;
417 0 my $vhost = $conn->vhost;
418  
419 0 ▶ my $broadcast = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
420 0 my $roster = shift;
421 0 foreach my $it ($roster->from_items) {
422 0 my $dpres = $self->clone;
423 0 $dpres->set_to($it->jid);
424 0 $dpres->set_from($from_jid);
425 0 $dpres->procdeliver($vhost);
426 }
427  
428 # For the purpose of presence broadcasting
429 # we act as if all of the other resources
430 # for this bare JID are on the roster.
431 # This means that resources of the same
432 # JID are aware of each other and can send
433 # messages to each other, etc.
434 0 foreach my $otherconn ($vhost->find_conns_of_bare($from_jid)) {
435 0 my $to_jid = $otherconn->bound_jid;
436 0 ▶ next if $from_jid->eq($to_jid);
Branch: 1 branch
BranchTrueFalseTotal
if $from_jid->eq($to_jid) 0 0 0
437 0 my $dpres = $self->clone;
438 0 $dpres->set_to($to_jid);
439 0 $dpres->set_from($from_jid);
440 0 $dpres->procdeliver($vhost);
441 }
442 0 };
443  
444 0 $vhost->get_roster($from_jid, on_success => $broadcast);
445 }
446  
447 0 ▶sub _process_outbound_available {
Subroutine _process_outbound_available: not called CC=5 SCAR=34.0
448 0 my ($self, $conn, $skip_alter) = @_;
449  
450 0 my $vhost = $conn->vhost;
451 0 ▶ if (!$skip_alter && $vhost->are_hooks("AlterPresenceAvailable")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $skip_alter and $vhost->are_hooks("AlterPresenceAvailable")) 0 0 0
Condition0%0/3
not $skip_alter and $vhost->are_hooks("AlterPresenceAvailable")
!ll&&!rl&&r
000
MC/DC0%0/2
not $skip_alter and $vhost->are_hooks("AlterPresenceAvailable")
not $skip_alter $vhost->are_hooks("AlterPresenceAvailable")
Truth table0%0/3
not(A) and B
A= not $skip_alterB= $vhost->are_hooks("AlterPresenceAvailable")
ABresult
0X0
100
111
452 $vhost->run_hook_chain(phase => "AlterPresenceAvailable",
453 args => [ $conn, $self ],
454 methods => {
455 0 ▶ done => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
456 0 ▶ return if $conn->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"closed"} 0 0 0
457 0 $self->_process_outbound_available($conn, 1);
458 },
459 },
460 0 );
461 0 return;
462 }
463  
464 0 ▶ if ($self->is_directed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_directed) 0 0 0
465 0 $conn->add_directed_presence($self->to_jid);
466 0 $self->deliver;
467 0 return;
468 }
469  
470 0 my $jid = $conn->bound_jid;
471 0 DJabberd::Presence->set_local_presence($jid, $self->clone);
472  
473 0 $conn->set_available(1);
474  
475 0 ▶ if ($conn->is_initial_presence) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn->is_initial_presence) 0 0 0
476 0 $conn->on_initial_presence;
477 }
478  
479 0 $self->broadcast_from($conn);
480 }
481  
482 0 ▶sub _process_outbound_unavailable {
Subroutine _process_outbound_unavailable: not called CC=5 SCAR=34.0
483 0 my ($self, $conn, $skip_alter) = @_;
484  
485 0 my $vhost = $conn->vhost;
486 0 ▶ if (!$skip_alter && $vhost->are_hooks("AlterPresenceUnavailable")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $skip_alter and $vhost->are_hooks("AlterPresenceUnavailable")) 0 0 0
Condition0%0/3
not $skip_alter and $vhost->are_hooks("AlterPresenceUnavailable")
!ll&&!rl&&r
000
MC/DC0%0/2
not $skip_alter and $vhost->are_hooks("AlterPresenceUnavailable")
not $skip_alter $vhost->are_hooks("AlterPresenceUnavailable")
Truth table0%0/3
not(A) and B
A= not $skip_alterB= $vhost->are_hooks("AlterPresenceUnavailable")
ABresult
0X0
100
111
487 0 warn "runnig hook chain unavailable";
488 $vhost->run_hook_chain(phase => "AlterPresenceUnavailable",
489 args => [ $conn, $self ],
490 methods => {
491 0 ▶ done => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
492 0 ▶ return if $conn->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"closed"} 0 0 0
493 0 $self->_process_outbound_unavailable($conn, 1);
494 },
495 },
496 0 );
497 0 return;
498 }
499  
500  
501 0 ▶ if ($self->is_directed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_directed) 0 0 0
502 0 delete($conn->{directed_presence}->{$self->to_jid});
503 0 $self->deliver;
504 0 return;
505 }
506  
507 # if we are becoming unavailable then we need to tell all our directed presences customers this
508 # per RFC 3921 5.1.4.2
509  
510 0 my $from_jid = $conn->bound_jid;
511 0 foreach my $to_jid ($conn->directed_presence) {
512 0 my $dpres = $self->clone;
513 0 $dpres->set_to($to_jid);
514 0 $dpres->set_from($from_jid);
515 # I think we only need to deliver and not procdeliver here
516 # because we don't actually want to process it anymore -- sky
517 # TODO: not sure of that. --brad
518 0 $dpres->deliver($conn->vhost);
519 }
520 0 $conn->clear_directed_presence;
521  
522 0 my $jid = $conn->bound_jid;
523 0 DJabberd::Presence->set_local_presence($jid, $self->clone);
524  
525 0 $conn->set_available(0);
526 0 $self->broadcast_from($conn);
527 }
528  
529  
530 0 ▶sub _process_outbound_unsubscribe {
Subroutine _process_outbound_unsubscribe: not called CC=3 SCAR=24.8
531 0 my ($self, $conn, $ritem) = @_;
532  
533 0 my $from_jid = $self->from_jid;
534 0 ▶ my $to_jid = $self->to_jid or die "Can't subscribe to bogus jid";
Branch: 1 branch
BranchTrueFalseTotal
my $to_jid = $self->to_jid or die "Can't subscribe to bogus jid" 0 0 0
535  
536 # we didn't have this user;
537 0 ▶ return unless $ritem;
Branch: 1 branch
BranchTrueFalseTotal
unless $ritem 0 0 0
538  
539 0 $ritem->subscription->got_outbound_unsubscribe;
540  
541 $conn->vhost->run_hook_chain(phase => "RosterSetItem",
542 args => [ $from_jid, $ritem ],
543 methods => {
544 0 ▶ done => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
545 # xmpp-ip 8.4.[12]
546 # roster push, (to => none, both => from)
547 # deliver.
548 0 $conn->vhost->roster_push($from_jid, $ritem);
549  
550 # let's bare-ifiy our from address, as per the SHOULD in XMPP-IM 8.2.5
551 # {=remove-resource-on-presence-out}
552 0 $self->set_from($self->from_jid->as_bare_string);
553 0 $self->procdeliver($conn->vhost);
554 },
555 0 ▶ error => sub { my $reason = $_[1]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
556 }
557 0 );
558  
559 }
560  
561 0 ▶sub _process_outbound_unsubscribed {
Subroutine _process_outbound_unsubscribed: not called CC=3 SCAR=24.8
562 0 my ($self, $conn, $ritem) = @_;
563  
564 0 ▶ my $deliver = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
565 0 $self->set_from($self->from_jid->as_bare_string);
566 0 $self->procdeliver($conn->vhost);
567 0 };
568  
569 # no relation, but deliver anyway....
570 0 ▶ unless ($ritem) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ritem) 0 0 0
571 # TODO: we should deliver these, I assume, as that's consistent
572 # with other parts of spec wrt inter-server sync issues? --brad
573 0 $deliver->();
574 0 return;
575 }
576  
577 0 my $from_jid = $conn->bound_jid;
578 0 ▶ my $contact_jid = $self->to_jid or die "Can't subscribe to bogus jid";
Branch: 1 branch
BranchTrueFalseTotal
my $contact_jid = $self->to_jid or die "Can't subscribe to bogus jid" 0 0 0
579  
580 # xmpp-ip 8.5.[12]
581 # roster push (from => none, both => to), clearing pendin as well...
582 0 $ritem->subscription->got_outbound_unsubscribed;
583  
584 $conn->vhost->run_hook_chain(phase => "RosterSetItem",
585 args => [ $from_jid, $ritem ],
586 methods => {
587 0 ▶ done => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
588 0 $conn->vhost->roster_push($from_jid, $ritem);
589  
590 # continue this packet along to contact
591 0 $self->set_from($self->from_jid->as_bare_string);
592 0 $self->procdeliver($conn->vhost);
593  
594 # send unavailable presence to contact:
595 0 my $unavail = DJabberd::Presence->unavailable_stanza;
596 0 $unavail->set_to($contact_jid);
597 0 $unavail->set_from($from_jid);
598 0 $unavail->deliver($conn->vhost); # procdeliver's useless: proc just delivers
599 },
600 0 ▶ error => sub { my $reason = $_[1]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
601 },
602 0 );
603 }
604  
605  
606 0 ▶sub _process_outbound_subscribe {
Subroutine _process_outbound_subscribe: not called CC=2 SCAR=17.9
607 0 my ($self, $conn) = @_;
608  
609 0 my $from_jid = $conn->bound_jid;
610 0 ▶ my $contact_jid = $self->to_jid or die "Can't subscribe to bogus jid";
Branch: 1 branch
BranchTrueFalseTotal
my $contact_jid = $self->to_jid or die "Can't subscribe to bogus jid" 0 0 0
611  
612 # XMPPIP-9.2-p2: MUST without exception
613 # route these, to combat sync issues
614 # between parties
615  
616 0 ▶ my $deliver = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
617 # let's bare-ifiy our from address, as per the SHOULD in XMPP-IM 8.2.5
618 # {=remove-resource-on-presence-out}
619 0 $self->set_from($self->from_jid->as_bare_string);
620  
621 0 $self->procdeliver($conn->vhost);
622 0 };
623  
624 0 ▶ my $save = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
625 0 my $ritem = shift;
626 $conn->vhost->run_hook_chain(phase => "RosterSetItem",
627 args => [ $from_jid, $ritem ],
628 methods => {
629 0 ▶ done => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
630 0 $conn->vhost->roster_push($from_jid, $ritem);
631 0 $deliver->();
632 },
633 0 ▶ error => sub { my $reason = $_[1]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
634 },
635 0 );
636 0 };
637  
638 0 ▶ my $on_load = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
639 0 my (undef, $ritem) = @_;
640  
641 # not in roster, skip.
642 0 ▶ $ritem ||= DJabberd::RosterItem->new($contact_jid);
Condition0%0/3
$ritem ||= "DJabberd::RosterItem"->new($contact_jid)
l!l&&r!l&&!r
000
MC/DC0%0/2
$ritem ||= "DJabberd::RosterItem"->new($contact_jid)
$ritem "DJabberd::RosterItem"->new($contact_jid)
Truth table0%0/3
A ||= B
A= $ritemB= "DJabberd::RosterItem"->new($contact_jid)
ABresult
1X1
011
000
643  
644 0 ▶ if ($ritem->subscription->got_outbound_subscribe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ritem->subscription->got_outbound_subscribe) { } 0 0 0
645 # subscription modified, must save, which will then
646 # deliver when done.
647 0 $save->($ritem);
648 } else {
649 0 $deliver->();
650 }
651 0 };
652  
653 $conn->vhost->run_hook_chain(phase => "RosterLoadItem",
654 args => [ $from_jid, $contact_jid ],
655 methods => {
656 0 ▶ error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
657 0 my (undef, $reason) = @_;
658 0 return $self->fail($conn, "RosterLoadItem hook failed: $reason");
659 },
660 0 set => $on_load,
661 });
662 }
663  
664  
665  
666 0 ▶sub _process_outbound_subscribed {
Subroutine _process_outbound_subscribed: not called CC=2 SCAR=17.9
667 0 my ($self, $conn) = @_;
668  
669 # user wanting to subscribe or approve subscription request to contact
670 0 ▶ my $contact_jid = $self->to_jid
Branch: 1 branch
BranchTrueFalseTotal
my $contact_jid = $self->to_jid or return $self->fail($conn, "no/invalid 'to' attribute") 0 0 0
671 or return $self->fail($conn, "no/invalid 'to' attribute");
672  
673 $conn->vhost->run_hook_chain(phase => "RosterLoadItem",
674 args => [ $conn->bound_jid, $contact_jid ],
675 methods => {
676 0 ▶ error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
677 0 my (undef, $reason) = @_;
678 0 return $self->fail($conn, "RosterLoadItem hook failed: $reason");
679 },
680 0 ▶ set => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
681 0 my (undef, $ritem) = @_;
682  
683 # not in roster, skip.
684 0 ▶ return unless $ritem;
Branch: 1 branch
BranchTrueFalseTotal
unless $ritem 0 0 0
685  
686 0 my $subs = $ritem->subscription;
687  
688 # skip unless we were in pending in state
689 0 ▶ return unless $subs->pending_in;
Branch: 1 branch
BranchTrueFalseTotal
unless $subs->pending_in 0 0 0
690  
691 0 $self->_process_outbound_subscribed_with_ritem($conn, $ritem);
692 },
693 0 });
694 }
695  
696 # second stage of outbound 'subscribed' processing, once we load the item and
697 # decide to skip processing or not. see above.
698 0 ▶sub _process_outbound_subscribed_with_ritem {
Subroutine _process_outbound_subscribed_with_ritem: not called CC=2 SCAR=17.9
699 0 my ($self, $conn, $ritem) = @_;
700 0 my $vhost = $conn->vhost;
701 0 $ritem->subscription->got_outbound_subscribed;
702  
703 0 ▶ my $from_jid = $conn->bound_jid || die("lacking from_jid");
Condition0%0/2
$conn->bound_jid || die('lacking from_jid')
l!l
00
MC/DC0%0/1
$conn->bound_jid || die('lacking from_jid')
$conn->bound_jid
Truth table0%0/2
A
A= $conn->bound_jid
Aresult
11
00
704 0 my $to_jid = $self->to_jid;
705  
706 $conn->vhost->run_hook_chain(phase => "RosterSetItem",
707 args => [ $conn->bound_jid, $ritem ],
708 methods => {
709 0 ▶ done => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
710 0 $conn->vhost->roster_push($conn->bound_jid, $ritem);
711 0 $self->procdeliver($conn->vhost);
712  
713 # the spec's a little unclear as to whether, on successful subscribe,
714 # host A sends probes vs. host B sends the presence out. we do both,
715 # as does ejabberd and other servers.
716 0 ▶ $vhost->check_presence($conn->bound_jid, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
717 0 my $map = shift;
718 0 foreach my $fullstr (keys %$map) {
719 0 my $stanza = $map->{$fullstr};
720 0 my $to_send = $stanza->clone;
721 0 $to_send->set_to($to_jid);
722 0 $to_send->deliver($vhost);
723 }
724 0 });
725 },
726 0 ▶ error => sub { my $reason = $_[1]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
727 },
728 0 );
729 }
730  
731  
732 20 1;
« DJabberd/Delivery/LocalVHosts.pm ↑ Summary DJabberd/Delivery/S2S.pm »