The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Custom Event Handler - prevent default behavior and replace Module output

Discussion in 'cPanel Developers' started by ruyrocha, Mar 20, 2012.

  1. ruyrocha

    ruyrocha Member

    Joined:
    Oct 12, 2010
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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:
    root@opsbox [/usr/local/cpanel/Cpanel]# cat CustomEventHandler.pm
    package Cpanel::CustomEventHandler;
    # cpanel12 - CustomEventHandler.pm                      Copyright(c) 2008 cPanel, Inc.
    #                                                           All rights Reserved.
    # copyright@cpanel.net                                         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
     
  2. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    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).
     
Loading...

Share This Page