File Coverage

blib/lib/Net/XMPP3/IQ.pm
Criterion Covered Total %
statement 39 40 97.5
branch 4 8 50.0
condition 1 3 33.3
subroutine 9 9 100.0
pod 0 3 0.0
total 53 63 84.1


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::IQ;
23              
24             =head1 NAME
25              
26             Net::XMPP3::IQ - XMPP Info/Query Module
27              
28             =head1 SYNOPSIS
29              
30             Net::XMPP3::IQ is a companion to the Net::XMPP3 module. It
31             provides the user a simple interface to set and retrieve all
32             parts of an XMPP IQ.
33              
34             =head1 DESCRIPTION
35              
36             Net::XMPP3::IQ differs from the other Net::XMPP3::* modules in that
37             the XMLNS of the query is split out into a submodule under
38             IQ. For specifics on each module please view the documentation
39             for the Net::XMPP3::Namespaces module.
40              
41             A Net::XMPP3::IQ object is passed to the callback function for the
42             message. Also, the first argument to the callback functions is the
43             session ID from XML::Stream. There are some cases where you might
44             want this information, like if you created a Client that connects to
45             two servers at once, or for writing a mini server.
46              
47             use Net::XMPP3;
48              
49             sub iq {
50             my ($sid,$IQ) = @_;
51             .
52             .
53             my $reply = $IQ->Reply();
54             my $replyQuery->GetQuery();
55             .
56             }
57              
58             You now have access to all of the retrieval functions available.
59              
60             To create a new iq to send to the server:
61              
62             use Net::XMPP3;
63              
64             $IQ = new Net::XMPP3::IQ();
65             $IQType = $IQ->NewChild( type );
66             $IQType->SetXXXXX("yyyyy");
67              
68             Now you can call the creation functions for the IQ, and for the
69             on the new query object itself. See below for the
70             functions, and in each query module for those functions.
71              
72             =head1 METHODS
73              
74             =head2 General functions
75              
76             Reply(%args) - Creates a return with the to and from
77             filled in correctly, and a query object already
78             added in. The %args that you pass are passed
79             to SetIQ() and will overwrite the IQ settings
80             that Reply sets.
81              
82             =head2 Retrieval functions
83              
84             GetTo() - returns either a string with the JID, or a
85             GetTo("jid") Net::XMPP3::JID object for the person who is
86             going to receive the . To get the JID
87             object set the string to "jid", otherwise leave
88             blank for the text string.
89              
90             $to = $IQ->GetTo();
91             $toJID = $IQ->GetTo("jid");
92              
93             GetFrom() - returns either a string with the JID, or a
94             GetFrom("jid") Net::XMPP3::JID object for the person who
95             sent the . To get the JID object set
96             the string to "jid", otherwise leave blank for the
97             text string.
98              
99             $from = $IQ->GetFrom();
100             $fromJID = $IQ->GetFrom("jid");
101              
102             GetType() - returns a string with the type this is.
103              
104             $type = $IQ->GetType();
105              
106             GetID() - returns an integer with the id of the .
107              
108             $id = $IQ->GetID();
109              
110             GetError() - returns a string with the text description of the error.
111              
112             $error = $IQ->GetError();
113              
114             GetErrorCode() - returns a string with the code of error.
115              
116             $errorCode = $IQ->GetErrorCode();
117              
118             GetQuery() - returns a Net::XMPP3::Stanza object that contains the data
119             in the query of the . Basically, it returns the
120             first child in the .
121              
122             $query = $IQ->GetQuery();
123              
124             GetQueryXMLNS() - returns a string with the namespace of the query
125             for this , if one exists.
126              
127             $xmlns = $IQ->GetQueryXMLNS();
128              
129             =head2 Creation functions
130              
131             SetIQ(to=>string|JID, - set multiple fields in the at one
132             from=>string|JID, time. This is a cumulative and over
133             id=>string, writing action. If you set the "to"
134             type=>string, attribute twice, the second setting is
135             errorcode=>string, what is used. If you set the status, and
136             error=>string) then set the priority then both will be in
137             the tag. For valid settings read the
138             specific Set functions below.
139              
140             $IQ->SetIQ(type=>"get",
141             to=>"bob\@jabber.org");
142              
143             $IQ->SetIQ(to=>"bob\@jabber.org",
144             errorcode=>403,
145             error=>"Permission Denied");
146              
147             SetTo(string) - sets the to attribute. You can either pass a string
148             SetTo(JID) or a JID object. They must be a valid Jabber
149             Identifiers or the server will return an error message.
150             (ie. bob@jabber.org, etc...)
151              
152             $IQ->SetTo("bob\@jabber.org");
153              
154             SetFrom(string) - sets the from attribute. You can either pass a
155             SetFrom(JID) string or a JID object. They must be a valid JIDs
156             or the server will return an error message.
157             (ie. bob@jabber.org, etc...)
158              
159             $IQ->SetFrom("me\@jabber.org");
160              
161             SetType(string) - sets the type attribute. Valid settings are:
162              
163             get request information
164             set set information
165             result results of a get
166             error there was an error
167              
168             $IQ->SetType("set");
169              
170             SetErrorCode(string) - sets the error code of the .
171              
172             $IQ->SetErrorCode(403);
173              
174             SetError(string) - sets the error string of the .
175              
176             $IQ->SetError("Permission Denied");
177              
178             NewChild(string) - creates a new Net::XMPP3::Stanza object with the
179             namespace in the string. In order for this
180             function to work with a custom namespace, you
181             must define and register that namespace with the
182             IQ module. For more information please read the
183             documentation for Net::XMPP3::Stanza.
184              
185             $queryObj = $IQ->NewChild("jabber:iq:auth");
186             $queryObj = $IQ->NewChild("jabber:iq:roster");
187              
188             Reply(hash) - creates a new IQ object and populates the to/from
189             fields. If you specify a hash the same as with SetIQ
190             then those values will override the Reply values.
191              
192             $iqReply = $IQ->Reply();
193             $iqReply = $IQ->Reply(type=>"result");
194              
195             =head2 Removal functions
196              
197             RemoveTo() - removes the to attribute from the .
198              
199             $IQ->RemoveTo();
200              
201             RemoveFrom() - removes the from attribute from the .
202              
203             $IQ->RemoveFrom();
204              
205             RemoveID() - removes the id attribute from the .
206              
207             $IQ->RemoveID();
208              
209             RemoveType() - removes the type attribute from the .
210              
211             $IQ->RemoveType();
212              
213             RemoveError() - removes the element from the .
214              
215             $IQ->RemoveError();
216              
217             RemoveErrorCode() - removes the code attribute from the
218             element in the .
219              
220             $IQ->RemoveErrorCode();
221              
222             =head2 Test functions
223              
224             DefinedTo() - returns 1 if the to attribute is defined in the ,
225             0 otherwise.
226              
227             $test = $IQ->DefinedTo();
228              
229             DefinedFrom() - returns 1 if the from attribute is defined in the
230             , 0 otherwise.
231              
232             $test = $IQ->DefinedFrom();
233              
234             DefinedID() - returns 1 if the id attribute is defined in the ,
235             0 otherwise.
236              
237             $test = $IQ->DefinedID();
238              
239             DefinedType() - returns 1 if the type attribute is defined in the
240             , 0 otherwise.
241              
242             $test = $IQ->DefinedType();
243              
244             DefinedError() - returns 1 if is defined in the ,
245             0 otherwise.
246              
247             $test = $IQ->DefinedError();
248              
249             DefinedErrorCode() - returns 1 if the code attribute is defined in
250             , 0 otherwise.
251              
252             $test = $IQ->DefinedErrorCode();
253              
254             DefinedQuery() - returns 1 if there is at least one namespaced
255             child in the object.
256              
257             =head1 AUTHOR
258              
259             Ryan Eatmon
260              
261             =head1 COPYRIGHT
262              
263             This module is free software, you can redistribute it and/or modify it
264             under the LGPL.
265              
266             =cut
267              
268             require 5.003;
269 11     11   57 use strict;
  11         19  
  11         365  
