| 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 Net::XMPP 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 Net::XMPP::Protocol. |
| 68 |
# |
| 69 |
|
| 70 |
$Con->Disconnect(); |
| 71 |
|
| 72 |
=head1 METHODS |
| 73 |
|
| 74 |
=head2 Basic Functions |
| 75 |
|
| 76 |
new(debuglevel=>0|1|2, - creates the Client object. debugfile |
| 77 |
debugfile=>string, should be set to the path for the debug |
| 78 |
debugtime=>0|1) log to be written. If set to "stdout" |
| 79 |
then the debug will go there. debuglevel |
| 80 |
controls the amount of debug. For more |
| 81 |
information about the valid setting for |
| 82 |
debuglevel, debugfile, and debugtime see |
| 83 |
Net::XMPP::Debug. |
| 84 |
|
| 85 |
Connect(hostname=>string, - opens a connection to the server |
| 86 |
port=>integer, listed in the hostname (default |
| 87 |
timeout=>int localhost), on the port (default |
| 88 |
connectiontype=>string, 5222) listed, using the |
| 89 |
tls=>0|1) connectiontype listed (default |
| 90 |
tcpip). The two connection types |
| 91 |
available are: |
| 92 |
tcpip standard TCP socket |
| 93 |
http TCP socket, but with the |
| 94 |
headers needed to talk |
| 95 |
through a web proxy |
| 96 |
If you specify tls, then it TLS |
| 97 |
will be used if it is available |
| 98 |
as a feature. |
| 99 |
|
| 100 |
Execute(hostname=>string, - Generic inner loop to handle |
| 101 |
port=>int, connecting to the server, calling |
| 102 |
tls=>0|1, Process, and reconnecting if the |
| 103 |
username=>string, connection is lost. There are |
| 104 |
password=>string, five callbacks available that are |
| 105 |
resource=>string, called at various places: |
| 106 |
register=>0|1, onconnect - when the client has |
| 107 |
connectiontype=>string, made a connection. |
| 108 |
connecttimeout=>string, onauth - when the connection is |
| 109 |
connectattempts=>int, made and user has been |
| 110 |
connectsleep=>int, authed. Essentially, |
| 111 |
processtimeout=>int) this is when you can |
| 112 |
start doing things |
| 113 |
as a Client. Like |
| 114 |
send presence, get your |
| 115 |
roster, etc... |
| 116 |
onprocess - this is the most |
| 117 |
inner loop and so |
| 118 |
gets called the most. |
| 119 |
Be very very careful |
| 120 |
what you put here |
| 121 |
since it can |
| 122 |
*DRASTICALLY* affect |
| 123 |
performance. |
| 124 |
ondisconnect - when the client |
| 125 |
disconnects from |
| 126 |
the server. |
| 127 |
onexit - when the function gives |
| 128 |
up trying to connect and |
| 129 |
exits. |
| 130 |
The arguments are passed straight |
| 131 |
on to the Connect function, except |
| 132 |
for connectattempts and connectsleep. |
| 133 |
connectattempts is the number of |
| 134 |
times that the Component should try |
| 135 |
to connect before giving up. -1 |
| 136 |
means try forever. The default is |
| 137 |
-1. connectsleep is the number of |
| 138 |
seconds to sleep between each |
| 139 |
connection attempt. |
| 140 |
|
| 141 |
If you specify register=>1, then the |
| 142 |
Client will attempt to register the |
| 143 |
sepecified account for you, if it |
| 144 |
does not exist. |
| 145 |
|
| 146 |
Process(integer) - takes the timeout period as an argument. If no |
| 147 |
timeout is listed then the function blocks until |
| 148 |
a packet is received. Otherwise it waits that |
| 149 |
number of seconds and then exits so your program |
| 150 |
can continue doing useful things. NOTE: This is |
| 151 |
important for GUIs. You need to leave time to |
| 152 |
process GUI commands even if you are waiting for |
| 153 |
packets. The following are the possible return |
| 154 |
values, and what they mean: |
| 155 |
|
| 156 |
1 - Status ok, data received. |
| 157 |
0 - Status ok, no data received. |
| 158 |
undef - Status not ok, stop processing. |
| 159 |
|
| 160 |
IMPORTANT: You need to check the output of every |
| 161 |
Process. If you get an undef then the connection |
| 162 |
died and you should behave accordingly. |
| 163 |
|
| 164 |
Disconnect() - closes the connection to the server. |
| 165 |
|
| 166 |
Connected() - returns 1 if the Transport is connected to the server, |
| 167 |
and 0 if not. |
| 168 |
|
| 169 |
=head1 AUTHOR |
| 170 |
|
| 171 |
Ryan Eatmon |
| 172 |
|
| 173 |
=head1 COPYRIGHT |
| 174 |
|
| 175 |
This module is free software; you can redistribute it and/or modify |
| 176 |
it under the same terms as Perl itself. |
| 177 |
|
| 178 |
=cut |
| 179 |
|
| 180 |
use strict; |
| 181 |
use Carp; |
| 182 |
use Net::XMPP::Connection; |
| 183 |
use base qw( Net::XMPP::Connection ); |
| 184 |
|
| 185 |
sub new |
| 186 |
{ |
| 187 |
my $proto = shift; |
| 188 |
my $self = { }; |
| 189 |
|
| 190 |
bless($self, $proto); |
| 191 |
$self->init(@_); |
| 192 |
|
| 193 |
$self->{SERVER}->{port} = 5222; |
| 194 |
$self->{SERVER}->{namespace} = "jabber:client"; |
| 195 |
$self->{SERVER}->{allow_register} = 1; |
| 196 |
|
| 197 |
return $self; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
sub _auth |
| 202 |
{ |
| 203 |
my $self = shift; |
| 204 |
|
| 205 |
my %args; |
| 206 |
while($#_ >= 0) { $args{ lc pop(@_) } = pop(@_); } |
| 207 |
|
| 208 |
my %auth; |
| 209 |
$auth{username} = $args{username}; |
| 210 |
$auth{password} = $args{password}; |
| 211 |
$auth{resource} = $args{resource} if exists($args{resource}); |
| 212 |
|
| 213 |
return $self->AuthSend(%auth); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
sub _connection_args |
| 218 |
{ |
| 219 |
my $self = shift; |
| 220 |
my (%args) = @_; |
| 221 |
|
| 222 |
my %connect; |
| 223 |
$connect{hostname} = $args{hostname}; |
| 224 |
$connect{port} = $args{port} if exists($args{port}); |
| 225 |
$connect{connectiontype} = $args{connectiontype} if exists($args{connectiontype}); |
| 226 |
$connect{timeout} = $args{connecttimeout} if exists($args{connecttimeout}); |
| 227 |
$connect{tls} = $args{tls} if exists($args{tls}); |
| 228 |
|
| 229 |
return %connect; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
sub _register |
| 234 |
{ |
| 235 |
my $self = shift; |
| 236 |
|
| 237 |
my %args; |
| 238 |
while($#_ >= 0) { $args{ lc pop(@_) } = pop(@_); } |
| 239 |
|
| 240 |
my %fields = $self->RegisterRequest(); |
| 241 |
|
| 242 |
$fields{username} = $args{username}; |
| 243 |
$fields{password} = $args{password}; |
| 244 |
|
| 245 |
$self->RegisterSend(%fields); |
| 246 |
|
| 247 |
return $self->_auth(%args); |
| 248 |
} |
| 249 |
|
| 250 |
1; |