| 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<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 L<Net::XMPP::Protocol>. | ||||||||||
| 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 | 15 | require 5.008; | |||||||||
| 251 | 15 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 252 | 15 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 253 | 15 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 254 | 15 | ▶ | use Net::XMPP::Connection; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 255 | 15 | ▶ | use base qw( Net::XMPP::Connection ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 256 | |||||||||||
| 257 | sub new | ||||||||||
| 258 | { | ||||||||||
| 259 | 4 | ▶ | my $proto = shift; | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 260 | 4 | my $self = { }; | |||||||||
| 261 | |||||||||||
| 262 | 4 | bless($self, $proto); | |||||||||
| 263 | 4 | $self->init(@_); | |||||||||
| 264 | |||||||||||
| 265 | 4 | $self->{SERVER}->{port} = 5222; | |||||||||
| 266 | 4 | $self->{SERVER}->{namespace} = "jabber:client"; | |||||||||
| 267 | 4 | $self->{SERVER}->{allow_register} = 1; | |||||||||
| 268 | |||||||||||
| 269 | 4 | return $self; | |||||||||
| 270 | } | ||||||||||
| 271 | |||||||||||
| 272 | |||||||||||
| 273 | sub _auth | ||||||||||
| 274 | { | ||||||||||
| 275 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_auth: not called CC=3 SCAR=24.8
| |||||||||||
| 276 | |||||||||||
| 277 | 0 | my %args; | |||||||||
| 278 | 0 | ▶ | while($#_ >= 0) { $args{ lc pop(@_) } = pop(@_); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 279 | |||||||||||
| 280 | 0 | my %auth; | |||||||||
| 281 | 0 | $auth{username} = $args{username}; | |||||||||
| 282 | 0 | $auth{password} = $args{password}; | |||||||||
| 283 | 0 | ▶ | $auth{resource} = $args{resource} if exists($args{resource}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 284 | |||||||||||
| 285 | 0 | return $self->AuthSend(%auth); | |||||||||
| 286 | } | ||||||||||
| 287 | |||||||||||
| 288 | |||||||||||
| 289 | sub _connection_args | ||||||||||
| 290 | { | ||||||||||
| 291 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_connection_args: not called CC=5 SCAR=34.0
| |||||||||||
| 292 | 0 | my (%args) = @_; | |||||||||
| 293 | |||||||||||
| 294 | 0 | my %connect; | |||||||||
| 295 | 0 | $connect{hostname} = $args{hostname}; | |||||||||
| 296 | 0 | ▶ | $connect{port} = $args{port} if exists($args{port}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 297 | 0 | ▶ | $connect{connectiontype} = $args{connectiontype} if exists($args{connectiontype}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 298 | 0 | ▶ | $connect{timeout} = $args{connecttimeout} if exists($args{connecttimeout}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 299 | 0 | ▶ | $connect{tls} = $args{tls} if exists($args{tls}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 300 | |||||||||||
| 301 | 0 | return %connect; | |||||||||
| 302 | } | ||||||||||
| 303 | |||||||||||
| 304 | |||||||||||
| 305 | sub _register | ||||||||||
| 306 | { | ||||||||||
| 307 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_register: not called CC=2 SCAR=17.9
| |||||||||||
| 308 | |||||||||||
| 309 | 0 | my %args; | |||||||||
| 310 | 0 | ▶ | while($#_ >= 0) { $args{ lc pop(@_) } = pop(@_); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 15 | 1; | |||||||||