DJabberd/Agent.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 171 CC 13 SCAR 23.5
CC
13
Cov
0%
handle_iq
30.0
handle_stanza
20.0
handle_iq_disco_info
12.0
CRAP 10.5
1  
2 =head1 NAME
3  
4 DJabberd::Agent - Abstract base class representing a software agent that talks XMPP
5  
6 =head1 SYNOPSIS
7  
8 New agents are created by making a sub-class of this class. C<set_config_...> methods can
9 be provided to add configuration options, which will then be supported both on this class's
10 constructor and in a configuration file when adding your agent to a VHost as a delivery plugin.
11  
12 package MyAgent;
13
14 use base qw(DJabberd::Agent);
15
16 sub handles_destination {
17 my ($self, $to_jid, $vhost) = @_;
18
19 return ($to_jid->domain eq 'example.com' && $to_jid->node eq 'blah') ? 1 : 0;
20 }
21
22 sub handle_message {
23 my ($self, $vhost, $stanza) = @_;
24 my $response = $stanza->make_response();
25 my $message = $self->{message} || "Hello, world!";
26 $response->set_raw("<body>".DJabberd::Util::exml($message)."</body>");
27 $response->deliver($vhost);
28 }
29
30 sub set_config_message {
31 my ($self, $message) = @_;
32 $self->{message} = $message;
33 }
34  
35 The following example shows how you might load the above agent class as a plugin:
36  
37 <Plugin MyAgent>
38 Message Kumquats are groovy!
39 </Plugin>
40  
41 It is also possible to use an instance directly as an object, to delegate stanza handling
42 from another delivery plugin:
43  
44 my $agent = MyAgent->new(
45 message => "Kumquats are groovy!",
46 );
47 $agent->handle_stanza($vhost, $stanza);
48  
49 In this case, the caller will need to handle the delivery hook callbacks itself
50 in an appropriate manner, but it can be assumed that a call to C<handle_stanza>
51 will always "deliver" the stanza in some sense.
52  
53 =head1 DESCRIPTION
54  
55 This module is the basis for several other higher-level DJabberd classes which provide
56 an API for creating software agents that communicate over XMPP. This includes components
57 which serve an entire domain (see L<DJabberd::Component>) and bot-like nodes which
58 respond only to a specific node name in a domain (see L<DJabberd::Agent::Node>).
59  
60 It is unlikely that other classes will inherit from this class directly. Most users
61 should inherit from one of the higher-level subclasses depending on what sort of
62 agent they wish to create. This class provides the basic machinery for handling
63 stanzas which all agents can use.
64  
65 =head1 API
66  
67 The API of this class is normally used by overriding certain methods in a subclass.
68 In many cases it is useful for these overriding methods to call into the base class
69 versions to get certain useful default functionality.
70  
71 The C<handles_destination> method is the most important methods for inheritors to
72 override, since incoming stanzas will only be delivered to your class if this
73 method returns true the given destination JID.
74  
75 It is assumed that if a true value is returned from this method you will handle the
76 incoming stanza in one way or another; this implies that in most cases you should
77 reply with some sort of error message if the stanza is unnacceptable in some way.
78  
79 The more specialized abstract subclasses of this class will generally implement
80 C<handles_destination> in a way that is appropriate for their role, so users
81 of these subclasses will not need to override this method.
82  
83 =cut
84  
85 package DJabberd::Agent;
86 use base qw(DJabberd::Delivery);
87 use strict;
88 use warnings;
89 use DJabberd::Util qw(exml);
90 use DJabberd::JID;
91 use DJabberd::Log;
92  
93 our $logger = DJabberd::Log->get_logger();
94  
95 =head2 handle_stanza($self, $vhost, $stanza)
96  
97 This method is the "entry point" of this class, and is called when a stanza is received
98 which this agent expressed a desire to handle. The default implementation dispatches
99 to one of the higher-level C<handle_...> methods depending on the type of stanza that
100 has been recieved. At present, only IQ, Message and Presence stanzas are supported.
101  
102 Inheritors can override this method to do low-level stanza processing, but unless
103 the version from this class is invoked the higher-level processing methods will
104 cease to operate.
105  
106 Callers that are using an agent instance as an object directly rather than having
107 it attached to a VHost as a plugin will generally call this method directly to
108 delegate stanza processing. In this case, it is the caller's responsibility to ensure
109 that the stanza is suitable for this agent; for example, the caller could make a call
110 to C<handles_destination> to determine if the stanza's destination JID is suitable.
111  
112 =cut
113 sub handle_stanza {
114 my ($self, $vhost, $stanza) = @_;
115  
116 return $self->handle_iq($vhost, $stanza) if $stanza->isa('DJabberd::IQ');
117 return $self->handle_message($vhost, $stanza) if $stanza->isa('DJabberd::Message');
118 return $self->handle_presence($vhost, $stanza) if $stanza->isa('DJabberd::Presence');
119  
120 }
121  
122 # Internal function allowing agents to be used directly as delivery plugins
123 sub deliver {
124  
125 my ($self, $vhost, $cb, $stanza) = @_;
126  
127 if ($self->handles_destination($stanza->to_jid, $vhost)) {
128 $self->handle_stanza($vhost, $stanza);
129 $cb->delivered;
130 }
131 else {
132 $cb->decline;
133 }
134  
135 }
136  
137 =head2 handles_destination($self, $to_jid, $vhost)
138  
139 When an incoming stanza is potentially to be delivered to your instance, this method
140 will be called to find out if your agent is responsible for the destination JID.
141  
142 This method is primarily used in the case where the agent is added directly to a VHost as a plugin.
143 If a true value is returned, the server will assume that your agent will handle the
144 stanza and will call C<handle_stanza>. If a false value is returned, the server will
145 continue to look for another appropriate delivery plugin to accept the stanza.
146  
147 =cut
148 sub handles_destination {
149 my ($self, $to_jid, $vhost) = @_;
150
151 return 0;
152 }
153  
154 # This method is no longer used
155 sub name {
156 my ($self) = @_;
157
158 return "";
159 }
160  
161 =head2 handle_message($self, $vhost, $stanza)
162  
163 When the default implementation of C<handle_stanza> is used, this method will be
164 called to handle incoming "message" stanzas. The C<$stanza> object passed in
165 will be an instance of L<DJabberd::Message|DJabberd::Message>.
166  
167 The default implementation of this method does nothing.
168  
169 =cut
170 sub handle_message {
171 my ($self, $vhost, $stanza) = @_;
172
173 # By default, ignore the message altogether
174 }
175  
176 =head2 handle_presence($self, $vhost, $stanza)
177  
178 When the default implementation of C<handle_stanza> is used, this method will be
179 called to handle incoming "presence" stanzas. The C<$stanza> object passed in
180 will be an instance of L<DJabberd::Presence|DJabberd::Presence>.
181  
182 The default implementation of this method does nothing.
183  
184 =cut
185 sub handle_presence {
186 my ($self, $vhost, $stanza) = @_;
187
188 # By default, ignore the presence altogether
189 }
190  
191 =head2 handle_iq($self, $vhost, $stanza)
192  
193 When the default implementation of C<handle_stanza> is used, this method will be
194 called to handle incoming "iq" stanzas. The C<$stanza> object passed in
195 will be an instance of L<DJabberd::IQ|DJabberd::IQ>.
196  
197 The default implementation of this method provides built-in support for basic
198 service discovery and vcard queries, and also causes any registered IQ handlers
199 to be called when appropriate.
200  
201 In most cases, it's better not to override this method completely but instead
202 to register an IQ handler for the particular IQ signature you are interested in
203 using C<register_iq_handler>.
204  
205 =cut
206 sub handle_iq {
207 my ($self, $vhost, $stanza) = @_;
208
209 my $sig = $stanza->signature;
210
211 if ($self->{djabberd_agent_iqcb} && $self->{djabberd_agent_iqcb}{$sig}) {
212 return $self->{djabberd_agent_iqcb}{$sig}->($vhost, $stanza);
213 }
214  
215 return $self->handle_iq_vcard($vhost,$stanza) if $sig eq 'get-{vcard-temp}vCard';
216 return $self->handle_iq_disco_info($vhost,$stanza) if $sig eq 'get-{http://jabber.org/protocol/disco#info}query';
217 return $self->handle_iq_disco_items($vhost,$stanza) if $sig eq 'get-{http://jabber.org/protocol/disco#items}query';
218
219 # If we've got this far, then we don't support this IQ type
220 $stanza->make_error_response('501', 'cancel', 'feature-not-implemented')->deliver($vhost);
221 }
222  
223 =head2 $self->register_iq_handler($signature, $handler)
224  
225 This method, unlike most methods of this class, is not intended to be overridden by subclasses.
226 Instead, subclasses should call this method to register a handler for a particular type of IQ
227 stanza. The default implementation of C<handle_iq> will then call the registered handlers
228 when an appropriate stanza is received.
229  
230 C<$signature> is a string containing the IQ signature using DJabberd's standard IQ signature
231 format. This consists of the IQ type (C<get>, C<set>, etc) followed by a dash, then the
232 IQ element namespace in braces followed by the element name. For example, the signature for
233 a vCard get request is C<"{vcard-temp}vCard">.
234  
235 C<$handler> is expected to be a CODE reference which accepts as arguments C<($vhost, $stanza>)>,
236 where C<$stanza> will be an instance of L<DJabberd::IQ|DJabberd::IQ>.
237  
238 If a handler is supplied for a signature for which this class has built-in support, the supplied
239 handler will replace the built-in handler.
240  
241 =cut
242 sub register_iq_handler {
243 my ($self, $signature, $handler) = @_;
244
245 $self->{djabberd_agent_iqcb} ||= {};
246 $self->{djabberd_agent_iqcb}{$signature} = $handler;
247 }
248  
249 # Internal method for handling vcard requests
250 sub handle_iq_vcard {
251 my ($self, $vhost, $stanza) = @_;
252
253 my $vcard = $self->vcard($stanza->to_jid);
254  
255 my $response = $stanza->make_response();
256 $response->set_raw("<vCard xmlns='vcard-temp'>".$vcard."</vCard>");
257 $response->deliver($vhost);
258 }
259  
260 # Internal method for handling service discovery "info" requests
261 sub handle_iq_disco_info {
262 my ($self, $vhost, $stanza) = @_;
263
264 my $query = $stanza->first_element();
265 my $disco_node = $query && $query->attr('{}node');
266  
267 my $features = $self->features($stanza->from_jid, $disco_node);
268 my $identities = $self->identities($stanza->from_jid, $disco_node);
269
270 my $response = $stanza->make_response();
271  
272 my $xml = "<query xmlns='http://jabber.org/protocol/disco#info'"
273 . ($disco_node? " node='".exml($disco_node)."'" : '');
274 if (@$features || @$identities) {
275 $xml .= '>'
276 . join('',map({ "<identity category='".exml($_->[0])."' type='".exml($_->[1])."' name='".exml($_->[2])."'/>" } @$identities))
277 . join('',map({ "<feature var='".exml($_)."' />" } @$features))
278 . "</query>";
279 }
280 else {
281 $xml .= ' />';
282 }
283
284 $response->set_raw($xml);
285 $response->deliver($vhost);
286 }
287  
288 # Internal method for handling service discovery "items" requests
289 sub handle_iq_disco_items {
290 my ($self, $vhost, $stanza) = @_;
291  
292 my $query = $stanza->first_element();
293 my $disco_node = $query && $query->attr('{}node');
294  
295 my $items = $self->child_services($stanza->from_jid, $disco_node);
296
297 my $response = $stanza->make_response();
298  
299 my $xml = "<query xmlns='http://jabber.org/protocol/disco#items'"
300 . ($disco_node? " node='".exml($disco_node)."'" : '');
301
302 if (@$items) {
303 $xml .= '>'
304 . join('',map({ "<item jid='".exml($_->[0])."' name='".exml($_->[1])."'/>" } @$items))
305 . "</query>";
306 }
307 else {
308 $xml .= ' />';
309 }
310
311 $response->set_raw($xml);
312 $response->deliver($vhost);
313 }
314  
315 =head2 vcard($self, $requester_jid)
316  
317 Called by this class's built-in vCard implementation to get the contents of the vCard of
318 this agent. Subclasses should override this and return a string containing the XML
319 representing the body of the vcard to be returned, without the C<vCard> container element.
320  
321 The default implementation simply returns an empty vCard. This is preferable to not returning
322 a response at all, because some clients do sub-optimal things which a vCard requests fails.
323  
324 =cut
325 sub vcard {
326 my ($self, $requester_jid) = @_;
327  
328 # Empty vCard by default
329 return "";
330 }
331  
332 =head2 features($self, $requester_jid)
333  
334 Called by this class's built-in Service Discovery implementation to get a list of
335 "features" supported by this agent. Subclasses should override this and return an
336 ARRAY ref containing a list of strings containing feature URIs.
337  
338 The default implementation returns the correct feature URIs to indicate support
339 for vCards and Service Discovery. It is suggested that overriding methods initially
340 call into the parent class to obtain the default list and then append their own
341 features to the list as appropriate.
342  
343 =cut
344 sub features {
345 my ($self, $requester_jid) = @_;
346
347 # We support disco and vCards by default
348 return [
349 'vcard-temp',
350 'http://jabber.org/protocol/disco#info',
351 'http://jabber.org/protocol/disco#items',
352 ];
353 }
354  
355 =head2 identities($self, $requester_jid)
356  
357 Called by this class's built-in Service Discovery implementation to get a list of
358 "identities" exposed by this agent. Subclasses should override this and return an
359 ARRAY ref containing a list of ARRAY refs of the form C<[$category,$type,$name]>,
360 where these values correspond to the attributes of the same name in the Service
361 Discovery specification (XEP-0030).
362  
363 For example, it may be appropriate for an agent providing a multi-user conference
364 room to return the identity C<["conference","text","General Discussion"]>.
365  
366 The default implementation currently returns an empty list. It is suggested that
367 overriding methods initially call into the parent class to obtain the default
368 list and then append their own features to the list as appropriate. Although
369 the list returned by this class is currently empty, the higher-level abstract
370 subclasses may add their own identities to this list.
371  
372 =cut
373 sub identities {
374 my ($self, $requester_jid) = @_;
375
376 # No identities by default
377 return [];
378 }
379  
380 =head2 child_services($self, $requester_jid)
381  
382 Called by this class's built-in Service Discovery implementation to get a list of
383 child services referenced by this agent. Subclasses should override this and return an
384 ARRAY ref containing a list of ARRAY refs of the form C<[$jid,$name]>,
385 where these values correspond to the attributes of the same name in the Service
386 Discovery specification (XEP-0030).
387  
388 The default implementation currently returns an empty list. It is suggested that
389 overriding methods initially call into the parent class to obtain the default
390 list and then append their own features to the list as appropriate. Although
391 the list returned by this class is currently empty, the higher-level abstract
392 subclasses may add their own child services to this list.
393  
394 =cut
395 sub child_services {
396 my ($self, $requester_jid) = @_;
397  
398 # None by default
399 return [];
400 }
401  
402 =head1 SEE ALSO
403  
404 See also L<DJabberd::Component>, L<DJabberd::Agent::Node>.
405  
406 =head1 COPYRIGHT
407  
408 Copyright 2007-2008 Martin Atkins
409  
410 This module is part of the DJabberd distribution and is covered by the distribution's
411 overall licence.
412  
413 =cut
414  
415 1;