I just made my first 2 modules for asterisk (The 1st one is depriciated already).
I was annoyed that i couldn't get GotoIf to take any expressions besides a boolean
then i made a module to mimic gotoif and parse a few expressions like (${var} > 12)
exten => 1,1,gotoif_expr,${var} > 12:1|4:1|5
Then I immediatly obseleted it with this new embedded perl module that lets
you implement stuff at will from perl instead of needing to make a module
Say you want to make gotoif that can parse expressions
exten => 1,1,Perl,gotoif:${var} > 12:1|4:1|5
say ${var} = 4
then gotoif perl sub is launched as
gotoif(4,"1|4","1|5");
and gotoif() looks like:
sub gotoif(@) {
my($expr,$goto1,$goto2) = @_;
my $test;
my($expr,$goto1,$goto2) = @_;
my $test;
eval "\$test = ($expr)";
my $ret = "goto:" . (($test) ? $goto1 : $goto2);
print "test: [$expr] = [$test] [$ret]\n";
$ret;
}
my $ret = "goto:" . (($test) ? $goto1 : $goto2);
print "test: [$expr] = [$test] [$ret]\n";
$ret;
}
Funy thing is I was too obcessed with what I had already seen so far in asterisk that
I didnt even notice AGI until yesterday =)
here is my beta README
app_perl 1.0
This is app_perl the "mod_perl" of sorts for asterisk. It is an actual live Perl Interpreter
embeded in a module so when it starts up the perl stays resident in memory the whole life of the
Asterisk process.
FEATURES:
It can call perl functions from extensions.conf in a special package called Asterisk::Perl.
that is loaded on startup from /etc/asterisk/Asterisk::Perl.pm.
that is loaded on startup from /etc/asterisk/Asterisk::Perl.pm.
exten => 1,1,Perl,myfunc:arg_1:arg_2:arg_n......
It then does it's business and returns one or more special directives that
are asterisk related operations processed by the module.
they are returned as a list (array) of scalar each containing a specially formated
command to feed back into asterisk via the C module.
are asterisk related operations processed by the module.
they are returned as a list (array) of scalar each containing a specially formated
command to feed back into asterisk via the C module.
These are counterparts to the real directives found in extensions.conf only probably
there is less error checking and more direct control a.k.a. likelyhood to crash.
there is less error checking and more direct control a.k.a. likelyhood to crash.
Valid commands so far.......
setvar:<name>:<value>
goto:<ext>|<line>
add_ext:<context>:<replace>:<extension>:<priority>:<application>:<data>:<callerid>
runapp:<app>:<data>
include:<new_context>:<existing_context>
ignorepat:<context>:<name>
switch:<context>:<app>:<data>
goto:<ext>|<line>
add_ext:<context>:<replace>:<extension>:<priority>:<application>:<data>:<callerid>
runapp:<app>:<data>
include:<new_context>:<existing_context>
ignorepat:<context>:<name>
switch:<context>:<app>:<data>
There are also some special commands: (1 for now)
thread:<function>
This will spin off a thread with the perl sub you specify running inside it.
the sub will get 1 scalar arg being the number of times it was run (starting with 1)
you can return an array full of more commands listed above with 1 exception:
if you put the keyword "loop" in the list, it will call the function again and increment the counter arg.
You can use this to say run 1 thread to listen on a tcp port and populate a shared data object
and use another to run in a loop and pass the altered data back to asterisk and relaunch
every 1 min or so. And of core all this stuff can be used to! horribly crash the program.
This will spin off a thread with the perl sub you specify running inside it.
the sub will get 1 scalar arg being the number of times it was run (starting with 1)
you can return an array full of more commands listed above with 1 exception:
if you put the keyword "loop" in the list, it will call the function again and increment the counter arg.
You can use this to say run 1 thread to listen on a tcp port and populate a shared data object
and use another to run in a loop and pass the altered data back to asterisk and relaunch
every 1 min or so. And of core all this stuff can be used to! horribly crash the program.
The function can return as many commands as it wants but of corse several goto or other such nonsense
may not be too smart.
may not be too smart.
The functions startup() and shutdown() are called respectively on load and unload of the module
allowing you to create on the fly contexts and configuration by looking up data from a database,more files etc.
you can even make the dialing of a certian extension cause a perl func to create more extensions or to
rewrite the existing one on the fly. Of course, you can't do any channel related commands (runapp etc)
from the startup function because there is no call in progress so they get ignored.
BUGS:
If you stare at the source code from about 2 feet back for approx 30 seconds you will start
to see that the entire thing is in itself 1 large bug. I am not a C programmer but rather
a Perl programmer so that was my motivation for this idea but it probably is sucky C style etc...
to see that the entire thing is in itself 1 large bug. I am not a C programmer but rather
a Perl programmer so that was my motivation for this idea but it probably is sucky C style etc...
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!