| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | DJabberd::Delivery::LocalVHosts - Controlled delivery to other local VHosts | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | <VHost mydomain.com> | ||
| 9 | |||
| 10 | [...] | ||
| 11 | |||
| 12 | <Plugin DJabberd::Delivery::LocalVHosts> | ||
| 13 | AllowVHost component1.mydomain.com | ||
| 14 | AllowVHost component2.mydomain.com | ||
| 15 | ... | ||
| 16 | </Plugin> | ||
| 17 | |||
| 18 | [...] | ||
| 19 | |||
| 20 | </VHost> | ||
| 21 | |||
| 22 | This delivery plugin provides a shortcut for delivering messages between two | ||
| 23 | domains that are running on the same server. It also allows inter-VHost | ||
| 24 | communication on hosts that do not have S2S enabled at all, enabling admins | ||
| 25 | to create a closed pool of vhosts on one server that can only talk to one another, | ||
| 26 | if configuring S2S is impossible or inconvenient. | ||
| 27 | |||
| 28 | This plugin won't do anything at all unless you allow at least one vhost as | ||
| 29 | shown in the example above. By explicitly listing the appropriate services | ||
| 30 | you can create segregated pools of vhosts that can't communicate between | ||
| 31 | each other but can communicate within the configured bounds. | ||
| 32 | |||
| 33 | TODO: more docs | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | package DJabberd::Delivery::LocalVHosts; | ||
| 38 | use strict; | ||
| 39 | use warnings; | ||
| 40 | use base 'DJabberd::Delivery'; | ||
| 41 | use DJabberd::Log; | ||
| 42 | |||
| 43 | our $logger = DJabberd::Log->get_logger; | ||
| 44 | |||
| 45 | sub set_config_allowvhost { | ||
| 46 | my ($self, $hostname) = @_; | ||
| 47 | $self->{allowed_vhosts} ||= {}; | ||
| 48 | $self->{allowed_vhosts}{$hostname} = 1; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub finalize { | ||
| 52 | my ($self) = @_; | ||
| 53 | |||
| 54 | $logger->warn("You haven't allowed any vhosts for inter-vhost delivery.") unless $self->{allowed_vhosts}; | ||
| 55 | $self->{allowed_vhosts} ||= {}; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub run_before { ("DJabberd::Delivery::S2S") } | ||
| 59 | |||
| 60 | sub deliver { | ||
| 61 | my ($self, $vhost, $cb, $stanza) = @_; | ||
| 62 | |||
| 63 | my $server = $vhost->server; | ||
| 64 | |||
| 65 | unless ($server) { | ||
| 66 | return $cb->decline; | ||
| 67 | } | ||
| 68 | |||
| 69 | my $to = $stanza->to_jid or return $cb->decline; | ||
| 70 | |||
| 71 | my $handler = $server->lookup_vhost($to->domain); | ||
| 72 | |||
| 73 | unless ($handler) { | ||
| 74 | $logger->debug("No local vhosts for ".$to->domain."."); | ||
| 75 | return $cb->decline; | ||
| 76 | } | ||
| 77 | |||
| 78 | my $handler_name = $handler->server_name; | ||
| 79 | |||
| 80 | # Sanity check: don't want to re-deliver to the same vhost | ||
| 81 | if ($handler == $vhost) { | ||
| 82 | return $cb->decline; | ||
| 83 | } | ||
| 84 | |||
| 85 | # Is it on the whitelist? | ||
| 86 | unless ($self->{allowed_vhosts}{$handler_name}) { | ||
| 87 | $logger->debug("Not delivering locally to $handler_name: not on allowed list."); | ||
| 88 | return $cb->decline; | ||
| 89 | } | ||
| 90 | |||
| 91 | if ($handler) { | ||
| 92 | $logger->debug($vhost->server_name." -> ".$handler->server_name); | ||
| 93 | $stanza->deliver($handler); | ||
| 94 | return $cb->delivered; | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | return $cb->decline; | ||
| 98 | } | ||
| 99 | |||
| 100 | } | ||
| 101 | |||
| 102 | 1; |