DJabberd/Component.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 72 CC 3 SCAR 13.9
CC
3
Cov
0%
handle_stanza
12.0
register
2.0
handles_destination
2.0
CRAP 4.0
1  
2 =head1 NAME
3  
4 DJabberd::Component - Abstract class representing a component in DJabberd
5  
6 =head1 SYNOPSIS
7  
8 package MyPackage::DJabberd::MyComponent;
9 use base DJabberd::Component;
10
11 # Example of a component which accepts XEP-0050 Ad-Hoc Commands
12
13 sub finalize {
14 my ($self, $opts) = @_;
15
16 # Let the parent class finalize
17 $self->SUPER::finalize;
18
19 $self->register_iq_handler(
20 "set-{http://jabber.org/protocol/commands}command",
21 sub { $self->handle_adhoc_command(@_) },
22 );
23 }
24
25 sub handle_adhoc_command {
26 my ($self, $vhost, $stanza) = @_;
27
28 # handle the DJabberd::IQ stanza in $stanza,
29 # or send some kind of error response.
30 }
31  
32 This class provides a parent class for all DJabberd components. Components
33 that inherit from this class can then be used directly by the server as delivery plugins,
34 or used directly by other classes.
35  
36 See L<DJabberd::Component::Example> for an example component implementation.
37  
38 =head1 USAGE
39  
40 This class is a specialization of L<DJabberd::Agent|DJabberd::Agent> which is aimed
41 at those wishing to create "components", which are software agents that handle
42 an entire XMPP domain, including all of the nodes within that domain.
43  
44 In most cases, users of this class will use the API exposed by L<DJabberd::Agent|DJabberd::Agent>.
45 However, there is some special behaviour to note and some additional methods
46 that are not part of the basic agent class.
47  
48 =cut
49  
50 package DJabberd::Component;
51  
52 use base 'DJabberd::Agent';
53 use strict;
54 use DJabberd::Log;
55 use DJabberd::Util qw(exml);
56  
57 our $logger = DJabberd::Log->get_logger();
58  
59 sub register {
60 my ($self, $vhost) = @_;
61
62 $self->SUPER::register($vhost);
63 $logger->debug("Component ".$self." will serve domain ".$self->domain);
64 return;
65
66 }
67  
68 =head2 handles_destination($self, $to_jid, $vhost)
69  
70 This class provides an overriden version of this method which responds with
71 a true value if and only if the domain of the JID given matches the domain
72 of the component itself. It is unlikely that you will need to override this.
73  
74 =cut
75 sub handles_destination {
76 my ($self, $to_jid, $vhost) = @_;
77 return ($to_jid && $to_jid->domain eq $self->domain);
78 }
79  
80 sub domain {
81 return $_[0]->vhost->server_name;
82 }
83  
84 =head2 handle_stanza($self, $vhost, $stanza)
85  
86 The overriden version of this method has special behavior when dealing with
87 stanzas that are addressed to nodes within the domain handled by your
88 component. (That is, jids of the form C<user@example.com>, rather than just C<example.com>.)
89  
90 When such a stanza is received, the C<get_node> method will be called to retrieve
91 an object representing that node.
92  
93 If you wish to do low-level handling of all incoming stanzas, you may override this
94 method. As with DJabberd::Agent, overriding this method will disable all of the
95 higher-level handler methods unless you delegate to the overriden method.
96  
97 If you would like to preserve the higher-level handler methods but not handle
98 nodes as separate objects, you may like to override this method to call the original
99 version from DJabberd::Agent as follows:
100  
101 sub handle_stanza {
102 $self->DJabberd::Agent::handle_stanza(@_);
103 }
104  
105 With this method in place, the C<get_node> method will no longer be used and all
106 suitable stanzas will be passed to this instance's own handler methods regardless of
107 the destination JID's node name.
108  
109 =cut
110 sub handle_stanza {
111 my ($self, $vhost, $stanza) = @_;
112  
113 if ($stanza->to_jid->node) {
114 my $node = $self->get_node($stanza->to_jid->node);
115 unless ($node) {
116 my $error = $stanza->make_error_response('404', 'cancel', 'item-not-found');
117 $error->deliver($vhost);
118 return;
119 }
120 return $node->handle_stanza($vhost, $stanza);
121 }
122 else {
123 $self->SUPER::handle_stanza($vhost, $stanza);
124 }
125  
126 }
127  
128 =head2 get_node($self, $nodename)
129  
130 Called when this class wants to find an agent to handle a given node within this
131 component's domain.
132  
133 Most normal implementations of this method will return an instance of a subclass of
134 L<DJabberd::Agent::Node|DJabberd::Agent::Node>, but really any
135 L<DJabberd::Agent|DJabberd::Agent> subclass will do as long as it'll accept stanzas
136 addressed to the nodename given.
137  
138 =cut
139 sub get_node {
140 my ($self, $nodename) = @_;
141  
142 return undef;
143 }
144  
145 sub name {
146 my ($self) = @_;
147
148 return $self->domain;
149 }
150  
151 =head2 vcard($self)
152  
153 The overriden version of this method will return a vCard where the full name is set
154 to the domain name of the component. Subclasses may override this to do something
155 more fancy.
156  
157 =cut
158 sub vcard {
159 my ($self) = @_;
160
161 return "<FN>".exml($self->name)."</FN>";
162 }
163  
164 =head2 identities($self)
165  
166 The overriden version of this method returns a single, generic identity which
167 indicates that the component is a "branch" in the Service Discovery tree.
168 This is included because some clients seem to get a little upset if a JID
169 supports disco but has no identities.
170  
171 =cut
172 sub identities {
173 my ($self) = @_;
174
175 return [
176 [ 'hierarchy', 'branch', $self->name ],
177 ];
178 }
179  
180 # Internal utility method.
181 # I don't think this is used anymore
182 sub send_stanza {
183 my ($self, $stanza) = @_;
184
185 $stanza->deliver($self->vhost);
186 }
187  
188 1;
189  
190 =head1 SEE ALSO
191  
192 See also L<DJabberd::Agent>, L<DJabberd::Agent::Node>.
193  
194 =head1 COPYRIGHT
195  
196 Copyright 2007-2008 Martin Atkins
197  
198 This module is part of the DJabberd distribution and is covered by the distribution's
199 overall licence.
200  
201 =cut