270 11     11   58 use Carp;
  11         18  
  11         685  
271 11     11   56 use vars qw( %FUNCTIONS );
  11         19  
  11         410  
272 11     11   56 use Net::XMPP3::Stanza;
  11         19  
  11         237  
273 11     11   63 use base qw( Net::XMPP3::Stanza );
  11         16  
  11         6584  
274              
275             sub new
276             {
277 4     4 0 1206 my $proto = shift;
278 4   33     31 my $class = ref($proto) || $proto;
279 4         12 my $self = { };
280              
281 4         12 bless($self, $proto);
282              
283 4         30 $self->{DEBUGHEADER} = "IQ";
284 4         12 $self->{TAG} = "iq";
285              
286 4         14 $self->{FUNCS} = \%FUNCTIONS;
287              
288 4         49 $self->_init(@_);
289              
290 4         13 return $self;
291             }
292              
293 1     1   2 sub _iq { my $self = shift; return new Net::XMPP3::IQ(); }
  1         5  
294              
295             $FUNCTIONS{Error}->{path} = 'error/text()';
296              
297             $FUNCTIONS{ErrorCode}->{path} = 'error/@code';
298              
299             $FUNCTIONS{From}->{type} = 'jid';
300             $FUNCTIONS{From}->{path} = '@from';
301              
302             $FUNCTIONS{ID}->{path} = '@id';
303              
304             $FUNCTIONS{To}->{type} = 'jid';
305             $FUNCTIONS{To}->{path} = '@to';
306              
307             $FUNCTIONS{Type}->{path} = '@type';
308              
309             $FUNCTIONS{XMLNS}->{path} = '@xmlns';
310              
311             $FUNCTIONS{IQ}->{type} = 'master';
312              
313             $FUNCTIONS{Child}->{type} = 'child';
314             $FUNCTIONS{Child}->{path} = '*[@xmlns]';
315             $FUNCTIONS{Child}->{child} = { };
316              
317             $FUNCTIONS{Query}->{type} = 'child';
318             $FUNCTIONS{Query}->{path} = '*[@xmlns][0]';
319             $FUNCTIONS{Query}->{child} = { child_index=>0 };
320              
321             ##############################################################################
322             #
323             # GetQueryXMLNS - returns the xmlns of the first child
324             #
325             ##############################################################################
326             sub GetQueryXMLNS
327             {
328 1     1 0 3 my $self = shift;
329 1 50       2 return $self->{CHILDREN}->[0]->GetXMLNS() if ($#{$self->{CHILDREN}} > -1);
  1         10  
330             }
331              
332              
333             ##############################################################################
334             #
335             # Reply - returns a Net::XMPP3::IQ object with the proper fields
336             # already populated for you.
337             #
338             ##############################################################################
339             sub Reply
340             {
341 1     1 0 6 my $self = shift;
342 1         2 my %args;
343 1         5 while($#_ >= 0) { $args{ lc pop(@_) } = pop(@_); }
  0         0  
344              
345 1         6 my $reply = $self->_iq();
346              
347 1 50       7 $reply->SetID($self->GetID()) if ($self->GetID() ne "");
348 1         25 $reply->SetType("result");
349              
350 1         21 $reply->NewChild($self->GetQueryXMLNS());
351              
352 1 50       7 $reply->SetIQ((($self->GetFrom() ne "") ?
    50          
353             (to=>$self->GetFrom()) :
354             ()
355             ),
356             (($self->GetTo() ne "") ?
357             (from=>$self->GetTo()) :
358             ()
359             ),
360             );
361 1         10 $reply->SetIQ(%args);
362              
363 1         5 return $reply;
364             }
365              
366              
367             1;