ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-XMPP2/lib/Net/XMPP2/Connection.pm
Revision: 1.33
Committed: Wed Jul 11 22:29:27 2007 UTC (19 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.32: +36 -1 lines
Log Message:
added send_*_hook event hooks

File Contents

# User Rev Content
1 elmex 1.1 package Net::XMPP2::Connection;
2     use strict;
3     use AnyEvent;
4     use IO::Socket::INET;
5     use Net::XMPP2::Parser;
6     use Net::XMPP2::Writer;
7 elmex 1.10 use Net::XMPP2::Util qw/split_jid/;
8 elmex 1.9 use Net::XMPP2::Event;
9     use Net::XMPP2::SimpleConnection;
10 elmex 1.1 use Net::XMPP2::Namespaces qw/xmpp_ns/;
11 elmex 1.28 use Net::XMPP2::Extendable;
12 elmex 1.10 use Net::XMPP2::Error;
13 elmex 1.1 use Net::DNS;
14 elmex 1.2
15 elmex 1.28 our @ISA = qw/Net::XMPP2::SimpleConnection Net::XMPP2::Event Net::XMPP2::Extendable/;
16 elmex 1.1
17     =head1 NAME
18    
19 elmex 1.23 Net::XMPP2::Connection - XML stream that implements the XMPP RFC 3920.
20 elmex 1.1
21     =head1 SYNOPSIS
22    
23     use Net::XMPP2::Connection;
24    
25     my $con =
26     Net::XMPP2::Connection->new (
27     username => "abc",
28     domain => "jabber.org",
29     resource => "Net::XMPP2"
30     );
31    
32     $con->connect or die "Couldn't connect to jabber.org: $!";
33     $con->init;
34     $con->reg_cb (stream_ready => sub { print "XMPP stream ready!\n" });
35    
36     =head1 DESCRIPTION
37    
38     This module represents a XMPP stream as described in RFC 3920. You can issue the basic
39     XMPP XML stanzas with methods like C<send_iq>, C<send_message> and C<send_presence>.
40    
41     And receive events with the C<reg_cb> event framework from the connection.
42    
43     If you need instant messaging stuff please take a look at C<Net::XMPP2::IM::Connection>.
44    
45     =head1 METHODS
46    
47 elmex 1.20 =over 4
48    
49     =item B<new (%args)>
50 elmex 1.1
51     Following arguments can be passed in C<%args>:
52    
53     =over 4
54    
55     =item language => $tag
56    
57     This should be the language of the human readable contents that
58     will be transmitted over the stream. The default will be 'en'.
59    
60     Please look in RFC 3066 how C<$tag> should look like.
61    
62 elmex 1.10 =item jid => $jid
63    
64     This can be used to set the settings C<username>, C<domain>
65     (and optionally C<resource>) from a C<$jid>.
66    
67 elmex 1.1 =item resource => $resource
68    
69     If this argument is given C<$resource> will be passed as desired
70     resource on resource binding.
71    
72     Note: You have to take care that the stringprep profile for
73     resources can be applied at: C<$resource>. Otherwise the server
74     might signal an error. See L<Net::XMPP2::Util> for utility functions
75     to check this.
76    
77     =item domain => $domain
78    
79     This is the destination host we are going to connect to.
80     As the connection won't be automatically connected use C<connect>
81     to initiate the connect.
82    
83     Note: A SRV RR lookup will be performed to discover the real hostname
84     and port to connect to. See also C<connect>.
85    
86 elmex 1.9 =item override_host => $host
87     =item override_port => $port
88    
89     This will be used as override to connect to.
90    
91 elmex 1.1 =item port => $port
92    
93     This is optional, the default port is 5222.
94    
95     Note: A SRV RR lookup will be performed to discover the real hostname
96     and port to connect to. See also C<connect>.
97    
98     =item username => $username
99    
100     This is your C<$username> (the userpart in the JID);
101    
102     Note: You have to take care that the stringprep profile for
103     nodes can be applied at: C<$username>. Otherwise the server
104     might signal an error. See L<Net::XMPP2::Util> for utility functions
105     to check this.
106    
107     =item password => $password
108    
109     This is the password for the C<username> above.
110    
111 elmex 1.5 =item disable_ssl => $bool
112    
113     If C<$bool> is true no SSL will be used.
114    
115 elmex 1.1 =back
116    
117     =cut
118    
119     sub new {
120     my $this = shift;
121     my $class = ref($this) || $this;
122 elmex 1.26 my $self = $class->SUPER::new (language => 'en', @_);
123 elmex 1.1
124     $self->{parser} = new Net::XMPP2::Parser;
125     $self->{writer} = Net::XMPP2::Writer->new (
126 elmex 1.33 write_cb => sub { $self->write_data ($_[0]) },
127     send_iq_cb => sub { $self->event (send_iq_hook => @_) },
128     send_msg_cb => sub { $self->event (send_message_hook => @_) },
129     send_pres_cb => sub { $self->event (send_presence_hook => @_) },
130 elmex 1.1 );
131    
132     $self->{parser}->set_stanza_cb (sub {
133     $self->handle_stanza (@_);
134     });
135 elmex 1.19 $self->{parser}->set_error_cb (sub {
136 elmex 1.29 my ($ex, $data, $type) = @_;
137     if ($type eq 'xml') {
138     my $pe = Net::XMPP2::Error::Parser->new (exception => $_[0], data => $_[1]);
139     $self->event (xml_parser_error => $pe);
140     $self->disconnect ("xml error: $_[0], $_[1]");
141     } else {
142     my $pe = Net::XMPP2::Error->new (
143     text => "uncaught exception in stanza handling: $ex"
144     );
145     $self->event (uncaught_exception_error => $pe);
146     $self->disconnect ($pe->string);
147     }
148 elmex 1.19 });
149 elmex 1.1
150 elmex 1.15 $self->{iq_id} = 1;
151     $self->{default_iq_timeout} = 60;
152 elmex 1.1
153     $self->{disconnect_cb} = sub {
154     my ($host, $port, $message) = @_;
155 elmex 1.7 delete $self->{authenticated};
156     delete $self->{ssl_enabled};
157 elmex 1.1 $self->event (disconnect => $host, $port, $message);
158     };
159    
160 elmex 1.10 if ($self->{jid}) {
161     my ($user, $host, $res) = split_jid ($self->{jid});
162     $self->{username} = $user;
163     $self->{domain} = $host;
164     $self->{resource} = $res if defined $res;
165     }
166    
167 elmex 1.7 for (qw/username password domain/) {
168     die "No '$_' argument given to new, but '$_' is required\n"
169     unless $self->{$_};
170     }
171    
172 elmex 1.28 my $proxy_cb = sub {
173     my ($self, $er) = @_;
174     $self->event (error => $er);
175     1
176     };
177    
178     $self->reg_cb (
179     xml_parser_error => $proxy_cb,
180     sasl_error => $proxy_cb,
181     stream_error => $proxy_cb,
182     bind_error => $proxy_cb,
183 elmex 1.31 iq_result_cb_exception => sub {
184     my ($self, $ex) = @_;
185     $self->event (error =>
186     Net::XMPP2::Error::Exception->new (
187     exception => $ex, context => 'iq result callback execution'
188     )
189     );
190     1
191     },
192 elmex 1.28 tls_error => sub {
193     my ($self) = @_;
194     $self->event (error =>
195     Net::XMPP2::Error->new (text => 'tls_error: tls negotiation failed')
196     );
197     1
198     },
199     );
200    
201 elmex 1.1 return $self;
202     }
203    
204 elmex 1.20 =item B<connect ($no_srv_rr)>
205 elmex 1.1
206     Try to connect to the domain and port passed in C<new>.
207    
208     A SRV RR lookup will be performed on the domain to discover
209     the host and port to use. If you don't want this set C<$no_srv_rr>
210     to a true value. C<$no_srv_rr> is false by default.
211    
212     As the SRV RR lookup might return multiple host and you fail to
213     connect to one you might just call this function again to try a
214     different host.
215    
216     If C<connect> was successful and we connected a true value is returned.
217     If the connect was unsuccessful undef is returned and C<$!> will be set
218     to the error that occured while connecting.
219    
220     If you want to know whether further connection attempts might be more
221     successful (as SRV RR lookup may return multiple hosts) call C<may_try_connect>
222     (see also C<may_try_connect>).
223    
224     Note that an internal list will be kept of tried hosts. Use
225     C<reset_connect_tries> to reset the internal list of tried hosts.
226    
227     =cut
228    
229     sub connect {
230     my ($self, $no_srv_rr) = @_;
231    
232     my ($host, $port) = ($self->{domain}, $self->{port} || 5222);
233 elmex 1.9 if ($self->{override_host}) {
234     ($host, $port) = ($self->{override_host}, $self->{override_port} || 5222);
235 elmex 1.1
236 elmex 1.9 } else {
237     unless ($no_srv_rr) {
238     my $res = Net::DNS::Resolver->new;
239     my $p = $res->query ('_xmpp-client._tcp.'.$host, 'SRV');
240     if ($p) {
241     my @srvs = grep { $_->type eq 'SRV' } $p->answer;
242     if (@srvs) {
243     @srvs = sort { $a->priority <=> $b->priority } @srvs;
244     @srvs = sort { $b->weight <=> $a->weight } @srvs; # TODO
245     $port = $srvs[0]->port;
246     $host = $srvs[0]->target;
247     }
248 elmex 1.1 }
249     }
250     }
251    
252     if ($self->SUPER::connect ($host, $port)) {
253     $self->event (connect => $host, $port);
254     return 1;
255     } else {
256     return undef;
257     }
258     }
259    
260 elmex 1.20 =item B<may_try_connect>
261 elmex 1.1
262     Returns the number of left alternatives of hosts to connect to for the
263     domain passed to C<new>.
264    
265     An internal list of tried hosts will be managed by C<connect> and those
266     hosts will be ignored by a SRV RR lookup (which will be done if you
267     call this function).
268    
269     Use C<reset_connect_tries> to reset the internal list of tried hosts.
270    
271     =cut
272    
273     sub may_try_connect {
274     # TODO
275     }
276    
277 elmex 1.20 =item B<reset_connect_tries>
278 elmex 1.1
279     This function resets the internal list of tried hosts for C<connect>.
280     See also C<connect>.
281    
282     =cut
283    
284     sub reset_connect_tries {
285     # TODO
286     }
287    
288     sub handle_data {
289     my ($self, $buf) = @_;
290     $self->event (debug_recv => $$buf);
291     $self->{parser}->feed (substr $$buf, 0, (length $$buf), '');
292     }
293    
294 elmex 1.5 sub debug_wrote_data {
295     my ($self, $data) = @_;
296     $self->event (debug_send => $data);
297     }
298    
299 elmex 1.1 sub write_data {
300     my ($self, $data) = @_;
301     $self->SUPER::write_data ($data);
302     }
303    
304     sub handle_stanza {
305     my ($self, $p, $node) = @_;
306    
307 elmex 1.18 if (not defined $node) { # got stream end
308     $self->disconnect ("end of 'XML' stream encountered");
309     return;
310     }
311    
312 elmex 1.31 $self->event (recv_stanza_xml => $node);
313    
314 elmex 1.1 if ($node->eq (stream => 'features')) {
315     $self->event (stream_features => $node);
316 elmex 1.28 $self->{features} = $node;
317 elmex 1.1 $self->handle_stream_features ($node);
318 elmex 1.5
319 elmex 1.2 } elsif ($node->eq (tls => 'proceed')) {
320     $self->enable_ssl;
321     $self->{parser}->init;
322     $self->{writer}->init;
323     $self->{writer}->send_init_stream ($self->{language}, $self->{domain});
324    
325 elmex 1.11 } elsif ($node->eq (tls => 'failure')) {
326     $self->event ('tls_error');
327     $self->disconnect ('TLS failure on TLS negotiation.');
328    
329 elmex 1.1 } elsif ($node->eq (sasl => 'challenge')) {
330     $self->handle_sasl_challenge ($node);
331 elmex 1.11
332 elmex 1.1 } elsif ($node->eq (sasl => 'success')) {
333     $self->handle_sasl_success ($node);
334 elmex 1.11
335     } elsif ($node->eq (sasl => 'failure')) {
336     my $error = Net::XMPP2::Error::SASL->new (node => $node);
337     $self->event (sasl_error => $error);
338 elmex 1.28 $self->disconnect ('SASL authentication failure: ' . $error->string);
339 elmex 1.11
340 elmex 1.1 } elsif ($node->eq (client => 'iq')) {
341 elmex 1.32 $self->event (iq_xml => $node);
342 elmex 1.1 $self->handle_iq ($node);
343 elmex 1.11
344 elmex 1.4 } elsif ($node->eq (client => 'message')) {
345 elmex 1.6 $self->event (message_xml => $node);
346 elmex 1.11
347 elmex 1.4 } elsif ($node->eq (client => 'presence')) {
348 elmex 1.6 $self->event (presence_xml => $node);
349 elmex 1.11
350 elmex 1.1 } elsif ($node->eq (stream => 'error')) {
351     $self->handle_error ($node);
352 elmex 1.11
353 elmex 1.1 } else {
354     warn "Didn't understood stanza: '" . $node->name . "'";
355     }
356     }
357    
358 elmex 1.20 =item B<init ()>
359 elmex 1.1
360     Initiate the XML stream.
361    
362     =cut
363    
364     sub init {
365     my ($self) = @_;
366     $self->{writer}->send_init_stream ($self->{language}, $self->{domain});
367     }
368    
369 elmex 1.20 =item B<is_connected ()>
370 elmex 1.10
371     Returns true if the connection is still connected and stanzas can be
372     sent.
373    
374     =cut
375    
376     sub is_connected {
377     my ($self) = @_;
378     $self->{authenticated}
379     }
380    
381 elmex 1.20 =item B<set_default_iq_timeout ($seconds)>
382 elmex 1.15
383     This sets the default timeout for IQ requests. If the timeout runs out
384     the request will be aborted and the callback called with a L<Net::XMPP2::Error::IQ> object
385 elmex 1.22 where the C<condition> method returns a special value (see also C<condition> method of L<Net::XMPP2::Error::IQ>).
386 elmex 1.15
387     The default timeout for IQ is 60 seconds.
388    
389     =cut
390    
391     sub set_default_iq_timeout {
392     my ($self, $sec) = @_;
393     $self->{default_iq_timeout} = $sec;
394     }
395    
396 elmex 1.20 =item B<send_iq ($type, $create_cb, $result_cb, %attrs)>
397 elmex 1.1
398     This method sends an IQ XMPP request.
399    
400     Please take a look at the documentation for C<send_iq> in Net::XMPP2::Writer
401 elmex 1.15 about the meaning of C<$type>, C<$create_cb> and C<%attrs> (with the exception
402     of the 'timeout' key of C<%attrs>, see below).
403 elmex 1.1
404 elmex 1.15 C<$result_cb> will be called when a result was received or the timeout reached.
405     The first argument to C<$result_cb> will be a Net::XMPP2::Node instance
406     containing the IQ result stanza contents.
407 elmex 1.1
408     If the IQ resulted in a stanza error the second argument to C<$result_cb> will
409     be C<undef> (if the error type was not 'continue') and the third argument will
410 elmex 1.10 be a L<Net::XMPP2::Error::IQ> object.
411 elmex 1.1
412 elmex 1.15 The timeout can be set by C<set_default_iq_timeout> or passed seperatly
413     in the C<%attrs> array as the value for the key C<timeout> (timeout in seconds btw.).
414    
415 elmex 1.4 This method returns the newly generated id for this iq request.
416    
417 elmex 1.1 =cut
418    
419     sub send_iq {
420     my ($self, $type, $create_cb, $result_cb, %attrs) = @_;
421     my $id = $self->{iq_id}++;
422     $self->{iqs}->{$id} = $result_cb;
423 elmex 1.15
424     my $timeout = delete $attrs{timeout} || $self->{default_iq_timeout};
425     if ($timeout) {
426     $self->{iq_timers}->{$id} =
427 elmex 1.25 AnyEvent->timer (after => $timeout, cb => sub {
428 elmex 1.16 delete $self->{iq_timers}->{$id};
429 elmex 1.15 my $cb = delete $self->{iqs}->{$id};
430 elmex 1.16 $cb->(undef, Net::XMPP2::Error::IQ->new)
431 elmex 1.15 });
432     }
433    
434 elmex 1.1 $self->{writer}->send_iq ($id, $type, $create_cb, %attrs);
435 elmex 1.4 $id
436     }
437    
438 elmex 1.20 =item B<reply_iq_result ($req_iq_node, $create_cb, %attrs)>
439 elmex 1.4
440     This method will generate a result reply to the iq request C<Net::XMPP2::Node>
441     in C<$req_iq_node>.
442    
443     Please take a look at the documentation for C<send_iq> in Net::XMPP2::Writer
444     about the meaning C<$create_cb> and C<%attrs>.
445    
446 elmex 1.6 Use C<$create_cb> to create the XML for the result.
447    
448 elmex 1.4 The type for this iq reply is 'result'.
449    
450     =cut
451    
452     sub reply_iq_result {
453     my ($self, $iqnode, $create_cb, %attrs) = @_;
454     $self->{writer}->send_iq ($iqnode->attr ('id'), 'result', $create_cb, %attrs);
455     }
456    
457 elmex 1.20 =item B<reply_iq_error ($req_iq_node, $error_type, $error, %attrs)>
458 elmex 1.4
459     This method will generate an error reply to the iq request C<Net::XMPP2::Node>
460     in C<$req_iq_node>.
461    
462     C<$error_type> is one of 'cancel', 'continue', 'modify', 'auth' and 'wait'.
463     C<$error> is one of the defined error conditions described in
464 elmex 1.22 C<write_error_tag> method of L<Net::XMPP2::Writer>.
465 elmex 1.4
466     Please take a look at the documentation for C<send_iq> in Net::XMPP2::Writer
467 elmex 1.6 about the meaning of C<%attrs>.
468 elmex 1.4
469     The type for this iq reply is 'error'.
470    
471     =cut
472    
473     sub reply_iq_error {
474     my ($self, $iqnode, $errtype, $error, %attrs) = @_;
475    
476     $self->{writer}->send_iq (
477     $iqnode->attr ('id'), 'error',
478     sub { $self->{writer}->write_error_tag ($iqnode, $errtype, $error) },
479     %attrs
480     );
481 elmex 1.1 }
482    
483     sub handle_iq {
484     my ($self, $node) = @_;
485    
486 elmex 1.4 my $type = $node->attr ('type');
487    
488 elmex 1.15 my $id = $node->attr ('id');
489     delete $self->{iq_timers}->{$id} if defined $id;
490    
491 elmex 1.4 if ($type eq 'result') {
492 elmex 1.15 if (my $cb = delete $self->{iqs}->{$id}) {
493 elmex 1.27 eval {
494     $cb->($node);
495     };
496     if ($@) { $self->event (iq_result_cb_exception => $@) }
497 elmex 1.1 }
498 elmex 1.9
499 elmex 1.4 } elsif ($type eq 'error') {
500 elmex 1.15 if (my $cb = delete $self->{iqs}->{$id}) {
501 elmex 1.1
502 elmex 1.10 my $error = Net::XMPP2::Error::IQ->new (node => $node);
503     $cb->(($error->type eq 'continue' ? $node : undef), $error);
504 elmex 1.1 }
505 elmex 1.4
506     } else {
507     my $handled = 0;
508 elmex 1.6 $self->event ("iq_${type}_request_xml" => $node, \$handled);
509 elmex 1.4
510     my @from;
511     push @from, (to => $node->attr ('from')) if $node->attr ('from');
512    
513     unless ($handled) {
514 elmex 1.21 $self->reply_iq_error ($node, undef, 'service-unavailable', @from);
515 elmex 1.4 }
516 elmex 1.1 }
517     }
518    
519 elmex 1.12 sub send_sasl_auth {
520     my ($self, @mechs) = @_;
521     $self->{writer}->send_sasl_auth (
522     (join ' ', map { $_->text } @mechs),
523     $self->{username}, $self->{domain}, $self->{password}
524     );
525     }
526    
527 elmex 1.1 sub handle_stream_features {
528     my ($self, $node) = @_;
529     my @bind = $node->find_all ([qw/bind bind/]);
530 elmex 1.2 my @tls = $node->find_all ([qw/tls starttls/]);
531 elmex 1.13
532     # and yet another weird thingie: in XEP-0077 it's said that
533     # the register feature MAY be advertised by the server. That means:
534     # it MAY not be advertised even if it is available... so we don't
535     # care about it...
536     # my @reg = $node->find_all ([qw/register register/]);
537 elmex 1.1
538 elmex 1.5 if (not ($self->{disable_ssl}) && not ($self->{ssl_enabled}) && @tls) {
539 elmex 1.2 $self->{writer}->send_starttls;
540    
541 elmex 1.12 } elsif (not $self->{authenticated}) {
542 elmex 1.28 my $continue = 1;
543     $self->event (stream_pre_authentication => \$continue);
544     if ($continue) {
545     $self->authenticate;
546 elmex 1.12 }
547 elmex 1.1
548     } elsif (@bind) {
549     $self->do_rebind ($self->{resource});
550     }
551     }
552    
553 elmex 1.28 =item B<authenticate>
554    
555     This method should be called after the C<stream_pre_authentication> event
556     was emitted to continue authentication of the stream.
557    
558     Usually this method only has to be called when you want to register before
559     you authenticate. See also the documentation of the C<stream_pre_authentication>
560     event below.
561    
562     =cut
563    
564     sub authenticate {
565     my ($self) = @_;
566     my $node = $self->{features};
567     my @mechs = $node->find_all ([qw/sasl mechanisms/], [qw/sasl mechanism/]);
568     my @iqa = $node->find_all ([qw/iqauth auth/]);
569    
570     if (@mechs) {
571     $self->send_sasl_auth (@mechs)
572     } elsif (@iqa) {
573     $self->do_iq_auth;
574     }
575     }
576    
577 elmex 1.1 sub handle_sasl_challenge {
578     my ($self, $node) = @_;
579     $self->{writer}->send_sasl_response ($node->text);
580     }
581    
582     sub handle_sasl_success {
583     my ($self, $node) = @_;
584     $self->{authenticated} = 1;
585     $self->{parser}->init;
586     $self->{writer}->init;
587     $self->{writer}->send_init_stream ($self->{language}, $self->{domain});
588     }
589    
590     sub handle_error {
591     my ($self, $node) = @_;
592 elmex 1.10 my $error = Net::XMPP2::Error::Stream->new (node => $node);
593    
594 elmex 1.18 $self->event (stream_error => $error);
595 elmex 1.1 $self->{writer}->send_end_of_stream;
596     }
597    
598 elmex 1.18 sub do_iq_auth {
599     my ($self) = @_;
600     # TODO
601     }
602    
603 elmex 1.20 =item B<send_presence ($type, $create_cb, %attrs)>
604 elmex 1.4
605     This method sends a presence stanza, for the meanings
606     of C<$type>, C<$create_cb> and C<%attrs> please take a look
607 elmex 1.22 at the documentation for C<send_presence> method of L<Net::XMPP2::Writer>.
608 elmex 1.4
609     This methods does attach an id attribute to the message stanza and
610     will return the id that was used (so you can react on possible replies).
611    
612     =cut
613    
614     sub send_presence {
615     my ($self, $type, $create_cb, %attrs) = @_;
616     my $id = $self->{iq_id}++;
617     $self->{writer}->send_presence ($id, $type, $create_cb, %attrs);
618     $id
619     }
620    
621 elmex 1.20 =item B<send_message ($to, $type, $create_cb, %attrs)>
622 elmex 1.4
623     This method sends a presence stanza, for the meanings
624     of C<$to>, C<$type>, C<$create_cb> and C<%attrs> please take a look
625 elmex 1.22 at the documentation for C<send_message> method of L<Net::XMPP2::Writer>.
626 elmex 1.4
627     This methods does attach an id attribute to the message stanza and
628     will return the id that was used (so you can react on possible replies).
629    
630     =cut
631    
632     sub send_message {
633     my ($self, $to, $type, $create_cb, %attrs) = @_;
634     my $id = $self->{iq_id}++;
635     $self->{writer}->send_message ($id, $to, $type, $create_cb, %attrs);
636     $id
637     }
638    
639 elmex 1.20 =item B<do_rebind ($resource)>
640 elmex 1.1
641     In case you got a C<bind_error> event and want to retry
642     binding you can call this function to set a new C<$resource>
643     and retry binding.
644    
645     If it fails again you can call this again. Becareful not to
646     end up in a loop!
647    
648     If binding was successful the C<stream_ready> event will be generated.
649    
650     =cut
651    
652     sub do_rebind {
653     my ($self, $resource) = @_;
654     $self->{resource} = $resource;
655     $self->send_iq (
656     set =>
657     sub {
658     my ($w) = @_;
659     if ($self->{resource}) {
660     $w->startTag ([xmpp_ns ('bind'), 'bind']);
661     $w->startTag ([xmpp_ns ('bind'), 'resource']);
662     $w->characters ($self->{resource});
663     $w->endTag;
664     $w->endTag;
665     } else {
666     $w->emptyTag ([xmpp_ns ('bind'), 'bind'])
667     }
668     },
669     sub {
670 elmex 1.13 my ($ret_iq, $error) = @_;
671 elmex 1.1
672 elmex 1.13 if ($error) {
673 elmex 1.28 # TODO: make bind error into a seperate error class?
674 elmex 1.30 if ($error->xml_node ()) {
675     my ($res) = $error->xml_node ()->find_all ([qw/bind bind/], [qw/bind resource/]);
676     $self->event (bind_error => $error, ($res ? $res : $self->{resource}));
677     } else {
678     $self->event (bind_error => $error);
679     }
680 elmex 1.1
681     } else {
682     my @jid = $ret_iq->find_all ([qw/bind bind/], [qw/bind jid/]);
683     my $jid = $jid[0]->text;
684     unless ($jid) { die "Got empty JID tag from server!\n" }
685     $self->{jid} = $jid;
686    
687     $self->event (stream_ready => $jid);
688     }
689     }
690     );
691     }
692    
693 elmex 1.20 =item B<jid>
694 elmex 1.1
695     After the stream has been bound to a resource the JID can be retrieved via this
696     method.
697    
698     =cut
699    
700     sub jid { $_[0]->{jid} }
701    
702 elmex 1.20 =item B<features>
703 elmex 1.4
704     Returns the last received <features> tag in form of an L<Net::XMPP2::Node> object.
705    
706     =cut
707    
708     sub features { $_[0]->{features} }
709    
710 elmex 1.20 =back
711    
712 elmex 1.1 =head1 EVENTS
713    
714     These events can be registered on with C<reg_cb>:
715    
716     =over 4
717    
718 elmex 1.11 =item stream_features => $node
719 elmex 1.1
720 elmex 1.4 This event is sent when a stream feature (<features>) tag is received. C<$node> is the
721     L<Net::XMPP2::Node> object that represents the <features> tag.
722 elmex 1.1
723 elmex 1.28 =item stream_pre_authentication => $rcontinue
724    
725     This event is emitted after TLS/SSL was initiated (if enabled) and before any
726     authentication happened. C<$rcontinue> is a reference to a scalar that per default
727     holds a true value. If that scalar is true the authentication will continue
728     after handling this event. If you set C<$$rcontinue> to a false value
729     the authentication will stop and you have to call the C<authenticate>
730     method later.
731    
732     This event is usually used when you want to do in-band registration,
733     see also L<Net::XMPP2::Ext::Registration>.
734    
735 elmex 1.1 =item stream_ready => $jid
736    
737     This event is sent if the XML stream has been established (and
738     resources have been bound) and is ready for transmitting regular stanzas.
739    
740     C<$jid> is the bound jabber id.
741    
742 elmex 1.28 =item error => $error
743    
744     This event is generated whenever some error occured.
745     C<$error> is an instance of L<Net::XMPP2::Error>.
746     Trivial error reporting may look like this:
747    
748     $con->reg_cb (error => sub { warn "xmpp error: " . $_[1]->string . "\n"; 1 });
749    
750     Basically this event is a collect event for all other error events.
751    
752 elmex 1.10 =item stream_error => $error
753 elmex 1.6
754     This event is sent if a XML stream error occured. C<$error>
755 elmex 1.10 is a L<Net::XMPP2::Error::Stream> object.
756 elmex 1.6
757 elmex 1.28 =item xml_parser_error => $error
758    
759     This event is generated whenever the parser trips over XML that it can't
760     read. C<$error> is a L<Net::XMPP2::Error::Parser> object.
761    
762 elmex 1.11 =item tls_error
763    
764     This event is emitted when a TLS error occured on TLS negotiation.
765     After this the connection will be disconnected.
766    
767     =item sasl_error => $error
768    
769     This event is emitted on SASL authentication error.
770    
771 elmex 1.10 =item bind_error => $error, $resource
772 elmex 1.6
773 elmex 1.10 This event is generated when the stream was unable to bind to
774     any or the in C<new> specified resource. C<$error> is a L<Net::XMPP2::Error::IQ>
775     object. C<$resource> is the errornous resource string or undef if none
776     was received.
777 elmex 1.6
778 elmex 1.10 The C<condition> of the C<$error> might be one of: 'bad-request',
779     'not-allowed' or 'conflict'.
780 elmex 1.1
781 elmex 1.20 Node: this is untested, I couldn't get the server to send a bind error
782 elmex 1.1 to test this.
783    
784     =item connect => $host, $port
785    
786     This event is generated when a successful connect was performed to
787     the domain passed to C<new>.
788    
789     Note: C<$host> and C<$port> might be different from the domain you passed to
790     C<new> if C<connect> performed a SRV RR lookup.
791    
792     If this connection is lost a C<disconnect> will be generated with the same
793     C<$host> and C<$port>.
794    
795     =item disconnect => $host, $port, $message
796    
797     This event is generated when the connection was lost or another error
798     occured while writing or reading from it.
799    
800     C<$message> is a humand readable error message for the failure.
801     C<$host> and C<$port> were the host and port we were connected to.
802    
803     Note: C<$host> and C<$port> might be different from the domain you passed to
804     C<new> if C<connect> performed a SRV RR lookup.
805    
806 elmex 1.31 =item recv_stanza_xml => $node
807    
808     This event is generated before any processing of a "XML" stanza happens.
809     C<$node> is the node of the stanza that is being processed, it's of
810     type L<Net::XMPP2::Node>.
811    
812     This method might not be as handy for debuggin purposes as C<debug_recv>.
813    
814     =item send_stanza_data => $data
815    
816     This event is generated shortly before data is sent to the socket.
817     C<$data> contains a complete "XML" stanza or the end of stream closing
818     tag. This method is useful for debugging purposes and I recommend
819     using XML::Twig or something like that to display it nicely.
820    
821     See also the event C<debug_send>.
822    
823     =item debug_send => $data
824    
825     This method is invoked whenever data is written out. This event
826     is mostly the same as C<send_stanza_data>.
827    
828     =item debug_recv => $data
829    
830     This method is incoked whenever a chunk of data was received.
831    
832     It works to filter C<$data> through L<XML::Twig> for debugging
833     display purposes sometimes, but as C<$data> is some arbitrary chunk
834     of bytes you might get a XML parse error (did I already mention that XMPP's
835     application of "XML" sucks?).
836    
837     So you might want to use C<recv_stanza_xml> to detect
838     complete stanzas. Unfortunately C<recv_stanza_xml> doesn't have the
839     bytes anymore and just a datastructure (L<Net::XMPP2::Node>).
840    
841 elmex 1.6 =item presence_xml => $node
842 elmex 1.4
843     This event is sent when a presence stanza is received. C<$node> is the
844     L<Net::XMPP2::Node> object that represents the <presence> tag.
845    
846 elmex 1.6 =item message_xml => $node
847 elmex 1.4
848     This event is sent when a message stanza is received. C<$node> is the
849     L<Net::XMPP2::Node> object that represents the <message> tag.
850    
851 elmex 1.32 =item iq_xml => $node
852    
853     This event is emitted when a iq stanza arrives. C<$node> is the
854     L<Net::XMPP2::Node> object that represents the <iq> tag.
855    
856 elmex 1.6 =item iq_set_request_xml => $node, $handled_ref
857 elmex 1.4
858 elmex 1.6 =item iq_get_request_xml => $node, $handled_ref
859 elmex 1.4
860     These events are sent when an iq request stanza of type 'get' or 'set' is received.
861     C<$type> will either be 'get' or 'set' and C<$node> will be the L<Net::XMPP2::Node>
862     object of the iq tag.
863    
864     If C<$$handled_ref> is true an event handler should not handle this message anymore.
865    
866     If one of the event handlers handled this message the scalar pointed at by
867     the reference in C<$handled_ref> should be set to 1 true value. If C<$$handled_ref>
868     is still false after all event handlers were executed an error iq will be generated.
869    
870 elmex 1.27 =item iq_result_cb_exception => $exception
871    
872     If the C<$result_cb> of a C<send_iq> operation somehow threw a exception
873     or failed this event will be generated.
874    
875 elmex 1.33 =item send_iq_hook => $id, $type, $attrs, $rcreate_cbs
876    
877     This event lets you add any desired number of additional create callbacks
878     to a IQ stanza that is about to be sent.
879    
880     C<$id>, C<$type> are described in the documentation of C<send_iq> of L<Net::XMPP2::Writer>.
881     C<$attrs> is the hashref to the C<%attrs> hash that can be passed to C<send_iq> and also has
882     the exact same semantics as described in the documentation of C<send_iq>.
883    
884     C<$rcreate_cbs> is a array reference where you should push further callbacks on
885     that have the same semantic as C<$create_cb> in the documentation of C<send_iq>.
886    
887     =item send_message_hook => $id, $to, $type, $attrs, $rcreate_cbs
888    
889     This event lets you add any desired number of additional create callbacks
890     to a message stanza that is about to be sent.
891    
892     C<$id>, C<$to>, C<$type> and the hashref C<$attrs> are described in the documentation
893     for C<send_message> of L<Net::XMPP2::Writer> (C<$attrs> is C<%attrs> there).
894    
895     For semantics of C<$rcreate_cbs> see also the documentation of the C<send_iq_hook> event above.
896    
897     =item send_presence_hook => $id, $type, $attrs, $rcreate_cbs
898    
899     This event lets you add any desired number of additional create callbacks
900     to a presence stanza that is about to be sent.
901    
902     C<$id>, C<$type> and the hashref C<$attrs> are described in the documentation
903     for C<send_presence> of L<Net::XMPP2::Writer> (C<$attrs> is C<%attrs> there).
904    
905     For semantics of C<$rcreate_cbs> see also the documentation of the C<send_iq_hook> event above.
906    
907 elmex 1.1 =back
908    
909     =head1 AUTHOR
910    
911 elmex 1.20 Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
912 elmex 1.1
913     =head1 COPYRIGHT & LICENSE
914    
915     Copyright 2007 Robin Redeker, all rights reserved.
916    
917     This program is free software; you can redistribute it and/or modify it
918     under the same terms as Perl itself.
919    
920     =cut
921    
922     1; # End of Net::XMPP2