Sample Perl plugins Print
Monday, 17 October 2011

EMG 5 includes support for plugins written in Perl thereby eliminating the need for developing and compiling C shared libraries.

Please note that changes in perl plugin source code require a restart of emgd process.

Sample plugins

Reject message based on message content

A non-zero return code from before_receive will cause the message to be rejected

sub before_receive
{
my ($request, $response) = @_;
# Retrieve connector instance
my $ci = ${$request}{'connector'};
# Retrieve queue entry (message)
my $qe = ${$request}{'qe'};
# Message body
my $msgtext = $qe->{'MESSAGE'} || '';
# Message encoding
my $charcode = $qe->{'CHARCODE'} || 1;

# If message is IA5 (plain text) and we find the forbidden text ("stupid"), then reject it
if($charcode == 1 && $msgtext =~ /stupid/i) {
 return 1;
}

return 0;
}

Custom routing

The plugin hook 'route' can be used to override EMG routing logic.

If $response->{'route'} is set when function returns with value 0 it will be used as the name of the connector to which the message will be routed.

If function returns a non-zero value normal EMG routing logic will be applied.

sub route {
my ($request, $response) = @_;
my $ci = ${$request}{'connector'};
my $qe = ${$request}{'qe'};
my $ret = 1;

if($qe->{'DESTADDR'} eq '123') {
 $response->{'route'} = "smsc-agg1";
  $ret = 0;
}

return $ret;
}

Adding an array of options

Starting with EMG 5.2.10 it is possible to add an array of option values for same keyword.
This should only be used for SMPPOPTION key.

sub before_receive {
my ($request, $response) = @_;
my $ci = ${$request}{'connector'};
my $qe = ${$request}{'qe'};

my @ar = ();
push(@ar, "0x1200:414243");
push(@ar, "0x1201:414244");
# You need to pass a reference to the array
$qe->{'SMPPOPTION'} = \@ar;

return 0;
}

Setting charges / billing info from plugin

Starting with EMG 5.3 it is possible to set the fee (towards customer) and cost (from supplier) from a plugin.

This is a separately licensed option requiring a license key with functionality enabled (available in evaluation licenses). In output of "emgd -v" you should see "Charge supported.".

The values set will also be set in the corresponding fields in "routelog" table making it easy to generate reports with revenue, cost and margin.

sub route {
my ($request, $response) = @_;
my $ci = ${$request}{'connector'};
my $qe = ${$request}{'qe'};
$qe->{CHARGE} = '0.03';
$qe->{CHARGE_COST} = '0.02';
$response->{'route'} = 'smsc2';
return 0;
}