File Coverage

blib/lib/Net/XMPP/Client.pm
Criterion Covered Total %
statement 23 50 46.0
branch 0 10 0.0
condition n/a
subroutine 6 9 66.6
pod 1 1 100.0
total 30 70 42.8


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::XMPP::Client;
23              
24             =head1 NAME
25              
26             Net::XMPP::Client - XMPP Client Module
27              
28             =head1 SYNOPSIS
29              
30             Net::XMPP::Client is a module that provides a developer easy access
31             to the Extensible Messaging and Presence Protocol (XMPP).
32              
33             =head1 DESCRIPTION
34              
35             Client.pm uses Protocol.pm to provide enough high level APIs and
36             automation of the low level APIs that writing an XMPP Client in
37             Perl is trivial. For those that wish to work with the low level
38             you can do that too, but those functions are covered in the
39             documentation for each module.
40              
41             Net::XMPP::Client provides functions to connect to an XMPP server,
42             login, send and receive messages, set personal information, create
43             a new user account, manage the roster, and disconnect. You can use
44             all or none of the functions, there is no requirement.
45              
46             For more information on how the details for how Net::XMPP is written
47             please see the help for L itself.
48              
49             For a full list of high level functions available please see
50             Net::XMPP::Protocol.
51              
52             =head2 Basic Functions
53              
54             use Net::XMPP;
55              
56             $Con = Net::XMPP::Client->new();
57              
58             $Con->SetCallbacks(...);
59              
60             $Con->Execute(hostname=>"jabber.org",
61             username=>"bob",
62             password=>"XXXX",
63             resource=>"Work"
64             );
65              
66              
67             For the list of available functions see L.
68              
69             $Con->Disconnect();
70              
71             =head1 METHODS
72              
73             =head1 Basic Functions
74              
75             =head2 new
76              
77             new(debuglevel=>0|1|2,
78             debugfile=>string,
79             debugtime=>0|1)
80              
81             creates the Client object. debugfile
82             should be set to the path for the debug
83             log to be written. If set to "stdout"
84             then the debug will go there. debuglevel
85             controls the amount of debug. For more
86             information about the valid setting for
87             debuglevel, debugfile, and debugtime see
88             Net::XMPP::Debug.
89              
90             =head2 Connect
91              
92             Connect(hostname=>string,
93             port=>integer,
94             timeout=>int,
95             connectiontype=>string,
96             tls=>0|1,
97             srv=>0|1,
98             componentname=>string)
99              
100             opens a connection to the server
101             listed in the hostname (default
102             localhost), on the port (default
103             5222) listed, using the
104             connectiontype listed (default
105             tcpip). The two connection types
106             available are:
107              
108             tcpip standard TCP socket
109             http TCP socket, but with the
110             headers needed to talk
111             through a web proxy
112              
113             If you specify tls, then it TLS
114             will be used if it is available
115             as a feature.
116              
117             If srv is specified AND Net::DNS is
118             installed and can be loaded, then
119             an SRV query is sent to srv.hostname
120             and the results processed to replace
121             the hostname and port. If the lookup
122             fails, or Net::DNS cannot be loaded,
123             then hostname and port are left alone
124             as the defaults.
125              
126            
127             Alternatively, you may manually specify
128             componentname as the domain portion of the
129             jid and leave hostname set to the actual
130             hostname of the XMPP server.
131              
132             =head2 Execute
133              
134             Execute(hostname=>string,
135             port=>int,
136             tls=>0|1,
137             username=>string,
138             password=>string,
139             resource=>string,
140             register=>0|1,
141             connectiontype=>string,
142             connecttimeout=>string,
143             connectattempts=>int,
144             connectsleep=>int,
145             processtimeout=>int)
146              
147              
148             Generic inner loop to handle
149             connecting to the server, calling
150             Process, and reconnecting if the
151             connection is lost. There are
152             five callbacks available that are
153             called at various places:
154              
155             onconnect - when the client has
156             made a connection.
157              
158             onauth - when the connection is
159             made and user has been
160             authed. Essentially,
161             this is when you can
162             start doing things
163             as a Client. Like
164             send presence, get your
165             roster, etc...
166              
167             onprocess - this is the most
168             inner loop and so
169             gets called the most.
170             Be very very careful
171             what you put here
172             since it can
173             *DRASTICALLY* affect
174             performance.
175              
176             ondisconnect - when the client
177             disconnects from
178             the server.
179              
180             onexit - when the function gives
181             up trying to connect and
182             exits.
183              
184             The arguments are passed straight
185             on to the Connect function, except
186             for connectattempts and connectsleep.
187             connectattempts is the number of
188             times that the Component should try
189             to connect before giving up. -1
190             means try forever. The default is
191             -1. connectsleep is the number of
192             seconds to sleep between each
193             connection attempt.
194              
195             If you specify register=>1, then the
196             Client will attempt to register the
197             sepecified account for you, if it
198             does not exist.
199              
200             =head2 Process
201              
202             Process(integer)
203              
204             takes the timeout period as an argument. If no
205             timeout is listed then the function blocks until
206             a packet is received. Otherwise it waits that
207             number of seconds and then exits so your program
208             can continue doing useful things. NOTE: This is
209             important for GUIs. You need to leave time to
210             process GUI commands even if you are waiting for
211             packets. The following are the possible return
212             values, and what they mean:
213              
214             1 - Status ok, data received.
215             0 - Status ok, no data received.
216             undef - Status not ok, stop processing.
217              
218             IMPORTANT: You need to check the output of every
219             Process. If you get an undef then the connection
220             died and you should behave accordingly.
221              
222             =head2 Disconnect
223              
224             Disconnect()
225              
226             closes the connection to the server.
227              
228             =head2 Connected
229              
230             Connected()
231              
232             returns 1 if the Transport is connected to the server,
233             and 0 if not.
234              
235             =head1 AUTHOR
236              
237             Originally authored by Ryan Eatmon.
238              
239             Previously maintained by Eric Hacker.
240              
241             Currently maintained by Darian Anthony Patrick.
242              
243             =head1 COPYRIGHT
244              
245             This module is free software, you can redistribute it and/or modify it
246             under the LGPL 2.1.
247              
248             =cut
249              
250             require 5.008;
251 15     15   71 use strict;
  15         20  
  15         588  
