| 1 | |||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | DJabberd::Component::Node - Specialization of DJabberd::Agent::Node that knows it belongs to a component | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | This abstract class is a specialization of L<DJabberd::Agent::Node|DJabberd::Agent::Node>. | ||
| 10 | The only difference between this class and its parent is that nodes represented by this class | ||
| 11 | "know" that they belong to a component and have a reference to that component stored. | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | Create your node class as a subclass of this class: | ||
| 16 | |||
| 17 | package MyPackage::DJabberd::MyNode; | ||
| 18 | use base DJabberd::Component::Node; | ||
| 19 | |||
| 20 | # Example of an agent that responds to incoming chat messages | ||
| 21 | |||
| 22 | sub handle_message { | ||
| 23 | my ($self, $vhost, $stanza) = @_; | ||
| 24 | |||
| 25 | my $response = $stanza->make_response(); | ||
| 26 | $response->set_raw("<body>Hello. I'm a software agent.</body>"); | ||
| 27 | $response->deliver($vhost); | ||
| 28 | } | ||
| 29 | |||
| 30 | The class can then be instantiated from inside a method in a L<DJabberd::Component|DJabberd::Component> instance: | ||
| 31 | |||
| 32 | # $self here is a DJabberd::Component object | ||
| 33 | my $node = MyPackage::DJabberd::MyNode->new( | ||
| 34 | component => $self, | ||
| 35 | nodename => "bob", | ||
| 36 | ); | ||
| 37 | |||
| 38 | The above created instance will accept stanzas addressed to the node "bob" inside the domain owned | ||
| 39 | by the given component. | ||
| 40 | |||
| 41 | It is not possible to directly use subclasses of this class as delivery plugins, since they | ||
| 42 | must be associated with a component at runtime. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | package DJabberd::Component::Node; | ||
| 47 | |||
| 48 | use base qw(DJabberd::Agent::Node); | ||
| 49 | use DJabberd::Util qw(exml); | ||
| 50 | use strict; | ||
| 51 | use warnings; | ||
| 52 | use DJabberd::Log; | ||
| 53 | |||
| 54 | our $logger = DJabberd::Log->get_logger(); | ||
| 55 | |||
| 56 | sub set_config_component { | ||
| 57 | $_[0]->{component_node_component} = $_[1]; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub finalize { | ||
| 61 | $logger->logdie("A component node requires a Component") unless ref($_[0]->{component_node_component}); | ||
| 62 | $_[0]->SUPER::finalize; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub handles_destination { | ||
| 66 | my ($self, $to_jid, $vhost) = @_; | ||
| 67 | |||
| 68 | return ($to_jid->node eq $self->nodename && $to_jid->domain eq $self->component->domain); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub bare_jid { | ||
| 72 | return new DJabberd::JID($_[0]->nodename."@".$_[0]->component->domain); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub resource_jid { | ||
| 76 | my ($self, $resourcename) = @_; | ||
| 77 | return new DJabberd::JID($self->bare_jid->as_string()."/".$resourcename); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub component { | ||
| 81 | return $_[0]->{component_node_component}; | ||
| 82 | } | ||
| 83 | |||
| 84 | =head1 SEE ALSO | ||
| 85 | |||
| 86 | See also L<DJabberd::Agent::Node>, L<DJabberd::Component>. | ||
| 87 | |||
| 88 | =head1 COPYRIGHT | ||
| 89 | |||
| 90 | Copyright 2007-2008 Martin Atkins | ||
| 91 | |||
| 92 | This module is part of the DJabberd distribution and is covered by the distribution's | ||
| 93 | overall licence. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | 1; |