XMPP3.pm

filter: Statementstmt 62.2% 71 / 114 Branchbran 17.2% 10 / 58 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 86.3% 19 / 22 Podpod 0.0% 0 / 4 totaltotal 47.4% 103 / 217 CC 34 SCAR 47.1
CC
34
Cov
46%
GetHumanTime
272.0
GetTimeStamp
91.1
printData
2.0
CRAP 111.0
« XMPP3/Protocol.pm ↑ Summary XMPP3/Stanza.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;
23  
24 =head1 NAME
25  
26 Net::XMPP3 - XMPP Perl Library
27  
28 =head1 SYNOPSIS
29  
30 Net::XMPP3 provides a Perl user with access to the Extensible
31 Messaging and Presence Protocol (XMPP).
32  
33 For more information about XMPP visit:
34  
35 http://www.xmpp.org
36  
37 =head1 DESCRIPTION
38 This is a little modified version of Net::XMPP, with fixed one bug, for correctly
39 works with ejabber servers. I do this, because author of Net::XMPP not available
40 from 2007 year.
41  
42  
43 Net::XMPP3 is a convenient tool to use for any perl script that would
44 like to utilize the XMPP Instant Messaging protocol. While not a
45 client in and of itself, it provides all of the necessary back-end
46 functions to make a CGI client or command-line perl client feasible
47 and easy to use. Net::XMPP3 is a wrapper around the rest of the
48 official Net::XMPP3::xxxxxx packages.
49  
50 There is are example scripts in the example directory that provide you
51 with examples of very simple XMPP programs.
52  
53  
54 NOTE: The parser that XML::Stream::Parser provides, as are most Perl
55 parsers, is synchronous. If you are in the middle of parsing a packet
56 and call a user defined callback, the Parser is blocked until your
57 callback finishes. This means you cannot be operating on a packet,
58 send out another packet and wait for a response to that packet. It
59 will never get to you. Threading might solve this, but as of this
60 writing threading in Perl is not quite up to par yet. This issue will
61 be revisted in the future.
62  
63  
64 =head1 EXAMPLES
65  
66 use Net::XMPP3;
67 my $client = new Net::XMPP3::Client();
68  
69 =head1 METHODS
70  
71 The Net::XMPP3 module does not define any methods that you will call
72 directly in your code. Instead you will instantiate objects that call
73 functions from this module to do work. The three main objects that
74 you will work with are the Message, Presence, and IQ modules. Each one
75 corresponds to the Jabber equivilant and allows you get and set all
76 parts of those packets.
77  
78 There are a few functions that are the same across all of the objects:
79  
80 =head2 Retrieval functions
81  
82 GetXML() - returns the XML string that represents the data contained
83 in the object.
84  
85 $xml = $obj->GetXML();
86  
87 GetChild() - returns an array of Net::XMPP3::Stanza objects
88 GetChild(namespace) that represent all of the stanzas in the object
89 that are namespaced. If you specify a namespace
90 then only stanza objects with that XMLNS are
91 returned.
92  
93 @xObj = $obj->GetChild();
94 @xObj = $obj->GetChild("my:namespace");
95  
96 GetTag() - return the root tag name of the packet.
97  
98 GetTree() - return the XML::Stream::Node object that contains the data.
99 See XML::Stream::Node for methods you can call on this
100 object.
101  
102 =head2 Creation functions
103  
104 NewChild(namespace) - creates a new Net::XMPP3::Stanza object with
105 NewChild(namespace,tag) the specified namespace and root tag of
106 whatever the namespace says its root tag
107 should be. Optionally you may specify
108 another root tag if the default is not
109 desired, or the namespace requres you to set
110 one.
111  
112 $xObj = $obj->NewChild("my:namespace");
113 $xObj = $obj->NewChild("my:namespace","foo");
114 ie. <foo xmlns='my:namespace'...></foo>
115  
116 InsertRawXML(string) - puts the specified string raw into the XML
117 packet that you call this on.
118  
119 $message->InsertRawXML("<foo></foo>")
120 <message...>...<foo></foo></message>
121  
122 $x = $message->NewChild(..);
123 $x->InsertRawXML("test");
124  
125 $query = $iq->GetChild(..);
126 $query->InsertRawXML("test");
127  
128 ClearRawXML() - removes the raw XML from the packet.
129  
130 =head2 Removal functions
131  
132 RemoveChild() - removes all of the namespaces child elements
133 RemoveChild(namespace) from the object. If a namespace is provided,
134 then only the children with that namespace are
135 removed.
136  
137 =head2 Test functions
138  
139 DefinedChild() - returns 1 if there are any known namespaced
140 DefinedChild(namespace) stanzas in the packet, 0 otherwise.
141 Optionally you can specify a namespace and
142 determine if there are any stanzas with that
143 namespace.
144  
145 $test = $obj->DefinedChild();
146 $test = $obj->DefinedChild("my:namespace");
147  
148 =head1 PACKAGES
149  
150 For more information on each of these packages, please see the man page
151 for each one.
152  
153 =head2 Net::XMPP3::Client
154  
155 This package contains the code needed to communicate with an XMPP
156 server: login, wait for messages, send messages, and logout. It uses
157 XML::Stream to read the stream from the server and based on what kind
158 of tag it encounters it calls a function to handle the tag.
159  
160 =head2 Net::XMPP3::Protocol
161  
162 A collection of high-level functions that Client uses to make their
163 lives easier. These methods are inherited by the Client.
164  
165 =head2 Net::XMPP3::JID
166  
167 The XMPP IDs consist of three parts: user id, server, and resource.
168 This module gives you access to those components without having to
169 parse the string yourself.
170  
171 =head2 Net::XMPP3::Message
172  
173 Everything needed to create and read a <message/> received from the
174 server.
175  
176 =head2 Net::XMPP3::Presence
177  
178 Everything needed to create and read a <presence/> received from the
179 server.
180  
181 =head2 Net::XMPP3::IQ
182  
183 IQ is a wrapper around a number of modules that provide support for
184 the various Info/Query namespaces that XMPP recognizes.
185  
186 =head2 Net::XMPP3::Stanza
187  
188 This module represents a namespaced stanza that is used to extend a
189 <message/>, <presence/>, and <iq/>.
190  
191 The man page for Net::XMPP3::Stanza contains a listing of all supported
192 namespaces, and the methods that are supported by the objects that
193 represent those namespaces.
194  
195 =head2 Net::XMPP3::Namespaces
196  
197 XMPP allows for any stanza to be extended by any bit of XML. This
198 module contains all of the internals for defining the XMPP based
199 extensions defined by the IETF. The documentation for this module
200 explains more about how to add your own custom namespace and have it
201 be supported.
202  
203 =head1 AUTHOR
204  
205 Ryan Eatmon
206 Currently maintained by Eric Hacker.
207 Currently fixed by Guruperl.
208  
209 =head1 BUGS
210  
211 Probably. There is at least one issue with XLM::Stream providing different node
212 structures depending on how the node is created. Net::XMPP3 should now be able to
213 handle this, but who knows what else lurks.
214  
215 =head1 COPYRIGHT
216  
217 This module is free software, you can redistribute it and/or modify it
218 under the LGPL.
219  
220 =cut
221  
222 11 require 5.005;
223 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
224 11 ▶use XML::Stream 1.22 qw( Node );
Subroutine BEGIN: called CC=1 SCAR=0.0
225 11 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
226 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
227 11 ▶use Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
228 11 ▶use Authen::SASL;
Subroutine BEGIN: called CC=1 SCAR=0.0
229 11 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
230 11 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
231 11 ▶use vars qw( $AUTOLOAD $VERSION $PARSING );
Subroutine BEGIN: called CC=1 SCAR=0.0
232  
233 11 $VERSION = "1.02";
234  
235 11 ▶use Net::XMPP3::Debug;
Subroutine BEGIN: called CC=1 SCAR=0.0
236 11 ▶use Net::XMPP3::JID;
Subroutine BEGIN: called CC=1 SCAR=0.0
237 11 ▶use Net::XMPP3::Namespaces;
Subroutine BEGIN: called CC=1 SCAR=0.0
238 11 ▶use Net::XMPP3::Stanza;
Subroutine BEGIN: called CC=1 SCAR=0.0
239 11 ▶use Net::XMPP3::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
240 11 ▶use Net::XMPP3::IQ;
Subroutine BEGIN: called CC=1 SCAR=0.0
241 11 ▶use Net::XMPP3::Presence;
Subroutine BEGIN: called CC=1 SCAR=0.0
242 11 ▶use Net::XMPP3::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
243 11 ▶use Net::XMPP3::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
244  
245  
246 ##############################################################################
247 #
248 # printData - debugging function to print out any data structure in an
249 # organized manner. Very useful for debugging XML::Parser::Tree
250 # objects. This is a private function that will only exist in
251 # in the development version.
252 #
253 ##############################################################################
254 sub printData
255 0 ▶{
Subroutine printData: not called CC=1 SCAR=6.9
Pod undocumented
256 0 print &sprintData(@_);
257 }
258  
259  
260 ##############################################################################
261 #
262 # sprintData - debugging function to build a string out of any data structure
263 # in an organized manner. Very useful for debugging
264 # XML::Parser::Tree objects and perl hashes of hashes.
265 #
266 # This is a private function.
267 #
268 ##############################################################################
269 sub sprintData
270 0 ▶{
Subroutine sprintData: not called CC=1 SCAR=6.9
Pod undocumented
271 0 return &XML::Stream::sprintData(@_);
272 }
273  
274  
275 ##############################################################################
276 #
277 # GetTimeStamp - generic funcion for getting a timestamp.
278 #
279 ##############################################################################
280 sub GetTimeStamp
281 4 ▶{
Subroutine GetTimeStamp: called CC=19 SCAR=45.1
Pod undocumented
282 4 my($type,$time,$length) = @_;
283  
284 4 ▶ return "" if (($type ne "local") && ($type ne "utc") && !($type =~ /^(local|utc)delay(local|utc|time)$/));
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "local" and $type ne "utc" and not $type =~ /^(local|utc)delay(local|utc|time)$/ 0 4 4
Condition33%2/6
$type ne "local" and $type ne "utc" and not $type =~ /^(local|utc)delay(local|utc|time)$/
!ll&&!rl&&r
400
$type ne "local" and $type ne "utc"
!ll&&!rl&&r
400
MC/DC0%0/3
$type ne "local" and $type ne "utc" and not $type =~ /^(local|utc)delay(local|utc|time)$/
$type ne "local" $type ne "utc" not $type =~ /^(local|utc)delay(local|utc|time)$/
Truth table25%1/4
A and B and not(C)
A= $type ne "local"B= $type ne "utc"C= not $type =~ /^(local|utc)delay(local|utc|time)$/
ABCresult
0XX0
10X0
1100
1111
285  
286 4 ▶ $length = "long" unless defined($length);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 4 0 4
287  
288 4 my ($sec,$min,$hour,$mday,$mon,$year,$wday);
289 4 ▶ if ($type =~ /utcdelay/)
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /utcdelay/) 0 4 4
290 {
291 0 ($year,$mon,$mday,$hour,$min,$sec) = ($time =~ /^(\d\d\d\d)(\d\d)(\d\d)T(\d\d)\:(\d\d)\:(\d\d)$/);
292 0 $mon--;
293 0 ($type) = ($type =~ /^utcdelay(.*)$/);
294 0 $time = timegm($sec,$min,$hour,$mday,$mon,$year);
295 }
296 4 ▶ if ($type =~ /localdelay/)
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /localdelay/) 0 4 4
297 {
298 0 ($year,$mon,$mday,$hour,$min,$sec) = ($time =~ /^(\d\d\d\d)(\d\d)(\d\d)T(\d\d)\:(\d\d)\:(\d\d)$/);
299 0 $mon--;
300 0 ($type) = ($type =~ /^localdelay(.*)$/);
301 0 $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
302 }
303  
304 4 ▶ return $time if ($type eq "time");
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "time" 0 4 4
305 4 ▶ ($sec,$min,$hour,$mday,$mon,$year,$wday) =
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "local" 4 0 4
defined $time && $time ne '' ? : 0 4 4
Condition33%1/3
defined $time and $time ne ""
!ll&&!rl&&r
400
MC/DC0%0/2
defined $time and $time ne ""
defined $time $time ne ""
Truth table33%1/3
A and B
A= defined $timeB= $time ne ""
ABresult
0X0
100
111
306 localtime(((defined($time) && ($time ne "")) ? $time : time)) if ($type eq "local");
307 4 ▶ ($sec,$min,$hour,$mday,$mon,$year,$wday) =
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "utc" 0 4 4
defined $time && $time ne '' ? : 0 0 0
Condition0%0/3
defined $time and $time ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $time and $time ne ""
defined $time $time ne ""
Truth table0%0/3
A and B
A= defined $timeB= $time ne ""
ABresult
0X0
100
111
308 gmtime(((defined($time) && ($time ne "")) ? $time : time)) if ($type eq "utc");
309  
310 4 ▶ return sprintf("%d%02d%02dT%02d:%02d:%02d",($year + 1900),($mon+1),$mday,$hour,$min,$sec) if ($length eq "stamp");
Branch: 1 branch
BranchTrueFalseTotal
if $length eq "stamp" 0 4 4
311  
312 4 $wday = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[$wday];
313  
314 4 my $month = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')[$mon];
315 4 $mon++;
316  
317 4 ▶ return sprintf("%3s %3s %02d, %d %02d:%02d:%02d",$wday,$month,$mday,($year + 1900),$hour,$min,$sec) if ($length eq "long");
Branch: 1 branch
BranchTrueFalseTotal
if $length eq "long" 4 0 4
318 0 ▶ return sprintf("%3s %d/%02d/%02d %02d:%02d",$wday,($year + 1900),$mon,$mday,$hour,$min) if ($length eq "normal");
Branch: 1 branch
BranchTrueFalseTotal
if $length eq "normal" 0 0 0
319 0 ▶ return sprintf("%02d:%02d:%02d",$hour,$min,$sec) if ($length eq "short");
Branch: 1 branch
BranchTrueFalseTotal
if $length eq "short" 0 0 0
320 0 ▶ return sprintf("%02d:%02d",$hour,$min) if ($length eq "shortest");
Branch: 1 branch
BranchTrueFalseTotal
if $length eq "shortest" 0 0 0
321 }
322  
323  
324 ##############################################################################
325 #
326 # GetHumanTime - convert seconds, into a human readable time string.
327 #
328 ##############################################################################
329 sub GetHumanTime
330 0 ▶{
Subroutine GetHumanTime: not called CC=16 SCAR=56.1
Pod undocumented
331 0 my $seconds = shift;
332  
333 0 my $minutes = 0;
334 0 my $hours = 0;
335 0 my $days = 0;
336 0 my $weeks = 0;
337  
338 0 ▶ while ($seconds >= 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seconds >= 60) 0 0 0
339 0 $minutes++;
340 0 ▶ if ($minutes == 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minutes == 60) 0 0 0
341 0 $hours++;
342 0 ▶ if ($hours == 24) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hours == 24) 0 0 0
343 0 $days++;
344 0 ▶ if ($days == 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($days == 7) 0 0 0
345 0 $weeks++;
346 0 $days -= 7;
347 }
348 0 $hours -= 24;
349 }
350 0 $minutes -= 60;
351 }
352 0 $seconds -= 60;
353 }
354  
355 0 my $humanTime;
356 0 ▶ $humanTime .= "$weeks week " if ($weeks == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $weeks == 1 0 0 0
357 0 ▶ $humanTime .= "$weeks weeks " if ($weeks > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $weeks > 1 0 0 0
358 0 ▶ $humanTime .= "$days day " if ($days == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $days == 1 0 0 0
359 0 ▶ $humanTime .= "$days days " if ($days > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $days > 1 0 0 0
360 0 ▶ $humanTime .= "$hours hour " if ($hours == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $hours == 1 0 0 0
361 0 ▶ $humanTime .= "$hours hours " if ($hours > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $hours > 1 0 0 0
362 0 ▶ $humanTime .= "$minutes minute " if ($minutes == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $minutes == 1 0 0 0
363 0 ▶ $humanTime .= "$minutes minutes " if ($minutes > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $minutes > 1 0 0 0
364 0 ▶ $humanTime .= "$seconds second " if ($seconds == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $seconds == 1 0 0 0
365 0 ▶ $humanTime .= "$seconds seconds " if ($seconds > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $seconds > 1 0 0 0
366  
367 0 ▶ $humanTime = "none" if ($humanTime eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $humanTime eq "" 0 0 0
368  
369 0 return $humanTime;
370 }
371  
372 11 1;
« XMPP3/Protocol.pm ↑ Summary XMPP3/Stanza.pm »