Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # i-MSCP Listener::Named::AddMxDNSRecords listener file
  2. # Copyright (C) 2015 UncleJ, Arthur Mayer <mayer.arthur@gmail.com>
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2.1 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this library; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. #
  19. ## i-MSCP listener file modifies the zone files, adds custom out-of-zone MX DNS entries
  20. #
  21.  
  22. package Listener::Named::AddMxDNSRecords;
  23.  
  24. use iMSCP::EventManager;
  25. use iMSCP::TemplateParser;
  26.  
  27. #
  28. ## Configuration variables
  29. #
  30.  
  31. # Define here your out-of-zone backup-mx mail-servers
  32. # Please replace the entries below by your own entries
  33. my %perDomainAdditionalMailservers = (
  34.     'testdomain.local' => {
  35.         'mx40' => '40'
  36.     },
  37.     '<domain1.tld>' => {
  38.         '<mx1-name>' => '<mx1-priority>',
  39.         '<mx2-name>' => '<mx2-priority>'
  40.     },
  41.     '<domain2.tld>' => {
  42.         '<mx1-name>' => '<mx1-priority>',
  43.         '<mx2-name>' => '<mx2-priority>'
  44.     }
  45. );
  46.  
  47. # Please replace the entries below by your own entries
  48. my %additionalMailservers = ( 'mx50' => '50' );
  49.  
  50. #
  51. ## Please, don't edit anything below this line
  52. #
  53.  
  54. sub addMxDNSrecords
  55. {
  56.     my ($tplDbFileContent, $data) = @_;
  57.  
  58.     my %mxServerEntries;
  59.  
  60.     if(exists $perDomainAdditionalMailservers{$data->{'DOMAIN_NAME'}}) {
  61.         %mxServerEntries = (%additionalMailservers, %{$perDomainAdditionalMailservers{$data->{'DOMAIN_NAME'}});
  62.     } else {
  63.         %mxServerEntries = %additionalMailservers;
  64.     }
  65.  
  66.     if(%mxServerEntries) {
  67.         my @formattedEntries = ('; custom MX DNS entries BEGIN');
  68.  
  69.         while(my($mxHost, $mxPriority) = each(%mxServerEntries)) {
  70.             push @formattedEntries, sprintf("@\tIN\tMX\t%s %s", $mxHost, $mxPriority);
  71.         }
  72.  
  73.         undef %mxServerEntries;
  74.         push @formattedEntries, '; custom MX DNS entries ENDING';
  75.  
  76.         $$tplDbFileContent = replaceBloc(
  77.             "; dmn MAIL entry BEGIN\n",
  78.             "; dmn MAIL entry ENDING\n",
  79.             "; dmn MAIL entry BEGIN\n" .
  80.                 getBloc(
  81.                     "; dmn MAIL entry BEGIN\n",
  82.                     ";dmn MAIL entry ENDING\n",
  83.                     $$tplDbFileContent
  84.                 ) .
  85.                 join("\n", @formattedEntries) . "\n" .
  86.             "; dmn MAIL entry ENDING\n",
  87.             $$tplDbFileContent
  88.         );
  89.     }
  90.  
  91.     0;
  92. }
  93.  
  94. sub addMxDNSrecordsSub
  95. {
  96.     my ($wrkDbFileContent, $subEntry, $data) = @_;
  97.  
  98.     my %mxServerEntries;
  99.  
  100.     if(exists $perDomainAdditionalMailservers{$data->{'DOMAIN_NAME'}}) {
  101.         %mxServerEntries = (%additionalMailservers, %{$perDomainAdditionalMailservers{$data->{'DOMAIN_NAME'}});
  102.     } else {
  103.         %mxServerEntries = %additionalMailservers;
  104.     }
  105.  
  106.     if(%mxServerEntries) {
  107.         my @formattedEntries = ('; custom MX DNS entries BEGIN');
  108.  
  109.         while(my($mxHost, $mxPriority) = each(%mxServerEntries)) {
  110.             push @formattedEntries, sprintf("@\tIN\tMX\t%s %s", $mxHost, $mxPriority);
  111.         }
  112.  
  113.         undef %mxServerEntries;
  114.         push @formattedEntries, '; custom MX DNS entries ENDING';
  115.  
  116.         $$subEntry = replaceBloc(
  117.             "; sub MX entry BEGIN\n",
  118.             "; sub MX entry ENDING\n",
  119.             "; sub MX entry BEGIN\n" .
  120.                 getBloc(
  121.                     "; sub MX entry BEGIN\n",
  122.                     "; sub MX entry ENDING\n",
  123.                     $$subEntry
  124.                 ) .
  125.                 join("\n", @formattedEntries) . "\n" .
  126.             "; sub MX entry ENDING\n",
  127.             $$subEntry
  128.         );
  129.     }
  130.  
  131.     0;
  132. }
  133.  
  134. my $eventManager = iMSCP::EventManager->getInstance();
  135. $eventManager->register('beforeNamedAddDmnDb', \&addMxDNSrecords);
  136. $eventManager->register('beforeNamedAddSub', \&addMxDNSrecordsSub);
  137.  
  138. 1;
  139. __END__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement