XMPP/Component.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« XMPP/Ext.pm ↑ Summary XMPP/Error.pm »
1 package AnyEvent::XMPP::Component;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 0 ▶use AnyEvent::XMPP::Connection;
Subroutine BEGIN: called CC=1 SCAR=2.6
4 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
5 our @ISA = qw/AnyEvent::XMPP::Connection/;
6  
7 =head1 NAME
8  
9 AnyEvent::XMPP::Component - "XML" stream that implements the XEP-0114
10  
11 =head1 SYNOPSIS
12  
13 use AnyEvent::XMPP::Component;
14  
15 my $con = AnyEvent::XMPP::Component->new (
16 domain => 'chat.jabber.org'
17 host => 'jabber.org',
18 port => 5347,
19 secret => 'insecurepasswordforthehackers'
20 );
21 $con->reg_cb (session_ready => sub { ... });
22 $con->connect;
23  
24 =head1 DESCRIPTION
25  
26 This module represents a XMPP connection to a server that authenticates as
27 component.
28  
29 This module is a subclass of C<AnyEvent::XMPP::Connection> and inherits all methods.
30 For example C<reg_cb> and the stanza sending routines.
31  
32 For additional events that can be registered to look below in the EVENTS section.
33  
34 Please note that for component several functionality in L<AnyEvent::XMPP::Connection>
35 might have no effect or not the desired effect. Basically you should
36 use the L<AnyEvent::XMPP::Component> as component and only handle events
37 the handle with incoming data. And only use functions that send stanzas.
38  
39 No effect has the event C<stream_pre_authentication> and the C<authenticate>
40 method of L<AnyEvent::XMPP::Connection>, because those handle the usual SASL or iq-auth
41 authentication. "Jabber" components have a completly different authentication
42 mechanism.
43  
44 Also note that the support for some XEPs in L<AnyEvent::XMPP::Ext> is just thought
45 for client side usage, if you miss any functionaly don't hesitate to ask the
46 author or send him a patch! (See L<AnyEvent::XMPP> for contact information).
47  
48 =head1 METHODS
49  
50 =over 4
51  
52 =item B<new (%args)>
53  
54 This is the constructor. It takes the same arguments as
55 the constructor of L<AnyEvent::XMPP::Connection> along with a
56 few others:
57  
58 B<NOTE>: Please note that some arguments that L<AnyEvent::XMPP::Connection>
59 usually takes have no effect when using this class. (That would be
60 the 'username', 'password', 'resource' and 'jid' arguments for example.)
61  
62 =over 4
63  
64 =item secret => $secret
65  
66 C<$secret> is the secret that will be used for authentication with the server.
67  
68 =back
69  
70 =cut
71  
72 sub new {
73 my $this = shift;
74 my $class = ref($this) || $this;
75  
76 my %args = @_;
77  
78 unless (exists $args{initial_presence}) {
79 $args{stream_namespace} = 'component';
80 }
81
82 $args{host} ||= delete $args{server};
83 $args{host}
84 or die "Required 'host' argument missing to new for this component!";
85  
86 unless (defined $args{port}) {
87 $args{port} = 5347;
88 }
89  
90 my $self = $class->SUPER::new (%args);
91  
92 $self->{parser}->set_stream_cb (sub {
93 my $secret = $self->{parser}->{parser}->xml_escape ($self->{secret});
94 my $id = $self->{stream_id} = $_[0]->attr ('id');
95 $self->{writer}->send_handshake ($id, $secret);
96 });
97  
98 $self->reg_cb (recv_stanza_xml => sub {
99 my ($self, $node) = @_;
100 if ($node->eq (component => 'handshake')) {
101 $self->{authenticated} = 1;
102 $self->event ('session_ready');
103 }
104 },
105 stream_pre_authentication => sub {
106 my ($self, $rcon) = @_;
107 $$rcon = 0;
108 });
109  
110 $self
111 }
112  
113 sub default_namespace {
114 return 'component';
115 }
116  
117 sub authenticate {
118 warn "authenticate called! Please read the documentation of "
119 ."AnyEvent::XMPP::Component why this is an error!"
120 }
121  
122 =back
123  
124 =head1 EVENTS
125  
126 These additional events can be registered on with C<reg_cb>:
127  
128 NOTE: The event C<stream_pre_authentication> should _not_ be handled
129 and just ignored. Don't attach callbacks to it!
130  
131 =over 4
132  
133 =item session_ready
134  
135 This event indicates that the component has connected successfully
136 and can now be used to transmit stanzas.
137  
138 =back
139  
140 =head1 AUTHOR
141  
142 Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
143  
144 =head1 COPYRIGHT & LICENSE
145  
146 Copyright 2007, 2008 Robin Redeker, all rights reserved.
147  
148 This program is free software; you can redistribute it and/or modify it
149 under the same terms as Perl itself.
150  
151 =cut
152  
153 1; # End of AnyEvent::XMPP::Component
« XMPP/Ext.pm ↑ Summary XMPP/Error.pm »