File Coverage

blib/lib/Net/XMPP3/JID.pm
Criterion Covered Total %
statement 41 77 53.2
branch 14 32 43.7
condition 2 15 13.3
subroutine 8 14 57.1
pod 0 12 0.0
total 65 150 43.3


line stmt bran cond sub pod time code
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             require 5.003;
142 11     11   56 use strict;
  11         18  
  11         362  
143 11     11   51 use Carp;
  11         19  
  11         11951  
144              
145             sub new
146             {
147 35     35 0 1938 my $proto = shift;
148 35   33     174 my $class = ref($proto) || $proto;
149 35         65 my $self = { };
150              
151 35         113 bless($self, $proto);
152              
153 35 50       149 if ("@_" ne (""))
154             {
155 35         72 my ($jid) = @_;
156 35 50 33     120 return $jid if ((ref($jid) ne "") && ($jid->isa("Net::XMPP3::JID")));
157 35         151 $self->{JID} = $jid;
158             }
159             else
160             {
161 0         0 $self->{JID} = "";
162             }
163 35         133 $self->ParseJID();
164              
165 35         1309 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             {
177 35     35 0 53 my $self = shift;
178              
179 35         73 my $userid;
180             my $server;
181 0         0 my $resource;
182              
183 35         335 ($userid,$server,$resource) =
184             ($self->{JID} =~ /^([^\@\/'"&:<>]*)\@([A-Za-z0-9\.\-\_]+)\/?(.*?)$/);
185 35 100       120 if (!defined($server))
186             {
187 1         8 ($server,$resource) =
188             ($self->{JID} =~ /^([A-Za-z0-9\.\-\_]+)\/?(.*?)$/);
189             }
190              
191 35 100       105 $userid = "" unless defined($userid);
192 35 50       108 $server = "" unless defined($server);
193 35 50       120 $resource = "" unless defined($resource);
194              
195 35         81 $self->{USERID} = $userid;
196 35         74 $self->{SERVER} = $server;
197 35         96 $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             {
209 0     0 0 0 my $self = shift;
210 0         0 $self->{JID} = $self->{USERID};
211 0 0       0 $self->{JID} .= "\@" if ($self->{USERID} ne "");
212 0 0 0     0 $self->{JID} .= $self->{SERVER} if (exists($self->{SERVER}) &&
213             defined($self->{SERVER}));
214 0 0 0     0 $self->{JID} .= "/".$self->{RESOURCE} if (exists($self->{RESOURCE}) &&
      0        
215             defined($self->{RESOURCE}) &&
216             ($self->{RESOURCE} ne ""));
217             }
218              
219              
220             ##############################################################################
221             #
222             # GetUserID - returns the userid of the JID.
223             #
224             ##############################################################################
225             sub GetUserID
226             {
227 26     26 0 32914 my $self = shift;
228 26         77 my $userid = $self->{USERID};
229 26         73 $userid =~ s/\%/\@/;
230 26         143 return $userid;
231             }
232              
233              
234             ##############################################################################
235             #
236             # GetServer - returns the server of the JID.
237             #
238             ##############################################################################
239             sub GetServer
240             {
241 26     26 0 62 my $self = shift;
242 26         158 return $self->{SERVER};
243             }
244              
245              
246             ##############################################################################
247             #
248             # GetResource - returns the resource of the JID.
249             #
250             ##############################################################################
251             sub GetResource
252             {
253 26     26 0 85 my $self = shift;
254 26         157 return $self->{RESOURCE};
255             }
256              
257              
258             ##############################################################################
259             #
260             # GetJID - returns the full jid of the JID.
261             #
262             ##############################################################################
263             sub GetJID
264             {
265 15     15 0 3188 my $self = shift;
266 15         19 my $type = shift;
267 15 100       45 $type = "" unless defined($type);
268 15 100       51 return $self->{JID} if ($type eq "full");
269 13 100       83 return $self->{USERID}."\@".$self->{SERVER} if ($self->{USERID} ne "");
270 1         5 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             {
282 0     0 0   my $self = shift;
283 0           my %jid;
284              
285 0 0         if ($#_ > 0 ) {
286 0           while($#_ >= 0) { $jid{ lc pop(@_) } = pop(@_); }
  0            
287              
288 0 0         $self->SetUserID($jid{userid}) if exists($jid{userid});
289 0 0         $self->SetServer($jid{server}) if exists($jid{server});
290 0 0         $self->SetResource($jid{resource}) if exists($jid{resource});
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             {
305 0     0 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             {
320 0     0 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             {
334 0     0 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             {
348 0     0 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             1;