POSSA / freepbx-SIP-URI-handling

3 stars 4 forks source link

Allow [from-internal] extensions to dial URI #5

Open lgaetz opened 11 years ago

lgaetz commented 11 years ago

The module as currently written requires extensions to be in a custom context in order to dial a URI. This is less than ideal for various reasons, so I have figured out a way to avoid that step. Basically the extension dials the uri with a # in place of the @ such as lenny#itslenny.com. In the dial-trunk-predial-hook context, set a variable with the @ substituted for the # then call up the agi file. The agi file checks the contents of the variable and determines if it meets the format of a SIP URI and if so it sets a dial variable. If the dial variable is set, it is dialed otherwise regular dialplan continues.

add to extensions_custom.conf:

[macro-dialout-trunk-predial-hook]
exten => s,n,set(sipuri=${STRREPLACE(OUTNUM,#,@)})
exten => s,n,agi(sipuri.php,${sipuri})
exten => s,n,gotoif($["${sipruidial}"!=""]?dial)
exten => s,n,Return() 
exten => s,n(dial),dial(sip/${sipruidial})
exten => s,n,hangup()

/var/lib/asterisk/agi-bin/sipuri.php

#!/usr/bin/php -q
<?php
require('phpagi.php');
error_reporting(0);
$AGI = new AGI();
if (!isset($argv[1])) {
        $AGI->verbose('Missing dial string',3);
        exit(1);
}
// get dial string passed to agi
$sipuri = $argv[1]; 
$AGI->verbose('raw dial string: '.$sipuri,3);

$regex = "/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i";
preg_match($regex,$sipuri,$match);

if ( isset($match[0])) {
    $AGI->set_variable('sipruidial', $match[0]);
    $AGI->verbose("this is the URI: ".$match[0],3);
    }
?>

I have rough tested these blocks of code, and everything seems to work. You probably need an outbound route set as any/any so that the URI is not blocked by the outbound route dialing rules.