| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | DJabberd::Component::Example - An example DJabberd service component | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | <Subdomain example> | ||
| 9 | <Plugin DJabberd::Component::Example> | ||
| 10 | Greeting Hello, world! | ||
| 11 | </Plugin> | ||
| 12 | </Subdomain> | ||
| 13 | |||
| 14 | This class implements a very simple component that responds to all incoming messages | ||
| 15 | with a predefined greeting and which returns a vCard for itself when one is requested. | ||
| 16 | You can change the greeting returned using the optional Greeting configuration setting | ||
| 17 | as above. | ||
| 18 | |||
| 19 | This example also exposes a node called "somebloke" that responds to all messages with | ||
| 20 | authentic British surprise. | ||
| 21 | |||
| 22 | This example uses the higher-level API provided by L<DJabberd::Agent|DJabberd::Agent>, from which | ||
| 23 | L<DJabberd::Component|DJabberd::Component> inherits. | ||
| 24 | L<DJabberd::Component::External|DJabberd::Component::External> serves as an example of | ||
| 25 | how to handle stanzas at a lower level by overriding the C<handle_stanza> method. | ||
| 26 | |||
| 27 | =head1 COPYRIGHT | ||
| 28 | |||
| 29 | This module is Copyright (c) 2008 Martin Atkins | ||
| 30 | |||
| 31 | This module is distributed under the same terms as the main DJabberd distribution. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | package DJabberd::Component::Example; | ||
| 36 | use base DJabberd::Component; | ||
| 37 | use DJabberd::Util qw(exml); | ||
| 38 | use DJabberd::Log; | ||
| 39 | use DJabberd::JID; | ||
| 40 | |||
| 41 | our $logger = DJabberd::Log->get_logger(); | ||
| 42 | |||
| 43 | sub set_config_greeting { | ||
| 44 | my ($self, $greeting) = @_; | ||
| 45 | |||
| 46 | $self->{greeting} = $greeting; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub finalize { | ||
| 50 | my ($self, $opts) = @_; | ||
| 51 | |||
| 52 | $logger->info("initializing"); | ||
| 53 | $logger->info("My greeting is ", $self->{greeting}); | ||
| 54 | |||
| 55 | $self->{greeting} ||= "Hi! I'm an example DJabberd component!"; | ||
| 56 | $self->SUPER::finalize; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub handle_message { | ||
| 60 | my ($self, $vhost, $stanza) = @_; | ||
| 61 | |||
| 62 | my $from = $stanza->from_jid; | ||
| 63 | my $to = $stanza->to_jid; | ||
| 64 | |||
| 65 | $logger->info("Got message from ".$from->as_string); | ||
| 66 | |||
| 67 | my $response = $stanza->make_response(); | ||
| 68 | |||
| 69 | $response->attrs->{"{}type"} = "normal"; | ||
| 70 | $response->set_raw("<body>".exml($self->{greeting})."</body>"); | ||
| 71 | |||
| 72 | $logger->info("Responding with ".$response->as_xml); | ||
| 73 | |||
| 74 | $response->deliver($vhost); | ||
| 75 | |||
| 76 | } | ||
| 77 | |||
| 78 | sub vcard { | ||
| 79 | my ($self, $requester_jid) = @_; | ||
| 80 | |||
| 81 | return "<FN>Example DJabberd component</FN>"; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub get_node { | ||
| 85 | my ($self, $nodename) = @_; | ||
| 86 | |||
| 87 | if ($nodename eq 'somebloke') { | ||
| 88 | return DJabberd::Component::Example::ExampleNode->new( | ||
| 89 | nodename => 'somebloke', | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | return undef; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | package DJabberd::Component::Example::ExampleNode; | ||
| 98 | |||
| 99 | use base DJabberd::Agent::Node; | ||
| 100 | use DJabberd::Util qw(exml); | ||
| 101 | |||
| 102 | sub handle_message { | ||
| 103 | my ($self, $vhost, $stanza) = @_; | ||
| 104 | |||
| 105 | $logger->info("It's a message to some bloke."); | ||
| 106 | |||
| 107 | my $responsetext = "Gaw blimey, guvna!"; | ||
| 108 | |||
| 109 | my $response = $stanza->make_response(); | ||
| 110 | $response->set_raw("<body>".exml($responsetext)."</body>"); | ||
| 111 | $response->deliver($vhost); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub vcard { | ||
| 115 | return "<FN>Some Bloke</FN>"; | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; |