| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | DJabberd::Agent::Node - Abstract class for an agent that handles a specific node | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | package MyPackage::DJabberd::MyNode; | ||
| 9 | use base DJabberd::Agent::Node; | ||
| 10 | |||
| 11 | # Example of an agent that responds to incoming chat messages | ||
| 12 | |||
| 13 | sub handle_message { | ||
| 14 | my ($self, $vhost, $stanza) = @_; | ||
| 15 | |||
| 16 | my $response = $stanza->make_response(); | ||
| 17 | $response->set_raw("<body>Hello. I'm a software agent.</body>"); | ||
| 18 | $response->deliver($vhost); | ||
| 19 | } | ||
| 20 | |||
| 21 | This class provides a parent class for agents which recieve stanzas addressed to | ||
| 22 | a particular nodename within a domain. | ||
| 23 | |||
| 24 | Such an agent is often referred to as a "Bot". A higher-level API for implementing | ||
| 25 | "chat bots" is also provided in L<DJabberd::Bot|DJabberd::Bot>. | ||
| 26 | |||
| 27 | =head1 USAGE | ||
| 28 | |||
| 29 | This class is a specialization of L<DJabberd::Agent|DJabberd::Agent>. In most | ||
| 30 | cases, users of this class will use the API exposed by L<DJabberd::Agent|DJabberd::Agent>. | ||
| 31 | However, there is some special behaviour to note and some additional methods | ||
| 32 | that are not part of the basic agent class. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | package DJabberd::Agent::Node; | ||
| 37 | |||
| 38 | use base qw(DJabberd::Agent); | ||
| 39 | use DJabberd::Util qw(exml); | ||
| 40 | use strict; | ||
| 41 | use warnings; | ||
| 42 | use DJabberd::Log; | ||
| 43 | |||
| 44 | our $logger = DJabberd::Log->get_logger(); | ||
| 45 | |||
| 46 | sub set_config_nodename { | ||
| 47 | $_[0]->{agent_node_name} = $_[1]; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub finalize { | ||
| 51 | $logger->logdie("A node requires a NodeName") unless defined($_[0]->{agent_node_name}); | ||
| 52 | $_[0]->SUPER::finalize; | ||
| 53 | } | ||
| 54 | |||
| 55 | =head2 handles_destination($self, $to_jid, $vhost) | ||
| 56 | |||
| 57 | This class provides an overriden version of this method which responds with | ||
| 58 | a true value if and only if the domain and the nodename of the destination JID | ||
| 59 | match those that are handled by this instance. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | sub handles_destination { | ||
| 63 | my ($self, $to_jid, $vhost) = @_; | ||
| 64 | |||
| 65 | return ($to_jid->node eq $self->nodename && $to_jid->domain eq $vhost->server_name); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub name { | ||
| 69 | return $_[0]->nodename; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub nodename { | ||
| 73 | return $_[0]->{agent_node_name}; | ||
| 74 | } | ||
| 75 | |||
| 76 | =head2 vcard($self, $requester_jid) | ||
| 77 | |||
| 78 | The overriden version of this method will return a vCard where the full name is set | ||
| 79 | to the nodename of this instance. Subclasses will probably want to override this to | ||
| 80 | do something more fancy. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | sub vcard { | ||
| 84 | my ($self, $requester_jid) = @_; | ||
| 85 | |||
| 86 | return "<NICKNAME>".exml($self->name)."</NICKNAME>"; | ||
| 87 | } | ||
| 88 | |||
| 89 | =head1 SEE ALSO | ||
| 90 | |||
| 91 | See also L<DJabberd::Agent>, L<DJabberd::Component>. | ||
| 92 | |||
| 93 | =head1 COPYRIGHT | ||
| 94 | |||
| 95 | Copyright 2007-2008 Martin Atkins | ||
| 96 | |||
| 97 | This module is part of the DJabberd distribution and is covered by the distribution's | ||
| 98 | overall licence. | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | 1; |