Custom Event Handler - prevent default behavior and replace Module output

ruyrocha

Member
Oct 12, 2010
24
0
51
Brasil
Hello,

I've setup two development servers (A and B) to separate a few services from main cPanel installation, like email and DNS. I'm able to create, remove and update email accounts transparently on box B from server A using CustomEventHandler.pm, but cannot list those email accounts and get quota information. Let me show you:

Code:
[email protected] [/usr/local/cpanel/Cpanel]# cat CustomEventHandler.pm
package Cpanel::CustomEventHandler;
# cpanel12 - CustomEventHandler.pm                      Copyright(c) 2008 cPanel, Inc.
#                                                           All rights Reserved.
# [email protected]                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;
use Cpanel::Logger ();
use Cpanel::PublicAPI ();

sub event {
  my ( $apiv, $type, $module, $event, $cfgref, $dataref ) = @_;

  return 1 if ( $module ne 'email' );

  if ($module eq 'email') {

    if ($event eq 'listpopswithdisk' && $type eq 'post') {
      my $user     = $Cpanel::user;
      my $domain   = $cfgref->{'domain'};

      # get remote domain mailboxes
      if ( $domain eq 'abcdefg.com' ) {
	my $pubapi = Cpanel::PublicAPI->new('user' => 'root', 'pass' => 'superpassword', 'host' => 'x.x.x.x');
	my $emails = $pubapi->cpanel_api2_request(
	  'whostmgr',
	  { 'module' => 'Email',
	    'func'   => 'listpopswithdisk',
	    'user'   => $user,
	  },
	);
      }
    }

  }
  return 1;
}

1;
This piece of code is incomplete, but do you have any ideas how to properly get Email::listpopswithdisk from server B and show it on server A?

Regards,
Ruy Rocha
 

KostonConsulting

Well-Known Member
Verifed Vendor
Jun 17, 2010
255
1
68
San Francisco, CA
cPanel Access Level
Root Administrator
Re: Custom Event Handler - prevent default behavior and replace Module outp

Ruy,
I believe that having the module as 'email' rather than 'Email' is the cause for this not to execute. Try adding a line which checks the inputs to make sure:

Code:
print STDERR "MODULE: $module EVENT: $event TYPE: $type";
Other than that, your PublicAPI call seems ok. One other thing to note is that you should use the 'ip' parameter if you're using the IP of the remote server, rather than the hostname:

Code:
my $pubapi = Cpanel::PublicAPI->new('user' => 'root', 'pass' => 'superpassword', 'ip' => 'x.x.x.x');

You may wish to look at cPanel's Standardized Hooks System to capture Api2::Email::listpopswithdisk as there can only be one copy of CustomEventHandler.pm while the standardized hooks system allows for as many hooks into each function as required (less chance you'll need to keep adding code to CustomEventHandler.pm in the future or some third party program will overwrite it and remove your hook).