pre and post event handlers, meaning that an action is taken prior to an event (pre) or an action is taken after a specific event (post).
CustomEventHandler.pm the operation will fail.
/usr/local/cpanel/hooks as files with names similar to *.example.
CustomEvenHandler.pm will need to be created as: usr/local/cpanel/Cpanel/CustomEventHandler.pm CustomEventHandler.pm is located at: /usr/local/cpanel/Cpanel/CustomEventHandler.pm.sample
event() is the function within CustomEventHandler.pm that is called when an API1 or API2 call is made within cPanel.
The event() function will be passed the following types of parameters:
pre — Passed to the custom event handler before the API action is performed. This would be used when changing parameters or denying an event.
post — Passed after the API action has occurred. This should be used for reading events or modifying the data returned from the event.
email
addforward
$cfgref->{'fwdemail'} — If email forwarding was the selected option, this hash would contain the email address to which mail will be forwarded.
post, this can be used to modify data sent to the interface. event() will be called twice for every API call made: once before the API call and once after the API call.
event() returns 0, undef, a negative number or a blank string when the type parameter is set to pre, the API call will be skipped. In this case, an error will not be raised unless it is explicitly set to do so. By default, event() should return a value of 1.
CustomEventHandler.pm is parsed by cpsrvd. cpsrvd parses Perl files using an embedded Perl 5.6.2 interpreter with its own module paths. Non-standard modules need to be installed in /usr/local/cpanel/perl.
Any module in /usr/local/cpanel/Cpanel/ is available through the Cpanel:: namespace. All interactions performed by a custom event handler are performed as the user running the API calls. This is important because any file receiving output needs to be writable by all users.
README file. You will need to be familiar with Data::Dumper in order to understand the information that is returned by the debugger. If you require assistance working with the debugger, please refer to the cPanel forum's Developer Corner.
$Cpanel::CPERROR that allows you to raise an error in the interface similar to errors received, for example, when creating an email account that already exists.
In order to raise the error, you will need to define the context. This is normally set automatically in the $CPanel::context variable. Otherwise, this variable usually contains the module's name in lowercase.
$Cpanel::CPERROR{$Cpanel::context} = "This is an error message";
error_log. You can accomplish this by printing to STDERR within your CustomEventHandler.pm. For example:
print STDERR "This is another error message";
event() return a negative value.
To learn more about how to raise errors in a custom event handler, visit our Cpanel::Logger documentation here.
support or abuse and raise an error stating these values are reserved usernames.
Download Example
sub event {
my ( $apiv, $type, $module, $event, $cfgref, $dataref ) = @_;
return 1 if $module ne "email" || $event ne "addpop"; # Filter out non-email:addpop requests
my @blacklist = ( "abuse", "support" ); # setup the black list
if ( $module eq 'email' && $event eq 'addpop' && $type eq 'pre' && ref $cfgref->{'param0'} eq 'ARRAY' )
{ # determine that the event passed is the correct event
my $username = $cfgref->{'param0'}->[0]; # Define which variable is the user name
if ( grep( /$username/, @blacklist ) )
{ # if the username is in the blacklist
$Cpanel::CPERROR{$Cpanel::context} = "Sorry, $username is a reserved username"; # Throw an error
return; # tell email::addpop not to proceed
}
}
return 1; # in every other case, allow the event to proceed
}
Copyright © cPanel 2000-2009.