| 1 |
#!/usr/bin/perl -Tw |
| 2 |
# Author: Peter Nixon <[email protected]> |
| 3 |
# Date: April 2004 |
| 4 |
# Copy Policy: GNU Public Licence Version 2 or later |
| 5 |
# URL: http://www.peternixon.net/code/ |
| 6 |
# Supported: PostgreSQL, Oracle, MySQL |
| 7 |
# Copyright: 2004 Peter Nixon <[email protected]> |
| 8 |
# |
| 9 |
# This program is free software; you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU General Public License as published by |
| 11 |
# the Free Software Foundation; either version 2 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# This program is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU General Public License for more details. |
| 18 |
# |
| 19 |
# $Id$ |
| 20 |
# |
| 21 |
# Use these commands to create the appropriate SQL tables |
| 22 |
# If flags is 1 then the record is not included in the output extensions file |
| 23 |
# |
| 24 |
#CREATE TABLE extensions ( |
| 25 |
# context VARCHAR(20) DEFAULT 'default' NOT NULL, |
| 26 |
# extension VARCHAR(20) NOT NULL, |
| 27 |
# priority INTEGER DEFAULT '1' NOT NULL, |
| 28 |
# application VARCHAR(20) NOT NULL, |
| 29 |
# args VARCHAR(50), |
| 30 |
# descr TEXT, |
| 31 |
# flags BOOLEAN DEFAULT '0' NOT NULL, |
| 32 |
# PRIMARY KEY(context, extension, priority) |
| 33 |
#); |
| 34 |
|
| 35 |
#CREATE TABLE globals ( |
| 36 |
# variable VARCHAR(20) NOT NULL, |
| 37 |
# value VARCHAR(50) NOT NULL, |
| 38 |
# PRIMARY KEY(variable, value) |
| 39 |
#); |
| 40 |
|
| 41 |
use strict; # Make sure we write decent perl code |
| 42 |
|
| 43 |
require DBI; # We need database drivers for this thing to work |
| 44 |
|
| 45 |
################### BEGIN OF CONFIGURATION #################### |
| 46 |
|
| 47 |
my $table_name = "extensions"; # name of the extensions table |
| 48 |
my $global_table_name = "globals"; # name of the globals table |
| 49 |
my $extensions_conf = "/etc/asterisk/extensions.conf"; # path to extensions.conf |
| 50 |
# WARNING: this file will be substituted by the output of this program |
| 51 |
my $dbbrand = "Pg"; # Hint: "mysql" or any other Perl DBI driver. |
| 52 |
my $hostname = "localhost"; # The SQL server's hostname or IP |
| 53 |
my $database = "peter"; # the name of the database our tables are kept |
| 54 |
my $username = "peter"; # username to connect to the database |
| 55 |
my $password = ""; # password to connect to the database |
| 56 |
my $verbose = 1; # Verbosity Level (0 - 2) |
| 57 |
|
| 58 |
################### END OF CONFIGURATION ####################### |
| 59 |
|
| 60 |
# You should not need to edit anything below here |
| 61 |
my $dbh; |
| 62 |
|
| 63 |
sub db_connect { |
| 64 |
if ($verbose > 1) { print "DEBUG: Connecting to Database Host: $hostname\n" } |
| 65 |
if ($hostname eq 'localhost') { |
| 66 |
if ($verbose > 1) { print "DEBUG: SQL server is on localhost so using UNIX socket instead of network socket.\n" } |
| 67 |
$dbh = DBI->connect("DBI:$dbbrand:dbname=$database", "$username", "$password") |
| 68 |
or die "Couldn't connect to database: " . DBI->errstr; |
| 69 |
} |
| 70 |
else { |
| 71 |
$dbh = DBI->connect("DBI:$dbbrand:dbname=$database;host=$hostname", "$username", "$password") |
| 72 |
or die "Couldn't connect to database: " . DBI->errstr; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
sub db_disconnect { |
| 77 |
if ($verbose > 1) { print "DEBUG: Disconnecting from Database Host: $hostname\n" } |
| 78 |
$dbh->disconnect |
| 79 |
or warn "Disconnection failed: $DBI::errstr\n"; |
| 80 |
} |
| 81 |
|
| 82 |
sub get_globals { |
| 83 |
if ($verbose > 0) { print "Checking Database for [global] variables\n"; } |
| 84 |
my $sth = $dbh->prepare("SELECT variable, value FROM $global_table_name ORDER BY variable") |
| 85 |
or die "Couldn't prepare statement: " . $dbh->errstr; |
| 86 |
|
| 87 |
$sth->execute() # Execute the query |
| 88 |
or die "Couldn't execute SELECT statement: " . $sth->errstr; |
| 89 |
|
| 90 |
if ($sth->rows > 0) { |
| 91 |
print EXTEN "[globals]\n"; |
| 92 |
while (my @global = $sth->fetchrow_array()) { |
| 93 |
print EXTEN "$global[0] = $global[1]\n"; |
| 94 |
} |
| 95 |
print EXTEN "\n"; |
| 96 |
} else { |
| 97 |
print "WARNING: You have no global variables set\n"; |
| 98 |
} |
| 99 |
$sth->finish; |
| 100 |
} |
| 101 |
|
| 102 |
sub get_contexts { |
| 103 |
if ($verbose > 0) { print "Checking Database for contexts\n"; } |
| 104 |
my $sth = $dbh->prepare("SELECT context FROM $table_name GROUP BY context") |
| 105 |
or die "Couldn't prepare statement: " . $dbh->errstr; |
| 106 |
|
| 107 |
$sth->execute() # Execute the query |
| 108 |
or die "Couldn't execute SELECT statement: " . $sth->errstr; |
| 109 |
|
| 110 |
if ($sth->rows > 0) { |
| 111 |
while (my @context = $sth->fetchrow_array()) { |
| 112 |
print EXTEN "[$context[0]]\n"; |
| 113 |
&get_extensions($context[0]); |
| 114 |
print EXTEN "\n"; |
| 115 |
} |
| 116 |
print EXTEN "\n"; |
| 117 |
} else { |
| 118 |
print "WARNING: You have no contexts defined in the $table_name table\n"; |
| 119 |
} |
| 120 |
$sth->finish; |
| 121 |
} |
| 122 |
|
| 123 |
sub get_extensions { |
| 124 |
my $context = $_[0]; my @extension; |
| 125 |
if ($verbose > 0) { print " Checking Database for [$context] extensions\n"; } |
| 126 |
my $sth = $dbh->prepare("SELECT extension, priority, application, args, descr FROM $table_name WHERE context='$context' AND flags = '0' ORDER BY extension, priority") |
| 127 |
or die "Couldn't prepare statement: " . $dbh->errstr; |
| 128 |
|
| 129 |
$sth->execute() # Execute the query |
| 130 |
or die "Couldn't execute SELECT statement: " . $sth->errstr; |
| 131 |
|
| 132 |
if ($sth->rows > 0) { |
| 133 |
while (@extension = $sth->fetchrow_array()) { |
| 134 |
print EXTEN "exten => $extension[0],$extension[1],$extension[2]"; |
| 135 |
print EXTEN "($extension[3])" if defined $extension[3]; |
| 136 |
print EXTEN " ; $extension[4]" if defined $extension[4]; |
| 137 |
print EXTEN "\n"; |
| 138 |
} |
| 139 |
} else { |
| 140 |
print "WARNING: You have no extensions for [$context]\n"; |
| 141 |
} |
| 142 |
$sth->finish; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
sub main { |
| 147 |
open EXTEN, ">$extensions_conf" || die "Cannot create/overwrite extensions file: $extensions_conf\n"; |
| 148 |
&db_connect; |
| 149 |
&get_globals; |
| 150 |
&get_contexts; |
| 151 |
&db_disconnect; |
| 152 |
close EXTEN; # Close the file handle |
| 153 |
if ($verbose > 0) { print "New $extensions_conf successfully written.\n"; } |
| 154 |
return 1; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
exit &main(); |