[Astpp-commit] SF.net SVN: astpp:[2270] trunk/freeswitch/astpp-fs-xml.pl.mod_perl
Brought to you by:
darrenkw
|
From: <dar...@us...> - 2009-10-02 20:43:22
|
Revision: 2270
http://astpp.svn.sourceforge.net/astpp/?rev=2270&view=rev
Author: darrenkw
Date: 2009-10-02 20:43:15 +0000 (Fri, 02 Oct 2009)
Log Message:
-----------
Add temporary file.
Added Paths:
-----------
trunk/freeswitch/astpp-fs-xml.pl.mod_perl
Added: trunk/freeswitch/astpp-fs-xml.pl.mod_perl
===================================================================
--- trunk/freeswitch/astpp-fs-xml.pl.mod_perl (rev 0)
+++ trunk/freeswitch/astpp-fs-xml.pl.mod_perl 2009-10-02 20:43:15 UTC (rev 2270)
@@ -0,0 +1,334 @@
+#!/usr/bin/perl
+#
+# ASTPP - Open Source Voip Billing
+#
+# Copyright (C) 2008, Aleph Communications
+#
+# Darren Wiebe (da...@al...)
+#
+# This program is Free Software and is distributed under the
+# terms of the GNU General Public License version 2.
+############################################################
+#
+# Usage-example:
+#
+
+use DBI;
+use CGI;
+use CGI qw/:standard Vars/;
+use ASTPP;
+use strict;
+use lib qw(.);
+
+use vars
+ qw($params $ASTPP @output $config $freeswitch_db $astpp_db $verbosity );
+use Locale::gettext_pp qw(:locale_h);
+require "/usr/local/astpp/astpp-common.pl";
+$ENV{LANGUAGE} = "en"; # de, es, br - whatever
+print STDERR "Interface language is set to: " . $ENV{LANGUAGE} . "\n";
+bindtextdomain( "astpp", "/usr/local/share/locale" );
+textdomain("astpp");
+$verbosity = 2;
+@output = ("STDERR");
+$ASTPP = ASTPP->new;
+$ASTPP->set_verbosity(4); #Tell ASTPP debugging how verbose we want to be.
+
+sub initialize() {
+ $config = $ASTPP->load_config();
+ $astpp_db = $ASTPP->connect_db(
+ dbengine => $config->{astpp_dbengine},
+ dbname => $config->{dbname},
+ dbhost => $config->{dbhost},
+ dbuser => $config->{dbuser},
+ dbpass => $config->{dbpass}
+ );
+ $ASTPP->set_astpp_db($astpp_db);
+ $config = &load_config_db( $astpp_db, $config ) if $astpp_db;
+ $freeswitch_db = $ASTPP->connect_db(
+ dbengine => "MySQL",
+ dbname => $config->{freeswitch_dbname},
+ dbhost => $config->{freeswitch_dbhost},
+ dbuser => $config->{freeswitch_dbuser},
+ dbpass => $config->{freeswitch_dbpass}
+ );
+ $ASTPP->set_freeswitch_db($freeswitch_db);
+}
+
+################# Programs start here #######################################
+&initialize;
+my ( $ipinfo, $xml, $maxlength, $maxmins, $callstatus,$astppdid,$didinfo );
+foreach my $param ( param() ) {
+ $params->{$param} = param($param);
+ $ASTPP->debug( debug => "$param $params->{$param}" );
+}
+$xml = header( -type => 'text/plain' );
+
+$ASTPP->debug(
+ debug => "Destination = $params->{'Caller-Destination-Number'}" );
+
+if ( $params->{section} eq "dialplan" ) {
+ # Check to see if this is a DID. If it is we handle it differently.
+ #
+ $didinfo = $ASTPP->get_did( did => $params->{'Caller-Destination-Number'});
+ if ($didinfo->{number}) {
+ $astppdid = "ASTPP-DID";
+ $ASTPP->debug( debug => "This is a call for a DID: ");
+ $params->{variable_accountcode} = $didinfo->{account};
+ }
+
+
+ if ( !$params->{variable_accountcode} ) {
+
+ # First we strip off X digits to see if this account is prepending numbers
+ # as authentications
+ $ASTPP->debug( debug => "Checking CC Number: "
+ . $params->{'Caller-Destination-Number'} );
+ my $cc = substr( $params->{'Caller-Destination-Number'},
+ 0, $config->{cardlength} );
+ my $sql =
+ $astpp_db->prepare("SELECT number FROM accounts WHERE cc = $cc");
+ $sql->execute;
+ my $record = $sql->fetchrow_hashref;
+ $sql->finish;
+ $params->{variable_accountcode} = $record->{cardnum}
+ if ( $record->{cardnum} );
+ }
+ if ( !$params->{variable_accountcode} ) {
+ $ASTPP->debug(
+ debug => "Checking IP Address:" . $params->{'Hunt-Network-Addr'} );
+ $ipinfo = $ASTPP->ip_address_authenticate(
+ ip_address => $params->{'Hunt-Network-Addr'},
+ destination => $params->{'Caller-Destination-Number'}
+ );
+ if ($ipinfo->{account} ne "") {
+ $params->{variable_accountcode} = $ipinfo->{account};
+ $params->{'Caller-Destination-Number'} =~ s/$ipinfo->{prefix}//g;
+ }
+ }
+
+ $xml = $ASTPP->fs_dialplan_xml_header(
+ xml => $xml,
+ destination_number => $params->{'Caller-Destination-Number'},
+ DID => $didinfo->{number},
+ IP => $ipinfo->{account}
+ );
+
+ $ASTPP->debug( debug =>
+"$params->{variable_accountcode}, $params->{'Caller-Destination-Number'}"
+ );
+
+ my $carddata = $ASTPP->get_account(account => $params->{variable_accountcode});
+
+ if ( !$carddata->{number} )
+ { # Check to see if the account exists. If not then exit.
+ $ASTPP->debug( debug => "CALLSTATUS 2" );
+ $ASTPP->debug( debug => "CANNOT RETRIEVE CARD" );
+ $xml .=
+ "<action application=\"reject\" data=\"CANNOT RETRIEVE ACCOUNT\"/>\n";
+ $xml = $ASTPP->fs_dialplan_xml_footer( xml => $xml );
+# print $xml;
+ $ASTPP->debug( debug => "Returning nothing so dialplan can continue." );
+ print "";
+ exit(0);
+ }
+
+ if ( $carddata->{dialed_modify} ) {
+ my @regexs = split( m/,/m, $carddata->{dialed_modify} );
+ foreach my $regex (@regexs) {
+ $regex =~ s/"//g; #Strip off quotation marks
+ my ( $grab, $replace ) = split( m!/!i, $regex )
+ ; # This will split the variable into a "grab" and "replace" as needed
+ $ASTPP->debug( debug => "Grab: $grab" );
+ $ASTPP->debug( debug => "Replacement: $replace" );
+ $ASTPP->debug( debug =>
+ "Phone Before: $params->{'Caller-Destination-Number'}" );
+ $$params->{'Caller-Destination-Number'} =~ s/$grab/$replace/is;
+ $ASTPP->debug( debug =>
+ "Phone After: $params->{'Caller-Destination-Number'}" );
+ }
+ }
+
+ $ASTPP->debug( debug => "FINDING LIMIT FOR: " . $carddata->{number} );
+ my $pricelistdata = $ASTPP->get_pricelist(
+ pricelist => $carddata->{pricelist});
+ my $routeinfo;
+ ( $callstatus, $maxlength, $routeinfo ) = $ASTPP->max_length(
+ destination => $params->{'Caller-Destination-Number'},
+ call_max_length => $config->{call_max_length},
+ max_free_length => $config->{max_free_length},
+ markup => $pricelistdata->{markup},
+ account => $carddata->{number},
+ account_creditlimit => $carddata->{credit_limit},
+ account_maxchannels => $carddata->{maxchannels},
+ debug => $config->{debug},
+ account_pricelist => $carddata->{pricelist},
+ account_reseller => $carddata->{reseller},
+ default_brand => $config->{default_brand},
+
+ );
+# &max_length( $astpp_db, $config, $carddata,
+# $params->{'Caller-Destination-Number'} );
+
+# my $routeinfo = &get_route(
+# $astpp_db, $config,
+# $params->{'Caller-Destination-Number'},
+# $carddata->{pricelist}, $carddata,$astppdid
+# );
+
+ $ASTPP->debug( debug => "Cost: " . $routeinfo->{cost} );
+ $ASTPP->debug( debug => "Pricelist: " . $routeinfo->{pricelist} );
+ my $minimumcharge = $routeinfo->{cost};
+ my @reseller_list;
+ $ASTPP->debug( debug => "CALLSTATUS: $callstatus MAX_LENGTH: $maxlength" );
+
+ if (!$routeinfo->{cost} && !$routeinfo->{pricelist}) {
+ $ASTPP->debug( debug => "COULD NOT FIND ROUTE. EXITING SO DIALPLAN CAN TAKE OVER" );
+ exit(0);
+ }
+ while ( $carddata->{reseller} && $maxlength > 1 && $callstatus == 1 ) {
+ $ASTPP->debug( debug => "FINDING LIMIT FOR: $carddata->{reseller}" );
+ $carddata = &get_account( $astpp_db, $carddata->{reseller} );
+ push @reseller_list, $carddata->{number};
+ $ASTPP->debug( debug =>
+"ADDING $carddata->{number} to the list of resellers for this account"
+ );
+ my ( $resellercallstatus, $resellermaxlength ) =
+ &max_length( $astpp_db, $config, $carddata,
+ $params->{'Caller-Destination-Number'} );
+ my $routeinfo = &get_route(
+ $astpp_db, $config,
+ $params->{'Caller-Destination-Number'},
+ $carddata->{pricelist}, $carddata
+ );
+ if ( $resellercallstatus != 1 ) {
+ $carddata->{reseller} = "";
+ $callstatus = $resellercallstatus;
+ }
+ elsif ( $resellermaxlength < $maxlength ) {
+ $maxlength = $resellermaxlength;
+ }
+ $ASTPP->debug( debug =>
+"Reseller cost = $routeinfo->{cost} and minimum charge is $minimumcharge"
+ );
+ if ( $resellermaxlength < 1 || $routeinfo->{cost} > $minimumcharge ) {
+ $ASTPP->debug( debug =>
+ "Reseller call is priced too cheap! Call being barred!" );
+ $xml .=
+"<action application=\"reject\" data=\"Reseller call is priced too cheap! Call being barred!\"/>\n";
+ $xml = $ASTPP->fs_dialplan_xml_footer( xml => $xml );
+ print $xml;
+ exit(0);
+ }
+ $ASTPP->debug( debug => "RESELLER Max Length: $resellermaxlength" );
+ $ASTPP->debug( debug => "RESELLER Call Status: $resellercallstatus" );
+ }
+
+ if ( $config->{debug} == 1 ) {
+ $ASTPP->debug( debug => "PRINTING LIST OF RESELLERS FOR THIS ACCOUNT" );
+ foreach my $reseller (@reseller_list) {
+ $ASTPP->debugb( debug => "RESELLER: $reseller" );
+ }
+ }
+
+ $ASTPP->debug("Max Call Length: $maxlength minutes");
+ $ASTPP->debug("Call Status: $callstatus");
+
+ if ( $maxlength <= 1 ) {
+ $ASTPP->debug( debug => "NOT ENOUGH CREDIT" );
+ $xml .= "<action application=\"reject\" data=\"NOT ENOUGH CREDIT\"/>\n";
+ $xml = $ASTPP->fs_dialplan_xml_footer( xml => $xml );
+ print $xml;
+ exit(0);
+ } elsif ($config->{call_max_length} && $maxlength < $config->{call_max_length} / 1000){
+ $maxlength = $config->{call_max_length} / 1000;
+ }
+
+ $xml = $ASTPP->fs_dialplan_xml_timelimit(
+ xml => $xml,
+ max_length => $maxlength,
+ accountcode => $carddata->{number}
+ );
+
+# Set the timelimit as well as other variables which are needed in the dialplan.
+ my $timelimit =
+ "L(" . sprintf( "%.0f", $maxlength * 60 * 1000 ) . ":60000:30000)";
+
+ $ASTPP->debug( debug => "Looking for outbound Route" );
+ my $routeinfo = &get_route(
+ $astpp_db, $config,
+ $params->{'Caller-Destination-Number'},
+ $carddata->{pricelist}, $carddata, $astppdid
+ );
+
+ if ($didinfo->{number} ) {
+ $ASTPP->debug( debug => "THIS IS A DID CALL: $xml");
+ my ($returned_data) = $ASTPP->fs_dialplan_xml_did(
+ did => $params->{'Caller-Destination-Number'}
+ );
+ $xml .= $returned_data;
+ } else {
+ # Get the list of routes for the phone number.
+ my @outboundroutes =
+ &get_outbound_routes( $astpp_db, $params->{'Caller-Destination-Number'},
+ $carddata, $routeinfo, @reseller_list );
+ foreach my $route (@outboundroutes) {
+ $ASTPP->debug( debug =>
+ "$route->{trunk}: cost $route->{cost}\t $route->{pattern}" );
+ if ( $route->{cost} > $routeinfo->{cost} ) {
+ $ASTPP->debug( debug =>
+"$route->{trunk}: $route->{cost} > $routeinfo->{cost}, skipping"
+ );
+ }
+ else {
+ my ($returned_data,$junk) = $ASTPP->fs_dialplan_xml_bridge(
+ destination_number => $params->{'Caller-Destination-Number'},
+ route_prepend => $route->{prepend},
+ trunk_name => $route->{trunk},
+ route_id => $route->{id}
+ );
+ $xml .= $returned_data;
+ }
+ }
+ }
+ $xml = $ASTPP->fs_dialplan_xml_footer( xml => $xml );
+ $ASTPP->debug( debug => $xml );
+ print $xml;
+}
+elsif ( $params->{section} eq "directory" ) {
+
+ #hostname darren-laptop
+ #section directory
+ #tag_name domain
+ #key_name name
+ #key_value 192.168.2.119
+ #action sip_auth
+ #sip_profile internal
+ #sip_user_agent Zoiper rev.1118
+ #sip_auth_username 1000
+ #sip_auth_realm 192.168.2.119
+ #sip_auth_nonce 83005e62-7e13-11dd-9eb1-25560b0691a8
+ #sip_auth_uri sip:192.168.2.119;transport=UDP
+ #sip_auth_qop auth
+ #sip_auth_cnonce a79169d2656f292a
+ #sip_auth_nc 00000001
+ #sip_auth_response 4475154556879ec2017978f1347192a6
+ #sip_auth_method REGISTER
+ #key id
+ #user 1000
+ #domain 192.168.2.119
+ #ip 192.168.2.119
+
+ if ($params->{'user'}) {
+ $xml = $ASTPP->fs_directory_xml_header( xml => $xml );
+ $xml = $ASTPP->fs_directory_xml(
+ xml => $xml,
+ ip => $params->{'ip'},
+ user => $params->{'user'},
+ domain => $params->{'domain'}
+ );
+ $xml = $ASTPP->fs_directory_xml_footer( xml => $xml );
+ }
+ $ASTPP->debug( debug => $xml );
+ print $xml;
+}
+exit(0);
Property changes on: trunk/freeswitch/astpp-fs-xml.pl.mod_perl
___________________________________________________________________
Added: svn:executable
+ *
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|