| 1 |
#!/usr/bin/perl -w |
|---|
| 2 |
# Retrieves the sip user/peer entries from the database |
|---|
| 3 |
# Use these commands to create the appropriate tables in MySQL |
|---|
| 4 |
# |
|---|
| 5 |
#CREATE TABLE sip (id INT(11) DEFAULT -1 NOT NULL,keyword VARCHAR(20) NOT NULL,data VARCHAR(50) NOT NULL, flags INT(1) DEFAULT 0 NOT NULL,PRIMARY KEY (id,keyword)); |
|---|
| 6 |
# |
|---|
| 7 |
# if flags = 1 then the records are not included in the output file |
|---|
| 8 |
|
|---|
| 9 |
use FindBin; |
|---|
| 10 |
push @INC, "$FindBin::Bin"; |
|---|
| 11 |
|
|---|
| 12 |
use DBI; |
|---|
| 13 |
require "retrieve_parse_amportal_conf.pl"; |
|---|
| 14 |
|
|---|
| 15 |
################### BEGIN OF CONFIGURATION #################### |
|---|
| 16 |
|
|---|
| 17 |
# the name of the extensions table |
|---|
| 18 |
$table_name = "iax"; |
|---|
| 19 |
# the path to the extensions.conf file |
|---|
| 20 |
# WARNING: this file will be substituted by the output of this program |
|---|
| 21 |
$iax_conf = "/etc/asterisk/iax_additional.conf"; |
|---|
| 22 |
|
|---|
| 23 |
# cool hack by Julien BLACHE <jblache@debian.org> |
|---|
| 24 |
$ampconf = parse_amportal_conf( "/etc/amportal.conf" ); |
|---|
| 25 |
# username to connect to the database |
|---|
| 26 |
$username = $ampconf->{"AMPDBUSER"}; |
|---|
| 27 |
# password to connect to the database |
|---|
| 28 |
$password = $ampconf->{"AMPDBPASS"}; |
|---|
| 29 |
# the name of the box the MySQL database is running on |
|---|
| 30 |
$hostname = $ampconf->{"AMPDBHOST"}; |
|---|
| 31 |
# the name of the database our tables are kept |
|---|
| 32 |
$database = $ampconf->{"AMPDBNAME"}; |
|---|
| 33 |
|
|---|
| 34 |
# the engine to be used for the SQL queries, |
|---|
| 35 |
# if none supplied, backfall to mysql |
|---|
| 36 |
$db_engine = "mysql"; |
|---|
| 37 |
if (exists($ampconf->{"AMPDBENGINE"})) { |
|---|
| 38 |
$db_engine = $ampconf->{"AMPDBENGINE"}; |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
################### END OF CONFIGURATION ####################### |
|---|
| 42 |
|
|---|
| 43 |
if ( $db_engine eq "mysql" ) { |
|---|
| 44 |
$dbh = DBI->connect("dbi:mysql:dbname=$database;host=$hostname", "$username", "$password"); |
|---|
| 45 |
} |
|---|
| 46 |
elsif ( $db_engine eq "pgsql" ) { |
|---|
| 47 |
$dbh = DBI->connect("dbi:pgsql:dbname=$database;host=$hostname", "$username", "$password"); |
|---|
| 48 |
} |
|---|
| 49 |
elsif ( $db_engine eq "sqlite" ) { |
|---|
| 50 |
if (!exists($ampconf->{"AMPDBFILE"})) { |
|---|
| 51 |
print "No AMPDBFILE set in /etc/amportal.conf\n"; |
|---|
| 52 |
exit; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
my $db_file = $ampconf->{"AMPDBFILE"}; |
|---|
| 56 |
$dbh = DBI->connect("dbi:SQLite2:dbname=$db_file","",""); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
# items with id=-1 get added for all users |
|---|
| 60 |
$statement = "SELECT keyword,data from $table_name where id=-1 and keyword <> 'account' and flags <> 1"; |
|---|
| 61 |
my $result = $dbh->selectall_arrayref($statement); |
|---|
| 62 |
unless ($result) { |
|---|
| 63 |
# check for errors after every single database call |
|---|
| 64 |
print "dbh->selectall_arrayref($statement) failed!\n"; |
|---|
| 65 |
print "DBI::err=[$DBI::err]\n"; |
|---|
| 66 |
print "DBI::errstr=[$DBI::errstr]\n"; |
|---|
| 67 |
exit; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
open EXTEN, ">$iax_conf" or die "Cannot create/overwrite extensions file: $iax_conf\n"; |
|---|
| 71 |
$additional = ""; |
|---|
| 72 |
my @resultSet = @{$result}; |
|---|
| 73 |
if ( $#resultSet > -1 ) { |
|---|
| 74 |
foreach $row (@{ $result }) { |
|---|
| 75 |
my @result = @{ $row }; |
|---|
| 76 |
$additional .= $result[0]."=".$result[1]."\n"; |
|---|
| 77 |
} |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
# items with id like 9999999% get put at the top of the file |
|---|
| 81 |
$statement = "SELECT keyword,data from $table_name where id LIKE '9999999%' and keyword <> 'account' and flags <> 1"; |
|---|
| 82 |
$result = $dbh->selectall_arrayref($statement); |
|---|
| 83 |
unless ($result) { |
|---|
| 84 |
# check for errors after every single database call |
|---|
| 85 |
print "dbh->selectall_arrayref($statement) failed!\n"; |
|---|
| 86 |
print "DBI::err=[$DBI::err]\n"; |
|---|
| 87 |
print "DBI::errstr=[$DBI::errstr]\n"; |
|---|
| 88 |
exit; |
|---|
| 89 |
} |
|---|
| 90 |
@resultSet = @{$result}; |
|---|
| 91 |
if ( $#resultSet > -1 ) { |
|---|
| 92 |
foreach $row (@{ $result }) { |
|---|
| 93 |
my @result = @{ $row }; |
|---|
| 94 |
$top .= $result[0]."=".$result[1]."\n"; |
|---|
| 95 |
} |
|---|
| 96 |
print EXTEN "$top\n"; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
# select for unique accounts |
|---|
| 101 |
$statement = "SELECT data,id from $table_name where keyword='account' and flags <> 1 group by data"; |
|---|
| 102 |
$result = $dbh->selectall_arrayref($statement); |
|---|
| 103 |
unless ($result) { |
|---|
| 104 |
# check for errors after every single database call |
|---|
| 105 |
print "dbh->selectall_arrayref($statement) failed!\n"; |
|---|
| 106 |
print "DBI::err=[$DBI::err]\n"; |
|---|
| 107 |
print "DBI::errstr=[$DBI::errstr]\n"; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
@resultSet = @{$result}; |
|---|
| 111 |
if ( $#resultSet == -1 ) { |
|---|
| 112 |
print "No iax accounts defined in $table_name\n"; |
|---|
| 113 |
exit; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
#get the details for each account found above |
|---|
| 117 |
foreach my $row ( @{ $result } ) { |
|---|
| 118 |
my $account = @{ $row }[0]; |
|---|
| 119 |
my $id = @{ $row }[1]; |
|---|
| 120 |
print EXTEN "[$account]\n"; |
|---|
| 121 |
$statement = "SELECT keyword,data from $table_name where id=$id and keyword <> 'account' and flags <> 1 order by keyword DESC"; |
|---|
| 122 |
my $result = $dbh->selectall_arrayref($statement); |
|---|
| 123 |
unless ($result) { |
|---|
| 124 |
# check for errors after every single database call |
|---|
| 125 |
print "dbh->selectall_arrayref($statement) failed!\n"; |
|---|
| 126 |
print "DBI::err=[$DBI::err]\n"; |
|---|
| 127 |
print "DBI::errstr=[$DBI::errstr]\n"; |
|---|
| 128 |
exit; |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
my @resSet = @{$result}; |
|---|
| 132 |
if ( $#resSet == -1 ) { |
|---|
| 133 |
print "no results\n"; |
|---|
| 134 |
exit; |
|---|
| 135 |
} |
|---|
| 136 |
foreach my $row ( @{ $result } ) { |
|---|
| 137 |
my @result = @{ $row }; |
|---|
| 138 |
@opts=split("&",$result[1]); |
|---|
| 139 |
foreach $opt (@opts) { |
|---|
| 140 |
print EXTEN "$result[0]=$opt\n"; |
|---|
| 141 |
} |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
print EXTEN "$additional\n"; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
exit 0; |
|---|
| 148 |
|
|---|