252 15     15   62 use warnings;
  15         25  
  15         502  
253 15     15   61 use Carp;
  15         21  
  15         1061  
254 15     15   6015 use Net::XMPP::Connection;
  15         32  
  15         451  
255 15     15   61 use base qw( Net::XMPP::Connection );
  15         19  
  15         4811  
256              
257             sub new
258             {
259 11     11 1 43679 my $proto = shift;
260 11         50 my $self = { };
261              
262 11         58 bless($self, $proto);
263 11         96 $self->init(@_);
264              
265 11         36 $self->{SERVER}->{port} = 5222;
266 11         31 $self->{SERVER}->{namespace} = "jabber:client";
267 11         30 $self->{SERVER}->{allow_register} = 1;
268              
269 11         46 return $self;
270             }
271              
272              
273             sub _auth
274             {
275 0     0     my $self = shift;
276              
277 0           my %args;
278 0           while($#_ >= 0) { $args{ lc pop(@_) } = pop(@_); }
  0            
279              
280 0           my %auth;
281 0           $auth{username} = $args{username};
282 0           $auth{password} = $args{password};
283 0 0         $auth{resource} = $args{resource} if exists($args{resource});
284              
285 0           return $self->AuthSend(%auth);
286             }
287              
288              
289             sub _connection_args
290             {
291 0     0     my $self = shift;
292 0           my (%args) = @_;
293              
294 0           my %connect;
295 0           $connect{hostname} = $args{hostname};
296 0 0         $connect{port} = $args{port} if exists($args{port});
297 0 0         $connect{connectiontype} = $args{connectiontype} if exists($args{connectiontype});
298 0 0         $connect{timeout} = $args{connecttimeout} if exists($args{connecttimeout});
299 0 0         $connect{tls} = $args{tls} if exists($args{tls});
300              
301 0           return %connect;
302             }
303              
304              
305             sub _register
306             {
307 0     0     my $self = shift;
308              
309 0           my %args;
310 0           while($#_ >= 0) { $args{ lc pop(@_) } = pop(@_); }
  0            
311              
312 0           my %fields = $self->RegisterRequest();
313              
314 0           $fields{username} = $args{username};
315 0           $fields{password} = $args{password};
316              
317 0           $self->RegisterSend(%fields);
318              
319 0           return $self->_auth(%args);
320             }
321              
322             1;