我有一段asterisk使用的agi程式是用perl寫的....
但因為板子上沒有perl可以用
所以想把它改寫成C的方式...
#!/usr/bin/perl -w
#
use Asterisk::AGI;
my $debug = 1;
my %ext; # Hash that will contain our list of extensions to call
my $cidnum; # Caller ID Number for this call
my $cidname; # Caller ID Name for this call
my $timer; # Call timer for Dial command
my $dialopts;# options for dialing
my $rc; # Catch return code
my $priority;# Next priority
my $AGI = new Asterisk::AGI;
my %input = $AGI->ReadParse();
$AGI->setcallback(\&mycallback);
if ($debug >= 2) {
foreach $key (keys %input) {
debug("$key = " . $input{$key},3);
}
}
$priority = $input{'priority'} + 1;
if ($input{'callerid'} =~ /^\"(.*)\"\s+\<(\d+)\>\s*$/) {
$cidname = $1;
$cidnum = $2;
debug("Caller ID name is '$cidname' number is '$cidnum'", 1);
} else {
$cidname = undef;
$cidnum = undef;
debug("Caller ID is not set", 1);
}
$timer = $AGI->get_variable('ARG1') || 0;
$dialopts = $AGI->get_variable('ARG2') || '';
# Start with AC set to 3 as two args are used
my $arg_cnt = 3;
while(my $arg = $AGI->get_variable('ARG' . $arg_cnt)) {
$ext{$arg} = $arg;
debug("Added extension $arg to extension map", 3);
$arg_cnt++;
}
# Check for call forwarding first
# This doesn't currently check for multilevel forwards or loops
foreach my $k (keys %ext) {
my $cf = $AGI->database_get('CF',$k);
if ($cf) {
$ext{$k} = $cf;
debug("Extension $k has call forward set to $cf", 1);
} else {
debug("Extension $k cf is disabled", 3);
}
}
# Now check for DND
foreach my $k (keys %ext) {
my $dnd = $AGI->database_get('DND',$ext{$k});
if ($dnd) {
debug("Extension $ext{$k} has do not disturb enabled", 1);
delete $ext{$k};
} else {
debug("Extension $ext{$k} do not disturb is disabled", 3);
}
}
# Update Caller ID for calltrace application
foreach my $k (keys %ext) {
if ($cidnum) {
$rc = $AGI->database_put('CALLTRACE', $ext{$k}, $cidnum);
if ($rc == 1) {
debug("DbSet CALLTRACE/$ext{$k} to $cidnum", 3);
} else {
debug("Failed to DbSet CALLTRACE/$ext{$k} to $cidnum ($rc)", 1);
}
} else {
# We don't care about retval, this key may not exist
$AGI->database_del('CALLTRACE', $ext{$k});
debug("DbDel CALLTRACE/$ext{$k} - Caller ID is not defined", 3);
}
}
my $ds = '';
foreach my $k (keys %ext) {
$ds .= 'SIP/' . $ext{$k} . '&';
}
chop $ds if length($ds);
if (!length($ds)) {
$AGI->exec('NoOp');
} else {
$ds .= '|';
$ds .= $timer if ($timer);
$ds .= '|' . $dialopts; # pound to transfer, provide ringing
debug("About to execute Dial($ds)", 1);
$rc = $AGI->exec('Dial ' . $ds);
# returns 0 on no answer -1 on hangup
# i still need to check what it returns on busy
debug("Dial return value was $rc", 1);
if ($rc == -1) {
debug("Setting Priority to " . ($priority + 20) . ' from '
. $priority, 1);
$AGI->set_priority($priority + 20);
}
}
exit 0;
sub debug
{
my $string = shift;
my $level = shift || 3;
if ($debug) {
$AGI->verbose($string, $level);
}
return(0);
}
sub mycallback
{
my $rc = shift;
debug("User hung up. (rc=" . $rc . ")", 1);
exit ($rc)
}