XMPP3/JID.pm

filter: Statementstmt 55.7% 44 / 79 Branchbran 41.1% 14 / 34 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 57.1% 8 / 14 Podpod 0.0% 0 / 12 totaltotal 41.7% 68 / 163 CC 23 SCAR 30.1
CC
23
Cov
47%
BuildJID
56.0
SetJID
42.0
new
5.9
CRAP 20.2
« XMPP3/Roster.pm ↑ Summary XMPP3/PrivacyLists.pm »
1 ##############################################################################
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Library General Public
5 # License as published by the Free Software Foundation; either
6 # version 2 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Library General Public License for more details.
12 #
13 # You should have received a copy of the GNU Library General Public
14 # License along with this library; if not, write to the
15 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 # Boston, MA 02111-1307, USA.
17 #
18 # Copyright (C) 1998-2004 Jabber Software Foundation http://jabber.org/
19 #
20 ##############################################################################
21  
22 package Net::XMPP3::JID;
23  
24 =head1 NAME
25  
26 Net::XMPP3::JID - XMPP JID Module
27  
28 =head1 SYNOPSIS
29  
30 Net::XMPP3::JID is a companion to the Net::XMPP3 module.
31 It provides the user a simple interface to set and retrieve all
32 parts of a Jabber ID (userid on a server).
33  
34 =head1 DESCRIPTION
35  
36 To initialize the JID you must pass it the string that represents the
37 jid from the XML packet. Inside the XMPP modules this is done
38 automatically and the JID object is returned instead of a string.
39 For example, in the callback function for the XMPP object foo:
40  
41 use Net::XMPP3;
42  
43 sub foo {
44 my $foo = new Net::XMPP3::Foo(@_);
45 my $from = $foo->GetFrom();
46 my $JID = new Net::XMPP3::JID($from);
47 .
48 .
49 .
50 }
51  
52 You now have access to all of the retrieval functions available.
53  
54 To create a new JID to send to the server:
55  
56 use Net::XMPP3;
57  
58 $JID = new Net::XMPP3::JID();
59  
60 Now you can call the creation functions below to populate the tag
61 before sending it.
62  
63 =head2 Retrieval functions
64  
65 $userid = $JID->GetUserID();
66 $server = $JID->GetServer();
67 $resource = $JID->GetResource();
68  
69 $JID = $JID->GetJID();
70 $fullJID = $JID->GetJID("full");
71 $baseJID = $JID->GetJID("base");
72  
73 =head2 Creation functions
74  
75 $JID->SetJID(userid=>"bob",
76 server=>"jabber.org",
77 resource=>"Work");
78  
79 $JID->SetJID('blue@moon.org/Home');
80  
81 $JID->SetUserID("foo");
82 $JID->SetServer("bar.net");
83 $JID->SetResource("Foo Bar");
84  
85 =head1 METHODS
86  
87 =head2 Retrieval functions
88  
89 GetUserID() - returns a string with the userid of the JID.
90 If the string is an address (bob%jabber.org) then
91 the function will return it as an address
92 (bob@jabber.org).
93  
94 GetServer() - returns a string with the server of the JID.
95  
96 GetResource() - returns a string with the resource of the JID.
97  
98 GetJID() - returns a string that represents the JID stored
99 GetJID("full") within. If the "full" string is specified, then
100 GetJID("base") you get the full JID, including Resource, which
101 should be used to send to the server. If the "base",
102 string is specified, then you will just get
103 user@server, or the base JID.
104  
105 =head2 Creation functions
106  
107 SetJID(userid=>string, - set multiple fields in the jid at
108 server=>string, one time. This is a cumulative
109 resource=>string) and over writing action. If you set
110 SetJID(string) the "userid" attribute twice, the second
111 setting is what is used. If you set
112 the server, and then set the resource
113 then both will be in the jid. If all
114 you pass is a string, then that string
115 is used as the JID. For valid settings
116 read the specific Set functions below.
117  
118 SetUserID(string) - sets the userid. Must be a valid userid or the
119 server will complain if you try to use this JID
120 to talk to the server. If the string is an
121 address then it will be converted to the %
122 form suitable for using as a User ID.
123  
124 SetServer(string) - sets the server. Must be a valid host on the
125 network or the server will not be able to talk
126 to it.
127  
128 SetResource(string) - sets the resource of the userid to talk to.
129  
130 =head1 AUTHOR
131  
132 Ryan Eatmon
133  
134 =head1 COPYRIGHT
135  
136 This module is free software, you can redistribute it and/or modify it
137 under the LGPL.
138  
139 =cut
140  
141 11 require 5.003;
142 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 sub new
146 35 ▶{
Subroutine new: called CC=5 SCAR=17.8
Pod undocumented
147 35 my $proto = shift;
148 35 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0350
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
149 35 my $self = { };
150  
151 35 bless($self, $proto);
152  
153 35 ▶ if ("@_" ne (""))
Branch: 1 branch
BranchTrueFalseTotal
if (join($", @_) ne '') { } 35 0 35
154 {
155 35 my ($jid) = @_;
156 35 ▶ return $jid if ((ref($jid) ne "") && ($jid->isa("Net::XMPP3::JID")));
Branch: 1 branch
BranchTrueFalseTotal
if ref $jid ne "" and $jid->isa("Net::XMPP3::JID") 0 35 35
Condition33%1/3
ref $jid ne "" and $jid->isa("Net::XMPP3::JID")
!ll&&!rl&&r
3500
MC/DC0%0/2
ref $jid ne "" and $jid->isa("Net::XMPP3::JID")
ref $jid ne "" $jid->isa("Net::XMPP3::JID")
Truth table33%1/3
A and B
A= ref $jid ne ""B= $jid->isa("Net::XMPP3::JID")
ABresult
0X0
100
111
157 35 $self->{JID} = $jid;
158 }
159 else
160 {
161 0 $self->{JID} = "";
162 }
163 35 $self->ParseJID();
164  
165 35 return $self;
166 }
167  
168  
169 ##############################################################################
170 #
171 # ParseJID - private helper function that takes the JID and sets the
172 # the three parts of it.
173 #
174 ##############################################################################
175 sub ParseJID
176 35 ▶{
Subroutine ParseJID: called CC=5 SCAR=16.1
Pod undocumented
177 35 my $self = shift;
178  
179 35 my $userid;
180 my $server;
181 35 my $resource;
182  
183 ($userid,$server,$resource) =
184 35 ($self->{JID} =~ /^([^\@\/'"&:<>]*)\@([A-Za-z0-9\.\-\_]+)\/?(.*?)$/);
185 35 ▶ if (!defined($server))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $server) 1 34 35
186 {
187 ($server,$resource) =
188 1 ($self->{JID} =~ /^([A-Za-z0-9\.\-\_]+)\/?(.*?)$/);
189 }
190  
191 35 ▶ $userid = "" unless defined($userid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $userid 1 34 35
192 35 ▶ $server = "" unless defined($server);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server 0 35 35
193 35 ▶ $resource = "" unless defined($resource);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resource 0 35 35
194  
195 35 $self->{USERID} = $userid;
196 35 $self->{SERVER} = $server;
197 35 $self->{RESOURCE} = $resource;
198 }
199  
200  
201 ##############################################################################
202 #
203 # BuildJID - private helper function that takes the three parts and sets the
204 # JID from them.
205 #
206 ##############################################################################
207 sub BuildJID
208 0 ▶{
Subroutine BuildJID: not called CC=7 SCAR=40.3
Pod undocumented
209 0 my $self = shift;
210 0 $self->{JID} = $self->{USERID};
211 0 ▶ $self->{JID} .= "\@" if ($self->{USERID} ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"USERID"} ne "" 0 0 0
212 $self->{JID} .= $self->{SERVER} if (exists($self->{SERVER}) &&
213 0 ▶ defined($self->{SERVER}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"SERVER"} and defined $self->{"SERVER"} 0 0 0
Condition0%0/3
exists $self->{"SERVER"} and defined $self->{"SERVER"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"SERVER"} and defined $self->{"SERVER"}
exists $self->{"SERVER"} defined $self->{"SERVER"}
Truth table0%0/3
A and B
A= exists $self->{"SERVER"}B= defined $self->{"SERVER"}
ABresult
0X0
100
111
214 $self->{JID} .= "/".$self->{RESOURCE} if (exists($self->{RESOURCE}) &&
215 defined($self->{RESOURCE}) &&
216 0 ▶ ($self->{RESOURCE} ne ""));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"RESOURCE"} and defined $self->{"RESOURCE"} and $self->{"RESOURCE"} ne "" 0 0 0
Condition0%0/6
exists $self->{"RESOURCE"} and defined $self->{"RESOURCE"} and $self->{"RESOURCE"} ne ""
!ll&&!rl&&r
000
exists $self->{"RESOURCE"} and defined $self->{"RESOURCE"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $self->{"RESOURCE"} and defined $self->{"RESOURCE"} and $self->{"RESOURCE"} ne ""
exists $self->{"RESOURCE"} defined $self->{"RESOURCE"} $self->{"RESOURCE"} ne ""
Truth table0%0/4
A and B and C
A= exists $self->{"RESOURCE"}B= defined $self->{"RESOURCE"}C= $self->{"RESOURCE"} ne ""
ABCresult
0XX0
10X0
1100
1111
217 }
218  
219  
220 ##############################################################################
221 #
222 # GetUserID - returns the userid of the JID.
223 #
224 ##############################################################################
225 sub GetUserID
226 26 ▶{
Subroutine GetUserID: called CC=1 SCAR=0.0
Pod undocumented
227 26 my $self = shift;
228 26 my $userid = $self->{USERID};
229 26 $userid =~ s/\%/\@/;
230 26 return $userid;
231 }
232  
233  
234 ##############################################################################
235 #
236 # GetServer - returns the server of the JID.
237 #
238 ##############################################################################
239 sub GetServer
240 26 ▶{
Subroutine GetServer: called CC=1 SCAR=0.0
Pod undocumented
241 26 my $self = shift;
242 26 return $self->{SERVER};
243 }
244  
245  
246 ##############################################################################
247 #
248 # GetResource - returns the resource of the JID.
249 #
250 ##############################################################################
251 sub GetResource
252 26 ▶{
Subroutine GetResource: called CC=1 SCAR=0.0
Pod undocumented
253 26 my $self = shift;
254 26 return $self->{RESOURCE};
255 }
256  
257  
258 ##############################################################################
259 #
260 # GetJID - returns the full jid of the JID.
261 #
262 ##############################################################################
263 sub GetJID
264 15 ▶{
Subroutine GetJID: called CC=4 SCAR=13.9
Pod undocumented
265 15 my $self = shift;
266 15 my $type = shift;
267 15 ▶ $type = "" unless defined($type);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 11 4 15
268 15 ▶ return $self->{JID} if ($type eq "full");
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "full" 2 13 15
269 13 ▶ return $self->{USERID}."\@".$self->{SERVER} if ($self->{USERID} ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"USERID"} ne "" 12 1 13
270 1 return $self->{SERVER};
271 }
272  
273  
274 ##############################################################################
275 #
276 # SetJID - takes a hash of all of the things you can set on a JID and sets
277 # each one.
278 #
279 ##############################################################################
280 sub SetJID
281 0 ▶{
Subroutine SetJID: not called CC=6 SCAR=37.4
Pod undocumented
282 0 my $self = shift;
283 0 my %jid;
284  
285 0 ▶ if ($#_ > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ > 0) { } 0 0 0
286 0 ▶ while($#_ >= 0) { $jid{ lc pop(@_) } = pop(@_); }
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ >= 0) 0 0 0
287  
288 0 ▶ $self->SetUserID($jid{userid}) if exists($jid{userid});
Branch: 1 branch
BranchTrueFalseTotal
if exists $jid{"userid"} 0 0 0
289 0 ▶ $self->SetServer($jid{server}) if exists($jid{server});
Branch: 1 branch
BranchTrueFalseTotal
if exists $jid{"server"} 0 0 0
290 0 ▶ $self->SetResource($jid{resource}) if exists($jid{resource});
Branch: 1 branch
BranchTrueFalseTotal
if exists $jid{"resource"} 0 0 0
291 } else {
292 0 ($self->{JID}) = @_;
293 0 $self->ParseJID();
294 }
295 }
296  
297  
298 ##############################################################################
299 #
300 # SetUserID - sets the userid of the JID.
301 #
302 ##############################################################################
303 sub SetUserID
304 0 ▶{
Subroutine SetUserID: not called CC=1 SCAR=6.9
Pod undocumented
305 0 my $self = shift;
306 0 my ($userid) = @_;
307 0 $userid =~ s/\@/\%/;
308 0 $self->{USERID} = $userid;
309 0 $self->BuildJID();
310 }
311  
312  
313 ##############################################################################
314 #
315 # SetServer - sets the server of the JID.
316 #
317 ##############################################################################
318 sub SetServer
319 0 ▶{
Subroutine SetServer: not called CC=1 SCAR=6.9
Pod undocumented
320 0 my $self = shift;
321 0 my ($server) = @_;
322 0 $self->{SERVER} = $server;
323 0 $self->BuildJID();
324 }
325  
326  
327 ##############################################################################
328 #
329 # SetResource - sets the resource of the JID.
330 #
331 ##############################################################################
332 sub SetResource
333 0 ▶{
Subroutine SetResource: not called CC=1 SCAR=6.9
Pod undocumented
334 0 my $self = shift;
335 0 my ($resource) = @_;
336 0 $self->{RESOURCE} = $resource;
337 0 $self->BuildJID();
338 }
339  
340  
341 ##############################################################################
342 #
343 # debug - prints out the contents of the JID
344 #
345 ##############################################################################
346 sub debug
347 0 ▶{
Subroutine debug: not called CC=1 SCAR=6.9
Pod undocumented
348 0 my $self = shift;
349  
350 0 print "debug JID: $self\n";
351 0 print "UserID: (",$self->{USERID},")\n";
352 0 print "Server: (",$self->{SERVER},")\n";
353 0 print "Resource: (",$self->{RESOURCE},")\n";
354 0 print "JID: (",$self->{JID},")\n";
355 }
356  
357  
358 11 1;
« XMPP3/Roster.pm ↑ Summary XMPP3/PrivacyLists.pm »