forked from njdcamilli/agilebill
/
agilevoice-export-extensions.pl
executable file
·59 lines (51 loc) · 1.84 KB
/
agilevoice-export-extensions.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/perl -T
# Retrieves the extensions entries from the database
use DBI;
################### BEGIN OF CONFIGURATION ####################
# the name of the extensions table
$table_name = "ab_voip_did";
# the path to the extensions.conf file
# WARNING: this file will be substituted by the output of this program
$ext_conf = "/etc/asterisk/extensions_agilevoice.conf.tmp";
# the name of the box the MySQL database is running on
$hostname = "%%AGILE_DB_HOST%%";
# the name of the database our tables are kept
$database = "%%AGILE_DB_DATABASE%%";
# username to connect to the database
$username = "%%AGILE_DB_USERNAME%%";
# password to connect to the database
$password = "%%AGILE_DB_PASSWORD%%";
################### END OF CONFIGURATION #######################
my $dial = "";
my $dialarg = "";
open EXTEN, ">$ext_conf" or die "Cannot create/overwrite file: $ext_conf\n";
$dbh = DBI->connect("dbi:mysql:dbname=$database;host=$hostname", "$username", "$password");
$statement = "SELECT did, channel, channelarg from $table_name WHERE active=1 ORDER BY did";
my $result = $dbh->selectall_arrayref($statement);
unless ($result) {
# check for errors after every single database call
print "dbh->selectall_arrayref($statement) failed!\n";
print "DBI::err=[$DBI::err]\n";
print "DBI::errstr=[$DBI::errstr]\n";
exit;
}
my @resultSet = @{$result};
if ( $#resultSet > -1 ) {
foreach $row (@{ $result }) {
my @result = @{ $row };
$dial = $result[1];
$dialarg = $result[2];
if (0 == length($dialarg)) {
$dialarg = $result[0];
}
if ($dial =~ /IAX/) {
$dial = "IAX2/".$dialarg."/".$result[1];
} else {
$dial = "SIP/".$dialarg;
}
print EXTEN "exten => ".$result[0].",1,Dial(".$dial.")\n";
print EXTEN "exten => ".$result[0].",2,Macro(exten-vm,".$result[0].",".$dial.")\n";
print EXTEN "exten => ".$result[0].",3,Hangup\n";
}
}
exit 0;