2 # Author: Peter Nixon <codemonkey@peternixon.net>
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 <codemonkey@petenixon.net>
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.
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.
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
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,
31 # flags BOOLEAN DEFAULT '0' NOT NULL,
32 # PRIMARY KEY(context, extension, priority)
35 #CREATE TABLE globals (
36 # variable VARCHAR(20) NOT NULL,
37 # value VARCHAR(50) NOT NULL,
38 # PRIMARY KEY(variable, value)
41 use strict
; # Make sure we write decent perl code
43 require DBI
; # We need database drivers for this thing to work
45 ################### BEGIN OF CONFIGURATION ####################
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)
58 ################### END OF CONFIGURATION #######################
60 # You should not need to edit anything below here
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;
71 $dbh = DBI
->connect("DBI:$dbbrand:dbname=$database;host=$hostname", "$username", "$password")
72 or die "Couldn't connect to database: " . DBI
->errstr;
77 if ($verbose > 1) { print "DEBUG: Disconnecting from Database Host: $hostname\n" }
79 or warn "Disconnection failed: $DBI::errstr\n";
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;
87 $sth->execute() # Execute the query
88 or die "Couldn't execute SELECT statement: " . $sth->errstr;
91 print EXTEN
"[globals]\n";
92 while (my @global = $sth->fetchrow_array()) {
93 print EXTEN
"$global[0] = $global[1]\n";
97 print "WARNING: You have no global variables set\n";
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;
107 $sth->execute() # Execute the query
108 or die "Couldn't execute SELECT statement: " . $sth->errstr;
110 if ($sth->rows > 0) {
111 while (my @context = $sth->fetchrow_array()) {
112 print EXTEN
"[$context[0]]\n";
113 &get_extensions
($context[0]);
118 print "WARNING: You have no contexts defined in the $table_name table\n";
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;
129 $sth->execute() # Execute the query
130 or die "Couldn't execute SELECT statement: " . $sth->errstr;
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];
140 print "WARNING: You have no extensions for [$context]\n";
147 open EXTEN
, ">$extensions_conf" || die "Cannot create/overwrite extensions file: $extensions_conf\n";
152 close EXTEN
; # Close the file handle
153 if ($verbose > 0) { print "New $extensions_conf successfully written.\n"; }